clan_cli: flake_name -> flake_dir

This commit is contained in:
lassulus
2023-11-15 14:28:40 +01:00
parent 426be3dd37
commit 082d628497
35 changed files with 199 additions and 354 deletions

View File

@@ -13,7 +13,6 @@ from pydantic.tools import parse_obj_as
from root import CLAN_CORE
from clan_cli.dirs import nixpkgs_source
from clan_cli.types import FlakeName
log = logging.getLogger(__name__)
@@ -36,14 +35,13 @@ def substitute(
class FlakeForTest(NamedTuple):
name: FlakeName
path: Path
def create_flake(
monkeypatch: pytest.MonkeyPatch,
temporary_home: Path,
flake_name: FlakeName,
flake_name: str,
clan_core_flake: Path | None = None,
machines: list[str] = [],
remote: bool = False,
@@ -55,7 +53,7 @@ def create_flake(
template = Path(__file__).parent / flake_name
# copy the template to a new temporary location
flake = temporary_home / ".config/clan/flakes" / flake_name
flake = temporary_home / flake_name
shutil.copytree(template, flake)
# lookup the requested machines in ./test_machines and include them
@@ -91,16 +89,16 @@ def create_flake(
if remote:
with tempfile.TemporaryDirectory():
yield FlakeForTest(flake_name, flake)
yield FlakeForTest(flake)
else:
yield FlakeForTest(flake_name, flake)
yield FlakeForTest(flake)
@pytest.fixture
def test_flake(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]:
yield from create_flake(monkeypatch, temporary_home, FlakeName("test_flake"))
yield from create_flake(monkeypatch, temporary_home, "test_flake")
@pytest.fixture
@@ -114,7 +112,7 @@ def test_flake_with_core(
yield from create_flake(
monkeypatch,
temporary_home,
FlakeName("test_flake_with_core"),
"test_flake_with_core",
CLAN_CORE,
)
@@ -140,6 +138,6 @@ def test_flake_with_core_and_pass(
yield from create_flake(
monkeypatch,
temporary_home,
FlakeName("test_flake_with_core_and_pass"),
"test_flake_with_core_and_pass",
CLAN_CORE,
)

View File

@@ -4,22 +4,17 @@ import shlex
from clan_cli import create_parser
from clan_cli.custom_logger import get_caller
from clan_cli.dirs import get_clan_flake_toplevel
log = logging.getLogger(__name__)
class Cli:
def __init__(self) -> None:
self.parser = create_parser(prog="clan")
def run(self, args: list[str]) -> argparse.Namespace:
parser = create_parser(prog="clan")
cmd = shlex.join(["clan"] + args)
log.debug(f"$ {cmd}")
log.debug(f"Caller {get_caller()}")
parsed = self.parser.parse_args(args)
if parsed.flake is None:
parsed.flake = get_clan_flake_toplevel()
parsed = parser.parse_args(args)
if hasattr(parsed, "func"):
parsed.func(parsed)
return parsed

View File

@@ -6,9 +6,9 @@ from fixtures_flakes import FlakeForTest
@pytest.mark.with_core
def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest) -> None:
# retrieve the list of available clanModules
response = api.get(f"/api/{test_flake_with_core.name}/clan_modules")
response = api.get(f"/api/clan_modules?flake_dir={test_flake_with_core.path}")
assert response.status_code == 200, response.text
response_json = response.json()
assert response.status_code == 200
assert isinstance(response_json, dict)
assert "clan_modules" in response_json
assert len(response_json["clan_modules"]) > 0

View File

@@ -39,6 +39,8 @@ def test_set_some_option(
cli = Cli()
cli.run(
[
"--flake",
str(test_flake.path),
"config",
"--quiet",
"--options-file",
@@ -47,7 +49,6 @@ def test_set_some_option(
out_file.name,
]
+ args
+ [test_flake.name]
)
json_out = json.loads(open(out_file.name).read())
assert json_out == expected
@@ -61,11 +62,30 @@ def test_configure_machine(
) -> None:
cli = Cli()
cli.run(["config", "-m", "machine1", "clan.jitsi.enable", "true", test_flake.name])
cli.run(
[
"--flake",
str(test_flake.path),
"config",
"-m",
"machine1",
"clan.jitsi.enable",
"true",
]
)
# clear the output buffer
capsys.readouterr()
# read a option value
cli.run(["config", "-m", "machine1", "clan.jitsi.enable", test_flake.name])
cli.run(
[
"--flake",
str(test_flake.path),
"config",
"-m",
"machine1",
"clan.jitsi.enable",
]
)
# read the output
assert capsys.readouterr().out == "true\n"

View File

@@ -6,7 +6,6 @@ import pytest
from api import TestClient
from cli import Cli
from clan_cli.dirs import clan_flakes_dir
from clan_cli.flakes.create import DEFAULT_URL
@@ -19,13 +18,11 @@ def cli() -> Cli:
def test_create_flake_api(
monkeypatch: pytest.MonkeyPatch, api: TestClient, temporary_home: Path
) -> None:
monkeypatch.chdir(clan_flakes_dir())
flake_name = "flake_dir"
flake_dir = clan_flakes_dir() / flake_name
flake_dir = temporary_home / "test-flake"
response = api.post(
"/api/flake/create",
f"/api/flake/create?flake_dir={flake_dir}",
json=dict(
flake_name=str(flake_dir),
flake_dir=str(flake_dir),
url=str(DEFAULT_URL),
),
)
@@ -42,17 +39,15 @@ def test_create_flake(
temporary_home: Path,
cli: Cli,
) -> None:
monkeypatch.chdir(clan_flakes_dir())
flake_name = "flake_dir"
flake_dir = clan_flakes_dir() / flake_name
flake_dir = temporary_home / "test-flake"
cli.run(["flakes", "create", flake_name])
cli.run(["flakes", "create", str(flake_dir)])
assert (flake_dir / ".clan-flake").exists()
monkeypatch.chdir(flake_dir)
cli.run(["machines", "create", "machine1", flake_name])
cli.run(["machines", "create", "machine1"])
capsys.readouterr() # flush cache
cli.run(["machines", "list", flake_name])
cli.run(["machines", "list"])
assert "machine1" in capsys.readouterr().out
flake_show = subprocess.run(
["nix", "flake", "show", "--json"],
@@ -67,9 +62,7 @@ def test_create_flake(
pytest.fail("nixosConfigurations.machine1 not found in flake outputs")
# configure machine1
capsys.readouterr()
cli.run(
["config", "--machine", "machine1", "services.openssh.enable", "", flake_name]
)
cli.run(["config", "--machine", "machine1", "services.openssh.enable", ""])
capsys.readouterr()
cli.run(
[
@@ -78,6 +71,5 @@ def test_create_flake(
"machine1",
"services.openssh.enable",
"true",
flake_name,
]
)

View File

@@ -8,15 +8,6 @@ from fixtures_flakes import FlakeForTest
log = logging.getLogger(__name__)
@pytest.mark.impure
def test_list_flakes(api: TestClient, test_flake_with_core: FlakeForTest) -> None:
response = api.get("/api/flake/list")
assert response.status_code == 200, "Failed to list flakes"
data = response.json()
print("Data: ", data)
assert data.get("flakes") == ["test_flake_with_core"]
@pytest.mark.impure
def test_inspect_ok(api: TestClient, test_flake_with_core: FlakeForTest) -> None:
params = {"url": str(test_flake_with_core.path)}

View File

@@ -3,19 +3,20 @@ from api import TestClient
from fixtures_flakes import FlakeForTest
def test_create_and_list(api: TestClient, test_flake: FlakeForTest) -> None:
response = api.get(f"/api/{test_flake.name}/machines")
def test_machines(api: TestClient, test_flake: FlakeForTest) -> None:
response = api.get(f"/api/machines?flake_dir={test_flake.path}")
assert response.status_code == 200
assert response.json() == {"machines": []}
response = api.put(f"/api/{test_flake.name}/machines/test/config", json=dict())
response = api.put(
f"/api/machines/test/config?flake_dir={test_flake.path}", json={}
)
assert response.status_code == 200
response = api.get(f"/api/{test_flake.name}/machines/test")
response = api.get(f"/api/machines/test?flake_dir={test_flake.path}")
assert response.status_code == 200
assert response.json() == {"machine": {"name": "test", "status": "unknown"}}
response = api.get(f"/api/{test_flake.name}/machines")
response = api.get(f"/api/machines?flake_dir={test_flake.path}")
assert response.status_code == 200
assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]}
@@ -24,7 +25,7 @@ def test_create_and_list(api: TestClient, test_flake: FlakeForTest) -> None:
def test_schema_errors(api: TestClient, test_flake_with_core: FlakeForTest) -> None:
# make sure that eval errors do not raise an internal server error
response = api.put(
f"/api/{test_flake_with_core.name}/schema",
f"/api/schema?flake_dir={test_flake_with_core.path}",
json={"imports": ["some-invalid-import"]},
)
assert response.status_code == 422
@@ -39,7 +40,7 @@ def test_schema_invalid_clan_imports(
api: TestClient, test_flake_with_core: FlakeForTest
) -> None:
response = api.put(
f"/api/{test_flake_with_core.name}/schema",
f"/api/schema?flake_dir={test_flake_with_core.path}",
json={"clanImports": ["non-existing-clan-module"]},
)
assert response.status_code == 400
@@ -54,7 +55,8 @@ def test_create_machine_invalid_hostname(
api: TestClient, test_flake: FlakeForTest
) -> None:
response = api.put(
f"/api/{test_flake.name}/machines/-invalid-hostname/config", json=dict()
f"/api/machines/-invalid-hostname/config?flake_dir={test_flake.path}",
json=dict(),
)
assert response.status_code == 422
assert (
@@ -67,7 +69,7 @@ def test_verify_config_without_machine(
api: TestClient, test_flake_with_core: FlakeForTest
) -> None:
response = api.put(
f"/api/{test_flake_with_core.name}/machines/test/verify",
f"/api/machines/test/verify?flake_dir={test_flake_with_core.path}",
json=dict(),
)
assert response.status_code == 200
@@ -79,12 +81,14 @@ def test_ensure_empty_config_is_valid(
api: TestClient, test_flake_with_core: FlakeForTest
) -> None:
response = api.put(
f"/api/{test_flake_with_core.name}/machines/test/config",
f"/api/machines/test/config?flake_dir={test_flake_with_core.path}",
json=dict(),
)
assert response.status_code == 200
response = api.get(f"/api/{test_flake_with_core.name}/machines/test/verify")
response = api.get(
f"/api/machines/test/verify?flake_dir={test_flake_with_core.path}"
)
assert response.status_code == 200
assert response.json() == {"success": True, "error": None}
@@ -92,17 +96,21 @@ def test_ensure_empty_config_is_valid(
@pytest.mark.with_core
def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest) -> None:
# ensure error 404 if machine does not exist when accessing the config
response = api.get(f"/api/{test_flake_with_core.name}/machines/machine1/config")
response = api.get(
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}"
)
assert response.status_code == 404
# create the machine
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/config", json={}
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}", json={}
)
assert response.status_code == 200
# ensure an empty config is returned by default for a new machine
response = api.get(f"/api/{test_flake_with_core.name}/machines/machine1/config")
response = api.get(
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}"
)
assert response.status_code == 200
assert response.json() == {
"clanImports": [],
@@ -111,7 +119,7 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# get jsonschema for without imports
response = api.put(
f"/api/{test_flake_with_core.name}/schema",
f"/api/schema?flake_dir={test_flake_with_core.path}",
json={"clanImports": []},
)
assert response.status_code == 200
@@ -133,7 +141,7 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# verify an invalid config (foo option does not exist)
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/verify",
f"/api/machines/machine1/verify?flake_dir={test_flake_with_core.path}",
json=invalid_config,
)
assert response.status_code == 200
@@ -141,13 +149,15 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# set come invalid config (foo option does not exist)
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
json=invalid_config,
)
assert response.status_code == 200
# ensure the config has actually been updated
response = api.get(f"/api/{test_flake_with_core.name}/machines/machine1/config")
response = api.get(
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}"
)
assert response.status_code == 200
assert response.json() == dict(clanImports=[], **invalid_config)
@@ -162,20 +172,22 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
)
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
json=config2,
)
assert response.status_code == 200
# ensure the config has been applied
response = api.get(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
)
assert response.status_code == 200
assert response.json() == dict(clanImports=[], **config2)
# get the config again
response = api.get(f"/api/{test_flake_with_core.name}/machines/machine1/config")
response = api.get(
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}"
)
assert response.status_code == 200
assert response.json() == {"clanImports": [], **config2}
@@ -183,27 +195,29 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# For example, this should not result in the boot.loader.grub.devices being
# set twice (eg. merged)
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
json=config2,
)
assert response.status_code == 200
# ensure the config has been applied
response = api.get(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
)
assert response.status_code == 200
assert response.json() == dict(clanImports=[], **config2)
# verify the machine config evaluates
response = api.get(f"/api/{test_flake_with_core.name}/machines/machine1/verify")
response = api.get(
f"/api/machines/machine1/verify?flake_dir={test_flake_with_core.path}"
)
assert response.status_code == 200
assert response.json() == {"success": True, "error": None}
# get the schema with an extra module imported
response = api.put(
f"/api/{test_flake_with_core.name}/schema",
f"/api/schema?flake_dir={test_flake_with_core.path}",
json={"clanImports": ["diskLayouts"]},
)
# expect the result schema to contain the deltachat option
@@ -227,14 +241,14 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# set the fake-module.fake-flag option to true
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
json=config_with_imports,
)
assert response.status_code == 200
# ensure the config has been applied
response = api.get(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
)
assert response.status_code == 200
assert response.json() == {
@@ -248,7 +262,7 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# remove the import from the config
response = api.put(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
json=dict(
clanImports=[],
),
@@ -257,7 +271,7 @@ def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest)
# ensure the config has been applied
response = api.get(
f"/api/{test_flake_with_core.name}/machines/machine1/config",
f"/api/machines/machine1/config?flake_dir={test_flake_with_core.path}",
)
assert response.status_code == 200
assert response.json() == {

View File

@@ -7,16 +7,16 @@ def test_machine_subcommands(
test_flake: FlakeForTest, capsys: pytest.CaptureFixture
) -> None:
cli = Cli()
cli.run(["machines", "create", "machine1", test_flake.name])
cli.run(["--flake", str(test_flake.path), "machines", "create", "machine1"])
capsys.readouterr()
cli.run(["machines", "list", test_flake.name])
cli.run(["--flake", str(test_flake.path), "machines", "list"])
out = capsys.readouterr()
assert "machine1\n" == out.out
cli.run(["machines", "delete", "machine1", test_flake.name])
cli.run(["--flake", str(test_flake.path), "machines", "delete", "machine1"])
capsys.readouterr()
cli.run(["machines", "list", test_flake.name])
cli.run(["--flake", str(test_flake.path), "machines", "list"])
out = capsys.readouterr()
assert "" == out.out

View File

@@ -6,5 +6,5 @@ from clan_cli.config.schema import machine_schema
@pytest.mark.with_core
def test_schema_for_machine(test_flake_with_core: FlakeForTest) -> None:
schema = machine_schema(test_flake_with_core.name, config={})
schema = machine_schema(test_flake_with_core.path, config={})
assert "properties" in schema

View File

@@ -38,7 +38,7 @@ def test_generate_secret(
has_secret(test_flake_with_core.path, "vm1-zerotier-identity-secret")
has_secret(test_flake_with_core.path, "vm1-zerotier-subnet")
network_id = machine_get_fact(
test_flake_with_core.name, "vm1", "zerotier-network-id"
test_flake_with_core.path, "vm1", "zerotier-network-id"
)
assert len(network_id) == 16
secrets_folder = sops_secrets_folder(test_flake_with_core.path)
@@ -59,7 +59,7 @@ def test_generate_secret(
cli.run(["secrets", "generate", "vm2"])
assert has_secret(test_flake_with_core.path, "vm2-age.key")
assert has_secret(test_flake_with_core.path, "vm2-zerotier-identity-secret")
ip = machine_get_fact(test_flake_with_core.name, "vm1", "zerotier-ip")
ip = machine_get_fact(test_flake_with_core.path, "vm1", "zerotier-ip")
assert ipaddress.IPv6Address(ip).is_private
meshname = machine_get_fact(test_flake_with_core.name, "vm1", "zerotier-meshname")
meshname = machine_get_fact(test_flake_with_core.path, "vm1", "zerotier-meshname")
assert len(meshname) == 26

View File

@@ -41,7 +41,7 @@ def test_upload_secret(
subprocess.run(nix_shell(["pass"], ["pass", "init", "test@local"]), check=True)
cli.run(["secrets", "generate", "vm1"])
network_id = machine_get_fact(
test_flake_with_core_and_pass.name, "vm1", "zerotier-network-id"
test_flake_with_core_and_pass.path, "vm1", "zerotier-network-id"
)
assert len(network_id) == 16
identity_secret = (

View File

@@ -10,8 +10,6 @@ from httpx import SyncByteStream
from pydantic import AnyUrl
from root import CLAN_CORE
from clan_cli.types import FlakeName
if TYPE_CHECKING:
from age_keys import KeyPair
@@ -23,7 +21,7 @@ def flake_with_vm_with_secrets(
yield from create_flake(
monkeypatch,
temporary_home,
FlakeName("test_flake_with_core_dynamic_machines"),
"test_flake_with_core_dynamic_machines",
CLAN_CORE,
machines=["vm_with_secrets"],
)
@@ -36,7 +34,7 @@ def remote_flake_with_vm_without_secrets(
yield from create_flake(
monkeypatch,
temporary_home,
FlakeName("test_flake_with_core_dynamic_machines"),
"test_flake_with_core_dynamic_machines",
CLAN_CORE,
machines=["vm_without_secrets"],
remote=True,

View File

@@ -16,7 +16,7 @@ def test_inspect(
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture
) -> None:
cli = Cli()
cli.run(["vms", "inspect", "vm1", test_flake_with_core.name])
cli.run(["--flake", str(test_flake_with_core.path), "vms", "inspect", "vm1"])
out = capsys.readouterr() # empty the buffer
assert "Cores" in out.out