Skip to content

Commit

Permalink
Add Service.exists
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenGrohmann authored and philpep committed Nov 13, 2023
1 parent 70554d3 commit 76170a4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions testinfra/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -169,6 +174,11 @@ def _has_systemd_suffix(self):
unit_suffix = self.name.split(".")[-1]
return unit_suffix in self.suffix_list

@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)
Expand Down Expand Up @@ -227,6 +237,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 (
Expand Down Expand Up @@ -269,6 +283,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
Expand All @@ -285,6 +303,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
Expand All @@ -301,6 +323,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
Expand All @@ -311,6 +337,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 (
Expand Down

0 comments on commit 76170a4

Please sign in to comment.