forked from longtao.wu/droboticflow
91 lines
4.2 KiB
Python
91 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def load_module(name: str, relative: str):
|
|
spec = importlib.util.spec_from_file_location(name, ROOT / relative)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
BASES = load_module("validate_kaniko_operator_bases", "scripts/validate_kaniko_operator_bases.py")
|
|
REGISTRY = load_module("verify_registry_manifest", "scripts/verify_registry_manifest.py")
|
|
GPU_DIGEST = "registry.example/dataflow/gpu-base@sha256:" + "a" * 64
|
|
|
|
|
|
class FakeResponse:
|
|
def __init__(self, digest: str):
|
|
self.headers = {"Docker-Content-Digest": digest}
|
|
|
|
def read(self):
|
|
return json.dumps({"config": {"digest": "sha256:" + "b" * 64}, "layers": [{}, {}]}).encode("utf-8")
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *_args):
|
|
return False
|
|
|
|
|
|
class TestKanikoOperatorBasePreflight(unittest.TestCase):
|
|
def test_skipped_base_build_requires_exact_digest_only_for_selected_arg(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
dockerfile = Path(directory) / "Dockerfile"
|
|
dockerfile.write_text("ARG OPERATOR_GPU_BASE_IMAGE=registry.example/gpu:tag\nFROM ${OPERATOR_GPU_BASE_IMAGE}\n", encoding="utf-8")
|
|
values = {"operator_base": "", "operator_gpu_base": "registry.example/gpu:tag", "operator_holobrain_base": "", "operator_yolo_base": ""}
|
|
with self.assertRaisesRegex(RuntimeError, "must be digest-pinned"):
|
|
BASES.validate([dockerfile], values, True)
|
|
values["operator_gpu_base"] = GPU_DIGEST
|
|
result = BASES.validate([dockerfile], values, True)
|
|
self.assertEqual(result[0]["baseArgs"], {"OPERATOR_GPU_BASE_IMAGE": GPU_DIGEST})
|
|
|
|
def test_dockerfile_without_operator_base_does_not_require_unrelated_digest(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
dockerfile = Path(directory) / "Dockerfile"
|
|
dockerfile.write_text("FROM python:3.12-slim\n", encoding="utf-8")
|
|
values = {"operator_base": "", "operator_gpu_base": "", "operator_holobrain_base": "", "operator_yolo_base": ""}
|
|
self.assertEqual(BASES.validate([dockerfile], values, True), [{"dockerfile": str(dockerfile), "baseArgs": {}}])
|
|
|
|
def test_registry_preflight_uses_digest_and_never_echoes_auth(self):
|
|
image = "registry.example/dataflow/gpu-base@sha256:" + "c" * 64
|
|
captured = {}
|
|
|
|
def opener(request, timeout):
|
|
captured["url"] = request.full_url
|
|
captured["auth"] = request.get_header("Authorization")
|
|
captured["timeout"] = timeout
|
|
return FakeResponse("sha256:" + "c" * 64)
|
|
|
|
result = REGISTRY.verify(image, {"auths": {"registry.example": {"auth": "not-a-real-secret"}}}, opener)
|
|
self.assertEqual(result["manifestDigest"], "sha256:" + "c" * 64)
|
|
self.assertEqual(result["layerCount"], 2)
|
|
self.assertIn("/v2/dataflow/gpu-base/manifests/sha256:", captured["url"])
|
|
self.assertEqual(captured["auth"], "Basic not-a-real-secret")
|
|
self.assertEqual(
|
|
REGISTRY.bearer_challenge('Bearer realm="https://auth.example/token",service="registry.example",scope="repository:dataflow/gpu-base:pull"'),
|
|
{"realm": "https://auth.example/token", "service": "registry.example", "scope": "repository:dataflow/gpu-base:pull"},
|
|
)
|
|
with self.assertRaisesRegex(RuntimeError, "digest-pinned"):
|
|
REGISTRY.parse_image("registry.example/dataflow/gpu-base:tag")
|
|
|
|
def test_build_script_preserves_operator_only_component_selection_and_reports_safe_plan(self):
|
|
script = (ROOT / "scripts" / "build-poc-images-kaniko.sh").read_text(encoding="utf-8")
|
|
self.assertIn('POC_KANIKO_COMPONENTS="${POC_KANIKO_COMPONENTS-api,worker,drosync-coordinator,runner,headpose-service}"', script)
|
|
self.assertIn("POC_KANIKO_OPERATOR_FILTER selected zero operator images", script)
|
|
self.assertIn("KANIKO_BUILD_PLAN=contextSource=%s platformComponents=%s", script)
|
|
self.assertIn("failed at line %s (exit %s)", script)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|