generated from coulomb/repo-seed
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
"""Tests for warden.inventory."""
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from warden.inventory import (
|
|
ActorEntry,
|
|
InventoryError,
|
|
PrincipalsInventory,
|
|
load_inventory,
|
|
save_inventory,
|
|
)
|
|
from warden.models import ActorType
|
|
|
|
|
|
def test_empty_inventory_on_missing_file(tmp_path):
|
|
inv = load_inventory(tmp_path / "nonexistent.yaml")
|
|
assert inv.actors == {}
|
|
assert inv.hosts == {}
|
|
|
|
|
|
def test_roundtrip(tmp_path):
|
|
inv = PrincipalsInventory()
|
|
inv.actors["agt-test"] = ActorEntry(
|
|
name="agt-test",
|
|
actor_type=ActorType.AGT,
|
|
principals=["agt-task-test"],
|
|
ttl_hours=24,
|
|
description="test actor",
|
|
)
|
|
path = tmp_path / "inventory.yaml"
|
|
save_inventory(inv, path)
|
|
|
|
loaded = load_inventory(path)
|
|
assert "agt-test" in loaded.actors
|
|
entry = loaded.actors["agt-test"]
|
|
assert entry.actor_type == ActorType.AGT
|
|
assert entry.principals == ["agt-task-test"]
|
|
assert entry.ttl_hours == 24
|
|
assert entry.description == "test actor"
|
|
|
|
|
|
def test_roundtrip_multiple_actors(tmp_path):
|
|
inv = PrincipalsInventory()
|
|
inv.actors["adm-bernd"] = ActorEntry("adm-bernd", ActorType.ADM, ["adm-full"], 48)
|
|
inv.actors["atm-backup"] = ActorEntry("atm-backup", ActorType.ATM, ["atm-backup-daily"], 8)
|
|
path = tmp_path / "inventory.yaml"
|
|
save_inventory(inv, path)
|
|
|
|
loaded = load_inventory(path)
|
|
assert set(loaded.actors) == {"adm-bernd", "atm-backup"}
|
|
assert loaded.actors["adm-bernd"].actor_type == ActorType.ADM
|
|
|
|
|
|
def test_invalid_actor_type_raises(tmp_path):
|
|
path = tmp_path / "inventory.yaml"
|
|
path.write_text("actors:\n agt-test:\n type: bogus\n principals: []\n")
|
|
with pytest.raises(InventoryError, match="invalid type"):
|
|
load_inventory(path)
|
|
|
|
|
|
def test_actor_name_prefix_violation_raises(tmp_path):
|
|
path = tmp_path / "inventory.yaml"
|
|
path.write_text("actors:\n wrong-name:\n type: agt\n principals: [x]\n")
|
|
with pytest.raises(InventoryError):
|
|
load_inventory(path)
|
|
|
|
|
|
def test_default_principal_is_actor_name(tmp_path):
|
|
path = tmp_path / "inventory.yaml"
|
|
path.write_text("actors:\n agt-bridge:\n type: agt\n")
|
|
inv = load_inventory(path)
|
|
assert inv.actors["agt-bridge"].principals == ["agt-bridge"]
|
|
|
|
|
|
def test_default_ttl_applied(tmp_path):
|
|
path = tmp_path / "inventory.yaml"
|
|
path.write_text("actors:\n atm-cron:\n type: atm\n principals: [atm-cron]\n")
|
|
inv = load_inventory(path)
|
|
assert inv.actors["atm-cron"].ttl_hours == 8 # DEFAULT_TTL_HOURS[ATM]
|
|
|
|
|
|
def test_invalid_yaml_raises(tmp_path):
|
|
path = tmp_path / "inventory.yaml"
|
|
path.write_text(": : : invalid yaml :::")
|
|
with pytest.raises(InventoryError, match="Invalid YAML"):
|
|
load_inventory(path)
|