@@ -3,45 +3,46 @@ package registrytest
33import (
44 "archive/tar"
55 "bytes"
6- "context"
76 "crypto"
87 "encoding/hex"
98 "encoding/json"
9+ "fmt"
1010 "io"
11+ "net/http"
1112 "net/http/httptest"
1213 "net/url"
1314 "strings"
1415 "testing"
1516 "time"
1617
17- "github.com/distribution/distribution/v3/configuration"
18- "github.com/distribution/distribution/v3/registry/handlers"
1918 "github.com/google/go-containerregistry/pkg/name"
19+ "github.com/google/go-containerregistry/pkg/registry"
2020 v1 "github.com/google/go-containerregistry/pkg/v1"
2121 "github.com/google/go-containerregistry/pkg/v1/empty"
2222 "github.com/google/go-containerregistry/pkg/v1/mutate"
2323 "github.com/google/go-containerregistry/pkg/v1/partial"
2424 "github.com/google/go-containerregistry/pkg/v1/remote"
2525 "github.com/google/go-containerregistry/pkg/v1/types"
26- "github.com/sirupsen/logrus"
2726 "github.com/stretchr/testify/require"
2827
2928 // needed by the registry
3029 _ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
3130)
3231
33- // New creates a new registry server that discards all logs.
34- func New (t * testing.T ) string {
35- cfg := & configuration.Configuration {
36- Storage : configuration.Storage {
37- "inmemory" : configuration.Parameters {},
38- },
32+ // New starts a new Docker registry listening on localhost.
33+ // It will automatically shut down when the test finishes.
34+ // It will store data in memory.
35+ func New (t testing.TB , mws ... func (http.Handler ) http.Handler ) string {
36+ t .Helper ()
37+ regHandler := registry .New (registry .WithBlobHandler (registry .NewInMemoryBlobHandler ()))
38+ for _ , mw := range mws {
39+ regHandler = mw (regHandler )
3940 }
40- logrus . SetOutput ( io . Discard )
41- app := handlers . NewApp ( context . Background (), cfg )
42- srv := httptest . NewServer ( app )
43- t . Cleanup ( srv . Close )
44- return srv . URL
41+ regSrv := httptest . NewServer ( regHandler )
42+ t . Cleanup ( func () { regSrv . Close () } )
43+ regSrvURL , err := url . Parse ( regSrv . URL )
44+ require . NoError ( t , err )
45+ return fmt . Sprintf ( "localhost:%s" , regSrvURL . Port ())
4546}
4647
4748// WriteContainer uploads a container to the registry server.
@@ -96,11 +97,17 @@ func WriteContainer(t *testing.T, serverURL, containerRef, mediaType string, fil
9697 })
9798 require .NoError (t , err )
9899
100+ // url.Parse will interpret localhost:12345 as scheme localhost and host 12345
101+ // so we need to add a scheme to the URL
102+ if ! strings .HasPrefix (serverURL , "http://" ) {
103+ serverURL = "http://" + serverURL
104+ }
99105 parsed , err := url .Parse (serverURL )
100106 require .NoError (t , err )
101107 parsed .Path = containerRef
108+ parsedStr := parsed .String ()
102109
103- ref , err := name .ParseReference (strings .TrimPrefix (parsed . String () , "http://" ))
110+ ref , err := name .ParseReference (strings .TrimPrefix (parsedStr , "http://" ))
104111 require .NoError (t , err )
105112
106113 err = remote .Write (ref , image )
0 commit comments