Skip to content

Commit

Permalink
Service: systemd support
Browse files Browse the repository at this point in the history
  • Loading branch information
philpep committed May 19, 2015
1 parent 5a1f7f0 commit ef3e132
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
22 changes: 20 additions & 2 deletions testinfra/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ def is_enabled(self):
@classmethod
def as_fixture(cls):
@pytest.fixture(scope="module")
def f(SystemInfo):
def f(SystemInfo, Command, File):
if SystemInfo.type == "linux":
return LinuxService
if (
Command.run_test("which systemctl").rc == 0
and "systemd" in File("/sbin/init").linked_to
):
return SystemdService
else:
return LinuxService
elif SystemInfo.type == "freebsd":
return FreeBSDService
elif SystemInfo.type == "openbsd":
Expand Down Expand Up @@ -84,6 +90,18 @@ def is_enabled_with_level(self, level):
return False


class SystemdService(Service):

@property
def is_running(self):
return self.run_expect(
[0, 3], "systemctl is-active %s", self.name).rc == 0

@property
def is_enabled(self):
return self.run_test("systemctl is-enabled %s", self.name).rc == 0


class FreeBSDService(Service):

@property
Expand Down
12 changes: 9 additions & 3 deletions testinfra/test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ def test_ssh_service(_testinfra_host, Service, SystemInfo):
if _testinfra_host in (
"ubuntu_trusty", "debian_wheezy", "debian_jessie",
):
ssh = Service("ssh")
assert ssh.is_running
assert ssh.is_enabled
name = "ssh"
elif _testinfra_host in (
"centos_7", "fedora_21",
):
name = "sshd"
else:
pytest.skip()

ssh = Service(name)
assert ssh.is_running
assert ssh.is_enabled

0 comments on commit ef3e132

Please sign in to comment.