Merge pull request 'Tests(inventoryStore): add fixture, make extensible' (#3782) from flake-models into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3782
This commit is contained in:
@@ -4,7 +4,6 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -72,101 +71,104 @@ class MockFlake:
|
|||||||
folder_path = Path(__file__).parent.resolve()
|
folder_path = Path(__file__).parent.resolve()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.with_core
|
@pytest.fixture
|
||||||
def test_simple_read_write() -> None:
|
def setup_test_files(tmp_path: Path, request: pytest.FixtureRequest) -> Path:
|
||||||
entry_file = "1.nix"
|
entry_file = request.param # e.g., "1.nix"
|
||||||
inventory_file = entry_file.replace(".nix", ".json")
|
inventory_file = entry_file.replace(".nix", ".json")
|
||||||
|
|
||||||
nix_file = folder_path / f"fixtures/{entry_file}"
|
nix_src = folder_path / "fixtures" / entry_file
|
||||||
json_file = folder_path / f"fixtures/{inventory_file}"
|
json_src = folder_path / "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(
|
nix_dst = tmp_path / entry_file
|
||||||
flake=MockFlake(Path(tmp) / entry_file),
|
json_dst = tmp_path / inventory_file
|
||||||
inventory_file_name=inventory_file,
|
|
||||||
_keys=[], # disable toplevel filtering
|
|
||||||
)
|
|
||||||
store._flake.invalidate_cache()
|
|
||||||
data: dict = store.read() # type: ignore
|
|
||||||
assert data == {"foo": "bar", "protected": "protected"}
|
|
||||||
|
|
||||||
set_value_by_path(data, "foo", "foo") # type: ignore
|
shutil.copyfile(nix_src, nix_dst)
|
||||||
store.write(data, "test", commit=False) # type: ignore
|
shutil.copyfile(json_src, json_dst)
|
||||||
# Default method to access the inventory
|
|
||||||
assert store.read() == {"foo": "foo", "protected": "protected"}
|
|
||||||
|
|
||||||
# Test the data is actually persisted
|
return tmp_path
|
||||||
assert store._get_persisted() == {"foo": "foo"}
|
|
||||||
|
|
||||||
# clan_lib.errors.ClanError: Key 'protected' is not writeable.
|
|
||||||
invalid_data = {"protected": "foo"}
|
|
||||||
with pytest.raises(ClanError) as e:
|
|
||||||
store.write(invalid_data, "test", commit=False) # type: ignore
|
|
||||||
assert str(e.value) == "Key 'protected' is not writeable."
|
|
||||||
|
|
||||||
# Test the data is not touched
|
|
||||||
assert store.read() == data
|
|
||||||
assert store._get_persisted() == {"foo": "foo"}
|
|
||||||
|
|
||||||
# Remove the foo key from the persisted data
|
|
||||||
# Technically data = { } should also work
|
|
||||||
data = {"protected": "protected"}
|
|
||||||
store.write(data, "test", commit=False) # type: ignore
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.with_core
|
@pytest.mark.with_core
|
||||||
def test_read_deferred() -> None:
|
@pytest.mark.parametrize("setup_test_files", ["1.nix"], indirect=True)
|
||||||
entry_file = "deferred.nix"
|
def test_simple_read_write(setup_test_files: Path) -> None:
|
||||||
inventory_file = entry_file.replace(".nix", ".json")
|
files = list(setup_test_files.iterdir())
|
||||||
|
nix_file = next(f for f in files if f.suffix == ".nix")
|
||||||
|
json_file = next(f for f in files if f.suffix == ".json")
|
||||||
|
|
||||||
nix_file = folder_path / f"fixtures/{entry_file}"
|
assert nix_file.exists()
|
||||||
json_file = folder_path / f"fixtures/{inventory_file}"
|
assert json_file.exists()
|
||||||
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(
|
store = InventoryStore(
|
||||||
flake=MockFlake(Path(tmp) / entry_file),
|
flake=MockFlake(nix_file),
|
||||||
inventory_file_name=inventory_file,
|
inventory_file_name=json_file.name,
|
||||||
_allowed_path_transforms=["foo.*"],
|
_keys=[], # disable toplevel filtering
|
||||||
_keys=[], # disable toplevel filtering
|
)
|
||||||
)
|
store._flake.invalidate_cache()
|
||||||
|
data: dict = store.read() # type: ignore
|
||||||
|
assert data == {"foo": "bar", "protected": "protected"}
|
||||||
|
|
||||||
data = store.read()
|
set_value_by_path(data, "foo", "foo") # type: ignore
|
||||||
assert data == {"foo": {"a": {}, "b": {}}}
|
store.write(data, "test", commit=False) # type: ignore
|
||||||
|
# Default method to access the inventory
|
||||||
|
assert store.read() == {"foo": "foo", "protected": "protected"}
|
||||||
|
|
||||||
# Create a new "deferredModule" "C"
|
# Test the data is actually persisted
|
||||||
set_value_by_path(data, "foo.c", {})
|
assert store._get_persisted() == {"foo": "foo"}
|
||||||
store.write(data, "test", commit=False) # type: ignore
|
|
||||||
|
|
||||||
assert store.read() == {"foo": {"a": {}, "b": {}, "c": {}}}
|
# clan_lib.errors.ClanError: Key 'protected' is not writeable.
|
||||||
|
invalid_data = {"protected": "foo"}
|
||||||
|
with pytest.raises(ClanError) as e:
|
||||||
|
store.write(invalid_data, "test", commit=False) # type: ignore
|
||||||
|
assert str(e.value) == "Key 'protected' is not writeable."
|
||||||
|
|
||||||
# Remove the "deferredModule" "C"
|
# Test the data is not touched
|
||||||
delete_by_path(data, "foo.c") # type: ignore
|
assert store.read() == data
|
||||||
store.write(data, "test", commit=False)
|
assert store._get_persisted() == {"foo": "foo"}
|
||||||
assert store.read() == {"foo": {"a": {}, "b": {}}}
|
|
||||||
|
|
||||||
# Write settings into a new "deferredModule" "C" and read them back
|
# Remove the foo key from the persisted data
|
||||||
set_value_by_path(data, "foo.c", {"timeout": "1s"})
|
# Technically data = { } should also work
|
||||||
store.write(data, "test", commit=False) # type: ignore
|
data = {"protected": "protected"}
|
||||||
|
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
|
@pytest.mark.with_core
|
||||||
delete_by_path(data, "foo.c") # type: ignore
|
@pytest.mark.parametrize("setup_test_files", ["deferred.nix"], indirect=True)
|
||||||
store.write(data, "test", commit=False)
|
def test_read_deferred(setup_test_files: Path) -> None:
|
||||||
assert store.read() == {"foo": {"a": {}, "b": {}}}
|
files = list(setup_test_files.iterdir())
|
||||||
|
nix_file = next(f for f in files if f.suffix == ".nix")
|
||||||
|
json_file = next(f for f in files if f.suffix == ".json")
|
||||||
|
|
||||||
|
assert nix_file.exists()
|
||||||
|
assert json_file.exists()
|
||||||
|
|
||||||
|
store = InventoryStore(
|
||||||
|
flake=MockFlake(nix_file),
|
||||||
|
inventory_file_name=json_file.name,
|
||||||
|
_allowed_path_transforms=["foo.*"],
|
||||||
|
_keys=[], # disable toplevel filtering
|
||||||
|
)
|
||||||
|
|
||||||
|
data = store.read()
|
||||||
|
assert data == {"foo": {"a": {}, "b": {}}}
|
||||||
|
|
||||||
|
# Create a new "deferredModule" "C"
|
||||||
|
set_value_by_path(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
|
||||||
|
set_value_by_path(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