mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-10 04:36:40 +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>
59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
import unittest
|
|
import os
|
|
import yaml
|
|
|
|
from utils.cache.yaml import load_yaml_any
|
|
|
|
|
|
class TestBackupsEnabledIntegrity(unittest.TestCase):
|
|
def setUp(self):
|
|
# Path to the roles directory
|
|
self.roles_dir = os.path.abspath(
|
|
os.path.join(os.path.dirname(__file__), "../../../roles")
|
|
)
|
|
|
|
def test_backups_enabled_image_consistency(self):
|
|
"""
|
|
Ensure that if `backups.enabled` is set for any services[*]:
|
|
- it's a boolean value
|
|
- the containing service dict has an `image` entry at the same level
|
|
"""
|
|
for role in os.listdir(self.roles_dir):
|
|
docker_config_path = os.path.join(
|
|
self.roles_dir, role, "meta", "services.yml"
|
|
)
|
|
if not os.path.isfile(docker_config_path):
|
|
continue
|
|
|
|
try:
|
|
config = load_yaml_any(docker_config_path) or {}
|
|
except yaml.YAMLError as e:
|
|
self.fail(f"YAML parsing failed for {docker_config_path}: {e}")
|
|
continue
|
|
|
|
# Per req-008 the file root of meta/services.yml IS the
|
|
# services map (no `compose.services` wrapper).
|
|
services = config if isinstance(config, dict) else {}
|
|
|
|
for service_key, service in services.items():
|
|
if not isinstance(service, dict):
|
|
continue
|
|
|
|
backups_cfg = service.get("backups", {}) or {}
|
|
if "enabled" in backups_cfg:
|
|
with self.subTest(role=role, service=service_key):
|
|
self.assertIsInstance(
|
|
backups_cfg["enabled"],
|
|
bool,
|
|
f"`backups.enabled` in role '{role}', service '{service_key}' must be a boolean.",
|
|
)
|
|
self.assertIn(
|
|
"image",
|
|
service,
|
|
f"`image` is required in role '{role}', service '{service_key}' when `backups.enabled` is defined.",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|