webui: tests for machine config endpoints

This commit is contained in:
DavHau
2023-08-26 08:46:53 +02:00
parent 7dd5add64b
commit 518bf4197f
8 changed files with 76 additions and 48 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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}}}

View File

@@ -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