cli: rename clan_flake to test_flake
All checks were successful
checks-impure / test (pull_request) Successful in 9s
checks / test (pull_request) Successful in 1m24s

This commit is contained in:
Jörg Thalheim 2023-09-15 12:31:18 +02:00
parent 54ac8b6269
commit 87f0b090ef
12 changed files with 74 additions and 56 deletions

View File

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

View File

@ -12,5 +12,5 @@ pytest_plugins = [
"command",
"ports",
"host_group",
"clan_flake",
"test_flake",
]

View File

@ -50,7 +50,7 @@ def test_set_some_option(
def test_configure_machine(
clan_flake: Path,
test_flake: Path,
temporary_dir: Path,
capsys: pytest.CaptureFixture,
monkeypatch: pytest.MonkeyPatch,

View File

@ -0,0 +1,54 @@
import fileinput
import shutil
import tempfile
from pathlib import Path
from typing import Iterator
import pytest
from root import PROJECT_ROOT
from clan_cli.dirs import nixpkgs_source
@pytest.fixture(scope="module")
def monkeymodule() -> Iterator[pytest.MonkeyPatch]:
with pytest.MonkeyPatch.context() as mp:
yield mp
def create_flake(
monkeymodule: pytest.MonkeyPatch, name: str, clan_core_flake: Path | None = None
) -> Iterator[Path]:
template = Path(__file__).parent / name
# copy the template to a new temporary location
with tempfile.TemporaryDirectory() as tmpdir_:
home = Path(tmpdir_)
flake = home / name
shutil.copytree(template, flake)
# in the flake.nix file replace the string __CLAN_URL__ with the the clan flake
# provided by get_test_flake_toplevel
flake_nix = flake / "flake.nix"
for line in fileinput.input(flake_nix, inplace=True):
line = line.replace("__NIXPKGS__", str(nixpkgs_source()))
if clan_core_flake:
line = line.replace("__CLAN_CORE__", str(clan_core_flake))
print(line)
# check that an empty config is returned if no json file exists
monkeymodule.chdir(flake)
monkeymodule.setenv("HOME", str(home))
yield flake
@pytest.fixture(scope="module")
def test_flake(monkeymodule: pytest.MonkeyPatch) -> Iterator[Path]:
yield from create_flake(monkeymodule, "test_flake")
@pytest.fixture(scope="module")
def test_flake_with_core(monkeymodule: pytest.MonkeyPatch) -> Iterator[Path]:
clan_core_flake = PROJECT_ROOT.parent.parent
if not (clan_core_flake / "flake.nix").exists():
raise Exception(
"clan-core flake not found. This test requires the clan-core flake to be present"
)
yield from create_flake(monkeymodule, "test_flake_with_core", clan_core_flake)

View File

@ -1,8 +1,6 @@
{
inputs = {
# this placeholder is replaced by the path to nixpkgs
nixpkgs.url = "__NIXPKGS__";
};
# this placeholder is replaced by the path to nixpkgs
inputs.nixpkgs.url = "__NIXPKGS__";
outputs = inputs: {
nixosConfigurations.machine1 = inputs.nixpkgs.lib.nixosSystem {

View File

@ -10,7 +10,7 @@ if TYPE_CHECKING:
def test_import_sops(
test_root: Path,
clan_flake: Path,
test_flake: Path,
capsys: pytest.CaptureFixture,
monkeypatch: pytest.MonkeyPatch,
age_keys: list["KeyPair"],

View File

@ -3,7 +3,7 @@ from pathlib import Path
from api import TestClient
def test_machines(api: TestClient, clan_flake: Path) -> None:
def test_machines(api: TestClient, test_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, clan_flake: Path) -> None:
assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]}
def test_configure_machine(api: TestClient, clan_flake: Path) -> None:
def test_configure_machine(api: TestClient, test_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

View File

@ -4,7 +4,7 @@ import pytest
from cli import Cli
def test_machine_subcommands(clan_flake: Path, capsys: pytest.CaptureFixture) -> None:
def test_machine_subcommands(test_flake: Path, capsys: pytest.CaptureFixture) -> None:
cli = Cli()
cli.run(["machines", "create", "machine1"])

View File

@ -3,6 +3,6 @@ from pathlib import Path
from clan_cli.config import machine
def test_schema_for_machine(clan_flake: Path) -> None:
schema = machine.schema_for_machine("machine1", clan_flake)
def test_schema_for_machine(test_flake: Path) -> None:
schema = machine.schema_for_machine("machine1", test_flake)
assert "properties" in schema

View File

@ -14,12 +14,12 @@ if TYPE_CHECKING:
def _test_identities(
what: str,
clan_flake: Path,
test_flake: Path,
capsys: pytest.CaptureFixture,
age_keys: list["KeyPair"],
) -> None:
cli = Cli()
sops_folder = clan_flake / "sops"
sops_folder = test_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(
clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
test_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
) -> None:
_test_identities("users", clan_flake, capsys, age_keys)
_test_identities("users", test_flake, capsys, age_keys)
def test_machines(
clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
test_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
) -> None:
_test_identities("machines", clan_flake, capsys, age_keys)
_test_identities("machines", test_flake, capsys, age_keys)
def test_groups(
clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
test_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(clan_flake / "sops" / "groups")
groups = os.listdir(test_flake / "sops" / "groups")
assert len(groups) == 0
@ -114,7 +114,7 @@ def use_key(key: str, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
def test_secrets(
clan_flake: Path,
test_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(clan_flake / ".." / "age.key"))
monkeypatch.setenv("SOPS_AGE_KEY_FILE", str(test_flake / ".." / "age.key"))
cli.run(["secrets", "key", "generate"])
capsys.readouterr() # empty the buffer
cli.run(["secrets", "key", "show"])