Merge pull request 'Fix test_create.py' (#3818) from Qubasa/clan-core:minimized-2 into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3818
This commit is contained in:
Luis Hebendanz
2025-06-02 15:08:00 +00:00
3 changed files with 60 additions and 44 deletions

View File

@@ -149,7 +149,7 @@ class ModuleManifest(TypedDict):
@dataclass @dataclass
class ModuleInfo: class ModuleInfo(TypedDict):
manifest: ModuleManifest manifest: ModuleManifest
roles: dict[str, None] roles: dict[str, None]

View File

@@ -4,7 +4,7 @@ import shutil
import sys import sys
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any, cast
import clan_cli.clan.create import clan_cli.clan.create
import pytest import pytest
@@ -17,15 +17,23 @@ from clan_cli.ssh.host_key import HostKeyCheck
from clan_cli.vars.generate import generate_vars_for_machine, get_generators_closure from clan_cli.vars.generate import generate_vars_for_machine, get_generators_closure
from clan_lib.api.disk import hw_main_disk_options, set_machine_disk_schema from clan_lib.api.disk import hw_main_disk_options, set_machine_disk_schema
from clan_lib.api.modules import list_modules
from clan_lib.api.network import check_machine_online from clan_lib.api.network import check_machine_online
from clan_lib.cmd import RunOpts, run from clan_lib.cmd import RunOpts, run
from clan_lib.dirs import specific_machine_dir from clan_lib.dirs import specific_machine_dir
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.inventory import InventoryStore
from clan_lib.machines.machines import Machine from clan_lib.machines.machines import Machine
from clan_lib.nix import nix_command from clan_lib.nix import nix_command
from clan_lib.nix_models.clan import InventoryMachine from clan_lib.nix_models.clan import (
InventoryInstancesType,
InventoryMachine,
InventoryServicesType,
Unknown,
)
from clan_lib.nix_models.clan import InventoryMachineDeploy as MachineDeploy from clan_lib.nix_models.clan import InventoryMachineDeploy as MachineDeploy
from clan_lib.persist.util import set_value_by_path
from clan_lib.ssh.remote import Remote from clan_lib.ssh.remote import Remote
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -33,8 +41,8 @@ log = logging.getLogger(__name__)
@dataclass @dataclass
class InventoryWrapper: class InventoryWrapper:
services: dict[str, Any] services: InventoryServicesType
instances: dict[str, Any] instances: InventoryInstancesType
@dataclass @dataclass
@@ -58,16 +66,6 @@ def create_base_inventory(ssh_keys_pairs: list[SSHKeyPair]) -> InventoryWrapper:
"""Create the base inventory structure.""" """Create the base inventory structure."""
legacy_services: dict[str, Any] = { legacy_services: dict[str, Any] = {
"sshd": {
"someid": {
"roles": {
"server": {
"tags": ["all"],
"config": {},
}
}
}
},
"state-version": { "state-version": {
"someid": { "someid": {
"roles": { "roles": {
@@ -78,21 +76,27 @@ def create_base_inventory(ssh_keys_pairs: list[SSHKeyPair]) -> InventoryWrapper:
} }
}, },
} }
instances = {
"admin-1": { instances = InventoryInstancesType(
"module": {"name": "admin"}, {
"admin-inst": {
"module": {"name": "admin", "input": "clan-core"},
"roles": { "roles": {
"default": { "default": {
"tags": {"all": {}}, "tags": {"all": {}},
"settings": { "settings": cast(
Unknown,
{
"allowedKeys": { "allowedKeys": {
key.username: key.ssh_pubkey_txt for key in ssh_keys key.username: key.ssh_pubkey_txt for key in ssh_keys
}
}, },
}, ),
}, },
}, },
} }
} }
)
return InventoryWrapper(services=legacy_services, instances=instances) return InventoryWrapper(services=legacy_services, instances=instances)
@@ -187,7 +191,6 @@ def test_clan_create_api(
clan_dir_flake, inv_machine, target_host=f"{host.target}:{ssh_port_var}" clan_dir_flake, inv_machine, target_host=f"{host.target}:{ssh_port_var}"
) )
) )
machine = Machine( machine = Machine(
name=vm_name, name=vm_name,
flake=clan_dir_flake, flake=clan_dir_flake,
@@ -203,22 +206,35 @@ def test_clan_create_api(
result = check_machine_online(machine) result = check_machine_online(machine)
assert result == "Online", f"Machine {machine.name} is not online" assert result == "Online", f"Machine {machine.name} is not online"
# ssh_keys = [ ssh_keys = [
# SSHKeyPair( SSHKeyPair(
# private=private_key, private=private_key,
# public=public_key, public=public_key,
# ) )
# ] ]
# ===== CREATE BASE INVENTORY ====== # ===== CREATE BASE INVENTORY ======
# TODO(@Qubasa): This seems unused? inventory_conf = create_base_inventory(ssh_keys)
# inventory = create_base_inventory(ssh_keys) store = InventoryStore(clan_dir_flake)
# patch_inventory_with(Flake(str(dest_clan_dir)), "services", inventory.services) inventory = store.read()
modules = list_modules(str(clan_dir_flake.path))
assert (
modules["modulesPerSource"]["clan-core"]["admin"]["manifest"]["name"]
== "clan-core/admin"
)
set_value_by_path(inventory, "services", inventory_conf.services)
set_value_by_path(inventory, "instances", inventory_conf.instances)
store.write(
inventory,
"base config",
)
# Invalidate cache because of new inventory # Invalidate cache because of new inventory
clan_dir_flake.invalidate_cache() clan_dir_flake.invalidate_cache()
generators = get_generators_closure(machine.name, dest_clan_dir) generators = get_generators_closure(machine.name, machine.flake.path)
all_prompt_values = {} all_prompt_values = {}
for generator in generators: for generator in generators:
prompt_values = {} prompt_values = {}
@@ -232,9 +248,9 @@ def test_clan_create_api(
all_prompt_values[generator.name] = prompt_values all_prompt_values[generator.name] = prompt_values
generate_vars_for_machine( generate_vars_for_machine(
machine.name, machine_name=machine.name,
base_dir=machine.flake.path,
generators=[gen.name for gen in generators], generators=[gen.name for gen in generators],
base_dir=dest_clan_dir,
all_prompt_values=all_prompt_values, all_prompt_values=all_prompt_values,
) )
@@ -269,7 +285,6 @@ def test_clan_create_api(
set_machine_disk_schema(machine, "single-disk", placeholders) set_machine_disk_schema(machine, "single-disk", placeholders)
clan_dir_flake.invalidate_cache() clan_dir_flake.invalidate_cache()
# @Qubasa what does this assert check, why does it raise? with pytest.raises(ClanError) as exc_info:
# with pytest.raises(ClanError) as exc_info: machine.build_nix("config.system.build.toplevel")
# machine.build_nix("config.system.build.toplevel") assert "nixos-system-test-clan" in str(exc_info.value)
# assert "nixos-system-test-clan" in str(exc_info.value)

View File

@@ -22,6 +22,7 @@
"nixosModules" "nixosModules"
"flake.lock" "flake.lock"
"templates" "templates"
"clanServices"
]; ];
}; };
}; };