Fix(test): migrate machine cli command tests to use the new functions

This commit is contained in:
Johannes Kirschbauer
2025-05-27 17:57:08 +02:00
parent c293d813cc
commit ec6fddce03
2 changed files with 18 additions and 5 deletions

View File

@@ -137,6 +137,7 @@ def create_machine(
) )
inventory_store.write(inventory, message=f"machine '{machine_name}'") inventory_store.write(inventory, message=f"machine '{machine_name}'")
opts.clan_dir.invalidate_cache()
# Commit at the end in that order to avoid committing halve-baked machines # Commit at the end in that order to avoid committing halve-baked machines
# TODO: automatic rollbacks if something goes wrong # TODO: automatic rollbacks if something goes wrong

View File

@@ -1,3 +1,4 @@
# ruff: noqa: SLF001
import pytest import pytest
from clan_cli.secrets.folders import sops_machines_folder from clan_cli.secrets.folders import sops_machines_folder
from clan_cli.tests import fixtures_flakes from clan_cli.tests import fixtures_flakes
@@ -13,6 +14,10 @@ def test_machine_subcommands(
test_flake_with_core: fixtures_flakes.FlakeForTest, test_flake_with_core: fixtures_flakes.FlakeForTest,
capture_output: CaptureOutput, capture_output: CaptureOutput,
) -> None: ) -> None:
inventory_store = InventoryStore(Flake(str(test_flake_with_core.path)))
inventory = inventory_store.read()
assert "machine1" not in inventory.get("machines", {})
cli.run( cli.run(
[ [
"machines", "machines",
@@ -24,11 +29,14 @@ def test_machine_subcommands(
"vm", "vm",
] ]
) )
# Usually this is done by `inventory.write` but we created a separate flake object in the test that now holds stale data
inventory_store._flake.invalidate_cache()
inventory_store = InventoryStore(Flake(str(test_flake_with_core.path))) inventory = inventory_store.read()
inventory: dict = dict(inventory_store.read()) persisted_inventory = inventory_store._get_persisted()
assert "machine1" in inventory["machines"] assert "machine1" in inventory.get("machines", {})
assert "service" not in inventory
assert "services" not in persisted_inventory
with capture_output as output: with capture_output as output:
cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)])
@@ -41,10 +49,14 @@ def test_machine_subcommands(
cli.run( cli.run(
["machines", "delete", "--flake", str(test_flake_with_core.path), "machine1"] ["machines", "delete", "--flake", str(test_flake_with_core.path), "machine1"]
) )
# See comment above
inventory_store._flake.invalidate_cache()
inventory_2: dict = dict(inventory_store.read()) inventory_2: dict = dict(inventory_store.read())
assert "machine1" not in inventory_2["machines"] assert "machine1" not in inventory_2["machines"]
assert "service" not in inventory_2
persisted_inventory = inventory_store._get_persisted()
assert "services" not in persisted_inventory
with capture_output as output: with capture_output as output:
cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)])