mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
CodeQL py/cyclic-import alerts #1023-#1029 and #1112-#1113 map to three cycles: utils.inventory.tools <-> libretranslate, utils.env.builder <-> its handlers, and utils.roles.applications.variants <-> the inventory legacy resolver. Each cycle loaded only because one side deferred an import into a function body. The shared piece now lives in a leaf module both sides import: utils/inventory/tools_vars.py, utils/env/context.py (BuildContext, EnvBuilder), and _resolve_round_include inside variants.py. The stricter lint found four more cycles, each broken the same way: compose <-> down (cli/administration/deploy/development/docker_root.py), the utils.cache package web (utils/cache/reset.py), utils.cache.carrier <-> users (utils/cache/tokens.py replaces users/store.py) and credentials -> provision (cli/administration/inventory/ruamel_io.py).
tests/lint/repository/python/test_import_cycles.py fails on any runtime import cycle between the repository's own modules. It counts TYPE_CHECKING imports and implied parent-package edges, and skips only __main__ blocks and submodule-to-parent reads. Negative control: 3 of the 11 self-tests fail on the old logic. Whole repository: 12 passed. Targeted tests of every touched module: 4275 passed.
Accepted security findings: 78 medusa findings, all on lines that exist verbatim in base 28d05e0183. They come from code moved unchanged into docker_root.py, tools_vars.py and tokens.py, or from untouched lines of touched files (compose.py, credentials_generator.py, credentials/reset/cli.py, users/__init__.py, yaml.py). Reviewed as by design or false positive: argv-list subprocess calls, dev docker-root teardown, NotASecret test-lane keys, SafeLoader yaml.load, md5 memo key, Jinja expression strings.
🎯 Fix: 90 %
💥 Break: 5 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 86
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
"""Unit tests for the tools lane's address handoff.
|
|
|
|
The host resolves the runner's address and writes it down; a container of the
|
|
stack reads the file, because it cannot ask docker and the compose service
|
|
name resolves nowhere behind the stack's own CoreDNS.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
import unittest
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
from unittest.mock import patch
|
|
|
|
from utils.inventory import tools
|
|
from utils.inventory.tools import ADDRESS, lane_address, lane_ip
|
|
from utils.inventory.tools_vars import LANE_SERVICE
|
|
|
|
NETWORKS = {
|
|
"infinito-nexus-core_default": {"IPAddress": "172.30.0.9"},
|
|
"store-i18n_default": {"IPAddress": "172.30.5.2"},
|
|
}
|
|
|
|
|
|
def _completed(returncode: int, stdout: str) -> subprocess.CompletedProcess:
|
|
return subprocess.CompletedProcess(args=[], returncode=returncode, stdout=stdout)
|
|
|
|
|
|
class TestLaneIp(unittest.TestCase):
|
|
def _run(self, found, env=None) -> str:
|
|
environment = {"INFINITO_SUBNET": "172.30.5.0/24", "INFINITO_CONTAINER": "c"}
|
|
with (
|
|
patch.object(tools.subprocess, "run", return_value=found),
|
|
patch.object(tools, "environment", return_value=env or environment),
|
|
):
|
|
return lane_ip(Path("/nowhere"))
|
|
|
|
def test_the_address_inside_the_stack_subnet_wins(self) -> None:
|
|
self.assertEqual(self._run(_completed(0, json.dumps(NETWORKS))), "172.30.5.2")
|
|
|
|
def test_a_runner_docker_cannot_describe_answers_empty(self) -> None:
|
|
self.assertEqual(self._run(_completed(1, "")), "")
|
|
|
|
def test_output_that_is_not_json_answers_empty(self) -> None:
|
|
self.assertEqual(self._run(_completed(0, "no such object")), "")
|
|
|
|
def test_no_address_in_the_subnet_answers_empty(self) -> None:
|
|
other = {"bridge": {"IPAddress": "172.17.0.2"}}
|
|
self.assertEqual(self._run(_completed(0, json.dumps(other))), "")
|
|
|
|
def test_a_missing_subnet_answers_empty(self) -> None:
|
|
self.assertEqual(
|
|
self._run(
|
|
_completed(0, json.dumps(NETWORKS)), env={"INFINITO_CONTAINER": "c"}
|
|
),
|
|
"",
|
|
)
|
|
|
|
|
|
class TestLaneAddress(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.tmp = TemporaryDirectory()
|
|
self.addCleanup(self.tmp.cleanup)
|
|
self.root = Path(self.tmp.name)
|
|
(self.root / ADDRESS).parent.mkdir(parents=True)
|
|
|
|
def _write(self, body: str) -> None:
|
|
(self.root / ADDRESS).write_text(body, encoding="utf-8")
|
|
|
|
def test_a_written_address_is_read_back(self) -> None:
|
|
self._write("172.30.5.2\n")
|
|
self.assertEqual(lane_address(self.root), "172.30.5.2")
|
|
|
|
def test_a_missing_file_falls_back_to_the_service_name(self) -> None:
|
|
self.assertEqual(lane_address(self.root), LANE_SERVICE)
|
|
|
|
def test_an_empty_file_falls_back_to_the_service_name(self) -> None:
|
|
self._write("\n")
|
|
self.assertEqual(lane_address(self.root), LANE_SERVICE)
|
|
|
|
def test_remembering_an_unresolvable_address_writes_nothing(self) -> None:
|
|
with patch.object(tools, "lane_ip", return_value=""):
|
|
tools._remember_address(self.root)
|
|
self.assertFalse((self.root / ADDRESS).exists())
|
|
|
|
def test_remembering_a_resolved_address_makes_it_readable(self) -> None:
|
|
with patch.object(tools, "lane_ip", return_value="172.30.5.2"):
|
|
tools._remember_address(self.root)
|
|
self.assertEqual(lane_address(self.root), "172.30.5.2")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|