generated from Luis/nextjs-python-web-template
Merge pull request 'cli: rename clan_flake to test_flake' (#294) from Mic92-test_flake into main
This commit is contained in:
@@ -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
|
|
||||||
@@ -12,5 +12,5 @@ pytest_plugins = [
|
|||||||
"command",
|
"command",
|
||||||
"ports",
|
"ports",
|
||||||
"host_group",
|
"host_group",
|
||||||
"clan_flake",
|
"test_flake",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ def test_set_some_option(
|
|||||||
|
|
||||||
|
|
||||||
def test_configure_machine(
|
def test_configure_machine(
|
||||||
clan_flake: Path,
|
test_flake: Path,
|
||||||
temporary_dir: Path,
|
temporary_dir: Path,
|
||||||
capsys: pytest.CaptureFixture,
|
capsys: pytest.CaptureFixture,
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
|||||||
54
pkgs/clan-cli/tests/test_flake.py
Normal file
54
pkgs/clan-cli/tests/test_flake.py
Normal 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)
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
inputs = {
|
# this placeholder is replaced by the path to nixpkgs
|
||||||
# this placeholder is replaced by the path to nixpkgs
|
inputs.nixpkgs.url = "__NIXPKGS__";
|
||||||
nixpkgs.url = "__NIXPKGS__";
|
|
||||||
};
|
|
||||||
|
|
||||||
outputs = inputs: {
|
outputs = inputs: {
|
||||||
nixosConfigurations.machine1 = inputs.nixpkgs.lib.nixosSystem {
|
nixosConfigurations.machine1 = inputs.nixpkgs.lib.nixosSystem {
|
||||||
@@ -10,7 +10,7 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
def test_import_sops(
|
def test_import_sops(
|
||||||
test_root: Path,
|
test_root: Path,
|
||||||
clan_flake: Path,
|
test_flake: Path,
|
||||||
capsys: pytest.CaptureFixture,
|
capsys: pytest.CaptureFixture,
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
age_keys: list["KeyPair"],
|
age_keys: list["KeyPair"],
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from pathlib import Path
|
|||||||
from api import TestClient
|
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")
|
response = api.get("/api/machines")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json() == {"machines": []}
|
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"}]}
|
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
|
# ensure error 404 if machine does not exist when accessing the config
|
||||||
response = api.get("/api/machines/machine1/config")
|
response = api.get("/api/machines/machine1/config")
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import pytest
|
|||||||
from cli import Cli
|
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 = Cli()
|
||||||
cli.run(["machines", "create", "machine1"])
|
cli.run(["machines", "create", "machine1"])
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ from pathlib import Path
|
|||||||
from clan_cli.config import machine
|
from clan_cli.config import machine
|
||||||
|
|
||||||
|
|
||||||
def test_schema_for_machine(clan_flake: Path) -> None:
|
def test_schema_for_machine(test_flake: Path) -> None:
|
||||||
schema = machine.schema_for_machine("machine1", clan_flake)
|
schema = machine.schema_for_machine("machine1", test_flake)
|
||||||
assert "properties" in schema
|
assert "properties" in schema
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
def _test_identities(
|
def _test_identities(
|
||||||
what: str,
|
what: str,
|
||||||
clan_flake: Path,
|
test_flake: Path,
|
||||||
capsys: pytest.CaptureFixture,
|
capsys: pytest.CaptureFixture,
|
||||||
age_keys: list["KeyPair"],
|
age_keys: list["KeyPair"],
|
||||||
) -> None:
|
) -> None:
|
||||||
cli = Cli()
|
cli = Cli()
|
||||||
sops_folder = clan_flake / "sops"
|
sops_folder = test_flake / "sops"
|
||||||
|
|
||||||
cli.run(["secrets", what, "add", "foo", age_keys[0].pubkey])
|
cli.run(["secrets", what, "add", "foo", age_keys[0].pubkey])
|
||||||
assert (sops_folder / what / "foo" / "key.json").exists()
|
assert (sops_folder / what / "foo" / "key.json").exists()
|
||||||
@@ -60,19 +60,19 @@ def _test_identities(
|
|||||||
|
|
||||||
|
|
||||||
def test_users(
|
def test_users(
|
||||||
clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
test_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
||||||
) -> None:
|
) -> None:
|
||||||
_test_identities("users", clan_flake, capsys, age_keys)
|
_test_identities("users", test_flake, capsys, age_keys)
|
||||||
|
|
||||||
|
|
||||||
def test_machines(
|
def test_machines(
|
||||||
clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
test_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
||||||
) -> None:
|
) -> None:
|
||||||
_test_identities("machines", clan_flake, capsys, age_keys)
|
_test_identities("machines", test_flake, capsys, age_keys)
|
||||||
|
|
||||||
|
|
||||||
def test_groups(
|
def test_groups(
|
||||||
clan_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
test_flake: Path, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
||||||
) -> None:
|
) -> None:
|
||||||
cli = Cli()
|
cli = Cli()
|
||||||
capsys.readouterr() # empty the buffer
|
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-user", "group1", "user1"])
|
||||||
cli.run(["secrets", "groups", "remove-machine", "group1", "machine1"])
|
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
|
assert len(groups) == 0
|
||||||
|
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ def use_key(key: str, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
|
|||||||
|
|
||||||
|
|
||||||
def test_secrets(
|
def test_secrets(
|
||||||
clan_flake: Path,
|
test_flake: Path,
|
||||||
capsys: pytest.CaptureFixture,
|
capsys: pytest.CaptureFixture,
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
age_keys: list["KeyPair"],
|
age_keys: list["KeyPair"],
|
||||||
@@ -125,7 +125,7 @@ def test_secrets(
|
|||||||
assert capsys.readouterr().out == ""
|
assert capsys.readouterr().out == ""
|
||||||
|
|
||||||
monkeypatch.setenv("SOPS_NIX_SECRET", "foo")
|
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"])
|
cli.run(["secrets", "key", "generate"])
|
||||||
capsys.readouterr() # empty the buffer
|
capsys.readouterr() # empty the buffer
|
||||||
cli.run(["secrets", "key", "show"])
|
cli.run(["secrets", "key", "show"])
|
||||||
|
|||||||
Reference in New Issue
Block a user