From 07fb01d9db5a84f8731098510de54a648ce7338b Mon Sep 17 00:00:00 2001 From: DavHau Date: Sun, 9 Jun 2024 02:38:19 -0700 Subject: [PATCH] tests: add test for creating machine on minimal clan --- pkgs/clan-cli/clan_cli/api/util.py | 5 ++- pkgs/clan-cli/clan_cli/flakes/create.py | 9 ++++-- pkgs/clan-cli/clan_cli/machines/create.py | 3 +- pkgs/clan-cli/clan_cli/machines/list.py | 2 +- pkgs/clan-cli/tests/fixtures_flakes.py | 34 ++++++++++++++++++--- pkgs/clan-cli/tests/test_machines_config.py | 14 +++++++++ 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/api/util.py b/pkgs/clan-cli/clan_cli/api/util.py index 57e02a24..d356b28a 100644 --- a/pkgs/clan-cli/clan_cli/api/util.py +++ b/pkgs/clan-cli/clan_cli/api/util.py @@ -179,12 +179,15 @@ def type_to_dict(t: Any, scope: str = "", type_map: dict[TypeVar, type] = {}) -> raise JSchemaTypeError( f"Usage of the Any type is not supported for API functions. In: {scope}" ) - if t is pathlib.Path: return { # TODO: maybe give it a pattern for URI "type": "string", } + if t is dict: + raise JSchemaTypeError( + "Error: generic dict type not supported. Use dict[str. Any] instead" + ) # Optional[T] gets internally transformed Union[T,NoneType] if t is NoneType: diff --git a/pkgs/clan-cli/clan_cli/flakes/create.py b/pkgs/clan-cli/clan_cli/flakes/create.py index e698a5ef..133c0c84 100644 --- a/pkgs/clan-cli/clan_cli/flakes/create.py +++ b/pkgs/clan-cli/clan_cli/flakes/create.py @@ -9,7 +9,8 @@ from ..cmd import CmdOut, run from ..errors import ClanError from ..nix import nix_command, nix_shell -DEFAULT_TEMPLATE_URL: str = "git+https://git.clan.lol/clan/clan-core" +default_template_url: str = "git+https://git.clan.lol/clan/clan-core" +minimal_template_url: str = "git+https://git.clan.lol/clan/clan-core#templates.minimal" @dataclass @@ -21,7 +22,9 @@ class CreateClanResponse: @API.register -def create_clan(directory: Path, template_url: str) -> CreateClanResponse: +def create_clan( + directory: Path, template_url: str = minimal_template_url +) -> CreateClanResponse: if not directory.exists(): directory.mkdir() else: @@ -78,7 +81,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None: "--url", type=str, help="url to the clan template", - default=DEFAULT_TEMPLATE_URL, + default=default_template_url, ) parser.add_argument( "path", type=Path, help="Path to the clan directory", default=Path(".") diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 348d02f1..ae04690f 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -2,6 +2,7 @@ import argparse import logging from dataclasses import dataclass from pathlib import Path +from typing import Any from clan_cli.api import API from clan_cli.config.machine import set_config_for_machine @@ -12,7 +13,7 @@ log = logging.getLogger(__name__) @dataclass class MachineCreateRequest: name: str - config: dict[str, int] + config: dict[str, Any] @API.register diff --git a/pkgs/clan-cli/clan_cli/machines/list.py b/pkgs/clan-cli/clan_cli/machines/list.py index 56ae1c76..dd71bc7d 100644 --- a/pkgs/clan-cli/clan_cli/machines/list.py +++ b/pkgs/clan-cli/clan_cli/machines/list.py @@ -12,7 +12,7 @@ log = logging.getLogger(__name__) @API.register -def list_machines(flake_url: str | Path, debug: bool) -> list[str]: +def list_machines(flake_url: str | Path, debug: bool = False) -> list[str]: config = nix_config() system = config["system"] cmd = nix_eval( diff --git a/pkgs/clan-cli/tests/fixtures_flakes.py b/pkgs/clan-cli/tests/fixtures_flakes.py index ee6e7c91..0e640d9e 100644 --- a/pkgs/clan-cli/tests/fixtures_flakes.py +++ b/pkgs/clan-cli/tests/fixtures_flakes.py @@ -29,6 +29,9 @@ def substitute( line = line.replace("__NIXPKGS__", str(nixpkgs_source())) if clan_core_flake: line = line.replace("__CLAN_CORE__", str(clan_core_flake)) + line = line.replace( + "git+https://git.clan.lol/clan/clan-core", str(clan_core_flake) + ) line = line.replace("__CLAN_SOPS_KEY_PATH__", sops_key) line = line.replace("__CLAN_SOPS_KEY_DIR__", str(flake)) print(line, end="") @@ -67,6 +70,7 @@ def generate_flake( # copy the template to a new temporary location flake = temporary_home / "flake" shutil.copytree(flake_template, flake) + sp.run(["chmod", "+w", "-R", str(flake)], check=True) # substitute `substitutions` in all files of the template for file in flake.rglob("*"): @@ -107,7 +111,7 @@ def generate_flake( def create_flake( monkeypatch: pytest.MonkeyPatch, temporary_home: Path, - flake_template_name: str, + flake_template: str | Path, clan_core_flake: Path | None = None, # names referring to pre-defined machines from ../machines machines: list[str] = [], @@ -119,13 +123,19 @@ def create_flake( Creates a flake with the given name and machines. The machine names map to the machines in ./test_machines """ - template = Path(__file__).parent / flake_template_name + if isinstance(flake_template, Path): + template_path = flake_template + else: + template_path = Path(__file__).parent / flake_template + + flake_template_name = template_path.name # copy the template to a new temporary location flake = temporary_home / flake_template_name - shutil.copytree(template, flake) + shutil.copytree(template_path, flake) + sp.run(["chmod", "+w", "-R", str(flake)], check=True) - # lookup the requested machines in ./test_machines and include them + # add the requested machines to the flake if machines: (flake / "machines").mkdir(parents=True, exist_ok=True) for machine_name in machines: @@ -237,3 +247,19 @@ def test_flake_with_core_and_pass( "test_flake_with_core_and_pass", CLAN_CORE, ) + + +@pytest.fixture +def test_flake_minimal( + monkeypatch: pytest.MonkeyPatch, temporary_home: Path +) -> Iterator[FlakeForTest]: + if not (CLAN_CORE / "flake.nix").exists(): + raise Exception( + "clan-core flake not found. This test requires the clan-core flake to be present" + ) + yield from create_flake( + monkeypatch, + temporary_home, + CLAN_CORE / "templates" / "minimal", + CLAN_CORE, + ) diff --git a/pkgs/clan-cli/tests/test_machines_config.py b/pkgs/clan-cli/tests/test_machines_config.py index a1d02cf5..740958ba 100644 --- a/pkgs/clan-cli/tests/test_machines_config.py +++ b/pkgs/clan-cli/tests/test_machines_config.py @@ -2,9 +2,23 @@ import pytest from fixtures_flakes import FlakeForTest from clan_cli.config.schema import machine_schema +from clan_cli.machines.create import MachineCreateRequest, create_machine +from clan_cli.machines.list import list_machines @pytest.mark.with_core def test_schema_for_machine(test_flake_with_core: FlakeForTest) -> None: schema = machine_schema(test_flake_with_core.path, config={}) assert "properties" in schema + + +@pytest.mark.with_core +def test_create_machine_on_minimal_clan(test_flake_minimal: FlakeForTest) -> None: + assert list_machines(test_flake_minimal.path) == [] + create_machine( + test_flake_minimal.path, + MachineCreateRequest( + name="foo", config=dict(nixpkgs=dict(hostSystem="x86_64-linux")) + ), + ) + assert list_machines(test_flake_minimal.path) == ["foo"]