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:
1
pkgs/clan-cli/clan_lib/persist/fixtures/deferred.json
Normal file
1
pkgs/clan-cli/clan_lib/persist/fixtures/deferred.json
Normal file
@@ -0,0 +1 @@
|
||||
{}
|
||||
28
pkgs/clan-cli/clan_lib/persist/fixtures/deferred.nix
Normal file
28
pkgs/clan-cli/clan_lib/persist/fixtures/deferred.nix
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -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": {}}}
|
||||
|
||||
Reference in New Issue
Block a user