Skip to content

Commit

Permalink
Fix the code to pass the pycodetest
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume <[email protected]>
  • Loading branch information
gthvn1 committed Oct 25, 2024
1 parent 584b480 commit 81c8abd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
28 changes: 14 additions & 14 deletions SOURCES/etc/xapi.d/plugins/smartctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@
import sys
import XenAPIPlugin

sys.path.append('.')
sys.path.append(".")
from xcpngutils import configure_logging, run_command, error_wrapped
from xcpngutils.operationlocker import OperationLocker


@error_wrapped
def _list_devices():
devices = []
result = run_command(['smartctl', '--scan'])
for line in result['stdout'].splitlines():
devices.append({'name': line.split()[0], 'type':line.split()[2]})
result = run_command(["smartctl", "--scan"])
for line in result["stdout"].splitlines():
devices.append({"name": line.split()[0], "type": line.split()[2]})
return devices


@error_wrapped
def get_information(session, args):
results = {}
with OperationLocker():
devices = _list_devices()
for device in devices:
cmd = run_command(["smartctl", "-j", "-a", "-d", device['name'], device['type']], check=False)
results[device] = json.loads(cmd['stdout'])
cmd = run_command(["smartctl", "-j", "-a", "-d", device["name"], device["type"]], check=False)
results[device] = json.loads(cmd["stdout"])
return json.dumps(results)


@error_wrapped
def get_health(session, args):
results = {}
with OperationLocker():
devices = _list_devices()
for device in devices:
cmd = run_command(["smartctl", "-j", "-H", "-d", device['name'], device['type']], check=False)
json_output = json.loads(cmd['stdout'])
if json_output['smart_status']['passed']:
cmd = run_command(["smartctl", "-j", "-H", "-d", device["name"], device["type"]], check=False)
json_output = json.loads(cmd["stdout"])
if json_output["smart_status"]["passed"]:
results[disk] = "PASSED"
else:
results[disk] = "FAILED"
return json.dumps(results)


_LOGGER = configure_logging('smartctl')
_LOGGER = configure_logging("smartctl")
if __name__ == "__main__":
XenAPIPlugin.dispatch({
'information': get_information,
'health': get_health
})
XenAPIPlugin.dispatch({"information": get_information, "health": get_health})
5 changes: 3 additions & 2 deletions tests/test_smartctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
}
}"""


@mock.patch("smartctl.run_command", autospec=True)
@mock.patch("smartctl._list_disks", autospec=True)
class TestSmartctl:
Expand All @@ -86,8 +87,8 @@ def test_smartctl_error(self, _list_disks, run_command, fs):

with pytest.raises(XenAPIPlugin.Failure) as e:
get_health(None, None)
assert e.value.params[0] == '-1'
assert e.value.params[1] == 'Error!'
assert e.value.params[0] == "-1"
assert e.value.params[1] == "Error!"

def test_smartctl_information(self, _list_disks, run_command, fs):
_list_disks.return_value = ["/dev/sda"]
Expand Down

0 comments on commit 81c8abd

Please sign in to comment.