Files
kevinveenbirkenbachandClaude Opus 5.5 326f063bd3 fix(codescan): clear the mechanical CodeQL notes and lint them away (WIP)
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>
2026-11-13 18:44:29 +00:00

85 lines
3.1 KiB
Python

"""The stage loop skips roles no host carries, and gives every host one list.
Dropping what nobody deploys is free: 132 of 139 entries were skipped one task
at a time on a single-app deploy, 278 task deltas, 41.8 seconds measured on
2026-09-23.
Dropping what *this* host does not carry is not free, and these cases forbid
it. 8aea5a9b35 measured that a per-host condition above the loop makes ansible
insert blocks in host order rather than loop order, after which
svc-storage-nfs-client mounted an export svc-storage-nfs-server had not
deployed yet (run 31645153977). The list therefore has to come out identical
for every host, and per-host narrowing stays in the body's ``when``.
"""
from __future__ import annotations
import unittest
import unittest.mock
from typing import ClassVar
from plugins.lookup.group_roles import LookupModule
ENTRIES = [
{"role": "web-app-alpha", "app": "alpha"},
{"role": "web-app-beta", "app": "beta"},
{"role": "web-app-gamma", "app": "gamma"},
]
def run(**kwargs):
with unittest.mock.patch(
"plugins.lookup.group_roles.ordered_roles", return_value=list(ENTRIES)
):
return LookupModule().run(["web-app"], **kwargs)[0]
class TestWithoutAnInventory(unittest.TestCase):
def test_no_groups_returns_the_whole_group(self) -> None:
self.assertEqual(run(), ENTRIES)
class TestDroppingWhatNobodyDeploys(unittest.TestCase):
def test_a_role_no_host_carries_is_dropped(self) -> None:
selected = run(inventory_groups={"alpha": ["h1"], "beta": []})
self.assertEqual([entry["app"] for entry in selected], ["alpha"])
def test_an_empty_inventory_deploys_nothing(self) -> None:
self.assertEqual(run(inventory_groups={}), [])
def test_run_after_order_survives_the_filter(self) -> None:
selected = run(inventory_groups={"gamma": ["h1"], "alpha": ["h1"]})
self.assertEqual(
[entry["app"] for entry in selected],
["alpha", "gamma"],
"run_after order is the contract; the inventory's key order is not",
)
class TestSameListForEveryHost(unittest.TestCase):
"""The property 8aea5a9b35 paid for; losing it inverts deploy order."""
GROUPS: ClassVar[dict] = {"alpha": ["h1"], "beta": ["h2"], "gamma": ["h1", "h2"]}
def test_a_role_only_one_host_carries_still_enters_the_loop(self) -> None:
selected = [entry["app"] for entry in run(inventory_groups=self.GROUPS)]
self.assertEqual(
selected,
["alpha", "beta", "gamma"],
"beta belongs to h2 only; dropping it for h1 would give the two "
"hosts different loops and let host order decide item order",
)
def test_the_lookup_reads_no_per_host_variable(self) -> None:
selected = run(inventory_groups=self.GROUPS, group_names=["alpha"])
self.assertEqual(
[entry["app"] for entry in selected],
["alpha", "beta", "gamma"],
"a host's own groups must not reach this lookup; narrowing to one "
"host belongs in the body's when",
)
if __name__ == "__main__":
unittest.main()