Skip to content

Commit

Permalink
Fix issues with wrong key for results
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume <[email protected]>
  • Loading branch information
gthvn1 committed Oct 26, 2024
1 parent 81c8abd commit bb0e2c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions SOURCES/etc/xapi.d/plugins/smartctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_information(session, args):
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"])
results[device["name"]] = json.loads(cmd["stdout"])
return json.dumps(results)


Expand All @@ -39,9 +39,9 @@ def get_health(session, args):
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"
results[device["name"]] = "PASSED"
else:
results[disk] = "FAILED"
results[device["name"]] = "FAILED"
return json.dumps(results)


Expand Down
14 changes: 7 additions & 7 deletions tests/test_smartctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@


@mock.patch("smartctl.run_command", autospec=True)
@mock.patch("smartctl._list_disks", autospec=True)
@mock.patch("smartctl._list_devices", autospec=True)
class TestSmartctl:
def test_smartctl_error(self, _list_disks, run_command, fs):
_list_disks.side_effect = Exception("Error!")
def test_smartctl_error(self, _list_devices, run_command, fs):
_list_devices.side_effect = Exception("Error!")

with pytest.raises(XenAPIPlugin.Failure) as e:
get_health(None, None)
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"]
def test_smartctl_information(self, _list_devices, run_command, fs):
_list_devices.return_value = [{"name": "/dev/sda", "type": "scsi"}]
run_command.return_value = {"stdout": SMARTCTL_INFO}

res = get_information(None, None)
assert json.loads(res) == json.loads(SMARTCTL_INFO_EXPECTED)

def test_smartctl_health(self, _list_disks, run_command, fs):
_list_disks.return_value = ["/dev/sda"]
def test_smartctl_health(self, _list_devices, run_command, fs):
_list_devices.return_value = [{"name": "/dev/sda", "type": "scsi"}]
run_command.return_value = {"stdout": SMARTCTL_HEALTH}

res = get_health(None, None)
Expand Down

0 comments on commit bb0e2c0

Please sign in to comment.