Files
2026-11-06 21:15:31 +03:00

212 lines
8.0 KiB
Python

import hashlib
import json
import math
import re
import subprocess
import sys
import xml.etree.ElementTree as ET
from datetime import datetime, timezone, timedelta
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from pipeline_support import compose, sql, broker, ready_api
def main():
ready_api()
stamp = datetime.now(timezone(timedelta(hours=3))).isoformat()
ids = compose("ps", "-q").splitlines()
inspection = json.loads(subprocess.check_output(["docker", "inspect", *ids], text=True))
allowed = {
"ROLE",
"JOB_MAX_ATTEMPTS",
"JOB_TIMEOUT_SECONDS",
"JOB_LEASE_SECONDS",
"MAX_PENDING_JOBS",
"FAULT_INJECTION_ENABLED",
}
containers = []
for item in inspection:
environment = {
v.split("=", 1)[0]: v.split("=", 1)[1]
for v in item["Config"]["Env"]
if v.split("=", 1)[0] in allowed
}
containers.append(
{
"name": item["Name"],
"image": item["Image"],
"started_at": item["State"]["StartedAt"],
"state": item["State"]["Status"],
"health": item["State"].get("Health", {}).get("Status"),
"volumes": [
{"name": m["Name"], "target": m["Destination"]}
for m in item["Mounts"]
if m["Type"] == "volume"
],
"memory_limit_bytes": item["HostConfig"]["Memory"],
"nano_cpus": item["HostConfig"]["NanoCpus"],
"profile": environment,
}
)
info = {
"time_moscow": stamp,
"base_commit": subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(),
"source_lab4": "cc0ef71",
"worktree": "modified; source hashes below",
"docker": subprocess.check_output(
["docker", "info", "--format", "{{.ServerVersion}} CPU={{.NCPU}} RAM={{.MemTotal}}"],
text=True,
).strip(),
"containers": containers,
"job_states": sql(
"select status,count(*) from processing_jobs group by status order by status;"
),
"redis_durability": broker("CONFIG", "GET", "appendonly", "appendfsync"),
"broker_stream_entries": broker("XLEN", "attachment-jobs"),
"broker_publication_index_entries": broker("HLEN", "job-publications"),
}
(ROOT / "evidence/environment.json").write_text(json.dumps(info, ensure_ascii=False, indent=2))
capture_schema()
capture_metrics()
capture_final_checks()
files = list((ROOT / "src").rglob("*.java")) + list(
(ROOT / "src/main/resources").rglob("*.sql")
)
files += (
list((ROOT / "worker").rglob("*.py"))
+ list((ROOT / "tests").glob("*.py"))
+ list((ROOT / "scripts").glob("*"))
)
files += [
ROOT / p
for p in [
"build.gradle",
"settings.gradle",
".editorconfig",
"pyproject.toml",
"requirements-tools.txt",
"gradle/wrapper/gradle-wrapper.properties",
"Dockerfile",
"worker/Dockerfile",
"worker/requirements.txt",
"docker-compose.yml",
"compose.test.yaml",
"compose.local.yaml",
"nginx.conf",
"policy.json",
]
]
manifest = {
p.relative_to(ROOT).as_posix(): hashlib.sha256(p.read_bytes()).hexdigest()
for p in files
if p.is_file()
}
(ROOT / "evidence/source-sha256.json").write_text(json.dumps(manifest, indent=2))
summary = [
{k: v for k, v in ET.parse(p).getroot().attrib.items() if k != "hostname"}
for p in (ROOT / "build/test-results/test").glob("TEST-*.xml")
]
(ROOT / "evidence/unit-summary.json").write_text(json.dumps(summary, indent=2))
targets = re.findall(r"\]\(([^)]+)\)", (ROOT / "REPORT.md").read_text())
missing = [t for t in targets if not t.startswith(("http", "#")) and not (ROOT / t).exists()]
if missing:
raise AssertionError("Missing report links: " + str(missing))
print("PASS environment, job schema, metrics, source hashes and report links")
def capture_schema():
from jsonschema import Draft202012Validator, FormatChecker
schema = json.loads((ROOT / "contracts/jobs.schema.json").read_text())
validator = Draft202012Validator(schema, format_checker=FormatChecker())
payloads = json.loads(
sql("select coalesce(json_agg(payload),'[]'::json) from processing_outbox;")
)
for payload in payloads:
validator.validate(payload)
(ROOT / "evidence/job-schema.json").write_text(
json.dumps({"validated_outbox_templates": len(payloads), "valid": True}, indent=2)
)
def capture_final_checks():
public = [
"README.md",
"lab.json",
"contracts",
"tests/client.py",
"tests/smoke.py",
"tests/resilience.py",
"tests/load.py",
"tests/image_probe.py",
]
subprocess.run(["git", "diff", "--exit-code", "--", *public], cwd=ROOT, check=True)
subprocess.run(["git", "diff", "--check"], cwd=ROOT, check=True)
names = (
subprocess.check_output(
["git", "ls-files", "--cached", "--others", "--exclude-standard", "-z"], cwd=ROOT
)
.decode("utf-8")
.split("\0")
)
credentials = [
path.read_bytes().strip()
for path in (ROOT / "secrets").glob("*.txt")
if "password" in path.stem or "secret" in path.stem
]
for name in filter(None, names):
path = ROOT / name
if path.is_file() and any(value and value in path.read_bytes() for value in credentials):
raise AssertionError("Local credential value found in a Git-visible file")
subprocess.run(["git", "check-ignore", "--quiet", ".env"], cwd=ROOT, check=True)
for path in (ROOT / "secrets").glob("*.txt"):
subprocess.run(["git", "check-ignore", "--quiet", str(path)], cwd=ROOT, check=True)
(ROOT / "evidence/final-checks.txt").write_text(
"PASS public lab5 assignment, contract and executable tests unchanged\n"
"PASS no local credential values in Git-visible files; secrets and .env ignored\n"
"PASS git diff --check\n"
"Controller DI, single service call, API Javadoc and formatting: evidence/style.txt\n"
)
def capture_metrics():
raw = json.loads((ROOT / "evidence/backpressure.json").read_text())
peaks = {}
for sample in raw["stats_samples"]:
for name, value, unit, cpu in re.findall(
r"(\S+) ([\d.]+)(KiB|MiB|GiB) / \S+ ([\d.]+)%", sample
):
value = float(value) * {"KiB": 1 / 1024, "MiB": 1, "GiB": 1024}[unit]
current = peaks.setdefault(name, {"sampled_peak_MiB": 0, "sampled_peak_CPU_percent": 0})
current["sampled_peak_MiB"] = max(current["sampled_peak_MiB"], value)
current["sampled_peak_CPU_percent"] = max(
current["sampled_peak_CPU_percent"], float(cpu)
)
result = {
"profile": "6 accepted / 4 unavailable; 10 clients; 800x600 PNG",
"limits": "API 512MiB, worker 1GiB, dispatcher 256MiB",
"samples": len(raw["stats_samples"]),
"peaks": peaks,
"limitations": "discrete RSS/CPU samples, not true maximum or steady-state throughput",
}
(ROOT / "evidence/resource-summary.json").write_text(json.dumps(result, indent=2))
observations = sorted(raw["seconds_to_observed_ready"])
drain = {
"observed_ready_seconds": {
str(p): observations[max(0, math.ceil(p / 100 * len(observations)) - 1)]
for p in (50, 95, 99)
},
"accepted": raw["accepted"],
"expected_unavailable": raw["rejected"],
"image_bytes": raw["image_bytes"],
"polling_and_startup_included": True,
"interpretation": "upper bounds from sequential polls, n=6; not a steady-state latency SLA",
}
(ROOT / "evidence/drain-summary.json").write_text(json.dumps(drain, indent=2))
if __name__ == "__main__":
main()