2021-11-29 19:16:08 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-11-24 14:16:42 +00:00
|
|
|
from selfprivacy_api.services.service import ServiceStatus
|
|
|
|
from selfprivacy_api.services.bitwarden import Bitwarden
|
|
|
|
from selfprivacy_api.services.gitea import Gitea
|
|
|
|
from selfprivacy_api.services.mailserver import MailServer
|
|
|
|
from selfprivacy_api.services.nextcloud import Nextcloud
|
|
|
|
from selfprivacy_api.services.ocserv import Ocserv
|
|
|
|
from selfprivacy_api.services.pleroma import Pleroma
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2023-12-25 16:36:40 +00:00
|
|
|
def expected_status_call(service_name: str):
|
|
|
|
return ["systemctl", "show", service_name]
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def call_args_asserts(mocked_object):
|
2022-08-25 17:03:56 +00:00
|
|
|
assert mocked_object.call_count == 7
|
2023-12-25 16:36:40 +00:00
|
|
|
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",
|
|
|
|
"gitea.service",
|
|
|
|
"phpfpm-nextcloud.service",
|
|
|
|
"ocserv.service",
|
|
|
|
"pleroma.service",
|
|
|
|
]
|
2021-11-29 19:16:08 +00:00
|
|
|
]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2022-08-25 17:03:56 +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
|
|
|
|
"""
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-12-25 16:36:40 +00:00
|
|
|
def mock_popen_systemctl_service_ok(mocker):
|
2022-08-25 17:03:56 +00:00
|
|
|
mock = mocker.patch(
|
|
|
|
"subprocess.check_output", autospec=True, return_value=SUCCESSFUL_STATUS
|
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-12-25 16:36:40 +00:00
|
|
|
def mock_popen_systemctl_service_not_ok(mocker):
|
2021-11-29 19:16:08 +00:00
|
|
|
mock = mocker.patch(
|
2022-08-25 17:03:56 +00:00
|
|
|
"subprocess.check_output", autospec=True, return_value=FAILED_STATUS
|
2021-11-29 19:16:08 +00:00
|
|
|
)
|
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2023-12-25 16:36:40 +00:00
|
|
|
|
|
|
|
def test_systemctl_ok(mock_popen_systemctl_service_ok):
|
|
|
|
assert MailServer.get_status() == ServiceStatus.ACTIVE
|
2023-11-24 14:16:42 +00:00
|
|
|
assert Bitwarden.get_status() == ServiceStatus.ACTIVE
|
|
|
|
assert Gitea.get_status() == ServiceStatus.ACTIVE
|
|
|
|
assert Nextcloud.get_status() == ServiceStatus.ACTIVE
|
|
|
|
assert Ocserv.get_status() == ServiceStatus.ACTIVE
|
|
|
|
assert Pleroma.get_status() == ServiceStatus.ACTIVE
|
2023-12-25 16:36:40 +00:00
|
|
|
call_args_asserts(mock_popen_systemctl_service_ok)
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
2023-12-25 16:36:40 +00:00
|
|
|
def test_systemctl_failed_service(mock_popen_systemctl_service_not_ok):
|
|
|
|
assert MailServer.get_status() == ServiceStatus.FAILED
|
2023-11-24 14:16:42 +00:00
|
|
|
assert Bitwarden.get_status() == ServiceStatus.FAILED
|
|
|
|
assert Gitea.get_status() == ServiceStatus.FAILED
|
|
|
|
assert Nextcloud.get_status() == ServiceStatus.FAILED
|
|
|
|
assert Ocserv.get_status() == ServiceStatus.FAILED
|
|
|
|
assert Pleroma.get_status() == ServiceStatus.FAILED
|
2023-12-25 16:36:40 +00:00
|
|
|
call_args_asserts(mock_popen_systemctl_service_not_ok)
|