Files
computer-playbook/tests/unit/python/plugins/lookup/test_api.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

114 lines
4.0 KiB
Python

import unittest
from ansible.errors import AnsibleError
from plugins.lookup.api import LookupModule
class _FakeTemplar:
"""Minimal templar: deep-substitutes string leaves via ``mapping``."""
def __init__(self, mapping=None, available=None):
self._mapping = mapping or {}
self.available_variables = available or {}
def template(self, value):
if isinstance(value, dict):
return {k: self.template(v) for k, v in value.items()}
if isinstance(value, list):
return [self.template(v) for v in value]
return self._mapping.get(value, value)
class TestApiLookup(unittest.TestCase):
def setUp(self):
self.lookup = LookupModule()
self.api = {
"cloudflare": {"api_token": "cf-token"},
"telegram": {"bot_token": "", "api_id": "12345", "api_hash": "h"},
"github": {"client_id": "gh-id", "client_secret": "gh-secret"},
}
self.vars = {"API": self.api}
def test_resolves_nested_scalar(self):
out = self.lookup.run(["cloudflare.api_token"], variables=self.vars)
self.assertEqual(out, ["cf-token"])
def test_resolves_deeper_provider_key(self):
self.assertEqual(
self.lookup.run(["github.client_secret"], variables=self.vars)[0],
"gh-secret",
)
def test_resolves_empty_string_value(self):
self.assertEqual(
self.lookup.run(["telegram.bot_token"], variables=self.vars)[0], ""
)
def test_returns_subtree_when_path_is_provider_only(self):
self.assertEqual(
self.lookup.run(["cloudflare"], variables=self.vars)[0],
{"api_token": "cf-token"},
)
def test_path_is_stripped(self):
self.assertEqual(
self.lookup.run([" cloudflare.api_token "], variables=self.vars)[0],
"cf-token",
)
def test_unknown_provider_raises(self):
with self.assertRaises(AnsibleError) as ctx:
self.lookup.run(["nope.client_id"], variables=self.vars)
self.assertIn("nope", str(ctx.exception))
def test_unknown_leaf_key_raises_at_failing_part(self):
with self.assertRaises(AnsibleError) as ctx:
self.lookup.run(["cloudflare.missing"], variables=self.vars)
msg = str(ctx.exception)
self.assertIn("cloudflare.missing", msg)
self.assertIn("missing", msg)
def test_descend_into_scalar_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["cloudflare.api_token.extra"], variables=self.vars)
def test_no_terms_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run([], variables=self.vars)
def test_too_many_terms_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["a.b", "c.d"], variables=self.vars)
def test_empty_path_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run([" "], variables=self.vars)
def test_api_not_a_mapping_raises(self):
with self.assertRaises(AnsibleError) as ctx:
self.lookup.run(["cloudflare.api_token"], variables={"API": "nope"})
self.assertIn("mapping", str(ctx.exception))
def test_missing_api_global_raises_unknown_key(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["cloudflare.api_token"], variables={})
def test_templar_resolves_jinja_values(self):
self.lookup._templar = _FakeTemplar(mapping={"{{ vault_cf }}": "secret-cf"})
out = self.lookup.run(
["cloudflare.api_token"],
variables={"API": {"cloudflare": {"api_token": "{{ vault_cf }}"}}},
)
self.assertEqual(out, ["secret-cf"])
def test_falls_back_to_templar_available_variables(self):
self.lookup._templar = _FakeTemplar(available={"API": self.api})
self.assertEqual(
self.lookup.run(["telegram.api_id"], variables=None)[0], "12345"
)
if __name__ == "__main__":
unittest.main()