Skip to content

Commit 76170a4

Browse files
CarstenGrohmannphilpep
authored andcommitted
Add Service.exists
1 parent 70554d3 commit 76170a4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

testinfra/modules/service.py

+33
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def __init__(self, name):
3333
self.name = name
3434
super().__init__()
3535

36+
@property
37+
def exists(self):
38+
"""Test if service is exists"""
39+
raise NotImplementedError
40+
3641
@property
3742
def is_running(self):
3843
"""Test if service is running"""
@@ -169,6 +174,11 @@ def _has_systemd_suffix(self):
169174
unit_suffix = self.name.split(".")[-1]
170175
return unit_suffix in self.suffix_list
171176

177+
@property
178+
def exists(self):
179+
cmd = self.run_test('systemctl list-unit-files | grep -q"^%s"', self.name)
180+
return cmd.rc == 0
181+
172182
@property
173183
def is_running(self):
174184
out = self.run_expect([0, 1, 3], "systemctl is-active %s", self.name)
@@ -227,6 +237,10 @@ def systemd_properties(self):
227237

228238

229239
class UpstartService(SysvService):
240+
@property
241+
def exists(self):
242+
return self._host.file(f"/etc/init/{self.name}.conf").exists
243+
230244
@property
231245
def is_enabled(self):
232246
if (
@@ -269,6 +283,10 @@ def is_enabled(self):
269283

270284

271285
class FreeBSDService(Service):
286+
@property
287+
def exists(self):
288+
return self._host.file(f"/etc/rc.d/{self.name}").exists
289+
272290
@property
273291
def is_running(self):
274292
return self.run_test("service %s onestatus", self.name).rc == 0
@@ -285,6 +303,10 @@ def is_enabled(self):
285303

286304

287305
class OpenBSDService(Service):
306+
@property
307+
def exists(self):
308+
return self._host.file(f"/etc/rc.d/{self.name}").exists
309+
288310
@property
289311
def is_running(self):
290312
return self.run_test("/etc/rc.d/%s check", self.name).rc == 0
@@ -301,6 +323,10 @@ def is_enabled(self):
301323

302324

303325
class NetBSDService(Service):
326+
@property
327+
def exists(self):
328+
return self._host.file(f"/etc/rc.d/{self.name}").exists
329+
304330
@property
305331
def is_running(self):
306332
return self.run_test("/etc/rc.d/%s onestatus", self.name).rc == 0
@@ -311,6 +337,13 @@ def is_enabled(self):
311337

312338

313339
class WindowsService(Service):
340+
@property
341+
def exists(self):
342+
out = self.check_output(
343+
f"Get-Service -Name {self.name} -ErrorAction SilentlyContinue"
344+
)
345+
return self.name in out
346+
314347
@property
315348
def is_running(self):
316349
return (

0 commit comments

Comments
 (0)