pr / done (pull_request) Successful in 1m42s
scripts/verify-compose-profile-contracts.sh는 prCheck의 게이트인데 판정 열 가지가 셸 안의 jq라 시험이 없다. 고장 난 입력을 넣어 보면 넷이 통과한다. - 계약의 lanes가 비면 lane 반복이 돌지 않고 "all 0 lanes match"로 끝난다. - persistence-jpa를 켠 lane에 expectedPersistenceVendor가 없으면 통과한다. smoke는 보고된 vendor를 그 칸과 비교하니 그 lane은 매번 실패한다. - heap dump 경로는 짧은 `tmpfs:` 목록의 같은 항목만 비교한다. 위 디렉터리의 tmpfs(/var/tmp)나 long syntax `type: tmpfs` volume은 통과한다. - Redis 계정을 이름 그대로가 아니라 정규식으로 찾아 `.`이 아무 글자와 맞는다. `ca.skeleton-application`을 가리키는 lane이 `ca-skeleton-application` 줄로 통과한다. 판정을 scripts/compose-contract-verdict.py로 옮긴다. 셸은 docker만 부른다. `version`으로 Compose 하한을 먼저 보고, `lanes`가 준 lane별 인자로 `config --services`와 `config --format json`을 돌려 출력과 rc를 파일에 쓴다. rc는 조건 밖에서 읽는다. 그다음 `judge`가 그 디렉터리를 읽는다. 판정은 위 넷을 실패로 보고, 파일 없는 lane, 겹치는 id, smoke가 읽는 목록이 없거나 그 목록에 이름이 아닌 원소가 든 lane, 셸이 렌더 기록을 남기지 않은 lane도 실패로 본다. docker 호출의 stdin은 /dev/null이라 lane 목록을 먹지 않는다. scripts/tests/test_compose_contract_verdict.py가 판정마다 고장 난 입력과 정상 입력을 보고, 셸을 가짜 docker로 돌려 lane 인자·rc 기록·판정 rc를 본다. prCheck의 testDocumentChecks가 돌린다. 루트 build.gradle의 주석은 PR 단계 Exec 태스크마다 시험 파일을 적어 사실과 맞춘다. 버린 안: 모델 JSON의 service key로 `config --services`를 대신하기. 렌더를 한 번 줄이지만 지금 lane 결과와 같은지 따로 보여야 해서 두 호출을 그대로 둔다. bats는 desktop과 runner 둘 다에 새 도구를 깔아야 한다. 위험·한계: 판정이 jq와 다른 곳은 위 넷 말고도 있다. main이 통과시키고 이제 실패로 보는 것: 같은 사용자를 두 줄에서 켠 ACL(Redis가 거절한다), 값이 null·false인 realm의 `_` key, 최상위 값이 둘인 realm, LANE_ID 꼴이 아닌 id, 빈 springRuntime, `files`가 없는 lane, 이름이 아닌 목록 원소, 읽을 수 없는 Compose 버전. main이 실패시키고 이제 통과하는 것: ACL 줄의 겹친 공백과 CRLF, 끝에 `/`가 붙은 bind target, 로캘에 따라 달라지던 service 정렬, list 꼴 environment, 파일 하나로 bind mount한 ACL(target이 --aclfile 경로와 같음). Redis가 ACL 줄의 `\r`와 겹친 공백을 받아들인다는 것은 acl.c를 열어 보지 않은 판단이다. 실제 계약 18 lane에서는 main과 이 트리의 출력 줄이 같다(PR 본문). 검증: 노트북 `python3 -m unittest discover -s scripts/tests` 275개 통과. 판정의 빈 lane 거절·vendor 검사·heap 상위 tmpfs·long syntax tmpfs·비밀번호 글자 비교· 정확한 service 집합·사용자 이름 비교·렌더 기록 없음·파일 없는 lane·목록 원소 검사를 되돌리거나, judge_lane에서 판정 하나를 빼거나 realm 판정을 빼거나, 셸의 두 렌더 rc를 `|| true`로 삼키거나, docker가 stdin을 읽게 두거나, judge 호출을 지우거나 `|| true`로 바꾸거나, version 실패를 무시하거나, profile 인자를 빼면 새 시험이 실패한다(변이 23개 모두). desktop RED·GREEN과 prCheck 결과는 PR 본문에 있다. Refs: REV-BE-0016
573 lines
26 KiB
Python
573 lines
26 KiB
Python
"""Judges the static Compose contract: every lane renders, and renders exactly what it claims.
|
|
|
|
Called by scripts/verify-compose-profile-contracts.sh, which keeps the docker calls: it asks this
|
|
script which lanes to render and with which arguments, writes what `docker compose config` printed and
|
|
exited with to one directory, and hands that directory back here. The judgements live here rather
|
|
than in jq inside the shell because a jq branch cannot be tried on a broken input without Docker, and
|
|
four of them passed input that should have failed them (scripts/tests has the cases):
|
|
|
|
- a contract with no lanes passed as "all 0 lanes match";
|
|
- a lane that turns persistence-jpa on and names no expectedPersistenceVendor passed, although the
|
|
smoke wrapper compares the reported vendor with that field and fails every run of the lane;
|
|
- the heap dump path passed when the tmpfs was a directory above it, or a long-syntax volume of
|
|
type tmpfs, because only an exact entry of the short `tmpfs:` list was compared;
|
|
- a Redis account was looked up with its name as a regular expression, so a lane that names
|
|
`ca.skeleton-application` passed on the ACL line of `ca-skeleton-application`, an account it never
|
|
authenticates as.
|
|
|
|
The ten checks, and why each exists, are listed in scripts/verify-compose-profile-contracts.sh.
|
|
|
|
Exit status 0 is a pass, 1 a failure with its reasons on stderr, 2 a call this script does not know,
|
|
78 a repository this script cannot read (no contract file, no -XX:HeapDumpPath in the Dockerfile).
|
|
|
|
python3 scripts/compose-contract-verdict.py version <contracts.json> <docker compose version>
|
|
python3 scripts/compose-contract-verdict.py lanes <contracts.json>
|
|
python3 scripts/compose-contract-verdict.py judge <contracts.json> <render-dir>
|
|
|
|
`lanes` prints one line per lane the contract lets it render: the lane id, then the arguments for
|
|
`docker compose`, separated by tabs. `judge` expects, for each of those lanes, `<id>.services` and
|
|
`<id>.services.err` from `config --services`, `<id>.json` from `config --format json`, and the exit
|
|
status of each call in `<id>.services.rc` and `<id>.json.rc`. A lane the contract describes but the
|
|
shell left no record for fails, so a broken loop in the shell cannot pass as a lane that matched.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import posixpath
|
|
import re
|
|
import shlex
|
|
import sys
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
DEFAULT_REPO_ROOT = Path(__file__).resolve().parent.parent
|
|
CONTRACT_LABEL = "src/bootstrap/app/compose-lanes/compose-profile-contracts.json"
|
|
REALM = "compose/keycloak/realms/ca-skeleton-realm.json"
|
|
HEAP_DUMP_PATH = re.compile(r'-XX:HeapDumpPath=([^ \\"]*)')
|
|
LANE_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]*")
|
|
VERSION = re.compile(r"v?(\d+(?:\.\d+)*)(?:[-+].*)?")
|
|
MEMORY_SIZE = re.compile(r"[1-9][0-9]*[kmg]")
|
|
CREDENTIAL_REFERENCE = re.compile(r"secret://([A-Za-z0-9_.-]+)@environment/([A-Za-z_][A-Za-z0-9_]*)")
|
|
REDIS_REFERENCES = (
|
|
"APP_REDIS_AUTHENTICATION_CREDENTIAL_REFERENCE",
|
|
"APP_REDIS_AUTHENTICATION_ADVANCED_CREDENTIAL_REFERENCE",
|
|
)
|
|
SETUP = 78
|
|
|
|
|
|
class SetupError(Exception):
|
|
"""The repository lacks something every judgement needs; exit 78 rather than judge."""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Rendered:
|
|
"""What the shell recorded for one lane. None is a file the shell never wrote."""
|
|
|
|
services_rc: int | None
|
|
services: str
|
|
services_error: str
|
|
model_rc: int | None
|
|
model: str
|
|
|
|
|
|
def scalar(value: object) -> str:
|
|
"""The value as `jq -r '... // empty'` prints it: null and false print nothing."""
|
|
if value is None or value is False:
|
|
return ""
|
|
if value is True:
|
|
return "true"
|
|
return str(value)
|
|
|
|
|
|
def strings(value: object) -> list[str]:
|
|
"""A list of strings, or an empty list for anything else (`// []`)."""
|
|
if isinstance(value, list):
|
|
return [item for item in value if isinstance(item, str)]
|
|
return []
|
|
|
|
|
|
# ---- 1. Compose version floor ----------------------------------------------------------------
|
|
|
|
def version_key(text: str) -> tuple[int, ...] | None:
|
|
match = VERSION.fullmatch(text.strip())
|
|
if match is None:
|
|
return None
|
|
return tuple(int(part) for part in match.group(1).split("."))
|
|
|
|
|
|
def compose_version(contract: dict, actual: str) -> list[str]:
|
|
"""The reasons this Compose is too old for the contract; empty when it is new enough."""
|
|
required = scalar(contract.get("minimumComposeVersion"))
|
|
floor, found = version_key(required), version_key(actual)
|
|
if floor is None:
|
|
return [f"minimumComposeVersion '{required}' is not a version such as 2.24.4"]
|
|
if found is None:
|
|
return [f"docker compose reports '{actual.strip()}', which is not a version"]
|
|
width = max(len(floor), len(found))
|
|
if found + (0,) * (width - len(found)) < floor + (0,) * (width - len(floor)):
|
|
return [
|
|
f"docker compose {actual.strip()} is below the required {required};",
|
|
" the dev overlay needs !override to replace the base tmpfs rather than merge with it.",
|
|
]
|
|
return []
|
|
|
|
|
|
# ---- the shape of the contract ----------------------------------------------------------------
|
|
|
|
def lane_shape(contract: dict, lane: object, repo_root: Path) -> list[str]:
|
|
"""The reasons a lane cannot be rendered or run as the contract writes it; empty when it can."""
|
|
if not isinstance(lane, dict):
|
|
return ["the lane is not an object"]
|
|
problems: list[str] = []
|
|
files = lane.get("files")
|
|
catalogue = contract.get("composeFiles") if isinstance(contract.get("composeFiles"), dict) else {}
|
|
if not isinstance(files, list) or not files:
|
|
problems.append("names no compose file under 'files', so `docker compose` would render whatever "
|
|
"file it finds in the repository root")
|
|
else:
|
|
for key in files:
|
|
path = catalogue.get(key) if isinstance(key, str) else None
|
|
if not isinstance(path, str):
|
|
problems.append(f"names compose file '{key}', which composeFiles does not list")
|
|
elif not (repo_root / path).is_file():
|
|
problems.append(f"{path} is missing")
|
|
for field in ("composeProfiles", "expectedCapabilitiesOn"):
|
|
if not isinstance(lane.get(field), list):
|
|
problems.append(f"has no '{field}' list; the smoke wrapper reads it as one")
|
|
# The checks below read these lists as names. An entry that is not one (null, a number, "") would
|
|
# drop out of them unseen, while the smoke wrapper reads every entry and fails the lane on it.
|
|
for field in ("composeProfiles", "expectedCapabilitiesOn", "services", "longRunningServices",
|
|
"preStartServices", "oneShotServices"):
|
|
entries = lane.get(field)
|
|
if isinstance(entries, list) and not all(isinstance(entry, str) and entry for entry in entries):
|
|
problems.append(f"'{field}' holds {json.dumps(entries)}, and every entry must be a name")
|
|
if not isinstance(lane.get("springRuntime"), str) or not lane["springRuntime"]:
|
|
problems.append("names no springRuntime")
|
|
if not isinstance(lane.get("services"), list) or not lane["services"]:
|
|
problems.append("lists no services")
|
|
return problems
|
|
|
|
|
|
def lanes(contract: dict, repo_root: Path) -> tuple[list[dict], list[str]]:
|
|
"""The lanes that can be rendered, and the reasons the others cannot (each with its lane id)."""
|
|
entries = contract.get("lanes")
|
|
if not isinstance(entries, list) or not entries:
|
|
return [], ["the contract declares no lanes, so nothing would be checked"]
|
|
renderable: list[dict] = []
|
|
problems: list[str] = []
|
|
seen: set[str] = set()
|
|
for index, lane in enumerate(entries):
|
|
lane_id = lane.get("id") if isinstance(lane, dict) else None
|
|
if not isinstance(lane_id, str) or LANE_ID.fullmatch(lane_id) is None:
|
|
problems.append(f"lane {index}: id {lane_id!r} is not a name such as off-local")
|
|
continue
|
|
if lane_id in seen:
|
|
problems.append(f"{lane_id}: the id is used by more than one lane")
|
|
continue
|
|
seen.add(lane_id)
|
|
shape = lane_shape(contract, lane, repo_root)
|
|
if shape:
|
|
problems.extend(f"{lane_id}: {problem}" for problem in shape)
|
|
continue
|
|
renderable.append(lane)
|
|
return renderable, problems
|
|
|
|
|
|
def compose_arguments(contract: dict, lane: dict, repo_root: Path) -> list[str]:
|
|
arguments: list[str] = []
|
|
for key in lane["files"]:
|
|
arguments += ["-f", str(repo_root / contract["composeFiles"][key])]
|
|
for profile in strings(lane["composeProfiles"]):
|
|
if profile:
|
|
arguments += ["--profile", profile]
|
|
return arguments
|
|
|
|
|
|
# ---- per-lane checks --------------------------------------------------------------------------
|
|
|
|
def service_set(lane: dict, rendered: str) -> list[str]:
|
|
"""2. The stack renders exactly the lane's services: not a superset, not a subset."""
|
|
expected = sorted(strings(lane.get("services")))
|
|
actual = sorted(line.strip() for line in rendered.splitlines() if line.strip())
|
|
if actual != expected:
|
|
return [f"services are [{','.join(actual)}], the contract says [{','.join(expected)}]"]
|
|
return []
|
|
|
|
|
|
def environment(service: dict) -> dict[str, object]:
|
|
value = service.get("environment")
|
|
if isinstance(value, dict):
|
|
return value
|
|
if isinstance(value, list):
|
|
return dict(item.split("=", 1) if "=" in item else (item, None) for item in strings(value))
|
|
return {}
|
|
|
|
|
|
def spring_runtime(lane: dict, model: dict) -> list[str]:
|
|
"""3. The application renders the Spring runtime the lane names."""
|
|
app = model.get("services", {}).get("app") or {}
|
|
rendered = scalar(environment(app).get("SPRING_PROFILES_ACTIVE"))
|
|
if rendered != lane["springRuntime"]:
|
|
return [f"app renders SPRING_PROFILES_ACTIVE='{rendered}', the contract says '{lane['springRuntime']}'"]
|
|
return []
|
|
|
|
|
|
def tmpfs_targets(service: dict) -> list[str]:
|
|
"""Every tmpfs the service mounts: the short `tmpfs:` list and long-syntax volumes of type tmpfs."""
|
|
short = service.get("tmpfs")
|
|
entries = [short] if isinstance(short, str) else strings(short)
|
|
targets = [entry.split(":")[0] for entry in entries]
|
|
targets += [volume["target"] for volume in mounts(service) if volume.get("type") == "tmpfs"]
|
|
return targets
|
|
|
|
|
|
def mounts(service: dict) -> list[dict]:
|
|
volumes = service.get("volumes")
|
|
if not isinstance(volumes, list):
|
|
return []
|
|
return [volume for volume in volumes if isinstance(volume, dict) and isinstance(volume.get("target"), str)]
|
|
|
|
|
|
def duplicate_mounts(model: dict) -> list[str]:
|
|
"""4. No service declares the same mount target twice."""
|
|
problems: list[str] = []
|
|
for name, service in (model.get("services") or {}).items():
|
|
if not isinstance(service, dict):
|
|
continue
|
|
short = service.get("tmpfs")
|
|
entries = [short] if isinstance(short, str) else strings(short)
|
|
targets = [volume["target"] for volume in mounts(service)] + [entry.split(":")[0] for entry in entries]
|
|
twice = sorted({target for target in targets if targets.count(target) > 1})
|
|
if twice:
|
|
problems.append(f"a mount target is declared twice — {name}: {', '.join(twice)}")
|
|
return problems
|
|
|
|
|
|
def role_partition(contract: dict, lane: dict) -> list[str]:
|
|
"""5. Every rendered service plays exactly one role, and no one-shot is a --wait target."""
|
|
problems: list[str] = []
|
|
services = strings(lane.get("services"))
|
|
waiting = strings(lane.get("longRunningServices"))
|
|
pre_start = strings(lane.get("preStartServices"))
|
|
one_shot = strings(lane.get("oneShotServices"))
|
|
filed = waiting + pre_start + one_shot
|
|
unfiled = [service for service in services if service not in filed]
|
|
not_rendered = [service for service in filed if service not in services]
|
|
twice = sorted({service for service in filed if filed.count(service) > 1})
|
|
if unfiled or not_rendered or twice:
|
|
problems.append(
|
|
f"services are not partitioned by role — unfiled=[{','.join(unfiled)}] "
|
|
f"not-rendered=[{','.join(not_rendered)}] twice=[{','.join(twice)}]"
|
|
)
|
|
catalogue = strings(contract.get("preStartServices")) + strings(contract.get("oneShotServices"))
|
|
never_waiting = [service for service in waiting if service in catalogue]
|
|
if never_waiting:
|
|
problems.append(f"[{','.join(never_waiting)}] are declared non-waiting but listed as --wait targets")
|
|
uncatalogued = [service for service in pre_start + one_shot if service not in catalogue]
|
|
if uncatalogued:
|
|
problems.append(
|
|
f"[{','.join(uncatalogued)}] are run with `run --rm` but are in neither top-level catalogue"
|
|
)
|
|
return problems
|
|
|
|
|
|
def application_report(contract: dict, lane: dict) -> list[str]:
|
|
"""6. What the lane expects /actuator/info to report is something the application can report."""
|
|
expected_on = strings(lane.get("expectedCapabilitiesOn"))
|
|
unknown = [name for name in expected_on if name not in strings(contract.get("capabilities"))]
|
|
jpa = "persistence-jpa" in expected_on
|
|
state = "on" if jpa else "off"
|
|
vendor = lane.get("expectedPersistenceVendor")
|
|
reasons: list[str] = []
|
|
if unknown:
|
|
reasons.append(f"uncatalogued=[{','.join(unknown)}]")
|
|
if not isinstance(vendor, str) or not vendor:
|
|
reasons.append(f"no expectedPersistenceVendor with persistence-jpa {state}")
|
|
elif jpa == (vendor == "none"):
|
|
reasons.append(f"vendor {vendor} with persistence-jpa {state}")
|
|
if reasons:
|
|
return [f"the expected application report is unreachable — {' '.join(reasons)}"]
|
|
return []
|
|
|
|
|
|
def redis_authentication(lane: dict, model: dict, repo_root: Path) -> list[str]:
|
|
"""7. A lane that switches Redis on authenticates as an account its own Redis provisions.
|
|
|
|
Read against the rendered model, so the ACL file checked is the one the container mounts. User,
|
|
password and key pattern are each compared, because each fails differently and only at runtime:
|
|
an unknown user or a wrong password is WRONGPASS at the first connection, and a key pattern that
|
|
does not cover the lane's namespace is NOPERM on the first key a capability writes.
|
|
"""
|
|
env = lane.get("activationEnv") if isinstance(lane.get("activationEnv"), dict) else {}
|
|
if scalar(env.get("APP_REDIS_ENABLED")) != "true":
|
|
return []
|
|
problems: list[str] = []
|
|
if scalar(env.get("APP_REDIS_AUTHENTICATION_ANONYMOUS_ACCESS_ACCEPTED")) == "true":
|
|
problems.append("accepts anonymous Redis access; the lane's Redis turns the default user off, "
|
|
"so the application has to name an account")
|
|
namespace = scalar(env.get("APP_REDIS_NAMESPACE_ENVIRONMENT"))
|
|
if not namespace:
|
|
problems.append("sets no APP_REDIS_NAMESPACE_ENVIRONMENT, so nothing says the account's key "
|
|
"pattern covers the keys the lane writes")
|
|
|
|
redis = model.get("services", {}).get("redis") or {}
|
|
command = redis.get("command")
|
|
words = shlex.split(command) if isinstance(command, str) else strings(command)
|
|
position = words.index("--aclfile") if "--aclfile" in words else None
|
|
acl_path = words[position + 1] if position is not None and position + 1 < len(words) else ""
|
|
if not acl_path:
|
|
problems.append("starts Redis without --aclfile, so the default user is open and nothing authenticates")
|
|
return problems
|
|
acl_file = acl_source(redis, acl_path)
|
|
if acl_file is None or not acl_file.is_file() or not os.access(acl_file, os.R_OK):
|
|
problems.append(f"starts Redis with --aclfile {acl_path}, which no read-only bind mount of a "
|
|
"repository file provides")
|
|
return problems
|
|
label = relative(acl_file, repo_root)
|
|
acl_lines = acl_file.read_text(encoding="utf-8", errors="replace").splitlines()
|
|
if "user default off" not in acl_lines:
|
|
problems.append(f"{label} leaves the default user on, which makes every account restriction optional")
|
|
test = (redis.get("healthcheck") or {}).get("test")
|
|
probe = test if isinstance(test, str) else " ".join(strings(test))
|
|
if "--user " not in probe:
|
|
problems.append("the Redis healthcheck does not authenticate as a named account, so it cannot "
|
|
"pass with the default user off")
|
|
|
|
for setting in REDIS_REFERENCES:
|
|
reference = scalar(env.get(setting))
|
|
match = CREDENTIAL_REFERENCE.fullmatch(reference)
|
|
if match is None:
|
|
problems.append(f"{setting} is '{reference}', not secret://<account>@environment/<variable>")
|
|
continue
|
|
user, name = match.group(1), match.group(2)
|
|
value = scalar(env.get(name))
|
|
if not value:
|
|
problems.append(f"{setting} reads {name}, which the lane does not supply")
|
|
continue
|
|
# Whole words, not patterns: the account is the line whose second word is the user name, so
|
|
# '.' in it is a dot, and the password is the exact word after '>'.
|
|
accounts = [line.split() for line in acl_lines if line.split()[:3] == ["user", user, "on"]]
|
|
if not accounts:
|
|
problems.append(f"{setting} names {user}, which {label} does not enable")
|
|
continue
|
|
if len(accounts) > 1:
|
|
problems.append(f"{label} enables {user} on more than one line, which Redis refuses to load")
|
|
continue
|
|
if f">{value}" not in accounts[0]:
|
|
problems.append(f"{name} is not the password {label} gives {user}")
|
|
if namespace and f"~{namespace}:*" not in accounts[0]:
|
|
problems.append(f"{user} may not touch {namespace}:*, the namespace this lane writes")
|
|
return problems
|
|
|
|
|
|
def acl_source(redis: dict, acl_path: str) -> Path | None:
|
|
"""The repository file a read-only bind mount puts at acl_path, if one does."""
|
|
for volume in mounts(redis):
|
|
if volume.get("type") != "bind" or volume.get("read_only") is not True:
|
|
continue
|
|
target, source = volume["target"], volume.get("source")
|
|
if not isinstance(source, str):
|
|
continue
|
|
if acl_path == target:
|
|
return Path(source)
|
|
if acl_path.startswith(target.rstrip("/") + "/"):
|
|
return Path(source + acl_path[len(target.rstrip("/")):])
|
|
return None
|
|
|
|
|
|
def memory_limit(lane: dict) -> list[str]:
|
|
"""8. The lane's own memory limit, when it sets one, is a Compose memory size."""
|
|
limit = scalar(lane.get("appMemoryLimit"))
|
|
if limit and MEMORY_SIZE.fullmatch(limit) is None:
|
|
return [f"appMemoryLimit '{limit}' is not a Compose memory size such as 1g"]
|
|
return []
|
|
|
|
|
|
def heap_dump(model: dict, heap_path: str) -> list[str]:
|
|
"""9. The heap dump path is on no tmpfs: not one at that path, and not one at a directory above it."""
|
|
app = model.get("services", {}).get("app") or {}
|
|
path = posixpath.normpath(heap_path)
|
|
for target in tmpfs_targets(app):
|
|
mount = posixpath.normpath(target)
|
|
if path == mount or path.startswith(mount.rstrip("/") + "/"):
|
|
return [f"app mounts {target} as a tmpfs, which holds the heap dump path {heap_path}; a tmpfs "
|
|
"counts against the memory limit and is gone when the container exits"]
|
|
return []
|
|
|
|
|
|
def keycloak_realm(repo_root: Path) -> list[str]:
|
|
"""10. The Keycloak realm artifact carries no key Keycloak refuses to deserialize."""
|
|
realm = repo_root / REALM
|
|
if not realm.is_file():
|
|
return []
|
|
try:
|
|
tree = json.loads(realm.read_text(encoding="utf-8"))
|
|
except (ValueError, UnicodeDecodeError):
|
|
return [f"{REALM} is not valid JSON"]
|
|
annotations = sorted(set(underscore_keys(tree)))
|
|
if annotations:
|
|
return [f"the Keycloak realm carries key(s) Keycloak refuses to deserialize: {', '.join(annotations)}"]
|
|
return []
|
|
|
|
|
|
def underscore_keys(tree: object) -> list[str]:
|
|
if isinstance(tree, dict):
|
|
found = [key for key in tree if key.startswith("_")]
|
|
for value in tree.values():
|
|
found += underscore_keys(value)
|
|
return found
|
|
if isinstance(tree, list):
|
|
return [key for item in tree for key in underscore_keys(item)]
|
|
return []
|
|
|
|
|
|
def judge_lane(contract: dict, lane: dict, rendered: Rendered, repo_root: Path, heap_path: str) -> list[str]:
|
|
"""The reasons one lane does not match the contract; empty when it does."""
|
|
if rendered.services_rc is None or rendered.model_rc is None:
|
|
return ["the shell left no record of rendering this lane"]
|
|
if rendered.services_rc != 0:
|
|
return [f"the stack does not render: {' '.join(rendered.services_error.split())}"]
|
|
problems = service_set(lane, rendered.services)
|
|
if problems:
|
|
return problems
|
|
try:
|
|
model = json.loads(rendered.model) if rendered.model_rc == 0 else None
|
|
except ValueError:
|
|
model = None
|
|
if not isinstance(model, dict) or not isinstance(model.get("services"), dict):
|
|
return ["the merged model does not render as JSON"]
|
|
return [
|
|
*spring_runtime(lane, model),
|
|
*duplicate_mounts(model),
|
|
*role_partition(contract, lane),
|
|
*application_report(contract, lane),
|
|
*redis_authentication(lane, model, repo_root),
|
|
*memory_limit(lane),
|
|
*heap_dump(model, heap_path),
|
|
]
|
|
|
|
|
|
def judge(contract: dict, render_dir: Path, repo_root: Path) -> tuple[list[str], list[str]]:
|
|
"""The lines for the lanes that matched, and every reason the contract did not."""
|
|
heap_path = dockerfile_heap_dump_path(repo_root)
|
|
renderable, problems = lanes(contract, repo_root)
|
|
matched: list[str] = []
|
|
for lane in renderable:
|
|
lane_problems = judge_lane(contract, lane, recorded(render_dir, lane["id"]), repo_root, heap_path)
|
|
if lane_problems:
|
|
problems += [f"{lane['id']}: {problem}" for problem in lane_problems]
|
|
else:
|
|
matched.append(f"{lane['id']}: {','.join(sorted(lane['services']))} @ {lane['springRuntime']}")
|
|
problems += keycloak_realm(repo_root)
|
|
return matched, problems
|
|
|
|
|
|
# ---- files ------------------------------------------------------------------------------------
|
|
|
|
def dockerfile_heap_dump_path(repo_root: Path) -> str:
|
|
dockerfile = repo_root / "Dockerfile"
|
|
match = HEAP_DUMP_PATH.search(read(dockerfile) or "")
|
|
if match is None or not match.group(1):
|
|
raise SetupError("the Dockerfile sets no -XX:HeapDumpPath")
|
|
return match.group(1)
|
|
|
|
|
|
def recorded(render_dir: Path, lane_id: str) -> Rendered:
|
|
return Rendered(
|
|
services_rc=status(read(render_dir / f"{lane_id}.services.rc")),
|
|
services=read(render_dir / f"{lane_id}.services") or "",
|
|
services_error=read(render_dir / f"{lane_id}.services.err") or "",
|
|
model_rc=status(read(render_dir / f"{lane_id}.json.rc")),
|
|
model=read(render_dir / f"{lane_id}.json") or "",
|
|
)
|
|
|
|
|
|
def status(text: str | None) -> int | None:
|
|
if text is None or not text.strip().isdigit():
|
|
return None
|
|
return int(text.strip())
|
|
|
|
|
|
def read(path: Path) -> str | None:
|
|
try:
|
|
return path.read_text(encoding="utf-8", errors="replace")
|
|
except FileNotFoundError:
|
|
return None
|
|
|
|
|
|
def relative(path: Path, root: Path) -> str:
|
|
try:
|
|
return str(path.resolve().relative_to(root.resolve()))
|
|
except ValueError:
|
|
return str(path)
|
|
|
|
|
|
def load_contract(path: Path) -> dict:
|
|
text = read(path)
|
|
if text is None:
|
|
raise SetupError(f"missing {path}")
|
|
try:
|
|
contract = json.loads(text)
|
|
except ValueError as error:
|
|
raise ContractError(f"{path} is not valid JSON: {error}") from error
|
|
if not isinstance(contract, dict):
|
|
raise ContractError(f"{path} is not a JSON object")
|
|
return contract
|
|
|
|
|
|
class ContractError(Exception):
|
|
"""The contract file cannot be read as a contract; a failure, not a setup problem."""
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
parser.add_argument("--repo-root", type=Path, default=DEFAULT_REPO_ROOT,
|
|
help="the repository the contract's paths are relative to")
|
|
commands = parser.add_subparsers(dest="command", required=True)
|
|
version_command = commands.add_parser("version")
|
|
version_command.add_argument("contract", type=Path)
|
|
version_command.add_argument("actual")
|
|
commands.add_parser("lanes").add_argument("contract", type=Path)
|
|
judge_command = commands.add_parser("judge")
|
|
judge_command.add_argument("contract", type=Path)
|
|
judge_command.add_argument("render_dir", type=Path)
|
|
args = parser.parse_args(argv)
|
|
|
|
try:
|
|
contract = load_contract(args.contract)
|
|
if args.command == "version":
|
|
problems = compose_version(contract, args.actual)
|
|
for problem in problems:
|
|
print(problem, file=sys.stderr)
|
|
if not problems:
|
|
print(f"compose version {args.actual.strip().removeprefix('v')} >= {contract['minimumComposeVersion']}")
|
|
return 1 if problems else 0
|
|
if args.command == "lanes":
|
|
for lane in lanes(contract, args.repo_root)[0]:
|
|
print("\t".join([lane["id"], *compose_arguments(contract, lane, args.repo_root)]))
|
|
return 0
|
|
matched, problems = judge(contract, args.render_dir, args.repo_root)
|
|
except SetupError as error:
|
|
print(error, file=sys.stderr)
|
|
return SETUP
|
|
except ContractError as error:
|
|
print(f" FAIL: {error}", file=sys.stderr)
|
|
return 1
|
|
for line in matched:
|
|
print(line)
|
|
for problem in problems:
|
|
print(f" FAIL: {problem}", file=sys.stderr)
|
|
if problems:
|
|
print(f"verify-compose-profile-contracts: {len(problems)} problem(s) against {CONTRACT_LABEL}",
|
|
file=sys.stderr)
|
|
return 1
|
|
print(f"verify-compose-profile-contracts: all {len(matched)} lanes match {CONTRACT_LABEL}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|