Files
computer-playbook/tests/unit/python/plugins/lookup/test_deployment.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

176 lines
6.4 KiB
Python

import unittest
from unittest.mock import patch
from plugins.lookup import deployment
from plugins.lookup.deployment import LookupModule
class TestDeploymentLookup(unittest.TestCase):
def setUp(self):
deployment._reset_cache_for_tests()
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_returns_dict_with_all_keys(self, mock_list):
mock_list.return_value = ["web-app-friendica", "web-app-baserow"]
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": ["web-app-friendica"],
"group_names": ["web-app-friendica", "web-app-baserow"],
},
)
self.assertEqual(len(result), 1)
self.assertEqual(
set(result[0].keys()),
{"whitelist", "running", "groups", "deployed", "runtime", "all"},
)
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_running_falls_back_to_groups_when_whitelist_empty(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": [],
"group_names": ["a", "b"],
},
)
self.assertEqual(result[0]["running"], ["a", "b"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_running_uses_whitelist_when_non_empty(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": ["a"],
"group_names": ["a", "b"],
},
)
self.assertEqual(result[0]["running"], ["a"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_whitelist_reflects_raw_input(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": ["web-app-foo"],
"group_names": ["web-app-bar"],
},
)
self.assertEqual(result[0]["whitelist"], ["web-app-foo"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_groups_reflects_raw_input(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": [],
"group_names": ["a", "b", "c"],
},
)
self.assertEqual(result[0]["groups"], ["a", "b", "c"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_all_reflects_invokable_apps(self, mock_list):
mock_list.return_value = ["x", "y", "z"]
result = LookupModule().run([], variables={"group_names": []})
self.assertEqual(result[0]["all"], ["x", "y", "z"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_missing_vars_treated_as_empty(self, mock_list):
mock_list.return_value = ["a"]
result = LookupModule().run([], variables={})
self.assertEqual(result[0]["whitelist"], [])
self.assertEqual(result[0]["groups"], [])
self.assertEqual(result[0]["running"], [])
self.assertEqual(result[0]["all"], ["a"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_caches_same_input(self, mock_list):
mock_list.return_value = ["a"]
LookupModule().run(
[], variables={"APPLICATIONS_WHITELIST": [], "group_names": ["a"]}
)
LookupModule().run(
[], variables={"APPLICATIONS_WHITELIST": [], "group_names": ["a"]}
)
self.assertEqual(mock_list.call_count, 1)
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_whitelist_csv_string_tokenized(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": "web-app-foo,web-app-bar",
"group_names": [],
},
)
self.assertEqual(result[0]["whitelist"], ["web-app-foo", "web-app-bar"])
self.assertEqual(result[0]["running"], ["web-app-foo", "web-app-bar"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_groups_csv_string_tokenized(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": [],
"group_names": "a,b,c",
},
)
self.assertEqual(result[0]["groups"], ["a", "b", "c"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_whitelist_csv_string_whitespace_trimmed(self, mock_list):
mock_list.return_value = []
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": " app1 , app2 ,, ",
"group_names": [],
},
)
self.assertEqual(result[0]["whitelist"], ["app1", "app2"])
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_deployed_uses_running_in_ephemeral_runtime(self, mock_list):
mock_list.return_value = []
for runtime in ("act", "github"):
deployment._reset_cache_for_tests()
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": ["only-this"],
"group_names": ["only-this", "and-this", "and-that"],
"RUNTIME": runtime,
},
)
self.assertEqual(result[0]["deployed"], ["only-this"])
self.assertEqual(result[0]["runtime"], runtime)
@patch("plugins.lookup.deployment.list_invokable_app_ids")
def test_deployed_uses_groups_in_persistent_runtime(self, mock_list):
mock_list.return_value = []
for runtime in ("host", "dev", ""):
deployment._reset_cache_for_tests()
result = LookupModule().run(
[],
variables={
"APPLICATIONS_WHITELIST": ["partial"],
"group_names": ["partial", "still-running", "also-running"],
"RUNTIME": runtime,
},
)
self.assertEqual(
result[0]["deployed"],
["partial", "still-running", "also-running"],
)
if __name__ == "__main__":
unittest.main()