Files
computer-playbook/tests/unit/utils/roles/validation/test_deploy_id.py
T
kevinveenbirkenbachandClaude Opus 4.7 85f1caccc7 refactor(utils): consolidate role-scoped helpers under utils/roles and split templating
Tighten the utils namespace so each subpackage owns one concern:

- utils/applications/* moves under utils/roles/applications/* (helpers
  that operate on the runtime applications dict and meta/services.yml).
- utils/service_registry.py and utils/database_service.py move under
  utils/roles/applications/services/ as registry.py and database.py.
- utils/entity_name_utils.py becomes utils/roles/entity_name.py.
- utils/invokable.py, utils/valid_deploy_id.py, and the existing
  utils/roles/resource_validation.py group under utils/roles/validation/
  as invokable.py, deploy_id.py, and resources.py.
- utils/templating.py and utils/jinja_strict.py split into
  utils/templating/ansible.py (Templar + TrustedAsTemplate, lookup-env
  shortcut) and utils/templating/jinja.py (vanilla Jinja2 Environment,
  multi-pass strict render).

Every caller in utils, cli, plugins, roles/<role>/{filter,lookup}_plugins,
tests, and the docs that link to the moved files is updated to the new
import paths. Test files mirror the new source layout
(tests/unit/utils/{roles,templating}/...).

New READMEs at utils/cleanup/, utils/roles/, utils/roles/applications/services/,
utils/roles/validation/, and utils/templating/ follow the
documentation.md and readme_md.md guidelines.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 03:53:43 +02:00

111 lines
3.9 KiB
Python

from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from utils.cache.yaml import dump_yaml
from utils.roles.validation.deploy_id import ValidDeployId
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 Path(path).open("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()