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