diff --git a/pkgs/clan-cli/tests/clan_flake.py b/pkgs/clan-cli/tests/clan_flake.py index e69de29bb..b0bc1e7a3 100644 --- a/pkgs/clan-cli/tests/clan_flake.py +++ b/pkgs/clan-cli/tests/clan_flake.py @@ -0,0 +1,34 @@ +import shutil +import tempfile +from pathlib import Path +from typing import Generator + +import pytest + +from clan_cli.dirs import nixpkgs_source + + +@pytest.fixture(scope="module") +def monkeymodule() -> Generator[pytest.MonkeyPatch, None, None]: + with pytest.MonkeyPatch.context() as mp: + yield mp + + +@pytest.fixture(scope="module") +def clan_flake(monkeymodule: pytest.MonkeyPatch) -> Generator[Path, None, None]: + template = Path(__file__).parent / "clan_flake" + # copy the template to a new temporary location + with tempfile.TemporaryDirectory() as tmpdir_: + home = Path(tmpdir_) + flake = home / "clan_flake" + shutil.copytree(template, flake) + # in the flake.nix file replace the string __CLAN_URL__ with the the clan flake + # provided by get_clan_flake_toplevel + flake_nix = flake / "flake.nix" + flake_nix.write_text( + flake_nix.read_text().replace("__NIXPKGS__", str(nixpkgs_source())) + ) + # check that an empty config is returned if no json file exists + monkeymodule.chdir(flake) + monkeymodule.setenv("HOME", str(home)) + yield flake diff --git a/pkgs/clan-cli/tests/machine_flake/.clan-flake b/pkgs/clan-cli/tests/clan_flake/.clan-flake similarity index 100% rename from pkgs/clan-cli/tests/machine_flake/.clan-flake rename to pkgs/clan-cli/tests/clan_flake/.clan-flake diff --git a/pkgs/clan-cli/tests/machine_flake/flake.nix b/pkgs/clan-cli/tests/clan_flake/flake.nix similarity index 100% rename from pkgs/clan-cli/tests/machine_flake/flake.nix rename to pkgs/clan-cli/tests/clan_flake/flake.nix diff --git a/pkgs/clan-cli/tests/machine_flake/nixosModules/machine1.nix b/pkgs/clan-cli/tests/clan_flake/nixosModules/machine1.nix similarity index 100% rename from pkgs/clan-cli/tests/machine_flake/nixosModules/machine1.nix rename to pkgs/clan-cli/tests/clan_flake/nixosModules/machine1.nix diff --git a/pkgs/clan-cli/tests/conftest.py b/pkgs/clan-cli/tests/conftest.py index 511698abd..7d0981d19 100644 --- a/pkgs/clan-cli/tests/conftest.py +++ b/pkgs/clan-cli/tests/conftest.py @@ -12,5 +12,5 @@ pytest_plugins = [ "command", "ports", "host_group", - "machine_flake", + "clan_flake", ] diff --git a/pkgs/clan-cli/tests/machine_flake.py b/pkgs/clan-cli/tests/machine_flake.py deleted file mode 100644 index 8660c58ef..000000000 --- a/pkgs/clan-cli/tests/machine_flake.py +++ /dev/null @@ -1,34 +0,0 @@ -import shutil -import tempfile -from pathlib import Path -from typing import Generator - -import pytest - -from clan_cli.dirs import nixpkgs_source - - -@pytest.fixture(scope="module") -def monkeymodule() -> Generator[pytest.MonkeyPatch, None, None]: - with pytest.MonkeyPatch.context() as mp: - yield mp - - -@pytest.fixture(scope="module") -def machine_flake(monkeymodule: pytest.MonkeyPatch) -> Generator[Path, None, None]: - template = Path(__file__).parent / "machine_flake" - # copy the template to a new temporary location - with tempfile.TemporaryDirectory() as tmpdir_: - home = Path(tmpdir_) - flake = home / "machine_flake" - shutil.copytree(template, flake) - # in the flake.nix file replace the string __CLAN_URL__ with the the clan flake - # provided by get_clan_flake_toplevel - flake_nix = flake / "flake.nix" - flake_nix.write_text( - flake_nix.read_text().replace("__NIXPKGS__", str(nixpkgs_source())) - ) - # check that an empty config is returned if no json file exists - monkeymodule.chdir(flake) - monkeymodule.setenv("HOME", str(home)) - yield flake diff --git a/pkgs/clan-cli/tests/machine_flake/.clan_flake b/pkgs/clan-cli/tests/machine_flake/.clan_flake deleted file mode 100644 index e69de29bb..000000000 diff --git a/pkgs/clan-cli/tests/test_config.py b/pkgs/clan-cli/tests/test_config.py index 79fc2d883..3def8ce9d 100644 --- a/pkgs/clan-cli/tests/test_config.py +++ b/pkgs/clan-cli/tests/test_config.py @@ -50,7 +50,7 @@ def test_set_some_option( def test_configure_machine( - machine_flake: Path, + clan_flake: Path, temporary_dir: Path, capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch, diff --git a/pkgs/clan-cli/tests/test_import_sops_cli.py b/pkgs/clan-cli/tests/test_import_sops_cli.py index 903b2b175..6ea6a854f 100644 --- a/pkgs/clan-cli/tests/test_import_sops_cli.py +++ b/pkgs/clan-cli/tests/test_import_sops_cli.py @@ -10,7 +10,7 @@ if TYPE_CHECKING: def test_import_sops( test_root: Path, - machine_flake: Path, + clan_flake: Path, capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch, age_keys: list["KeyPair"], diff --git a/pkgs/clan-cli/tests/test_machines_api.py b/pkgs/clan-cli/tests/test_machines_api.py index 6663b7dee..aced71a62 100644 --- a/pkgs/clan-cli/tests/test_machines_api.py +++ b/pkgs/clan-cli/tests/test_machines_api.py @@ -3,7 +3,7 @@ from pathlib import Path from api import TestClient -def test_machines(api: TestClient, machine_flake: Path) -> None: +def test_machines(api: TestClient, clan_flake: Path) -> None: response = api.get("/api/machines") assert response.status_code == 200 assert response.json() == {"machines": []} @@ -21,7 +21,7 @@ def test_machines(api: TestClient, machine_flake: Path) -> None: assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]} -def test_configure_machine(api: TestClient, machine_flake: Path) -> None: +def test_configure_machine(api: TestClient, clan_flake: Path) -> None: # ensure error 404 if machine does not exist when accessing the config response = api.get("/api/machines/machine1/config") assert response.status_code == 404 diff --git a/pkgs/clan-cli/tests/test_machines_cli.py b/pkgs/clan-cli/tests/test_machines_cli.py index 4e94d3f35..a2e3fa83e 100644 --- a/pkgs/clan-cli/tests/test_machines_cli.py +++ b/pkgs/clan-cli/tests/test_machines_cli.py @@ -4,9 +4,7 @@ import pytest from cli import Cli -def test_machine_subcommands( - machine_flake: Path, capsys: pytest.CaptureFixture -) -> None: +def test_machine_subcommands(clan_flake: Path, capsys: pytest.CaptureFixture) -> None: cli = Cli() cli.run(["machines", "create", "machine1"]) diff --git a/pkgs/clan-cli/tests/test_machines_config.py b/pkgs/clan-cli/tests/test_machines_config.py index 248443535..c7fe80caf 100644 --- a/pkgs/clan-cli/tests/test_machines_config.py +++ b/pkgs/clan-cli/tests/test_machines_config.py @@ -3,6 +3,6 @@ from pathlib import Path from clan_cli.config import machine -def test_schema_for_machine(machine_flake: Path) -> None: - schema = machine.schema_for_machine("machine1", machine_flake) +def test_schema_for_machine(clan_flake: Path) -> None: + schema = machine.schema_for_machine("machine1", clan_flake) assert "properties" in schema diff --git a/pkgs/clan-cli/tests/test_machines_update_cli.py b/pkgs/clan-cli/tests/test_machines_update_cli.py index 7da100125..62650f3de 100644 --- a/pkgs/clan-cli/tests/test_machines_update_cli.py +++ b/pkgs/clan-cli/tests/test_machines_update_cli.py @@ -8,13 +8,13 @@ from host_group import HostGroup def test_update( - machine_flake: Path, host_group: HostGroup, monkeypatch: pytest.MonkeyPatch + clan_flake: Path, host_group: HostGroup, monkeypatch: pytest.MonkeyPatch ) -> None: assert len(host_group.hosts) == 1 host = host_group.hosts[0] with TemporaryDirectory() as tmpdir: - host.meta["flake_uri"] = machine_flake + host.meta["flake_uri"] = clan_flake host.meta["flake_path"] = str(Path(tmpdir) / "rsync-target") host.ssh_options["SendEnv"] = "REALPATH" bin = Path(tmpdir).joinpath("bin") diff --git a/pkgs/clan-cli/tests/test_secrets_cli.py b/pkgs/clan-cli/tests/test_secrets_cli.py index 4cca9fcfd..af604a85a 100644 --- a/pkgs/clan-cli/tests/test_secrets_cli.py +++ b/pkgs/clan-cli/tests/test_secrets_cli.py @@ -14,12 +14,12 @@ if TYPE_CHECKING: def _test_identities( what: str, - machine_flake: Path, + clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"], ) -> None: cli = Cli() - sops_folder = machine_flake / "sops" + sops_folder = clan_flake / "sops" cli.run(["secrets", what, "add", "foo", age_keys[0].pubkey]) assert (sops_folder / what / "foo" / "key.json").exists() @@ -60,19 +60,19 @@ def _test_identities( def test_users( - machine_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] + clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] ) -> None: - _test_identities("users", machine_flake, capsys, age_keys) + _test_identities("users", clan_flake, capsys, age_keys) def test_machines( - machine_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] + clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] ) -> None: - _test_identities("machines", machine_flake, capsys, age_keys) + _test_identities("machines", clan_flake, capsys, age_keys) def test_groups( - machine_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] + clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"] ) -> None: cli = Cli() capsys.readouterr() # empty the buffer @@ -100,7 +100,7 @@ def test_groups( cli.run(["secrets", "groups", "remove-user", "group1", "user1"]) cli.run(["secrets", "groups", "remove-machine", "group1", "machine1"]) - groups = os.listdir(machine_flake / "sops" / "groups") + groups = os.listdir(clan_flake / "sops" / "groups") assert len(groups) == 0 @@ -114,7 +114,7 @@ def use_key(key: str, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: def test_secrets( - machine_flake: Path, + clan_flake: Path, capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch, age_keys: list["KeyPair"], @@ -125,7 +125,7 @@ def test_secrets( assert capsys.readouterr().out == "" monkeypatch.setenv("SOPS_NIX_SECRET", "foo") - monkeypatch.setenv("SOPS_AGE_KEY_FILE", str(machine_flake / ".." / "age.key")) + monkeypatch.setenv("SOPS_AGE_KEY_FILE", str(clan_flake / ".." / "age.key")) cli.run(["secrets", "key", "generate"]) capsys.readouterr() # empty the buffer cli.run(["secrets", "key", "show"])