Fix unit tests

This commit is contained in:
Johannes Kirschbauer
2024-07-24 12:56:41 +02:00
parent 5b4105b4d8
commit 1d51ae5c85
8 changed files with 50 additions and 31 deletions

View File

@@ -1,12 +1,11 @@
# !/usr/bin/env python3 # !/usr/bin/env python3
import argparse import argparse
import os import os
from dataclasses import dataclass, fields from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from clan_cli.api import API from clan_cli.api import API
from clan_cli.arg_actions import AppendOptionAction from clan_cli.inventory import Inventory, init_inventory
from clan_cli.inventory import Meta, load_inventory_json, save_inventory
from ..cmd import CmdOut, run from ..cmd import CmdOut, run
from ..errors import ClanError from ..errors import ClanError
@@ -29,11 +28,9 @@ class CreateClanResponse:
@dataclass @dataclass
class CreateOptions: class CreateOptions:
directory: Path | str directory: Path | str
# Metadata for the clan
# Metadata can be shown with `clan show`
meta: Meta | None = None
# URL to the template to use. Defaults to the "minimal" template # URL to the template to use. Defaults to the "minimal" template
template_url: str = minimal_template_url template_url: str = minimal_template_url
initial: Inventory | None = None
def git_command(directory: Path, *args: str) -> list[str]: def git_command(directory: Path, *args: str) -> list[str]:
@@ -88,17 +85,13 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
git_command(directory, "config", "user.email", "clan@example.com") git_command(directory, "config", "user.email", "clan@example.com")
) )
# Write inventory.json file
inventory = load_inventory_json(directory)
if options.meta is not None:
inventory.meta = options.meta
# Persist creates a commit message for each change
save_inventory(inventory, directory, "Init inventory")
flake_update = run( flake_update = run(
nix_shell(["nixpkgs#nix"], ["nix", "flake", "update"]), cwd=directory nix_shell(["nixpkgs#nix"], ["nix", "flake", "update"]), cwd=directory
) )
if options.initial:
init_inventory(options.directory, init=options.initial)
response = CreateClanResponse( response = CreateClanResponse(
flake_init=flake_init, flake_init=flake_init,
git_init=git_init, git_init=git_init,
@@ -118,15 +111,6 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
default=default_template_url, default=default_template_url,
) )
parser.add_argument(
"--meta",
help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(Meta)]) }""",
nargs=2,
metavar=("name", "value"),
action=AppendOptionAction,
default=[],
)
parser.add_argument( parser.add_argument(
"path", type=Path, help="Path to the clan directory", default=Path(".") "path", type=Path, help="Path to the clan directory", default=Path(".")
) )

View File

@@ -19,7 +19,8 @@ from pathlib import Path
from types import UnionType from types import UnionType
from typing import Any, get_args, get_origin from typing import Any, get_args, get_origin
from clan_cli.errors import ClanError from clan_cli.api import API
from clan_cli.errors import ClanCmdError, ClanError
from clan_cli.git import commit_file from clan_cli.git import commit_file
from ..cmd import run_no_stdout from ..cmd import run_no_stdout
@@ -216,7 +217,7 @@ def load_inventory_json(
Load the inventory file from the flake directory Load the inventory file from the flake directory
If not file is found, returns the default inventory If not file is found, returns the default inventory
""" """
inventory = default_inventory inventory = default
inventory_file = get_path(flake_dir) inventory_file = get_path(flake_dir)
if inventory_file.exists(): if inventory_file.exists():
@@ -228,6 +229,10 @@ def load_inventory_json(
# Error decoding the inventory file # Error decoding the inventory file
raise ClanError(f"Error decoding inventory file: {e}") raise ClanError(f"Error decoding inventory file: {e}")
if not inventory_file.exists():
# Copy over the meta from the flake if the inventory is not initialized
inventory.meta = load_inventory_eval(flake_dir).meta
return inventory return inventory
@@ -242,3 +247,22 @@ def save_inventory(inventory: Inventory, flake_dir: str | Path, message: str) ->
json.dump(dataclass_to_dict(inventory), f, indent=2) json.dump(dataclass_to_dict(inventory), f, indent=2)
commit_file(inventory_file, Path(flake_dir), commit_message=message) commit_file(inventory_file, Path(flake_dir), commit_message=message)
@API.register
def init_inventory(directory: str, init: Inventory | None = None) -> None:
inventory = None
# Try reading the current flake
if init is None:
try:
inventory = load_inventory_eval(directory)
except ClanCmdError:
pass
if init is not None:
inventory = init
# Write inventory.json file
if inventory is not None:
# Persist creates a commit message for each change
save_inventory(inventory, directory, "Init inventory")

View File

@@ -25,6 +25,7 @@ def create_machine(flake: FlakeId, machine: Machine) -> None:
) )
inventory = load_inventory_json(flake.path) inventory = load_inventory_json(flake.path)
full_inventory = load_inventory_eval(flake.path) full_inventory = load_inventory_eval(flake.path)
if machine.name in full_inventory.machines.keys(): if machine.name in full_inventory.machines.keys():

View File

@@ -20,12 +20,12 @@ def test_create_flake(
cli.run(["flakes", "create", str(flake_dir), f"--url={url}"]) cli.run(["flakes", "create", str(flake_dir), f"--url={url}"])
assert (flake_dir / ".clan-flake").exists() assert (flake_dir / ".clan-flake").exists()
# Replace the inputs.clan.url in the template flake.nix # Replace the inputs.clan.url in the template flake.nix
substitute( substitute(
flake_dir / "flake.nix", flake_dir / "flake.nix",
clan_core, clan_core,
) )
# Dont evaluate the inventory before the substitute call
monkeypatch.chdir(flake_dir) monkeypatch.chdir(flake_dir)
cli.run(["machines", "create", "machine1"]) cli.run(["machines", "create", "machine1"])

View File

@@ -32,7 +32,7 @@ let
} }
// flashDiskoConfig; // flashDiskoConfig;
# Important: The partition names need to be different to the clan install # Important: The partition names need to be different to the clan install
flashDiskoConfig = { flashDiskoConfig = {
boot.loader.grub.efiSupport = lib.mkDefault true; boot.loader.grub.efiSupport = lib.mkDefault true;
boot.loader.grub.efiInstallAsRemovable = lib.mkDefault true; boot.loader.grub.efiInstallAsRemovable = lib.mkDefault true;

View File

@@ -42,7 +42,15 @@ export const ClanForm = () => {
await toast.promise( await toast.promise(
(async () => { (async () => {
await callApi("create_clan", { await callApi("create_clan", {
options: { directory: target_dir, meta, template_url }, options: {
directory: target_dir,
template_url,
initial: {
meta,
services: {},
machines: {},
},
},
}); });
setActiveURI(target_dir); setActiveURI(target_dir);
setRoute("machines"); setRoute("machines");

View File

@@ -5,10 +5,7 @@
{ self, clan-core, ... }: { self, clan-core, ... }:
let let
# Usage see: https://docs.clan.lol # Usage see: https://docs.clan.lol
clan = clan-core.lib.buildClan { clan = clan-core.lib.buildClan { directory = self; };
meta.name = "miniclan";
directory = self;
};
in in
{ {
# all machines managed by Clan # all machines managed by Clan

View File

@@ -0,0 +1,5 @@
{
"meta": { "name": "__CHANGE_ME__" },
"machines": {},
"services": {}
}