Files
computer-playbook/tests/unit/cli/validate/test_inventory_hosts.py
T
kevinveenbirkenbachandClaude Opus 4.6 b70a783423 refactor(lookups): migrate to runtime applications/users lookups
Replaces inline `applications`/`users` merging with dedicated lookup
plugins and runtime hydration. Introduces `add_csp_hash` filter,
integrates sort into `find_dock_val_by_bkp_entr`, splits the Keycloak
one-shot realm import into `05a_realm_import_run.yml`, and fixes the
Mastodon administrator task YAML. Removes obsolete `csp_hashes` filter,
`sys-token-store` Jinja filter, and empty CLI setup modules.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-15 23:44:19 +02:00

60 lines
2.1 KiB
Python

import os
import re
import yaml
import tempfile
import shutil
import unittest
# Module under test
import cli.validate.inventory as inventory_mod
class TestValidateHostKeys(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.valid_ids = {"valid-service", "another-service"}
def tearDown(self):
shutil.rmtree(self.test_dir)
def write_devices_yaml(self, groups):
# Build the YAML structure
content = {"all": {"children": {}}}
for group, hosts in groups.items():
# hosts is a list of hostnames
content["all"]["children"][group] = {"hosts": {h: None for h in hosts}}
path = os.path.join(self.test_dir, "devices.yml")
with open(path, "w", encoding="utf-8") as f:
yaml.safe_dump(content, f)
return path
def test_no_invalid_groups(self):
# All groups valid
groups = {"valid-service": ["host1"], "another-service": ["host2", "host3"]}
self.write_devices_yaml(groups)
errors = inventory_mod.validate_host_keys(self.valid_ids, self.test_dir)
self.assertEqual(errors, [], f"Expected no errors, got {errors}")
def test_single_invalid_group(self):
# One invalid group
groups = {"valid-service": ["host1"], "invalid-service": ["host2"]}
self.write_devices_yaml(groups)
errors = inventory_mod.validate_host_keys(self.valid_ids, self.test_dir)
self.assertEqual(len(errors), 1)
self.assertRegex(
errors[0], r"Invalid group 'invalid-service'.*not in application_ids"
)
def test_multiple_invalid_groups(self):
# Multiple invalid groups
groups = {"bad-one": ["h1"], "bad-two": ["h2"], "valid-service": ["h3"]}
self.write_devices_yaml(groups)
errors = inventory_mod.validate_host_keys(self.valid_ids, self.test_dir)
self.assertEqual(len(errors), 2)
found = {re.search(r"Invalid group '(.+?)'", e).group(1) for e in errors}
self.assertSetEqual(found, {"bad-one", "bad-two"})
if __name__ == "__main__":
unittest.main()