1
0
forked from clan/clan-core

Merge pull request 'tests: add test for creating machine on minimal clan' (#1596) from DavHau/clan-core:DavHau-dave into main

This commit is contained in:
clan-bot 2024-06-10 04:58:02 +00:00
commit 1d542d4396
6 changed files with 57 additions and 10 deletions

View File

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

View File

@ -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(".")

View File

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

View File

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

View File

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

View File

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