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>
95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
import json
|
|
import unittest
|
|
import unittest.mock
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.address_pool_overlaps import LookupModule
|
|
from utils.preflight.context import DeployClosureContext
|
|
|
|
MODULE = "plugins.lookup.address_pool_overlaps"
|
|
SUBNETS = {"web-app-matomo": "192.168.31.0/24"}
|
|
RESERVED = {"NETWORK_MESH_POOL": "10.100.0.0/16"}
|
|
|
|
|
|
def _context():
|
|
return DeployClosureContext(
|
|
roles=["sys-ctl-hlth-csp", "web-app-matomo"],
|
|
applications={},
|
|
config=lambda role, path, default: SUBNETS.get(role, default),
|
|
lookup=unittest.mock.Mock(),
|
|
)
|
|
|
|
|
|
class TestAddressPoolOverlapsLookup(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,
|
|
),
|
|
unittest.mock.patch(
|
|
f"{MODULE}.entity_name", side_effect=lambda role: role.split("-", 2)[-1]
|
|
),
|
|
):
|
|
return LookupModule().run(terms, variables={})
|
|
|
|
@staticmethod
|
|
def _daemon(*bases):
|
|
return json.dumps(
|
|
{"default-address-pools": [{"base": base, "size": 24} for base in bases]}
|
|
)
|
|
|
|
def test_role_subnets_and_reserved_ranges_are_compared(self):
|
|
result = self._run(
|
|
[self._daemon("192.168.0.0/16", "10.0.0.0/8"), ["web-app-matomo"], RESERVED]
|
|
)
|
|
self.assertEqual(
|
|
result,
|
|
[
|
|
[
|
|
(
|
|
"docker default address pool 192.168.0.0/16 overlaps "
|
|
"192.168.31.0/24, which web-app-matomo declares for its "
|
|
"network 'matomo'"
|
|
),
|
|
(
|
|
"docker default address pool 10.0.0.0/8 overlaps 10.100.0.0/16, "
|
|
"which NETWORK_MESH_POOL reserves"
|
|
),
|
|
]
|
|
],
|
|
)
|
|
|
|
def test_a_clear_pool_and_an_absent_file_pass(self):
|
|
self.assertEqual(
|
|
self._run([self._daemon("10.208.0.0/12"), ["web-app-matomo"], RESERVED]),
|
|
[[]],
|
|
)
|
|
self.assertEqual(self._run(["{}", ["web-app-matomo"], RESERVED]), [[]])
|
|
|
|
def test_an_unparsable_daemon_json_is_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(["{not json", [], RESERVED])
|
|
|
|
def test_a_pool_without_base_is_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(['{"default-address-pools": [{"size": 24}]}', [], RESERVED])
|
|
|
|
def test_non_mapping_arguments_are_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(["[]", [], RESERVED])
|
|
|
|
def test_a_closure_error_becomes_an_ansible_error(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(["{}", [], RESERVED], side_effect=TypeError("x"))
|
|
|
|
def test_a_wrong_term_count_is_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
LookupModule().run(["{}", []])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|