Skip to content

Commit

Permalink
Merge pull request #5141 from tonistiigi/testutil-mirror-fixes
Browse files Browse the repository at this point in the history
testutil: optimization for mirror handling
  • Loading branch information
AkihiroSuda authored Jul 10, 2024
2 parents b6e33bc + f8ef1eb commit e38c064
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions util/testutil/integration/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime"
"sort"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -161,10 +162,7 @@ func Run(t *testing.T, testCases []Test, opt ...TestOpt) {
o(&tc)
}

mirror, cleanup, err := runMirror(t, tc.mirroredImages)
require.NoError(t, err)

t.Cleanup(func() { _ = cleanup() })
getMirror := lazyMirrorRunnerFunc(t, tc.mirroredImages)

matrix := prepareValueMatrix(tc)

Expand Down Expand Up @@ -200,7 +198,7 @@ func Run(t *testing.T, testCases []Test, opt ...TestOpt) {
ctx, cancel := context.WithCancelCause(ctx)
defer cancel(errors.WithStack(context.Canceled))

sb, closer, err := newSandbox(ctx, br, mirror, mv)
sb, closer, err := newSandbox(ctx, br, getMirror(), mv)
require.NoError(t, err)
t.Cleanup(func() { _ = closer() })
defer func() {
Expand Down Expand Up @@ -238,6 +236,11 @@ func copyImagesLocal(t *testing.T, host string, images map[string]string) error
}
localImageCache[host][to] = struct{}{}

// already exists check
if _, _, err := docker.NewResolver(docker.ResolverOptions{}).Resolve(context.TODO(), host+"/"+to); err == nil {
continue
}

var desc ocispecs.Descriptor
var provider content.Provider
var err error
Expand All @@ -257,12 +260,6 @@ func copyImagesLocal(t *testing.T, host string, images map[string]string) error
}
}

// already exists check
_, _, err = docker.NewResolver(docker.ResolverOptions{}).Resolve(context.TODO(), host+"/"+to)
if err == nil {
continue
}

ingester, err := contentutil.IngesterFromRef(host + "/" + to)
if err != nil {
return err
Expand Down Expand Up @@ -329,6 +326,20 @@ func WriteConfig(updaters []ConfigUpdater) (string, error) {
return filepath.Join(tmpdir, buildkitdConfigFile), nil
}

func lazyMirrorRunnerFunc(t *testing.T, images map[string]string) func() string {
var once sync.Once
var mirror string
return func() string {
once.Do(func() {
host, cleanup, err := runMirror(t, images)
require.NoError(t, err)
t.Cleanup(func() { _ = cleanup() })
mirror = host
})
return mirror
}
}

func runMirror(t *testing.T, mirroredImages map[string]string) (host string, _ func() error, err error) {
mirrorDir := os.Getenv("BUILDKIT_REGISTRY_MIRROR_DIR")

Expand Down

0 comments on commit e38c064

Please sign in to comment.