mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
Work-in-progress commit on the collective code-scanning sidetree, so main can be merged in; the single quality-high gate runs on the integration branch before the pull request. Fixes the CodeQL notes py/import-and-import-from, py/unused-import, py/unused-global-variable, py/empty-except, py/mixed-tuple-returns and py/unused-local-variable across 194 files, and adds one lint per rule under tests/lint/repository/python/ so none of them can come back: test_import_and_import_from, test_unused_imports_hidden_from_ruff, test_unused_global_variables, test_empty_except, test_mixed_tuple_returns, test_unused_local_variables, plus test_modules_import. Accepted security finding: MEDUSA-MP-SCAN-2056 (low) on utils/i18n/client.py:21, an import line matched by a keyword heuristic; operator-confirmed false positive. 🎯 Fix: 85 % 💥 Break: 8 % 💸 Cost: – ⏱️ Time: – 🛡️ Security: 100 % 🏆 Score: 78 🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
import unittest
|
|
import unittest.mock
|
|
|
|
from utils.preflight.context import deploy_closure_context
|
|
|
|
MODULE = "utils.preflight.context"
|
|
|
|
|
|
class TestDeployClosureContext(unittest.TestCase):
|
|
def _build(self, closure=None, side_effect=None):
|
|
plugins = {
|
|
"applications": unittest.mock.Mock(),
|
|
"config": unittest.mock.Mock(),
|
|
"domains": unittest.mock.Mock(),
|
|
}
|
|
plugins["applications"].run.return_value = [{"web-app-matomo": {}}]
|
|
plugins["config"].run.side_effect = lambda terms, variables=None: [
|
|
f"{terms[0]}:{terms[1]}"
|
|
]
|
|
plugins["domains"].run.return_value = [["matomo.example.org"]]
|
|
lookup = unittest.mock.Mock(_loader="loader", _templar=None)
|
|
with (
|
|
unittest.mock.patch(
|
|
f"{MODULE}.lookup_loader.get",
|
|
side_effect=lambda name, **kwargs: plugins[name],
|
|
),
|
|
unittest.mock.patch(
|
|
f"{MODULE}.types_from_group_names", return_value=["server"]
|
|
),
|
|
unittest.mock.patch(
|
|
f"{MODULE}.deploy_closure",
|
|
return_value=closure or {"web-app-matomo", "svc-db-mariadb"},
|
|
side_effect=side_effect,
|
|
) as closure_mock,
|
|
):
|
|
context = deploy_closure_context(
|
|
lookup, {"group_names": ["web-app-matomo"]}, ["web-app-matomo"]
|
|
)
|
|
readers = (
|
|
context.config("web-app-matomo", "services.matomo.name", ""),
|
|
context.lookup("domains", "web-app-matomo", []),
|
|
)
|
|
return context, readers, closure_mock
|
|
|
|
def test_the_closure_roles_come_sorted(self):
|
|
context, _readers, _closure = self._build()
|
|
self.assertEqual(context.roles, ["svc-db-mariadb", "web-app-matomo"])
|
|
self.assertEqual(context.applications, {"web-app-matomo": {}})
|
|
|
|
def test_the_host_groups_seed_the_closure(self):
|
|
_context, _readers, closure = self._build()
|
|
self.assertEqual(closure.call_args.args[1], ["web-app-matomo"])
|
|
self.assertEqual(closure.call_args.kwargs["host_types"], ["server"])
|
|
|
|
def test_the_readers_forward_to_their_lookups(self):
|
|
_context, readers, _closure = self._build()
|
|
self.assertEqual(
|
|
readers, ("web-app-matomo:services.matomo.name", ["matomo.example.org"])
|
|
)
|
|
|
|
def test_a_closure_error_propagates(self):
|
|
with self.assertRaises(ValueError):
|
|
self._build(side_effect=ValueError("broken"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|