From 0593c8cc8e82784d330aefb453eb52d584cde8f4 Mon Sep 17 00:00:00 2001 From: dettlaff Date: Sat, 25 May 2024 01:13:46 +0400 Subject: [PATCH] tests: add tests for flake services manager --- .../services/flake_service_manager.py | 2 +- tests/test_flake_services_manager.py | 72 +++++++++++++++++++ .../no_services.nix | 4 ++ .../some_services.nix | 12 ++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/test_flake_services_manager.py create mode 100644 tests/test_flake_services_manager/no_services.nix create mode 100644 tests/test_flake_services_manager/some_services.nix diff --git a/selfprivacy_api/services/flake_service_manager.py b/selfprivacy_api/services/flake_service_manager.py index 6538c8e..b1851f3 100644 --- a/selfprivacy_api/services/flake_service_manager.py +++ b/selfprivacy_api/services/flake_service_manager.py @@ -19,7 +19,7 @@ class FlakeServiceManager: def _extract_services( self, input_string: str ) -> Tuple[Optional[str], Optional[str]]: - pattern = r"inputs\.(\w+)\.url\s*=\s*(\S+);" + pattern = r"inputs\.([\w-]+)\.url\s*=\s*([\S]+);" match = re.search(pattern, input_string) if match: diff --git a/tests/test_flake_services_manager.py b/tests/test_flake_services_manager.py new file mode 100644 index 0000000..73195bf --- /dev/null +++ b/tests/test_flake_services_manager.py @@ -0,0 +1,72 @@ +import pytest + +from selfprivacy_api.services.flake_service_manager import FlakeServiceManager + + +@pytest.fixture +def some_services_flake_mock(mocker, datadir): + mocker.patch( + "selfprivacy_api.services.flake_service_manager.FLAKE_CONFIG_PATH", + new=datadir / "some_services.nix", + ) + + +@pytest.fixture +def no_services_flake_mock(mocker, datadir): + mocker.patch( + "selfprivacy_api.services.flake_service_manager.FLAKE_CONFIG_PATH", + new=datadir / "no_services.nix", + ) + + +def test_read_services_list(some_services_flake_mock): + + with FlakeServiceManager() as manager: + services = { + "bitwarden": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden", + "gitea": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea", + "jitsi-meet": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet", + } + assert manager.services == services + + +def test_change_services_list(some_services_flake_mock): + services = { + "bitwarden": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden", + "gitea": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea", + "jitsi-meet": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet", + "nextcloud": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/nextcloud", + "ocserv": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/ocserv", + "pleroma": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/pleroma", + "simple-nixos-mailserver": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/simple-nixos-mailserver", + } + + with FlakeServiceManager() as manager: + manager.services = services + + with FlakeServiceManager() as manager: + assert manager.services == services + + +def test_read_empty_services_list(no_services_flake_mock): + with FlakeServiceManager() as manager: + services = {} + assert manager.services == services + + +def test_change_empty_services_list(no_services_flake_mock): + services = { + "bitwarden": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden", + "gitea": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea", + "jitsi-meet": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet", + "nextcloud": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/nextcloud", + "ocserv": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/ocserv", + "pleroma": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/pleroma", + "simple-nixos-mailserver": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/simple-nixos-mailserver", + } + + with FlakeServiceManager() as manager: + manager.services = services + + with FlakeServiceManager() as manager: + assert manager.services == services diff --git a/tests/test_flake_services_manager/no_services.nix b/tests/test_flake_services_manager/no_services.nix new file mode 100644 index 0000000..5967016 --- /dev/null +++ b/tests/test_flake_services_manager/no_services.nix @@ -0,0 +1,4 @@ +{ + description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc"; + outputs = _: { }; +} diff --git a/tests/test_flake_services_manager/some_services.nix b/tests/test_flake_services_manager/some_services.nix new file mode 100644 index 0000000..4bbb919 --- /dev/null +++ b/tests/test_flake_services_manager/some_services.nix @@ -0,0 +1,12 @@ +{ + description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc"; + + + inputs.bitwarden.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden; + + inputs.gitea.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea; + + inputs.jitsi-meet.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet; + + outputs = _: { }; +}