From 47465e627c2de80e02820ae0adad7bb82fbf0825 Mon Sep 17 00:00:00 2001 From: Adam Trhon Date: Fri, 12 Nov 2021 10:38:47 +0100 Subject: [PATCH 1/3] linux: fix parsing of old systemd 'type' right after boot With old systemd (before 240), just after boot, the get_systemd_status_raw function fails with the following exception: ValueError: invalid literal for int() with base 10: '2 "star' Calling `print(path_and_id)` before parsing prints the following: /org/freedesktop/systemd1/unit/syslog_2eservice" 0 " /org/freedesktop/systemd1/unit/alsa_2drestore_2eservice" 0 " /org/freedesktop/systemd1/unit/dev_2dram0_2edevice" 0 " /org/freedesktop/systemd1/unit/multi_2duser_2etarget" 2 "start The last line causes the error which is fixed by this patch. Signed-off-by: Adam Trhon --- labgridhelper/linux.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/labgridhelper/linux.py b/labgridhelper/linux.py index 5c6a20e..8736dd9 100644 --- a/labgridhelper/linux.py +++ b/labgridhelper/linux.py @@ -85,11 +85,10 @@ def get_systemd_status_raw(command): services[name]["active"] = next(data) services[name]["sub"] = next(data) services[name]["follow"] = next(data) - path_and_id = next(data) - pos = path_and_id.index('"') - services[name]["path"] = path_and_id[:pos] - services[name]["id"] = int(path_and_id[pos+1:-1].strip(" ")) - services[name]["type"] = path_and_id[path_and_id.rfind('"'):] + path_and_id = next(data).split('\"') + services[name]["path"] = path_and_id[0] + services[name]["id"] = int(path_and_id[1].strip(" ")) + services[name]["type"] = path_and_id[2] services[name]["objpath"] = next(data) return services From f7e1af1b45b0a058d997355a000a0228668e9811 Mon Sep 17 00:00:00 2001 From: Adam Trhon Date: Sun, 14 Nov 2021 22:22:30 +0100 Subject: [PATCH 2/3] tests: setup test infrastructure Setup test infrastructure, populate it with a simple test of get_systemd_version function. Signed-off-by: Adam Trhon --- .gitignore | 3 +++ dev-requirements.txt | 2 ++ tests/conftest.py | 12 ++++++++++++ tests/test_linux.py | 8 ++++++++ tox.ini | 9 +++++++++ 5 files changed, 34 insertions(+) create mode 100644 dev-requirements.txt create mode 100644 tests/conftest.py create mode 100644 tests/test_linux.py create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore index e4edf8f..54ea191 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /build *.egg-info __pycache__ +.tox +*.eggs/README.txt +/.coverage diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..7dfb6e0 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,2 @@ +pytest-cov==3.0.0 +labgrid diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..25bf78f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,12 @@ +import pytest + +from labgrid.driver.fake import FakeCommandDriver +from labgrid import Target + + +@pytest.fixture +def command(): + t = Target("dummy") + d = FakeCommandDriver(t, "command") + t.activate(d) + return d diff --git a/tests/test_linux.py b/tests/test_linux.py new file mode 100644 index 0000000..38aedb1 --- /dev/null +++ b/tests/test_linux.py @@ -0,0 +1,8 @@ +from labgridhelper import linux + +def test_get_systemd_version(command, monkeypatch): + systemd_version = 'systemd 249 (249.5-2-arch)\n+PAM +AUDIT -SELINUX\n' + + monkeypatch.setattr(command, 'run_check', lambda cmd: [systemd_version]) + + assert linux.get_systemd_version(command) == 249 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..dd418ad --- /dev/null +++ b/tox.ini @@ -0,0 +1,9 @@ +[tox] +envlist = py35, py36, py37 + +[testenv] +deps = -rdev-requirements.txt +commands = + pytest tests {posargs} \ + --cov={envsitepackagesdir}/labgridhelper \ + --cov-report=html:htmlcov From 5d1bcde4e42efee233729933b75714498ce12328 Mon Sep 17 00:00:00 2001 From: Adam Trhon Date: Sun, 14 Nov 2021 22:24:21 +0100 Subject: [PATCH 3/3] linux: fix get_systemd_version not parsing raw type correctly The newly added test of get_systemd_status failed for old systemd with > assert status['systemd-resolved.service']["type"] == '' E assert '"' == '' E + " tests/test_linux.py:36: AssertionError so refactor parsing of path_and_id to fix that. Signed-off-by: Adam Trhon --- tests/test_linux.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_linux.py b/tests/test_linux.py index 38aedb1..de7d48d 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -1,4 +1,5 @@ from labgridhelper import linux +import pytest def test_get_systemd_version(command, monkeypatch): systemd_version = 'systemd 249 (249.5-2-arch)\n+PAM +AUDIT -SELINUX\n' @@ -6,3 +7,30 @@ def test_get_systemd_version(command, monkeypatch): monkeypatch.setattr(command, 'run_check', lambda cmd: [systemd_version]) assert linux.get_systemd_version(command) == 249 + +@pytest.mark.parametrize("systemd_version", [230, 240]) +def test_get_systemd_status(command, monkeypatch, systemd_version): + monkeypatch.setattr(linux, 'get_systemd_version', lambda cmd: systemd_version) + + status = { + 230: 'a(ssssssouso) 1 "systemd-resolved.service" "Network Name Resolution" "loaded" "active"' + \ + ' "running" "" "/org/freedesktop/systemd1/unit/systemd_2dresolved_2eservice" 0 "" "/"', + 240: '{"type":"a(ssssssouso)","data":[[["systemd-resolved.service",' + \ + '"Network Name Resolution","loaded","active","running","",' + \ + '"/org/freedesktop/systemd1/unit/systemd_2dresolved_2eservice",0,"","/"]]]}', + } + + monkeypatch.setattr(command, 'run_check', lambda cmd: [status[systemd_version]]) + + status = linux.get_systemd_status(command) + + assert len(status.keys()) == 1 + assert status['systemd-resolved.service']["description"] == 'Network Name Resolution' + assert status['systemd-resolved.service']["load"] == 'loaded' + assert status['systemd-resolved.service']["active"] == 'active' + assert status['systemd-resolved.service']["sub"] == 'running' + assert status['systemd-resolved.service']["follow"] == '' + assert status['systemd-resolved.service']["path"] == '/org/freedesktop/systemd1/unit/systemd_2dresolved_2eservice' + assert status['systemd-resolved.service']["id"] == 0 + assert status['systemd-resolved.service']["type"] == '' + assert status['systemd-resolved.service']["objpath"] == '/'