Skip to content

Extend list of valid suffixes for systemd units #740

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

Merged
merged 2 commits 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
44 changes: 37 additions & 7 deletions testinfra/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ def is_enabled(self):


class SystemdService(SysvService):
suffix_list = [
"service",
"socket",
"device",
"mount",
"automount",
"swap",
"target",
"path",
"timer",
"slice",
"scope",
]
"""
List of valid suffixes for systemd unit files

See systemd.unit(5) for more details
"""

def _has_systemd_suffix(self):
"""
Check if service name has a known systemd unit suffix
"""
unit_suffix = self.name.split(".")[-1]
return unit_suffix in self.suffix_list

@property
def is_running(self):
out = self.run_expect([0, 1, 3], "systemctl is-active %s", self.name)
Expand All @@ -158,14 +184,20 @@ def is_enabled(self):
return True
if cmd.stdout.strip() == "disabled":
return False
# Fallback on SysV
# Fallback on SysV - only for non-systemd units
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760616
return super().is_enabled
if not self._has_systemd_suffix():
return super().is_enabled
raise RuntimeError(
"Unable to determine state of {0}. Does this service exist?".format(
self.name
)
)

@property
def is_valid(self):
# systemd-analyze requires a full path.
if self.name.endswith(".service"):
# systemd-analyze requires a full unit name.
if self._has_systemd_suffix():
name = self.name
else:
name = self.name + ".service"
Expand Down Expand Up @@ -264,9 +296,7 @@ def is_enabled(self):
if self.name in self.check_output("rcctl ls off").splitlines():
return False
raise RuntimeError(
"Unable to determine state of {0}. Does this service exist?".format(
self.name
)
f"Unable to determine state of {self.name}. Does this service exist?"
)


Expand Down