Skip to content

Commit

Permalink
Use a dict to avoid usage of index
Browse files Browse the repository at this point in the history
Using index is error prone and less readable than using a dict a naming
things. This patch also fixes pycodecheck errors.

Signed-off-by: Guillaume <[email protected]>
  • Loading branch information
gthvn1 committed Oct 25, 2024
1 parent 075077e commit e3560d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
37 changes: 16 additions & 21 deletions SOURCES/etc/xapi.d/plugins/smartctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +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_disks():
disks = []
result = run_command(['smartctl', '--scan'])
for line in result['stdout'].splitlines():
disks.append(line.split()[2])
disks.append(line.split()[0])
result = run_command(["smartctl", "--scan"])
for line in result["stdout"].splitlines():
disks.append({"name": line.split()[0], "type": line.split()[2]})
return disks


@error_wrapped
def get_information(session, args):
results = {}
i = 0
with OperationLocker():
disks = _list_disks()
for disk in disks:
cmd = run_command(["smartctl", "-j", "-a", "-d", disks[i], disks[i+1]], check=False)
results[disk] = json.loads(cmd['stdout'])
i = i + 2
cmd = run_command(["smartctl", "-j", "-a", "-d", disk["type"], disk["name"]], check=False)
results[disk["name"]] = json.loads(cmd["stdout"])
return json.dumps(results)


@error_wrapped
def get_health(session, args):
results = {}
i = 0
with OperationLocker():
disks = _list_disks()
for disk in disks:
cmd = run_command(["smartctl", "-j", "-H", "-d", disks[i], disks[i+1]])
json_output = json.loads(cmd['stdout'])
if json_output['smart_status']['passed']:
results[disk] = "PASSED"
cmd = run_command(["smartctl", "-j", "-H", "-d", disk["type"], disk["name"]], check=False)
json_output = json.loads(cmd["stdout"])
if json_output["smart_status"]["passed"]:
results[disk["name"]] = "PASSED"
else:
results[disk] = "FAILED"
i = i + 2
results[disk["name"]] = "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})
9 changes: 5 additions & 4 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,18 +87,18 @@ 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"]
_list_disks.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"]
_list_disks.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 e3560d6

Please sign in to comment.