diff --git a/pkgs/clan-cli/clan_cli/webui/routers/machines.py b/pkgs/clan-cli/clan_cli/webui/routers/machines.py index f7aa419cf..8367b8de0 100644 --- a/pkgs/clan-cli/clan_cli/webui/routers/machines.py +++ b/pkgs/clan-cli/clan_cli/webui/routers/machines.py @@ -46,7 +46,7 @@ async def create_machine( return MachineResponse(machine=Machine(name=machine.name, status=Status.UNKNOWN)) -@router.get("/api/machines/{name}") +@router.get("/api/{flake_name}/machines/{name}") async def get_machine(name: str) -> MachineResponse: log.error("TODO") return MachineResponse(machine=Machine(name=name, status=Status.UNKNOWN)) diff --git a/pkgs/clan-cli/tests/command.py b/pkgs/clan-cli/tests/command.py index 72551ba31..ba58243a5 100644 --- a/pkgs/clan-cli/tests/command.py +++ b/pkgs/clan-cli/tests/command.py @@ -1,7 +1,8 @@ import os import signal import subprocess -from typing import IO, Any, Dict, Iterator, List, Union +from pathlib import Path +from typing import IO, Any, Dict, Iterator, List, Optional, Union import pytest @@ -19,6 +20,7 @@ class Command: stdin: _FILE = None, stdout: _FILE = None, stderr: _FILE = None, + workdir: Optional[Path] = None, ) -> subprocess.Popen[str]: env = os.environ.copy() env.update(extra_env) @@ -31,6 +33,7 @@ class Command: stderr=stderr, stdin=stdin, text=True, + cwd=workdir, ) self.processes.append(p) return p diff --git a/pkgs/clan-cli/tests/fixtures_flakes.py b/pkgs/clan-cli/tests/fixtures_flakes.py index ebedf19d4..575be18ea 100644 --- a/pkgs/clan-cli/tests/fixtures_flakes.py +++ b/pkgs/clan-cli/tests/fixtures_flakes.py @@ -6,6 +6,7 @@ from pathlib import Path from typing import Iterator, NamedTuple import pytest +from command import Command from root import CLAN_CORE from clan_cli.dirs import nixpkgs_source @@ -40,6 +41,7 @@ def create_flake( monkeypatch: pytest.MonkeyPatch, temporary_home: Path, flake_name: FlakeName, + command: Command, clan_core_flake: Path | None = None, machines: list[str] = [], remote: bool = False, @@ -67,6 +69,14 @@ def create_flake( flake_nix = flake / "flake.nix" # this is where we would install the sops key to, when updating substitute(flake_nix, clan_core_flake, flake) + + # Init git + command.run(["git", "init"], workdir=flake) + command.run(["git", "add", "."], workdir=flake) + command.run(["git", "config", "user.name", "clan-tool"], workdir=flake) + command.run(["git", "config", "user.email", "clan@example.com"], workdir=flake) + command.run(["git", "commit", "-a", "-m", "Initial commit"], workdir=flake) + if remote: with tempfile.TemporaryDirectory() as workdir: monkeypatch.chdir(workdir) @@ -80,28 +90,33 @@ def create_flake( @pytest.fixture def test_flake( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path + monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command ) -> Iterator[FlakeForTest]: - yield from create_flake(monkeypatch, temporary_home, FlakeName("test_flake")) + yield from create_flake( + monkeypatch, temporary_home, FlakeName("test_flake"), command + ) @pytest.fixture def test_flake_with_core( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path + monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command ) -> Iterator[FlakeForTest]: if not (CLAN_CORE / "flake.nix").exists(): raise Exception( "clan-core flake not found. This test requires the clan-core flake to be present" ) yield from create_flake( - monkeypatch, temporary_home, FlakeName("test_flake_with_core"), CLAN_CORE + monkeypatch, + temporary_home, + FlakeName("test_flake_with_core"), + command, + CLAN_CORE, ) @pytest.fixture def test_flake_with_core_and_pass( - monkeypatch: pytest.MonkeyPatch, - temporary_home: Path, + monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command ) -> Iterator[FlakeForTest]: if not (CLAN_CORE / "flake.nix").exists(): raise Exception( @@ -111,5 +126,6 @@ def test_flake_with_core_and_pass( monkeypatch, temporary_home, FlakeName("test_flake_with_core_and_pass"), + command, CLAN_CORE, ) diff --git a/pkgs/clan-cli/tests/test_import_sops_cli.py b/pkgs/clan-cli/tests/test_import_sops_cli.py index 58d8e3f7e..5815f8d17 100644 --- a/pkgs/clan-cli/tests/test_import_sops_cli.py +++ b/pkgs/clan-cli/tests/test_import_sops_cli.py @@ -41,7 +41,7 @@ def test_import_sops( str(test_root.joinpath("data", "secrets.yaml")), test_flake.name, ] - repro_env_break(work_dir=test_flake.path, cmd=cmd) + cli.run(cmd) capsys.readouterr() cli.run(["secrets", "users", "list", test_flake.name]) diff --git a/pkgs/clan-cli/tests/test_machines_api.py b/pkgs/clan-cli/tests/test_machines_api.py index 7287f18a4..1249949db 100644 --- a/pkgs/clan-cli/tests/test_machines_api.py +++ b/pkgs/clan-cli/tests/test_machines_api.py @@ -7,9 +7,9 @@ def test_machines(api: TestClient, test_flake: FlakeForTest) -> None: assert response.status_code == 200 assert response.json() == {"machines": []} - # TODO: Fails because the test_flake fixture needs to init a git repo, which it currently does not response = api.post(f"/api/{test_flake.name}/machines", json={"name": "test"}) assert response.status_code == 201 + assert response.json() == {"machine": {"name": "test", "status": "unknown"}} response = api.get(f"/api/{test_flake.name}/machines/test") @@ -91,13 +91,13 @@ def test_configure_machine(api: TestClient, test_flake: FlakeForTest) -> None: devices=["/dev/fake_disk"], ), ), - f"/api/{test_flake.name}machines/machine1/config", + json=dict( clan=dict( jitsi=True, ) ), - ) + )) # set some valid config config2 = dict( diff --git a/pkgs/clan-cli/tests/test_machines_cli.py b/pkgs/clan-cli/tests/test_machines_cli.py index 6b9264574..b63a5c7a5 100644 --- a/pkgs/clan-cli/tests/test_machines_cli.py +++ b/pkgs/clan-cli/tests/test_machines_cli.py @@ -2,20 +2,20 @@ from pathlib import Path import pytest from cli import Cli +from fixtures_flakes import FlakeForTest - -def test_machine_subcommands(test_flake: Path, capsys: pytest.CaptureFixture) -> None: +def test_machine_subcommands(test_flake: FlakeForTest, capsys: pytest.CaptureFixture) -> None: cli = Cli() - cli.run(["machines", "create", "machine1"]) + cli.run(["machines", "create", "machine1", test_flake.name]) capsys.readouterr() - cli.run(["machines", "list"]) + cli.run(["machines", "list", test_flake.name]) out = capsys.readouterr() assert "machine1\n" == out.out - cli.run(["machines", "remove", "machine1"]) + cli.run(["machines", "delete", "machine1", test_flake.name]) capsys.readouterr() - cli.run(["machines", "list"]) + cli.run(["machines", "list", test_flake.name]) out = capsys.readouterr() assert "" == out.out diff --git a/pkgs/clan-cli/tests/test_vms_api_create.py b/pkgs/clan-cli/tests/test_vms_api_create.py index 71b7b81be..820eefd33 100644 --- a/pkgs/clan-cli/tests/test_vms_api_create.py +++ b/pkgs/clan-cli/tests/test_vms_api_create.py @@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Iterator import pytest from api import TestClient from cli import Cli +from command import Command from fixtures_flakes import FlakeForTest, create_flake from httpx import SyncByteStream from root import CLAN_CORE @@ -17,12 +18,13 @@ if TYPE_CHECKING: @pytest.fixture def flake_with_vm_with_secrets( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path + monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command ) -> Iterator[FlakeForTest]: yield from create_flake( monkeypatch, temporary_home, FlakeName("test_flake_with_core_dynamic_machines"), + command, CLAN_CORE, machines=["vm_with_secrets"], ) @@ -30,12 +32,13 @@ def flake_with_vm_with_secrets( @pytest.fixture def remote_flake_with_vm_without_secrets( - monkeypatch: pytest.MonkeyPatch, temporary_home: Path + monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command ) -> Iterator[FlakeForTest]: yield from create_flake( monkeypatch, temporary_home, FlakeName("test_flake_with_core_dynamic_machines"), + command, CLAN_CORE, machines=["vm_without_secrets"], remote=True,