test(persist/inventory): add test for adding deferredModule

This feature will allow us to read/write to 'settings' of service
'instances' which are of type deferredModule.
Usually a deferredModule needs to be evaulated, but because we use our
own type, which forces some constraints, we can safely perform read and
write
This commit is contained in:
Johannes Kirschbauer
2025-05-23 21:49:16 +02:00
parent f746184bf6
commit d8a835c7ae
3 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1,28 @@
{ clanLib, lib, ... }:
let
eval = lib.evalModules {
modules = [
{
# Trying to write into the default
options.foo = lib.mkOption {
type = lib.types.attrsOf clanLib.types.uniqueDeferredSerializableModule;
};
}
{
foo = {
a = { };
b = { };
};
}
# Merge the "inventory.json"
(builtins.fromJSON (builtins.readFile ./deferred.json))
];
};
in
{
clanInternals.inventoryClass.inventory = eval.config;
clanInternals.inventoryClass.introspection = clanLib.introspection.getPrios {
options = eval.options;
};
}

View File

@@ -11,6 +11,7 @@ import pytest
from clan_lib.errors import ClanError
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch, delete_by_path
class MockFlake:
@@ -112,3 +113,51 @@ def test_simple_read_write() -> None:
# Technically data = { } should also work
data = {"protected": "protected"}
store.write(data, "test", commit=False) # type: ignore
def test_read_deferred() -> None:
entry_file = "deferred.nix"
inventory_file = entry_file.replace(".nix", ".json")
nix_file = folder_path / f"fixtures/{entry_file}"
json_file = folder_path / f"fixtures/{inventory_file}"
with TemporaryDirectory() as tmp:
shutil.copyfile(
str(nix_file),
str(Path(tmp) / entry_file),
)
shutil.copyfile(
str(json_file),
str(Path(tmp) / inventory_file),
)
store = InventoryStore(
flake=MockFlake(Path(tmp) / entry_file),
inventory_file_name=inventory_file,
_allowed_path_transforms=["foo.*"],
)
data = store.read()
assert data == {"foo": {"a": {}, "b": {}}}
# Create a new "deferredModule" "C"
apply_patch(data, "foo.c", {})
store.write(data, "test", commit=False) # type: ignore
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}}
# Remove the "deferredModule" "C"
delete_by_path(data, "foo.c") # type: ignore
store.write(data, "test", commit=False)
assert store.read() == {"foo": {"a": {}, "b": {}}}
# Write settings into a new "deferredModule" "C" and read them back
apply_patch(data, "foo.c", {"timeout": "1s"})
store.write(data, "test", commit=False) # type: ignore
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {"timeout": "1s"}}}
# Remove the "deferredModule" "C" along with its settings
delete_by_path(data, "foo.c") # type: ignore
store.write(data, "test", commit=False)
assert store.read() == {"foo": {"a": {}, "b": {}}}