Files
computer-playbook/tests/lint/docker/dockerfile/test_arg_scope.py
T
Environment TestandClaude Sonnet 4.6 9ea2ad6ff1 refactor(docker): introduce utils/docker/image_discovery as single SPOT for image enumeration
- Move iter_role_images + all helpers + ImageRef from cli/mirror/util.py and
  cli/mirror/model.py into utils/docker/image_discovery.py; add source_file field
  to ImageRef to distinguish config/main.yml vs vars/main.yml entries
- Delete cli/mirror/util.py; update all callers to import from utils.docker.image_discovery
- Move utils into subpackages: utils/docker/ (image_ref, paths_utils, hadolint) and
  utils/roles/ (dependency_resolver, resource_validation); update all imports
- Reorganise tests into subpackages: tests/lint/docker/dockerfile/, tests/integration/docker/,
  tests/unit/utils/docker/, tests/unit/utils/roles/; fix parents[N] offsets after relocation
- tests/lint/docker/test_image_versions.py now uses iter_role_images (same SPOT as mirror)
- Add vars/main.yml images.<name>.{image,version} support for non-application roles
  (test-e2e-playwright, sys-ctl-hlth-csp); eliminate hardcoded duplicates in defaults/tasks
- Add web-app-matrix matrix-chatgpt-bot and web-app-mini-qr mini-qr entries in config/main.yml;
  replace hardcoded image strings in vars/templates with lookup('config', ...) references
- Fix sync/__main__.py log line to use correct registry prefix for non-Docker-Hub images
- Update docs/contributing/code/role/dockerfile.md with image declaration rules and SPOT reference

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 19:32:31 +02:00

93 lines
3.6 KiB
Python

"""Verify that ARG declarations used in FROM instructions are in the global scope.
In multi-stage Dockerfiles every ``ARG`` whose value is referenced by a
``FROM`` instruction must be declared *before* the first ``FROM`` (the global
scope). An ``ARG`` declared inside a build stage is scoped to that stage only
and is invisible to subsequent ``FROM`` instructions, which causes Docker to
treat the variable as empty and fail with::
WARN: UndefinedArgInFrom: FROM argument '...' is not declared
failed to solve: failed to parse stage name ":": invalid reference format
The test scans all ``files/Dockerfile`` files under ``roles/`` and fails for
any file where an ``ARG`` used in a ``FROM`` instruction is not declared before
the first ``FROM``.
"""
from __future__ import annotations
import re
import unittest
from pathlib import Path
_REPO_ROOT = Path(__file__).resolve().parents[4]
_ROLES_ROOT = _REPO_ROOT / "roles"
# Matches a FROM line that references at least one ${VAR}
_FROM_ARG_RE = re.compile(r"^\s*FROM\s+.*\$\{(\w+)\}", re.IGNORECASE)
# Matches an ARG declaration: ARG VARNAME or ARG VARNAME=default
_ARG_DECL_RE = re.compile(r"^\s*ARG\s+(\w+)", re.IGNORECASE)
# Matches any FROM line (marks the end of the global scope)
_FROM_RE = re.compile(r"^\s*FROM\b", re.IGNORECASE)
def _collect_dockerfiles() -> list[Path]:
return sorted(_ROLES_ROOT.glob("*/files/Dockerfile"))
def _undeclared_from_args(dockerfile: Path) -> list[tuple[int, str, str]]:
"""Return a list of (line_no, from_line, var_name) for each ARG used in a
FROM instruction that was not declared in the global scope."""
lines = dockerfile.read_text(encoding="utf-8").splitlines()
# Pass 1 — collect ARGs declared before the first FROM (global scope)
global_args: set[str] = set()
for line in lines:
if _FROM_RE.match(line):
break
m = _ARG_DECL_RE.match(line)
if m:
global_args.add(m.group(1))
# Pass 2 — check every FROM that references an ARG
violations: list[tuple[int, str, str]] = []
for lineno, line in enumerate(lines, start=1):
for m in re.finditer(r"\$\{(\w+)\}", line):
if _FROM_RE.match(line) and m.group(1) not in global_args:
violations.append((lineno, line.strip(), m.group(1)))
return violations
class TestDockerfileArgScope(unittest.TestCase):
"""Fail when ARGs used in FROM instructions are not in the global scope."""
def test_from_args_declared_globally(self) -> None:
dockerfiles = _collect_dockerfiles()
self.assertTrue(
dockerfiles or True, # pass even when no Dockerfiles exist yet
"No Dockerfiles found — check _ROLES_ROOT path.",
)
failures: list[str] = []
for dockerfile in dockerfiles:
violations = _undeclared_from_args(dockerfile)
for lineno, from_line, var_name in violations:
relative = dockerfile.relative_to(_REPO_ROOT).as_posix()
failures.append(
f"{relative}:{lineno}: ARG '{var_name}' used in FROM but not "
f"declared in global scope — '{from_line}'"
)
self.assertFalse(
failures,
"The following Dockerfiles reference ARGs in FROM instructions that are "
"not declared before the first FROM (global scope).\n"
"ARGs used in FROM must be declared globally so Docker can resolve them "
"in multi-stage builds:\n\n" + "\n".join(f" {f}" for f in failures),
)
if __name__ == "__main__":
unittest.main()