selfprivacy-rest-api/tests/test_services_systemctl.py

80 lines
1.9 KiB
Python
Raw Permalink Normal View History

import pytest
from selfprivacy_api.services.service import ServiceStatus
from selfprivacy_api.services.mailserver import MailServer
2021-11-30 21:53:39 +00:00
def expected_status_call(service_name: str):
return ["systemctl", "show", service_name]
def call_args_asserts(mocked_object):
assert mocked_object.call_count == 7
calls = [callargs[0][0] for callargs in mocked_object.call_args_list]
assert calls == [
expected_status_call(service)
for service in [
"dovecot2.service",
"postfix.service",
"vaultwarden.service",
2024-06-30 19:02:07 +00:00
"forgejo.service",
"phpfpm-nextcloud.service",
"ocserv.service",
"pleroma.service",
]
]
2021-11-30 21:53:39 +00:00
SUCCESSFUL_STATUS = b"""
Type=oneshot
ExitType=main
Restart=no
NotifyAccess=none
RestartUSec=100ms
LoadState=loaded
ActiveState=active
FreezerState=running
SubState=exited
"""
FAILED_STATUS = b"""
Type=oneshot
ExitType=main
Restart=no
NotifyAccess=none
RestartUSec=100ms
LoadState=loaded
ActiveState=failed
FreezerState=running
SubState=exited
"""
@pytest.fixture
def mock_popen_systemctl_service_ok(mocker):
mock = mocker.patch(
"subprocess.check_output", autospec=True, return_value=SUCCESSFUL_STATUS
)
return mock
@pytest.fixture
def mock_popen_systemctl_service_not_ok(mocker):
mock = mocker.patch(
"subprocess.check_output", autospec=True, return_value=FAILED_STATUS
)
return mock
###############################################################################
2024-12-24 17:02:04 +00:00
# def test_systemctl_ok(mock_popen_systemctl_service_ok):
# assert MailServer.get_status() == ServiceStatus.ACTIVE
# call_args_asserts(mock_popen_systemctl_service_ok)
2024-12-24 17:02:04 +00:00
# def test_systemctl_failed_service(mock_popen_systemctl_service_not_ok):
# assert MailServer.get_status() == ServiceStatus.FAILED
# call_args_asserts(mock_popen_systemctl_service_not_ok)