Skip to content

Commit 32b8e9b

Browse files
authored
test: replace live r8.im registry with local test registry in integration tests (#2816)
Use registry-seed with crane.Copy to seed base images into a local test registry instead of hitting the live r8.im registry, which is flaky in CI. - Add registry-seed harness command supporting both local and external sources - Convert build_base_image_sha, build_cog_version_match, build_python313_base_image to seed python:3.12/3.13-slim from Docker Hub via crane - Convert torch_cuda_baseimage to use cog debug with a dummy seeded image - Fix name.Insecure missing from ParseReference in build.go (consistent with pkg/registry/)
1 parent c5a97d6 commit 32b8e9b

6 files changed

Lines changed: 97 additions & 7 deletions

File tree

integration-tests/harness/harness.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sync"
2121
"time"
2222

23+
"github.com/google/go-containerregistry/pkg/crane"
2324
"github.com/rogpeppe/go-internal/testscript"
2425

2526
"github.com/replicate/cog/pkg/registry"
@@ -247,6 +248,7 @@ func (h *Harness) Commands() map[string]func(ts *testscript.TestScript, neg bool
247248
// Registry and OCI bundle testing commands
248249
NewCommand("registry-start", h.cmdRegistryStart),
249250
NewCommand("registry-inspect", h.cmdRegistryInspect),
251+
NewCommand("registry-seed", h.cmdRegistrySeed),
250252
NewCommand("docker-push", h.cmdDockerPush),
251253
NewCommand("mock-weights", h.cmdMockWeights),
252254

@@ -886,6 +888,61 @@ func (h *Harness) stopRegistryByWorkDir(workDir string) {
886888
}
887889
}
888890

891+
// cmdRegistrySeed copies an image into the test registry under a new repository:tag.
892+
// The source can be a local reference (relative to $TEST_REGISTRY) or an absolute
893+
// reference to an external registry (e.g., docker.io/library/python:3.12-slim).
894+
// The destination is always relative to $TEST_REGISTRY.
895+
//
896+
// Usage: registry-seed <source> <dest-repo:tag>
897+
// Examples:
898+
//
899+
// registry-seed alpine:latest cog-base:cuda11.8-python3.10-torch2.0.1
900+
// registry-seed docker.io/library/python:3.12-slim cog-base:python3.12
901+
func (h *Harness) cmdRegistrySeed(ts *testscript.TestScript, neg bool, args []string) {
902+
if neg {
903+
ts.Fatalf("registry-seed: does not support negation")
904+
}
905+
if len(args) < 2 {
906+
ts.Fatalf("registry-seed: usage: registry-seed <source> <dest-repo:tag>")
907+
}
908+
909+
src := os.Expand(args[0], ts.Getenv)
910+
dst := os.Expand(args[1], ts.Getenv)
911+
912+
testRegistry := ts.Getenv("TEST_REGISTRY")
913+
if testRegistry == "" {
914+
ts.Fatalf("registry-seed: TEST_REGISTRY not set (call registry-start first)")
915+
}
916+
917+
// If the source looks like an absolute reference (contains a registry host
918+
// with a dot, e.g. "docker.io/library/python:3.12-slim"), use it as-is.
919+
// Otherwise treat it as relative to the test registry.
920+
srcRef := src
921+
if !isAbsoluteImageRef(src) {
922+
srcRef = testRegistry + "/" + src
923+
}
924+
dstRef := testRegistry + "/" + dst
925+
926+
if err := crane.Copy(srcRef, dstRef, crane.Insecure); err != nil {
927+
ts.Fatalf("registry-seed: failed to copy %s to %s: %v", srcRef, dstRef, err)
928+
}
929+
930+
ts.Logf("registry-seed: copied %s to %s", srcRef, dstRef)
931+
}
932+
933+
// isAbsoluteImageRef returns true if ref looks like it contains an explicit
934+
// registry host (e.g. "docker.io/library/python:3.12-slim" or
935+
// "ghcr.io/foo/bar:latest"). It checks whether the part before the first
936+
// slash contains a dot or a colon (port), which distinguishes a registry
937+
// host from a simple repository name like "alpine:latest".
938+
func isAbsoluteImageRef(ref string) bool {
939+
host, _, ok := strings.Cut(ref, "/")
940+
if !ok {
941+
return false
942+
}
943+
return strings.Contains(host, ".") || strings.Contains(host, ":")
944+
}
945+
889946
// cmdRegistryInspect inspects a registry manifest and outputs JSON.
890947
// Usage: registry-inspect <image-ref>
891948
// Outputs the manifest result as JSON to stdout.

integration-tests/tests/build_base_image_sha.txtar

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Test that base image SHA is recorded in labels with --use-cog-base-image
22
# Source: test_build.py::test_build_base_image_sha
3+
#
4+
# Uses a local test registry to avoid depending on the live r8.im registry.
35

4-
# Build with --use-cog-base-image flag
6+
# Start local registry and seed a cog-base image from Docker Hub's python:3.12-slim
7+
registry-start
8+
registry-seed docker.io/library/python:3.12-slim cog-base:python3.12
9+
10+
# Build with --use-cog-base-image flag against the local registry
11+
env COG_REGISTRY_HOST=$TEST_REGISTRY
512
cog build -t $TEST_IMAGE --use-cog-base-image
613

714
# Verify the base image layer label matches one of the actual layers

integration-tests/tests/build_cog_version_match.txtar

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
#
88
# This test verifies that when building with --use-cog-base-image,
99
# the installed Python cog package has a valid version number.
10+
#
11+
# Uses a local test registry to avoid depending on the live r8.im registry.
12+
13+
# Start local registry and seed a cog-base image from Docker Hub's python:3.12-slim
14+
registry-start
15+
registry-seed docker.io/library/python:3.12-slim cog-base:python3.12
1016

11-
# Build using --use-cog-base-image
17+
# Build using --use-cog-base-image against local registry
18+
env COG_REGISTRY_HOST=$TEST_REGISTRY
1219
cog build -t $TEST_IMAGE --use-cog-base-image=true
1320

1421
# Compare the embedded version label with the Python package version inside the image

integration-tests/tests/build_python313_base_image.txtar

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Test Python 3.13 works with --use-cog-base-image
22
# Source: test_build.py::test_python_313_base_images
3+
#
4+
# Uses a local test registry to avoid depending on the live r8.im registry.
35

4-
# Build using Python 3.13 with --use-cog-base-image
6+
# Start local registry and seed a cog-base image from Docker Hub's python:3.13-slim
7+
registry-start
8+
registry-seed docker.io/library/python:3.13-slim cog-base:python3.13
9+
10+
# Build using Python 3.13 with --use-cog-base-image against local registry
11+
env COG_REGISTRY_HOST=$TEST_REGISTRY
512
cog build -t $TEST_IMAGE --use-cog-base-image
613

714
# Verify build succeeded by running a prediction

integration-tests/tests/torch_cuda_baseimage.txtar

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
[short] skip 'slow test - run without -short flag'
1+
# Test that cog correctly resolves a cog-base image for Torch 2.0.1+cu118
2+
# and generates the expected Dockerfile with --use-cog-base-image.
3+
#
4+
# Uses a local test registry seeded with a dummy image to avoid depending
5+
# on the live r8.im registry, which can be flaky in CI.
26

3-
# Test Torch 2.0.1+cu118 with --use-cog-base-image flag
4-
cog build -t $TEST_IMAGE --use-cog-base-image
7+
# Start local registry and seed a dummy cog-base image
8+
registry-start
9+
registry-seed alpine:latest cog-base:cuda11.8-python3.10-torch2.0.1
10+
11+
# Generate Dockerfile using the local registry as the cog-base source
12+
env COG_REGISTRY_HOST=$TEST_REGISTRY
13+
cog debug --use-cog-base-image
14+
stdout 'FROM.*cog-base:cuda11.8-python3.10-torch2.0.1'
515

616
-- cog.yaml --
717
build:

pkg/image/build.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ func Build(
300300
if cogBaseImageName != "" {
301301
labels[global.LabelNamespace+"cog-base-image-name"] = cogBaseImageName
302302

303-
ref, err := name.ParseReference(cogBaseImageName)
303+
// name.Insecure allows HTTP fallback for local/test registries,
304+
// consistent with ParseReference calls in pkg/registry/.
305+
ref, err := name.ParseReference(cogBaseImageName, name.Insecure)
304306
if err != nil {
305307
return "", fmt.Errorf("Failed to parse cog base image reference: %w", err)
306308
}

0 commit comments

Comments
 (0)