mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-12 21:56:21 +00:00
Stops every direct `yaml.safe_load`/`safe_load_all`/`safe_dump`/`dump`
call across the test corpus from re-parsing files the production cache
already holds. Each migrated test loads role meta, group_vars, inventory
fixtures, etc. through the shared cache, so a single `make test` run
parses every YAML file at most once across all suites.
Cache module changes (utils/cache/yaml.py):
- Unify single- and multi-doc cache: one `_CACHE: Dict[sig, list]`
holding the `safe_load_all` payload. Single-doc helpers
(load_yaml / load_yaml_any) unwrap docs[0] and raise on >1
document, preserving the old strict semantics. A file consumed
both as single-doc and multi-doc is now parsed exactly once.
- Add load_yaml_all (path-cached) and load_yaml_all_str (string,
no cache) so multi-doc consumers no longer need a direct
`yaml.safe_load_all` plus a separate file read.
- dump_yaml now accepts any safe-dumpable structure (dict, list,
scalar) so test fixtures writing list-of-tasks YAML can use it.
Lint guard (tests/lint/repository/test_no_direct_yaml_calls.py):
- Extend the scanned tree to include `tests/` so future test code
is forced through the cache, with the existing
`# noqa: direct-yaml` per-call opt-out for genuine raw-yaml
fixture needs.
Test corpus:
- 89 test files migrated to load_yaml / load_yaml_any /
load_yaml_str / load_yaml_all / load_yaml_all_str / dump_yaml /
dump_yaml_str. No `# noqa` shortcuts.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
import os
|
|
import re
|
|
import tempfile
|
|
import shutil
|
|
import unittest
|
|
|
|
# Module under test
|
|
import cli.validate.inventory as inventory_mod
|
|
|
|
from utils.cache.yaml import dump_yaml
|
|
|
|
|
|
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")
|
|
dump_yaml(path, content)
|
|
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()
|