api/machines: add tests for tags readOnly

This commit is contained in:
Johannes Kirschbauer
2025-08-11 18:06:06 +02:00
parent 1009c61c9f
commit 4f29f2e2ca
4 changed files with 21 additions and 4 deletions

View File

@@ -11,9 +11,9 @@ from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import (
get_value_by_path,
is_writeable_key,
list_difference,
retrieve_typed_field_names,
set_value_by_path,
list_difference,
)
@@ -134,7 +134,9 @@ def get_machine_fields_schema(machine: Machine) -> dict[str, FieldSchema]:
persisted_data = inventory_store._get_persisted() # noqa: SLF001
inventory = inventory_store.read() #
all_tags = get_value_by_path(inventory, f"machines.{machine.name}.tags", [])
persisted_tags = get_value_by_path(persisted_data, f"machines.{machine.name}.tags", [])
persisted_tags = get_value_by_path(
persisted_data, f"machines.{machine.name}.tags", []
)
nix_tags = list_difference(all_tags, persisted_tags)
return {

View File

@@ -9,6 +9,8 @@ from clan_lib.flake import Flake
from clan_lib.machines import actions as actions_module
from clan_lib.machines.machines import Machine
from clan_lib.nix_models.clan import Clan, InventoryMachine, Unknown
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import get_value_by_path, set_value_by_path
from .actions import get_machine, get_machine_fields_schema, list_machines, set_machine
@@ -191,6 +193,18 @@ def test_get_machine_writeability(clan_flake: Callable[..., Flake]) -> None:
},
)
# TODO: Move this into the api
inventory_store = InventoryStore(flake=flake)
inventory = inventory_store.read()
curr_tags = get_value_by_path(inventory, "machines.jon.tags", [])
new_tags = ["managed1", "managed2"]
set_value_by_path(inventory, "machines.jon.tags", [*curr_tags, *new_tags])
inventory_store.write(inventory, message="Test writeability")
# Check that the tags were updated
persisted = inventory_store._get_persisted() # noqa: SLF001
assert get_value_by_path(persisted, "machines.jon.tags", []) == new_tags
write_info = get_machine_fields_schema(Machine("jon", flake))
# {'tags': {'writable': True, 'reason': None}, 'machineClass': {'writable': False, 'reason': None}, 'name': {'writable': False, 'reason': None}, 'description': {'writable': True, 'reason': None}, 'deploy.buildHost': {'writable': True, 'reason': None}, 'icon': {'writable': True, 'reason': None}, 'deploy.targetHost': {'writable': True, 'reason': None}}
@@ -207,3 +221,5 @@ def test_get_machine_writeability(clan_flake: Callable[..., Flake]) -> None:
"icon",
}
assert read_only_fields == {"machineClass", "name"}
assert write_info["tags"]["readonly_members"] == ["nix1", "all", "nixos"]

View File

@@ -461,7 +461,6 @@ def get_value_by_path(d: DictLike, path: str, fallback: Any = None) -> Any:
return fallback
def set_value_by_path(d: DictLike, path: str, content: Any) -> None:
"""
Update the value at a specific dot-separated path in a nested dictionary.

View File

@@ -9,10 +9,10 @@ from clan_lib.persist.util import (
calc_patches,
delete_by_path,
determine_writeability,
list_difference,
merge_objects,
path_match,
set_value_by_path,
list_difference,
)