Skip to content

Commit 9fbee9c

Browse files
committed
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 <[email protected]>
1 parent 55922ea commit 9fbee9c

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

labgridhelper/linux.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ def get_systemd_status_raw(command):
8585
services[name]["active"] = next(data)
8686
services[name]["sub"] = next(data)
8787
services[name]["follow"] = next(data)
88-
path_and_id = next(data)
89-
pos = path_and_id.index('"')
90-
services[name]["path"] = path_and_id[:pos]
91-
services[name]["id"] = int(path_and_id[pos+1:-1].strip(" "))
92-
services[name]["type"] = path_and_id[path_and_id.rfind('"'):]
88+
path_and_id = next(data).split('\"')
89+
services[name]["path"] = path_and_id[0]
90+
services[name]["id"] = int(path_and_id[1].strip(" "))
91+
services[name]["type"] = path_and_id[2]
9392
services[name]["objpath"] = next(data)
9493

9594
return services

tests/test_linux.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
from labgridhelper import linux
2+
import pytest
23

34
def test_get_systemd_version(command, monkeypatch):
45
systemd_version = 'systemd 249 (249.5-2-arch)\n+PAM +AUDIT -SELINUX\n'
56

67
monkeypatch.setattr(command, 'run_check', lambda cmd: [systemd_version])
78

89
assert linux.get_systemd_version(command) == 249
10+
11+
@pytest.mark.parametrize("systemd_version", [230, 240])
12+
def test_get_systemd_status(command, monkeypatch, systemd_version):
13+
monkeypatch.setattr(linux, 'get_systemd_version', lambda cmd: systemd_version)
14+
15+
status = {
16+
230: 'a(ssssssouso) 1 "systemd-resolved.service" "Network Name Resolution" "loaded" "active"' + \
17+
' "running" "" "/org/freedesktop/systemd1/unit/systemd_2dresolved_2eservice" 0 "" "/"',
18+
240: '{"type":"a(ssssssouso)","data":[[["systemd-resolved.service",' + \
19+
'"Network Name Resolution","loaded","active","running","",' + \
20+
'"/org/freedesktop/systemd1/unit/systemd_2dresolved_2eservice",0,"","/"]]]}',
21+
}
22+
23+
monkeypatch.setattr(command, 'run_check', lambda cmd: [status[systemd_version]])
24+
25+
status = linux.get_systemd_status(command)
26+
27+
assert len(status.keys()) == 1
28+
assert status['systemd-resolved.service']["description"] == 'Network Name Resolution'
29+
assert status['systemd-resolved.service']["load"] == 'loaded'
30+
assert status['systemd-resolved.service']["active"] == 'active'
31+
assert status['systemd-resolved.service']["sub"] == 'running'
32+
assert status['systemd-resolved.service']["follow"] == ''
33+
assert status['systemd-resolved.service']["path"] == '/org/freedesktop/systemd1/unit/systemd_2dresolved_2eservice'
34+
assert status['systemd-resolved.service']["id"] == 0
35+
assert status['systemd-resolved.service']["type"] == ''
36+
assert status['systemd-resolved.service']["objpath"] == '/'

0 commit comments

Comments
 (0)