generate_test_vars: fix script

This commit is contained in:
DavHau
2025-09-16 14:42:22 +07:00
parent 5d38824d8e
commit 1f3aa0075e
3 changed files with 46 additions and 4 deletions

View File

@@ -9,7 +9,7 @@ from functools import cache
from hashlib import sha1 from hashlib import sha1
from pathlib import Path from pathlib import Path
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from typing import Any from typing import TYPE_CHECKING, Any
from clan_lib.cmd import Log, RunOpts, run from clan_lib.cmd import Log, RunOpts, run
from clan_lib.dirs import select_source, user_cache_dir from clan_lib.dirs import select_source, user_cache_dir
@@ -22,6 +22,12 @@ from clan_lib.nix import (
nix_test_store, nix_test_store,
) )
if TYPE_CHECKING:
from clan_lib.machines.actions import (
ListOptions,
MachineResponse,
)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -1102,6 +1108,15 @@ class Flake:
full_selector = f'clanInternals.machines."{system}"."{machine_name}".{selector}' full_selector = f'clanInternals.machines."{system}"."{machine_name}".{selector}'
return self.select(full_selector) return self.select(full_selector)
def list_machines(
self,
opts: "ListOptions | None" = None,
) -> "dict[str, MachineResponse]":
"""List machines of a clan"""
from clan_lib.machines.actions import list_machines # noqa: PLC0415
return list_machines(self, opts)
def require_flake(flake: Flake | None) -> Flake: def require_flake(flake: Flake | None) -> Flake:
"""Require that a flake argument is provided, if not in a clan flake. """Require that a flake argument is provided, if not in a clan flake.

View File

@@ -8,7 +8,6 @@ from clan_cli.vars.migration import check_can_migrate, migrate_files
from clan_lib.api import API from clan_lib.api import API
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_lib.machines.actions import list_machines
from clan_lib.machines.machines import Machine from clan_lib.machines.machines import Machine
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -39,7 +38,7 @@ def get_generators(
msg = "At least one machine must be provided" msg = "At least one machine must be provided"
raise ClanError(msg) raise ClanError(msg)
all_machines = list_machines(machines[0].flake).keys() all_machines = machines[0].flake.list_machines().keys()
requested_machines = [machine.name for machine in machines] requested_machines = [machine.name for machine in machines]
all_generators_list = Generator.get_machine_generators( all_generators_list = Generator.get_machine_generators(

View File

@@ -9,7 +9,7 @@ import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from typing import Any, override from typing import TYPE_CHECKING, Any, override
from clan_cli.vars.generator import Generator from clan_cli.vars.generator import Generator
from clan_cli.vars.prompt import PromptType from clan_cli.vars.prompt import PromptType
@@ -20,6 +20,12 @@ from clan_lib.machines.machines import Machine
from clan_lib.nix import nix_config, nix_eval, nix_test_store from clan_lib.nix import nix_config, nix_eval, nix_test_store
from clan_lib.vars.generate import run_generators from clan_lib.vars.generate import run_generators
if TYPE_CHECKING:
from clan_lib.machines.actions import (
ListOptions,
MachineResponse,
)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
sops_priv_key = ( sops_priv_key = (
@@ -87,6 +93,26 @@ class TestFlake(Flake):
full_selector = f'checks."{test_system}".{self.check_attr}.machinesCross.{system}."{machine_name}".{selector}' full_selector = f'checks."{test_system}".{self.check_attr}.machinesCross.{system}."{machine_name}".{selector}'
return self.select(full_selector) return self.select(full_selector)
# we don't want to evaluate all machines of the flake. Only the ones defined in the test
def set_machine_names(self, machine_names: list[str]) -> None:
"""Set the machine names for this flake instance to fake the machines defined by the test"""
self._machine_names = machine_names
def list_machines(
self,
opts: "ListOptions | None" = None, # noqa: ARG002
) -> "dict[str, MachineResponse]":
"""List machines of a clan"""
from clan_lib.machines.actions import ( # noqa: PLC0415
InventoryMachine,
MachineResponse,
)
res = {}
for name in self._machine_names:
res[name] = MachineResponse(data=InventoryMachine())
return res
class TestMachine(Machine): class TestMachine(Machine):
"""Machine class which is able to deal with not having an actual flake. """Machine class which is able to deal with not having an actual flake.
@@ -203,6 +229,8 @@ def main() -> None:
test_system, test_system,
) )
flake.set_machine_names(machine_names)
flake.precache( flake.precache(
[ [
f"checks.{test_system}.{opts.check_attr}.machinesCross.{system}.{{{','.join(machine_names)}}}.config.clan.core.vars.generators.*.validationHash", f"checks.{test_system}.{opts.check_attr}.machinesCross.{system}.{{{','.join(machine_names)}}}.config.clan.core.vars.generators.*.validationHash",