diff --git a/testinfra/modules/service.py b/testinfra/modules/service.py index 830d066d..ffdd6eaf 100644 --- a/testinfra/modules/service.py +++ b/testinfra/modules/service.py @@ -33,6 +33,11 @@ def __init__(self, name): self.name = name super().__init__() + @property + def exists(self): + """Test if service is exists""" + raise NotImplementedError + @property def is_running(self): """Test if service is running""" @@ -143,6 +148,11 @@ def is_enabled(self): class SystemdService(SysvService): + @property + def exists(self): + cmd = self.run_test('systemctl list-unit-files | grep -q"^%s"', self.name) + return cmd.rc == 0 + @property def is_running(self): out = self.run_expect([0, 1, 3], "systemctl is-active %s", self.name) @@ -195,6 +205,10 @@ def systemd_properties(self): class UpstartService(SysvService): + @property + def exists(self): + return self._host.file(f"/etc/init/{self.name}.conf").exists + @property def is_enabled(self): if ( @@ -237,6 +251,10 @@ def is_enabled(self): class FreeBSDService(Service): + @property + def exists(self): + return self._host.file(f"/etc/rc.d/{self.name}").exists + @property def is_running(self): return self.run_test("service %s onestatus", self.name).rc == 0 @@ -253,6 +271,10 @@ def is_enabled(self): class OpenBSDService(Service): + @property + def exists(self): + return self._host.file(f"/etc/rc.d/{self.name}").exists + @property def is_running(self): return self.run_test("/etc/rc.d/%s check", self.name).rc == 0 @@ -271,6 +293,10 @@ def is_enabled(self): class NetBSDService(Service): + @property + def exists(self): + return self._host.file(f"/etc/rc.d/{self.name}").exists + @property def is_running(self): return self.run_test("/etc/rc.d/%s onestatus", self.name).rc == 0 @@ -281,6 +307,13 @@ def is_enabled(self): class WindowsService(Service): + @property + def exists(self): + out = self.check_output( + f"Get-Service -Name {self.name} -ErrorAction SilentlyContinue" + ) + return self.name in out + @property def is_running(self): return (