droboticflow/tests/backend/build_push_images_script_test.py
d-robotics 54a960284c 🏷️ Stamp images with their build commit and time
Dockerfiles bake DROBOTICFLOW_IMAGE_BUILD_COMMIT from a build-arg that
defaults to 'unknown', and the API prefers it over the deployment stamp, so
release images built without the arg reported buildCommit 'unknown'.

Add scripts/release-build-api-image.sh for release.py builds: it refuses
uncommitted tracked changes, passes the checkout commit and UTC build time,
pushes, and prints the digest reference for prepare --image. The general
build-and-push script now passes both args too. Document the step in the
release README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 00:36:20 +08:00

76 lines
2.8 KiB
Python

import os
import pathlib
import subprocess
ROOT = pathlib.Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "build-and-push-images.sh"
def run_build_script(extra_env: dict[str, str]) -> subprocess.CompletedProcess:
env = os.environ.copy()
for key in (
"API_IMAGE",
"DEPLOY_ENV",
"DROSYNC_COORDINATOR_IMAGE",
"DROBOTICFLOW_DEPLOY_ENV_LOADED",
"IMAGE_TAG",
"OPERATOR_RUNNER_IMAGE",
"PUSH",
"REGISTRY_HOST",
"REGISTRY_NAMESPACE",
"WORKER_IMAGE",
):
env.pop(key, None)
env["DROBOTICFLOW_DEPLOY_ENV_LOADED"] = "test"
env.update(extra_env)
return subprocess.run(["bash", str(SCRIPT)], cwd=ROOT, env=env, text=True, capture_output=True, check=False)
def test_build_and_push_requires_explicit_images_or_registry_configuration():
result = run_build_script({})
assert result.returncode == 1
assert "API_IMAGE is required unless REGISTRY_HOST and REGISTRY_NAMESPACE are both set" in result.stderr
def test_build_and_push_rejects_invalid_explicit_image_before_docker():
result = run_build_script(
{
"API_IMAGE": "registry.example.com/dataflow/droboticflow-api:test bad",
"WORKER_IMAGE": "registry.example.com/dataflow/droboticflow-worker:test",
"DROSYNC_COORDINATOR_IMAGE": "registry.example.com/dataflow/drosync-coordinator:test",
"OPERATOR_RUNNER_IMAGE": "registry.example.com/dataflow/droboticflow-operator-runner:test",
}
)
assert result.returncode == 1
assert "API_IMAGE must be a non-empty container image reference" in result.stderr
def test_build_and_push_stamps_every_image_with_commit_and_time(tmp_path):
fake = tmp_path / "docker"
log = tmp_path / "calls"
fake.write_text('#!/usr/bin/env bash\n[[ "$2" == build ]] && printf \'%s\\n\' "$*" >> "$FAKE_DOCKER_LOG"\nexit 0\n')
fake.chmod(0o755)
result = run_build_script(
{
"PATH": f"{tmp_path}:{os.environ['PATH']}",
"FAKE_DOCKER_LOG": str(log),
"PUSH": "false",
"DROBOTICFLOW_BUILD_COMMIT": "abc123",
"DROBOTICFLOW_BUILD_TIME": "2026-10-28T00:00:00Z",
"API_IMAGE": "registry.example.com/dataflow/droboticflow-api:test",
"WORKER_IMAGE": "registry.example.com/dataflow/droboticflow-worker:test",
"DROSYNC_COORDINATOR_IMAGE": "registry.example.com/dataflow/drosync-coordinator:test",
"OPERATOR_RUNNER_IMAGE": "registry.example.com/dataflow/droboticflow-operator-runner:test",
}
)
assert result.returncode == 0, result.stderr
builds = log.read_text().splitlines()
assert len(builds) == 4
for build in builds:
assert "--build-arg DROBOTICFLOW_BUILD_COMMIT=abc123" in build
assert "--build-arg DROBOTICFLOW_BUILD_TIME=2026-10-28T00:00:00Z" in build