mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
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>
166 lines
4.9 KiB
Python
166 lines
4.9 KiB
Python
import importlib
|
|
import importlib.util
|
|
import sys
|
|
import unittest
|
|
import unittest.mock as mock
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
|
|
def _load_module(rel_path: str, name: str):
|
|
"""Import ``rel_path`` fresh, evicting sibling-test stubs of
|
|
``utils.roles.applications.config`` from ``sys.modules`` first.
|
|
"""
|
|
for key in (
|
|
"utils.roles.applications.config",
|
|
"utils.roles.applications",
|
|
"utils",
|
|
"utils.cache.applications",
|
|
"utils.cache",
|
|
):
|
|
sys.modules.pop(key, None)
|
|
importlib.import_module("utils.roles.applications.config")
|
|
importlib.import_module("utils.cache.applications")
|
|
|
|
path = PROJECT_ROOT / rel_path
|
|
spec = importlib.util.spec_from_file_location(name, str(path))
|
|
mod = importlib.util.module_from_spec(spec)
|
|
assert spec and spec.loader
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
def _apps(*, oidc_enabled=True, ldap_enabled=None, flavor=None, include_app=True):
|
|
"""Build a minimal merged `applications` dict for the Nextcloud role.
|
|
|
|
`oidc_enabled` defaults to True so existing flavor-selection tests
|
|
stay focused on the ldap/flavor axis. The OIDC-off fast-path has
|
|
its own dedicated tests.
|
|
"""
|
|
if not include_app:
|
|
return {}
|
|
sso_block: dict = {"enabled": oidc_enabled}
|
|
if flavor is not None:
|
|
sso_block["oidc"] = {"plugin": flavor}
|
|
services_block: dict = {"sso": sso_block}
|
|
if ldap_enabled is not None:
|
|
services_block["ldap"] = {"enabled": ldap_enabled}
|
|
return {
|
|
"web-app-nextcloud": {
|
|
"services": services_block,
|
|
},
|
|
}
|
|
|
|
|
|
class OidcFlavorLookupTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.mod = _load_module(
|
|
"plugins/lookup/sso_oidc_plugin.py",
|
|
"sso_oidc_plugin",
|
|
)
|
|
|
|
def setUp(self):
|
|
self._patcher = mock.patch.object(self.mod, "lookup_loader")
|
|
self._loader_mock = self._patcher.start()
|
|
self._stub_payload = None
|
|
self._loader_mock.get.return_value = mock.MagicMock(
|
|
run=lambda *_a, **_k: [self._stub_payload]
|
|
)
|
|
|
|
def tearDown(self):
|
|
self._patcher.stop()
|
|
|
|
def _run(self, applications, *, terms=None):
|
|
self._stub_payload = applications
|
|
lk = self.mod.LookupModule()
|
|
lk._loader = mock.MagicMock()
|
|
return lk.run(terms or [], variables={"applications": applications})
|
|
|
|
def test_ldap_enabled_returns_oidc_login(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=True)),
|
|
["oidc_login"],
|
|
)
|
|
|
|
def test_ldap_disabled_returns_sociallogin(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=False)),
|
|
["sociallogin"],
|
|
)
|
|
|
|
def test_ldap_missing_defaults_to_sociallogin(self):
|
|
self.assertEqual(
|
|
self._run(_apps()),
|
|
["sociallogin"],
|
|
)
|
|
|
|
def test_missing_application_returns_empty(self):
|
|
self.assertEqual(
|
|
self._run({"some-other-app": {}}),
|
|
[""],
|
|
)
|
|
|
|
def test_oidc_disabled_returns_empty(self):
|
|
self.assertEqual(
|
|
self._run(_apps(oidc_enabled=False)),
|
|
[""],
|
|
)
|
|
self.assertEqual(
|
|
self._run(_apps(oidc_enabled=False, ldap_enabled=True)),
|
|
[""],
|
|
)
|
|
self.assertEqual(
|
|
self._run(_apps(oidc_enabled=False, flavor="oidc_login")),
|
|
[""],
|
|
)
|
|
|
|
def test_explicit_flavor_overrides_ldap_fallback(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=True, flavor="sociallogin")),
|
|
["sociallogin"],
|
|
)
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=False, flavor="user_oidc")),
|
|
["user_oidc"],
|
|
)
|
|
|
|
def test_explicit_flavor_is_stripped(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=False, flavor=" oidc_login ")),
|
|
["oidc_login"],
|
|
)
|
|
|
|
def test_blank_explicit_flavor_falls_back_to_ternary(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=True, flavor=" ")),
|
|
["oidc_login"],
|
|
)
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=False, flavor="")),
|
|
["sociallogin"],
|
|
)
|
|
|
|
def test_null_explicit_flavor_falls_back_to_ternary(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=True, flavor=None)),
|
|
["oidc_login"],
|
|
)
|
|
|
|
def test_non_string_explicit_flavor_is_ignored(self):
|
|
self.assertEqual(
|
|
self._run(_apps(ldap_enabled=True, flavor=42)),
|
|
["oidc_login"],
|
|
)
|
|
|
|
def test_rejects_positional_terms(self):
|
|
lk = self.mod.LookupModule()
|
|
with self.assertRaises(AnsibleError):
|
|
lk.run(["unexpected"], variables={"applications": _apps()})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|