Skip to content

Commit 6b0d230

Browse files
Ignore non-relevant messages from "systemd-analyze verify"
1 parent 1ccd87a commit 6b0d230

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

test/test_modules.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,63 @@ def test_ssh_service(host, docker_image):
127127
assert ssh.is_enabled
128128

129129

130-
def test_service_systemd_mask(host):
131-
ssh = host.service("ssh")
130+
def test_service_systemd_mask(host, docker_image):
131+
name = "sshd" if docker_image == "rockylinux9" else "ssh"
132+
ssh = host.service(name)
132133
assert not ssh.is_masked
133134
host.run("systemctl mask ssh")
134135
assert ssh.is_masked
135136
host.run("systemctl unmask ssh")
136137
assert not ssh.is_masked
137138

138139

140+
@all_images
141+
def test_service_systemd_ssh(host, docker_image):
142+
name = "sshd" if docker_image == "rockylinux9" else "ssh"
143+
ssh_service = host.service(name)
144+
assert ssh_service.exists
145+
assert ssh_service.is_valid
146+
assert ssh_service.is_enabled
147+
assert ssh_service.is_running
148+
149+
150+
def test_service_systemd_root_mount(host):
151+
root_mount_service = host.service("-.mount") # systemd unit for mounting /
152+
assert root_mount_service.exists
153+
assert root_mount_service.is_valid
154+
assert root_mount_service.is_running
155+
156+
157+
# is_enabled does not work in Rocky Linux 9
158+
# $ systemctl status -- -.mount
159+
# AssertionError: ● -.mount - Root Mount
160+
# Loaded: loaded
161+
# Active: active (mounted) since Wed 2025-04-16 21:03:04 UTC; 6s ago
162+
# Until: Wed 2025-04-16 21:03:04 UTC; 6s ago
163+
# Where: /
164+
# What: overlay
165+
#
166+
# Notice: journal has been rotated since unit was started, output may be incomplete.
167+
#
168+
# $ systemctl is-enabled -- -.mount
169+
# Failed to get unit file state for -.mount: No such file or directory
170+
@pytest.mark.testinfra_hosts("docker://rockylinux9")
171+
@pytest.mark.xfail(
172+
reason='"systemctl is-enabled -- -.mount" fails even if "systemctl status" succeeds'
173+
)
174+
def test_service_systemd_root_mount_is_enabled(host):
175+
root_mount_service = host.service("-.mount") # systemd unit for mounting /
176+
assert not root_mount_service.is_enabled
177+
178+
179+
def test_service_systemd_tmp_mount(host):
180+
tmp = host.service("tmp.mount")
181+
assert tmp.exists
182+
assert tmp.is_valid
183+
assert not tmp.is_enabled
184+
assert not tmp.is_running
185+
186+
139187
def test_salt(host):
140188
ssh_version = host.salt("pkg.version", "openssh-server", local=True)
141189
assert ssh_version.startswith("1:9.2")

testinfra/modules/service.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,28 @@ def is_valid(self):
219219
name = self.name if self._has_systemd_suffix() else f"{self.name}.service"
220220
cmd = self.run("systemd-analyze verify %s", name)
221221
# A bad unit file still returns a rc of 0, so check the
222-
# stdout for anything. Nothing means no warns/errors.
222+
# stdout for anything. Nothing means no warns/errors.
223223
# Docs at https://www.freedesktop.org/software/systemd/man/systemd
224224
# -analyze.html#Examples%20for%20verify
225-
assert (cmd.stdout, cmd.stderr) == ("", "")
226-
return True
225+
226+
# Ignore non-relevant messages from the output of "systemd-analyze
227+
# verify":
228+
# "Unit is bound to inactive unit"
229+
# "ssh.service: Command 'man sshd(8)' failed with code"
230+
# --man=no: suppress the man page existence check
231+
# implemented in Systemd 235 (2017-10-06)
232+
# "Suspicious symlink /etc/systemd/system/[...] treating as alias."
233+
# probably a bug in systemd https://github.com/systemd/systemd/issues/30166
234+
stderr_lines = [
235+
i
236+
for i in cmd.stderr.splitlines()
237+
if "Unit is bound to inactive unit" not in i
238+
and ": Command 'man" not in i
239+
and "Suspicious symlink /" not in i
240+
]
241+
242+
stderr = "".join(stderr_lines)
243+
return (cmd.stdout, stderr) == ("", "")
227244

228245
@property
229246
def is_masked(self):

0 commit comments

Comments
 (0)