Fix tests on amazonlinux-2 container

This commit is contained in:
Daniel A. Wozniak 2025-01-07 15:37:12 -07:00 committed by Daniel Wozniak
parent f7e331a746
commit 8909607ab9
3 changed files with 50 additions and 1 deletions

View file

@ -76,6 +76,32 @@ def offline(context=None):
return ret
def status(context=None):
"""Return True if systemd status succeeds. When False, the system may have
been booted with systemd but systemd is in a degraded state.
.. versionadded:: 3006.0
"""
contextkey = "salt.utils.systemd.status"
if isinstance(context, (dict, salt.loader.context.NamedLoaderContext)):
# Can't put this if block on the same line as the above if block,
# because it will break the elif below.
if contextkey in context:
return context[contextkey]
elif context is not None:
raise SaltInvocationError("context must be a dictionary if passed")
proc = subprocess.run(
["systemctl", "status"],
check=False,
capture_output=True,
)
ret = (
b"Failed to get D-Bus connection: No such file or directory" not in proc.stderr
)
context[contextkey] = ret
return ret
def version(context=None):
"""
Attempts to run systemctl --version. Returns None if unable to determine
@ -123,7 +149,10 @@ def has_scope(context=None):
_sd_version = version(context)
if _sd_version is None:
return False
return _sd_version >= 205
if status(context):
return _sd_version >= 205
else:
return False
def pid_to_service(pid):

View file

@ -1,4 +1,5 @@
import os
import subprocess
import pytest
@ -14,6 +15,15 @@ pytestmark = [
]
def _check_systemctl():
if not hasattr(_check_systemctl, "memo"):
proc = subprocess.run(["systemctl"], capture_output=True, check=False)
_check_systemctl.memo = (
b"Failed to get D-Bus connection: No such file or directory" in proc.stderr
)
return _check_systemctl.memo
@pytest.fixture
def service_name(grains, modules):
# For local testing purposes
@ -68,6 +78,7 @@ def setup_service(service_name, modules):
modules.service.disable(service_name)
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_status_running(modules, service_name):
"""
test service.status execution module
@ -88,6 +99,7 @@ def test_service_status_dead(modules, service_name):
assert not check_service
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_restart(modules, service_name):
"""
test service.restart
@ -95,6 +107,7 @@ def test_service_restart(modules, service_name):
assert modules.service.stop(service_name)
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_enable(modules, service_name):
"""
test service.get_enabled and service.enable module
@ -106,6 +119,7 @@ def test_service_enable(modules, service_name):
assert service_name in modules.service.get_enabled()
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_disable(modules, service_name):
"""
test service.get_disabled and service.disable module

View file

@ -25,6 +25,8 @@ def check_hostnamectl():
proc = subprocess.run(["hostnamectl"], capture_output=True, check=False)
check_hostnamectl.memo = (
b"Failed to connect to bus: No such file or directory" in proc.stderr
or b"Failed to create bus connection: No such file or directory"
in proc.stderr
)
return check_hostnamectl.memo
@ -61,6 +63,7 @@ def fmt_str():
@pytest.fixture(scope="function")
def setup_teardown_vars(file, service, system):
_systemd_timesyncd_available_ = None
_orig_time = datetime.datetime.utcnow()
if os.path.isfile("/etc/machine-info"):
@ -192,6 +195,7 @@ def _test_hwclock_sync(system, hwclock_has_compare):
log.error("Failed to check hwclock sync")
@pytest.mark.skipif(check_hostnamectl(), reason="hostnamctl degraded.")
def test_get_system_date_time(setup_teardown_vars, system, fmt_str):
"""
Test we are able to get the correct time
@ -203,6 +207,7 @@ def test_get_system_date_time(setup_teardown_vars, system, fmt_str):
assert _same_times(t1, t2, seconds_diff=3), msg
@pytest.mark.skipif(check_hostnamectl(), reason="hostnamctl degraded.")
def test_get_system_date_time_utc(setup_teardown_vars, system, fmt_str):
"""
Test we are able to get the correct time with utc
@ -406,6 +411,7 @@ def test_set_computer_desc_multiline(setup_teardown_vars, system):
@pytest.mark.skip_if_not_root
@pytest.mark.skipif(check_hostnamectl(), reason="hostnamctl degraded.")
def test_has_hwclock(setup_teardown_vars, system, grains, hwclock_has_compare):
"""
Verify platform has a settable hardware clock, if possible.