Files
computer-playbook/tests/unit/plugins/lookup/test_applications.py
T
kevinveenbirkenbachandClaude Opus 4.7 c55ad69c41 refactor: drop requirement-file references from code, add doc rule + lint
Strip every `req <NNN>`, `req-<NNN>`, `Requirement <NNN>`,
`requirement-<NNN>`, and `docs/requirements/<NNN>-...md` mention from
non-Markdown source files. Comments anchored to requirement numbers
rot when requirements are renumbered or rewritten and conflate
present-state behaviour (which the comment should describe) with the
audit trail of how the code got there (which belongs in commit
messages, PR descriptions, and the requirement file itself).

* Document the rule in `docs/contributing/documentation.md` under
  "Comments". Code MUST describe the *why* in present-state language;
  contract enforcement lives in tests, not folklore.
* Clean 254 source files across `roles/`, `tests/`, `plugins/`,
  `cli/`, `scripts/`, `utils/`, `group_vars/`, and the repo-root
  `eslint.config.js`. Pure comment / docstring / log-message edits;
  no behavioural change.
* Add `tests/lint/repository/test_no_req_references_in_code.py` to
  enforce the rule going forward across `.js`, `.py`, `.yml`,
  `.yaml`, `.j2`, and `.sh` files. The legacy
  `test_no_stale_requirement_refs.py` is exempt as the only file
  whose purpose is to detect these very tokens.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 16:25:22 +02:00

106 lines
3.6 KiB
Python

from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from ansible.errors import AnsibleError
from plugins.lookup.applications import LookupModule, _reset_cache_for_tests
from utils.cache.yaml import dump_yaml_str
from utils.roles.mapping import ROLE_FILE_META_SERVICES
def _write_config(base_dir: Path, application_id: str, config: dict) -> None:
config_path = base_dir / "roles" / application_id / ROLE_FILE_META_SERVICES
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(dump_yaml_str(config), encoding="utf-8")
class TestApplicationsLookup(unittest.TestCase):
def setUp(self) -> None:
self.lookup = LookupModule()
self._cwd = str(Path.cwd())
self._tmpdir = tempfile.TemporaryDirectory()
self._tmp = Path(self._tmpdir.name)
os.chdir(self._tmp)
_reset_cache_for_tests()
_write_config(
self._tmp,
"web-app-foo",
{"smtp": {"host": "mail.example.org"}, "feature": {"enabled": True}},
)
_write_config(self._tmp, "web-app-bar", {})
def tearDown(self) -> None:
_reset_cache_for_tests()
os.chdir(self._cwd)
self._tmpdir.cleanup()
def test_returns_full_mapping(self) -> None:
# Per the file root of meta/services.yml is the services map,
# so the loader puts it under `services` in the materialised payload.
result = self.lookup.run([], variables={}, roles_dir=str(self._tmp / "roles"))[
0
]
self.assertIn("web-app-foo", result)
self.assertIn("web-app-bar", result)
self.assertEqual(
result["web-app-foo"]["services"]["smtp"]["host"], "mail.example.org"
)
def test_returns_single_entry(self) -> None:
result = self.lookup.run(
["web-app-foo"],
variables={},
roles_dir=str(self._tmp / "roles"),
)[0]
self.assertEqual(result["services"]["smtp"]["host"], "mail.example.org")
def test_applies_inventory_override(self) -> None:
result = self.lookup.run(
["web-app-foo"],
variables={
"applications": {
"web-app-foo": {
"services": {"smtp": {"host": "override.example.org"}}
}
}
},
roles_dir=str(self._tmp / "roles"),
)[0]
self.assertEqual(result["services"]["smtp"]["host"], "override.example.org")
self.assertTrue(result["services"]["feature"]["enabled"])
def test_ignores_non_mapping_runtime_applications_placeholder(self) -> None:
result = self.lookup.run(
["web-app-foo"],
variables={"applications": "web-app-foo"},
roles_dir=str(self._tmp / "roles"),
)[0]
self.assertEqual(result["services"]["smtp"]["host"], "mail.example.org")
self.assertTrue(result["services"]["feature"]["enabled"])
def test_missing_entry_raises_without_default(self) -> None:
with self.assertRaises(AnsibleError):
self.lookup.run(
["web-app-missing"],
variables={},
roles_dir=str(self._tmp / "roles"),
)
def test_missing_entry_returns_default_when_provided(self) -> None:
default_value = {"fallback": True}
result = self.lookup.run(
["web-app-missing", default_value],
variables={},
roles_dir=str(self._tmp / "roles"),
)[0]
self.assertEqual(result, default_value)
if __name__ == "__main__":
unittest.main()