diff --git a/pkgs/clan-cli/clan_cli/config/machine.py b/pkgs/clan-cli/clan_cli/config/machine.py index 0bf2de590..37187f1aa 100644 --- a/pkgs/clan-cli/clan_cli/config/machine.py +++ b/pkgs/clan-cli/clan_cli/config/machine.py @@ -4,7 +4,7 @@ import sys from pathlib import Path from typing import Optional -from ..dirs import get_clan_flake_toplevel +from clan_cli.dirs import get_clan_flake_toplevel def config_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict: @@ -14,9 +14,7 @@ def config_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict: # read the config from a json file located at {flake}/machines/{machine_name}.json config_path = flake / "machines" / f"{machine_name}.json" if not config_path.exists(): - raise Exception( - f"Machine {machine_name} does not exist in {flake / 'machines'}" - ) + return {} with open(config_path) as f: return json.load(f) @@ -29,6 +27,7 @@ def set_config_for_machine( flake = get_clan_flake_toplevel() # write the config to a json file located at {flake}/machines/{machine_name}.json config_path = flake / "machines" / f"{machine_name}.json" + config_path.parent.mkdir(parents=True, exist_ok=True) with open(config_path, "w") as f: json.dump(config, f) diff --git a/pkgs/clan-cli/tests/config/test_machine_schema.py b/pkgs/clan-cli/tests/config/test_machine_schema.py deleted file mode 100644 index 799f7f075..000000000 --- a/pkgs/clan-cli/tests/config/test_machine_schema.py +++ /dev/null @@ -1,44 +0,0 @@ -import os -import tempfile -from pathlib import Path -from typing import Generator - -import pytest - -from clan_cli.config import machine - -CLAN_NIXPKGS = os.environ.get("CLAN_NIXPKGS", "") -if CLAN_NIXPKGS == "": - raise Exception("CLAN_NIXPKGS not set") - - -# fixture for the example flake located under ./example_flake -# The flake is a template that is copied to a temporary location. -# Variables like __CLAN_NIXPKGS__ are replaced with the value of the -# CLAN_NIXPKGS environment variable. -@pytest.fixture -def flake_dir() -> Generator[Path, None, None]: - template = Path(__file__).parent / "example_flake" - # copy the template to a new temporary location - with tempfile.TemporaryDirectory() as tmpdir_: - tmpdir = Path(tmpdir_) - for path in template.glob("**/*"): - if path.is_dir(): - (tmpdir / path.relative_to(template)).mkdir() - else: - (tmpdir / path.relative_to(template)).write_text(path.read_text()) - # in the flake.nix file replace the string __CLAN_URL__ with the the clan flake - # provided by get_clan_flake_toplevel - flake_nix = tmpdir / "flake.nix" - flake_nix.write_text( - flake_nix.read_text().replace("__CLAN_NIXPKGS__", CLAN_NIXPKGS) - ) - yield tmpdir - - -def test_schema_for_machine( - flake_dir: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.chdir(tmp_path) - schema = machine.schema_for_machine("machine1", flake_dir) - assert "properties" in schema diff --git a/pkgs/clan-cli/tests/conftest.py b/pkgs/clan-cli/tests/conftest.py index 4eea3deee..1bf68a708 100644 --- a/pkgs/clan-cli/tests/conftest.py +++ b/pkgs/clan-cli/tests/conftest.py @@ -1,5 +1,10 @@ import os import sys +import tempfile +from pathlib import Path +from typing import Generator + +import pytest sys.path.append(os.path.join(os.path.dirname(__file__), "helpers")) @@ -14,3 +19,40 @@ pytest_plugins = [ "ports", "host_group", ] + + +@pytest.fixture(scope="module") +def monkeymodule() -> Generator[pytest.MonkeyPatch, None, None]: + with pytest.MonkeyPatch.context() as mp: + yield mp + + +# fixture for the example flake located under ./example_flake +# The flake is a template that is copied to a temporary location. +# Variables like __CLAN_NIXPKGS__ are replaced with the value of the +# CLAN_NIXPKGS environment variable. +@pytest.fixture(scope="module") +def machine_flake(monkeymodule: pytest.MonkeyPatch) -> Generator[Path, None, None]: + CLAN_NIXPKGS = os.environ.get("CLAN_NIXPKGS", "") + if CLAN_NIXPKGS == "": + raise Exception("CLAN_NIXPKGS not set") + template = Path(__file__).parent / "machine_flake" + # copy the template to a new temporary location + with tempfile.TemporaryDirectory() as tmpdir_: + flake = Path(tmpdir_) + for path in template.glob("**/*"): + if path.is_dir(): + (flake / path.relative_to(template)).mkdir() + else: + (flake / path.relative_to(template)).write_text(path.read_text()) + # 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("__CLAN_NIXPKGS__", CLAN_NIXPKGS) + ) + # check that an empty config is returned if no json file exists + monkeymodule.chdir(flake) + # monkeypatch get_clan_flake_toplevel to return the flake + monkeymodule.setattr("clan_cli.dirs.get_clan_flake_toplevel", lambda: flake) + yield flake diff --git a/pkgs/clan-cli/tests/config/example_flake/clanModules/machine1.nix b/pkgs/clan-cli/tests/machine_flake/clanModules/machine1.nix similarity index 100% rename from pkgs/clan-cli/tests/config/example_flake/clanModules/machine1.nix rename to pkgs/clan-cli/tests/machine_flake/clanModules/machine1.nix diff --git a/pkgs/clan-cli/tests/config/example_flake/flake.nix b/pkgs/clan-cli/tests/machine_flake/flake.nix similarity index 100% rename from pkgs/clan-cli/tests/config/example_flake/flake.nix rename to pkgs/clan-cli/tests/machine_flake/flake.nix diff --git a/pkgs/clan-cli/tests/test_api_machines.py b/pkgs/clan-cli/tests/test_api_machines.py index 9510996b5..30edb0993 100644 --- a/pkgs/clan-cli/tests/test_api_machines.py +++ b/pkgs/clan-cli/tests/test_api_machines.py @@ -19,3 +19,26 @@ def test_machines(api: TestClient, clan_flake: Path) -> None: response = api.get("/api/machines") assert response.status_code == 200 assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]} + + +def test_configure_machine(api: TestClient, machine_flake: Path) -> None: + response = api.get("/api/machines/machine1/config") + assert response.status_code == 200 + assert response.json() == {"config": dict()} + + # set some config + response = api.put( + "/api/machines/machine1/config", + json=dict( + clan=dict( + jitsi=True, + ) + ), + ) + assert response.status_code == 200 + assert response.json() == {"config": {"clan": {"jitsi": True}}} + + # get the config again + response = api.get("/api/machines/machine1/config") + assert response.status_code == 200 + assert response.json() == {"config": {"clan": {"jitsi": True}}} diff --git a/pkgs/clan-cli/tests/config/test_config.py b/pkgs/clan-cli/tests/test_config.py similarity index 100% rename from pkgs/clan-cli/tests/config/test_config.py rename to pkgs/clan-cli/tests/test_config.py diff --git a/pkgs/clan-cli/tests/test_config_machine.py b/pkgs/clan-cli/tests/test_config_machine.py new file mode 100644 index 000000000..248443535 --- /dev/null +++ b/pkgs/clan-cli/tests/test_config_machine.py @@ -0,0 +1,8 @@ +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) + assert "properties" in schema