Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Service.exists #741

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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)
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 (
Expand Down