Skip to content

Commit 1ccd87a

Browse files
Update SystemdService.exists to find all unit files
1 parent dca7b05 commit 1ccd87a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

testinfra/modules/service.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,18 @@ def _has_systemd_suffix(self):
176176

177177
@property
178178
def exists(self):
179-
cmd = self.run_test('systemctl list-unit-files | grep -q "^%s"', self.name)
180-
return cmd.rc == 0
179+
# systemctl return codes based on https://man7.org/linux/man-pages/man1/systemctl.1.html:
180+
# 0: unit is active
181+
# 1: unit not failed (used by is-failed)
182+
# 2: unused
183+
# 3: unit is not active
184+
# 4: no such unit
185+
cmd = self.run_expect([0, 1, 3, 4], "systemctl status %s", self.name)
186+
return cmd.rc < 4
181187

182188
@property
183189
def is_running(self):
184-
# based on https://man7.org/linux/man-pages/man1/systemctl.1.html
190+
# systemctl return codes based on https://man7.org/linux/man-pages/man1/systemctl.1.html:
185191
# 0: program running
186192
# 1: program is dead and pid file exists
187193
# 3: not running and pid file does not exists

0 commit comments

Comments
 (0)