Skip to content

Commit 5d3f323

Browse files
Update SystemdService.exists to find all unit files
1 parent 2531d2d commit 5d3f323

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

testinfra/modules/service.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,25 @@ class SystemdService(SysvService):
169169

170170
def _has_systemd_suffix(self):
171171
"""
172-
Check if service name has a known systemd unit suffix
172+
Check if the service name has a known systemd unit suffix
173173
"""
174174
unit_suffix = self.name.split(".")[-1]
175175
return unit_suffix in self.suffix_list
176176

177177
@property
178178
def exists(self):
179-
cmd = self.run_test('systemctl list-unit-files | grep -q "^%s"', self.name)
179+
# systemctl offers “list-units” and “list-unit-files” to list unit
180+
# files. Unfortunately, neither command provides a complete list of
181+
# unit files. Therefore, both commands are queried one after the
182+
# other.
183+
cmd = self.run_test(
184+
r'systemctl list-units --all | grep -q "^[[:space:]]*%s"', self.name
185+
)
186+
if cmd.rc == 0:
187+
return True
188+
cmd = self.run_test(
189+
r'systemctl list-unit-files | grep -q "^[[:space:]]*%s"', self.name
190+
)
180191
return cmd.rc == 0
181192

182193
@property

0 commit comments

Comments
 (0)