Files
computer-playbook/tests/lint/docker/dockerfile/test_arg_scope.py
T
kevinveenbirkenbachandClaude Opus 4.7 7dc6d6e491 chore(ruff): enable TC + N rules
TC (flake8-type-checking): typing-only imports moved into
``if TYPE_CHECKING:`` blocks across 8 source files via auto-fix
(117 hits). Reduces runtime import surface and rules out circular
imports for type-only deps. Project-wide ``from __future__ import
annotations`` (FA, already enabled) keeps annotations as strings,
so the moved imports never need to load at runtime.

N (pep8-naming): 32 hits handled.

Renames (semantic + lint):
- ``isSmallerThenMaximumBackupSize`` -> ``is_larger_than_maximum_backup_size``
  (snake_case + fixes both the typo "Then -> Than" and the
  semantic mismatch: the function returns ``current > maximum``)
- ``isDirectoryDeletable`` / ``deleteVersion`` /
  ``getAmountOfIteration`` / ``deleteIteration`` -> snake_case
- 8 test functions with ALL-CAPS env-var fragments
  (``test_..._DOMAIN_PRIMARY_...`` etc.) -> snake_case
- Mock alias ``VH`` -> ``vh`` in test_host_vars.py

Exception class renames (``N818`` Error suffix):
- ``TimeLimitExceededException`` -> ``TimeLimitExceededError``
- ``AttemptException`` -> ``AttemptError``
- ``PathNotFound`` -> ``PathNotFoundError`` (propagated across
  4 callers under tests/integration/lookups/config/)
- ``_ExitJson`` / ``_FailJson`` -> ``*_Error``

Refactor:
- ``METADATA = {...}`` local constant in two functions of
  ``plugins/filter/invokable_paths.py`` lifted to module-level
  ``_METADATA_KEYS``.
- ``from typing import TYPE_CHECKING`` in
  ``tasks/utils/migrate_meta_layout/__main__.py`` moved above
  the ``sys.path.insert`` hack to satisfy E402 (collateral from
  the TC auto-fix that landed it after the path mutation).

Per-file ignores in pyproject.toml for the legitimate cases the
rule cannot accept:
- ``pull-specific-host.py`` -> ``N999`` (Ansible-invoked role-level
  script, hyphenated filename pinned by the surrounding stack).
- 4 matrix-deploy files -> ``N806`` for the ``include_R`` /
  ``raw_include_R`` round-index notation; the uppercase ``R``
  here mirrors the formula in the docstring rather than naming
  a constant.
- ``plugins/filter/value_generator.py`` -> ``N806`` for the
  lazy-loaded class binding ``ValueGenerator =
  _value_generator_cls()``, which retains the imported class
  identifier per PEP 8.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 18:57:07 +02:00

102 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 typing import TYPE_CHECKING
from utils.cache.files import read_text
from . import PROJECT_ROOT
if TYPE_CHECKING:
from pathlib import Path
_REPO_ROOT = PROJECT_ROOT
_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 = read_text(str(dockerfile)).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):
violations.extend(
(lineno, line.strip(), m.group(1))
for m in _FROM_ARG_RE.finditer(line)
if m.group(1) not in global_args
)
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()