Skip to content

Commit 5d5c51f

Browse files
CarstenGrohmannphilpep
authored andcommitted
Extend list of valid suffixes for systemd units
The old implementation limits the usage of SystemdService.is_valid() to ".service" units. This implementation allows checking all types of unit files with is_valid() (systemd-analyze verify <unitname.suffix>).
1 parent 2f78038 commit 5d5c51f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

testinfra/modules/service.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ def is_enabled(self):
143143

144144

145145
class SystemdService(SysvService):
146+
suffix_list = [
147+
"service",
148+
"socket",
149+
"device",
150+
"mount",
151+
"automount",
152+
"swap",
153+
"target",
154+
"path",
155+
"timer",
156+
"slice",
157+
"scope",
158+
]
159+
"""
160+
List of valid suffixes for systemd unit files
161+
162+
See systemd.unit(5) for more details
163+
"""
164+
165+
def _has_systemd_suffix(self):
166+
"""
167+
Check if service name has a known systemd unit suffix
168+
"""
169+
unit_suffix = self.name.split(".")[-1]
170+
return unit_suffix in self.suffix_list
171+
146172
@property
147173
def is_running(self):
148174
out = self.run_expect([0, 1, 3], "systemctl is-active %s", self.name)
@@ -164,8 +190,8 @@ def is_enabled(self):
164190

165191
@property
166192
def is_valid(self):
167-
# systemd-analyze requires a full path.
168-
if self.name.endswith(".service"):
193+
# systemd-analyze requires a full unit name.
194+
if self._has_systemd_suffix():
169195
name = self.name
170196
else:
171197
name = self.name + ".service"

0 commit comments

Comments
 (0)