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

Update check in SystemdService.exists and add tests #754

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
74 changes: 41 additions & 33 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,31 @@
]
)

# content: ssh version, release shortcut, service name
ssh_pkg_info = {
"rockylinux9": ("8.", ".el9", "sshd"),
"debian_bookworm": ("1:9.2", None, "ssh"),
}

# content: distribution, codename, architecture, release_regex
docker_image_info = {
"rockylinux9": ("rocky", None, "x86_64", r"^9.\d+$"),
"debian_bookworm": ("debian", "bookworm", "amd64", r"^12"),
}


@all_images
def test_package(host, docker_image):
assert not host.package("zsh").is_installed
ssh = host.package("openssh-server")
version = {
"rockylinux9": "8.",
"debian_bookworm": "1:9.2",
}[docker_image]
ssh_version, sshd_release = ssh_pkg_info[docker_image][:2]
assert ssh.is_installed
assert ssh.version.startswith(version)
release = {
"rockylinux9": ".el9",
"debian_bookworm": None,
}[docker_image]
if release is None:
assert ssh.version.startswith(ssh_version)
if sshd_release is None:
with pytest.raises(NotImplementedError):
ssh.release
else:
assert release in ssh.release
assert sshd_release in ssh.release


def test_held_package(host):
Expand Down Expand Up @@ -89,42 +94,46 @@ def test_uninstalled_package_version(host):
def test_systeminfo(host, docker_image):
assert host.system_info.type == "linux"

release, distribution, codename, arch = {
"rockylinux9": (r"^9.\d+$", "rocky", None, "x86_64"),
"debian_bookworm": (r"^12", "debian", "bookworm", "x86_64"),
}[docker_image]

distribution, codename, unused_arch, release_regex = docker_image_info[docker_image]
assert host.system_info.distribution == distribution
assert host.system_info.codename == codename
assert re.match(release, host.system_info.release)
assert re.match(release_regex, host.system_info.release)


@all_images
def test_ssh_service(host, docker_image):
if docker_image == "rockylinux9":
name = "sshd"
else:
name = "ssh"

ssh = host.service(name)
service_name = ssh_pkg_info[docker_image][2]
ssh_svc = host.service(service_name)
# wait at max 10 seconds for ssh is running
for _ in range(10):
if ssh.is_running:
if ssh_svc.is_running:
break
time.sleep(1)
else:
raise AssertionError("ssh is not running")

assert ssh.is_enabled
assert ssh_svc.is_enabled


@all_images
def test_systemdservice_exists(host, docker_image):
service_name = ssh_pkg_info[docker_image][2]
for name in [service_name, f"{service_name}.service"]:
ssh_svc = host.service(name)
assert ssh_svc.exists

for name in ["non-existing", "non-existing.service", "non-existing.timer"]:
non_existing_service = host.service(name)
assert not non_existing_service.exists


def test_service_systemd_mask(host):
ssh = host.service("ssh")
assert not ssh.is_masked
ssh_svc = host.service("ssh")
assert not ssh_svc.is_masked
host.run("systemctl mask ssh")
assert ssh.is_masked
assert ssh_svc.is_masked
host.run("systemctl unmask ssh")
assert not ssh.is_masked
assert not ssh_svc.is_masked


def test_salt(host):
Expand Down Expand Up @@ -318,7 +327,7 @@ def test_file(host):


def test_ansible_unavailable(host):
expected = "Ansible module is only available with " "ansible connection backend"
expected = "Ansible module is only available with ansible connection backend"
with pytest.raises(RuntimeError) as excinfo:
host.ansible("setup")
assert expected in str(excinfo.value)
Expand Down Expand Up @@ -428,9 +437,8 @@ def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
)
if service.status == "RUNNING":
break
else:
assert service.status == "STARTING"
time.sleep(0.5)
assert service.status == "STARTING"
time.sleep(0.5)
else:
raise RuntimeError("No running tail in supervisor")

Expand Down
7 changes: 6 additions & 1 deletion testinfra/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ def _has_systemd_suffix(self):

@property
def exists(self):
cmd = self.run_test('systemctl list-unit-files | grep -q"^%s"', self.name)
# list-unit-files: Older systemd versions only show native unit files
# list-units: List all units that Systemd currently has in memory,
# this includes generated units like SysV files also
cmd = self.run_test(
r'systemctl list-units --all | grep -q "^[[:space:]]*%s"', self.name
)
return cmd.rc == 0

@property
Expand Down