Skip to content

Commit 0f5ae25

Browse files
authored
Implement Service.systemd_properties (#612)
This expose units informations from systemctl show Also fix archlinux image reference from docker hub
1 parent 2d9dfe7 commit 0f5ae25

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

images/archlinux/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM archlinux/base
1+
FROM archlinux/archlinux:base
22

33
RUN pacman -Syu --noconfirm --noprogressbar --quiet python openssh iputils procps systemd systemd-sysvcompat && \
44
systemctl enable sshd.service

test/test_modules.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ def test_service(host, name, running, enabled):
163163
service = host.service(name)
164164
assert service.is_running == running
165165
assert service.is_enabled == enabled
166+
# check with systemd_properties
167+
assert (
168+
service.systemd_properties["UnitFileState"] == "enabled"
169+
if enabled
170+
else "disabled"
171+
)
172+
assert (
173+
service.systemd_properties["ActiveState"] in ["active"]
174+
if running
175+
else ["failed", "inactive"]
176+
)
166177

167178

168179
def test_salt(host):

testinfra/modules/service.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def is_valid(self):
4747
"""Test if service is valid
4848
4949
This method is only available in the systemd implementation,
50-
it will raise NotImplementedError in others implementation
50+
it will raise ``NotImplementedError`` in others implementation
5151
"""
5252
raise NotImplementedError
5353

@@ -56,7 +56,27 @@ def is_masked(self):
5656
"""Test if service is masked
5757
5858
This method is only available in the systemd implementation,
59-
it will raise NotImplementedError in others implementations
59+
it will raise ``NotImplementedError`` in others implementations
60+
"""
61+
raise NotImplementedError
62+
63+
@cached_property
64+
def systemd_properties(self):
65+
"""Properties of the service (unit).
66+
67+
Return service properties as a `dict`,
68+
empty properties are not returned.
69+
70+
>>> ntp = host.service("ntp")
71+
>>> ntp.systemd_properties["FragmentPath"]
72+
'/lib/systemd/system/ntp.service'
73+
74+
This method is only available in the systemd implementation,
75+
it will raise ``NotImplementedError`` in others implementations
76+
77+
Note: based on `systemctl show`_
78+
79+
.. _systemctl show: https://man7.org/linux/man-pages/man1/systemctl.1.html
6080
"""
6181
raise NotImplementedError
6282

@@ -159,6 +179,17 @@ def is_masked(self):
159179
cmd = self.run_test("systemctl is-enabled %s", self.name)
160180
return cmd.stdout.strip() == "masked"
161181

182+
@cached_property
183+
def systemd_properties(self):
184+
out = self.check_output("systemctl show %s", self.name)
185+
out_d = {}
186+
if out:
187+
# maxsplit is required because values can contain `=`
188+
out_d = dict(
189+
map(lambda pair: pair.split("=", maxsplit=1), out.splitlines())
190+
)
191+
return out_d
192+
162193

163194
class UpstartService(SysvService):
164195
@property

0 commit comments

Comments
 (0)