fix(clan/machines): move machineClass attribute selector and add a unit test for class detection

This commit is contained in:
Johannes Kirschbauer
2025-04-09 18:29:43 +02:00
parent 35379d3b86
commit ab9c0d2904
2 changed files with 49 additions and 1 deletions

View File

@@ -62,7 +62,7 @@ class Machine:
def _class_(self) -> str: def _class_(self) -> str:
try: try:
return self.flake.select( return self.flake.select(
f"clanInternals.inventory.machineClass.{self.name}" f"clanInternals.inventory.machines.{self.name}.machineClass"
) )
except ClanCmdError as e: except ClanCmdError as e:
if re.search(f"error: attribute '{self.name}' missing", e.cmd.stderr): if re.search(f"error: attribute '{self.name}' missing", e.cmd.stderr):

View File

@@ -0,0 +1,48 @@
import pytest
from clan_cli.flake import Flake
from clan_cli.machines.machines import Machine
# Functions to test
from clan_cli.tests.fixtures_flakes import FlakeForTest
@pytest.mark.parametrize(
"test_flake_with_core",
[
# Two nixos machines
{
"inventory_expr": r"""{
machines.jon1 = { };
machines.jon2 = { machineClass = "nixos"; };
machines.sara = { machineClass = "darwin"; };
}"""
},
],
# Important!
# tells pytest to pass these values to the fixture
# So we can write it to the flake fixtures
indirect=True,
)
@pytest.mark.with_core
def test_inventory_deserialize_variants(
test_flake_with_core: FlakeForTest,
) -> None:
"""
Testing different inventory deserializations
Inventory should always be deserializable to a dict
"""
machine_jon1 = Machine(
name="jon",
flake=Flake(str(test_flake_with_core.path)),
)
machine_jon2 = Machine(
name="jon2",
flake=Flake(str(test_flake_with_core.path)),
)
machine_sara = Machine(
name="sara",
flake=Flake(str(test_flake_with_core.path)),
)
assert machine_jon1._class_ == "nixos"
assert machine_jon2._class_ == "nixos"
assert machine_sara._class_ == "darwin"