mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +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>
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
import unittest
|
|
import unittest.mock
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.public_ports import LookupModule
|
|
from utils.preflight.context import DeployClosureContext
|
|
|
|
MODULE = "plugins.lookup.public_ports"
|
|
APPLICATIONS = {
|
|
"web-app-mailu": {
|
|
"services": {
|
|
"mailu": {"ports": {"public": {"smtp": 25, "imaps": "993"}}},
|
|
"redis": {"enabled": False},
|
|
}
|
|
},
|
|
"web-svc-coturn": {
|
|
"services": {
|
|
"coturn": {
|
|
"ports": {
|
|
"public": {
|
|
"stun_turn": 3478,
|
|
"relay": {"start": 20000, "end": 39999},
|
|
},
|
|
"local": {"http": 8064},
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"web-app-other": {
|
|
"services": {"other": {"ports": {"public": {"smtp": 25, "flag": True}}}}
|
|
},
|
|
"sys-ctl-hlth-csp": {},
|
|
}
|
|
|
|
|
|
def _config(role, path, default):
|
|
key = path.split(".")[1]
|
|
return APPLICATIONS[role]["services"][key]["ports"]["public"]
|
|
|
|
|
|
def _context():
|
|
return DeployClosureContext(
|
|
roles=sorted(APPLICATIONS),
|
|
applications=APPLICATIONS,
|
|
config=_config,
|
|
lookup=unittest.mock.Mock(),
|
|
)
|
|
|
|
|
|
class TestPublicPortsLookup(unittest.TestCase):
|
|
def _run(self, terms, side_effect=None):
|
|
with unittest.mock.patch(
|
|
f"{MODULE}.deploy_closure_context",
|
|
return_value=_context(),
|
|
side_effect=side_effect,
|
|
):
|
|
return LookupModule().run(terms, variables={})
|
|
|
|
def test_single_ports_map_onto_every_role_that_binds_them(self):
|
|
self.assertEqual(
|
|
self._run([["web-app-mailu"]]),
|
|
[
|
|
{
|
|
"25": ["web-app-mailu (mailu.smtp)", "web-app-other (other.smtp)"],
|
|
"993": ["web-app-mailu (mailu.imaps)"],
|
|
"3478": ["web-svc-coturn (coturn.stun_turn)"],
|
|
}
|
|
],
|
|
)
|
|
|
|
def test_a_closure_error_becomes_an_ansible_error(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run([[]], side_effect=ValueError("x"))
|
|
|
|
def test_a_wrong_term_count_is_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
LookupModule().run([])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|