mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-09 04:06:42 +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>
111 lines
3.8 KiB
Python
111 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from utils.valid_deploy_id import ValidDeployId
|
|
|
|
from utils.cache.yaml import dump_yaml
|
|
|
|
|
|
class TestValidDeployId(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
# Uses real repo roles/ via ValidDeployId internal repo-root resolution
|
|
self.validator = ValidDeployId()
|
|
|
|
# pick a real application id from roles for positive tests
|
|
self.assertTrue(
|
|
self.validator.valid_ids,
|
|
"Expected at least one application id discovered from repo roles/",
|
|
)
|
|
self.existing_app = sorted(self.validator.valid_ids)[0]
|
|
|
|
# and a guaranteed non-existing id for negative tests
|
|
self.missing_app = "this-app-id-should-not-exist-xyz-123"
|
|
|
|
def _write_ini_inventory(self, content: str) -> str:
|
|
fd, path = tempfile.mkstemp(suffix=".ini")
|
|
os.close(fd)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
return path
|
|
|
|
def _write_yaml_inventory(self, data) -> str:
|
|
fd, path = tempfile.mkstemp(suffix=".yml")
|
|
os.close(fd)
|
|
dump_yaml(path, data)
|
|
return path
|
|
|
|
def test_valid_in_roles_and_ini_inventory(self) -> None:
|
|
ini_content = f"""
|
|
[servers]
|
|
{self.existing_app},otherhost
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
self.assertEqual(
|
|
result, {}, "app should be valid when in roles and ini inventory"
|
|
)
|
|
|
|
def test_missing_in_roles(self) -> None:
|
|
ini_content = f"""
|
|
[servers]
|
|
{self.missing_app}
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": True}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_in_inventory_ini(self) -> None:
|
|
ini_content = """
|
|
[servers]
|
|
otherhost
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
expected = {self.existing_app: {"in_roles": True, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_both_ini(self) -> None:
|
|
ini_content = """
|
|
[servers]
|
|
otherhost
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_valid_in_roles_and_yaml_inventory(self) -> None:
|
|
data = {self.existing_app: {"hosts": ["localhost"]}}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
self.assertEqual(result, {}, "app should be valid in roles and yaml inventory")
|
|
|
|
def test_missing_in_roles_yaml(self) -> None:
|
|
data = {self.missing_app: {}}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": True}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_in_inventory_yaml(self) -> None:
|
|
data = {"group": {"other": {}}}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
expected = {self.existing_app: {"in_roles": True, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_both_yaml(self) -> None:
|
|
data = {}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|