|
| 1 | +"""Guards the image's ``@coana-tech/cli`` install against drifting from the runtime pin. |
| 2 | +
|
| 3 | +The launcher asks npx for ``@coana-tech/cli@DEFAULT_COANA_CLI_VERSION``, and npx reuses the |
| 4 | +image's global install only when the versions match; on a mismatch it downloads the engine |
| 5 | +again on every scan. The Dockerfile therefore derives the version from ``reachability.py`` |
| 6 | +with a ``sed`` expression instead of repeating it. These tests run that expression against |
| 7 | +the real source, so a reformatted constant or a broken expression fails here rather than |
| 8 | +silently producing a mismatched image. |
| 9 | +""" |
| 10 | + |
| 11 | +import re |
| 12 | +import subprocess |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +from socketsecurity.core.tools.reachability import DEFAULT_COANA_CLI_VERSION |
| 16 | + |
| 17 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 18 | +DOCKERFILE = REPO_ROOT / "Dockerfile" |
| 19 | +REACHABILITY_SOURCE = ( |
| 20 | + REPO_ROOT / "socketsecurity" / "core" / "tools" / "reachability.py" |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _dockerfile_sed_expression() -> str: |
| 25 | + """Return the pin-extraction expression the Dockerfile runs.""" |
| 26 | + match = re.search( |
| 27 | + r"sed -n '(s/\^DEFAULT_COANA_CLI_VERSION[^']*)'", DOCKERFILE.read_text() |
| 28 | + ) |
| 29 | + assert match, "Dockerfile no longer extracts DEFAULT_COANA_CLI_VERSION with sed" |
| 30 | + return match.group(1) |
| 31 | + |
| 32 | + |
| 33 | +def test_dockerfile_expression_extracts_the_pinned_version(): |
| 34 | + extracted = subprocess.run( |
| 35 | + ["sed", "-n", _dockerfile_sed_expression(), str(REACHABILITY_SOURCE)], |
| 36 | + capture_output=True, |
| 37 | + text=True, |
| 38 | + check=True, |
| 39 | + ).stdout.strip() |
| 40 | + assert extracted == DEFAULT_COANA_CLI_VERSION |
| 41 | + |
| 42 | + |
| 43 | +def test_dockerfile_never_installs_coana_unpinned(): |
| 44 | + install_lines = [ |
| 45 | + line for line in DOCKERFILE.read_text().splitlines() if "npm install" in line |
| 46 | + ] |
| 47 | + assert install_lines, "Dockerfile no longer installs anything with npm" |
| 48 | + for line in install_lines: |
| 49 | + if "@coana-tech/cli" in line: |
| 50 | + assert "@coana-tech/cli@" in line, f"unpinned coana install: {line.strip()}" |
0 commit comments