From 19effa1a1fc0c4431075684b8cd6b2a42b0fc73c Mon Sep 17 00:00:00 2001 From: Amir Rossert Date: Tue, 2 Jul 2024 13:42:13 +0300 Subject: [PATCH 1/2] Add 4 to the expected exit code when running "systemctl is-active" Signed-off-by: Amir Rossert --- testinfra/modules/service.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/testinfra/modules/service.py b/testinfra/modules/service.py index 9a3d704a..1d56016c 100644 --- a/testinfra/modules/service.py +++ b/testinfra/modules/service.py @@ -181,7 +181,13 @@ def exists(self): @property def is_running(self): - out = self.run_expect([0, 1, 3], "systemctl is-active %s", self.name) + # based on https://man7.org/linux/man-pages/man1/systemctl.1.html + # 0: program running + # 1: program is dead and pid file exists + # 3: not running and pid file does not exists + # 4: Unable to determine status (no such unit) + out = self.run_expect([0, 1, 3, 4], "systemctl is-active %s", + self.name) if out.rc == 1: # Failed to connect to bus: No such file or directory return super().is_running From 4018bddfa0c17becdc340e4a2be7eedc4c76ef5e Mon Sep 17 00:00:00 2001 From: Amir Rossert Date: Tue, 2 Jul 2024 13:51:54 +0300 Subject: [PATCH 2/2] Refactor Signed-off-by: Amir Rossert --- testinfra/modules/service.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testinfra/modules/service.py b/testinfra/modules/service.py index 1d56016c..c6be4f56 100644 --- a/testinfra/modules/service.py +++ b/testinfra/modules/service.py @@ -186,8 +186,7 @@ def is_running(self): # 1: program is dead and pid file exists # 3: not running and pid file does not exists # 4: Unable to determine status (no such unit) - out = self.run_expect([0, 1, 3, 4], "systemctl is-active %s", - self.name) + out = self.run_expect([0, 1, 3, 4], "systemctl is-active %s", self.name) if out.rc == 1: # Failed to connect to bus: No such file or directory return super().is_running