@@ -3,45 +3,46 @@ package registrytest
3
3
import (
4
4
"archive/tar"
5
5
"bytes"
6
- "context"
7
6
"crypto"
8
7
"encoding/hex"
9
8
"encoding/json"
9
+ "fmt"
10
10
"io"
11
+ "net/http"
11
12
"net/http/httptest"
12
13
"net/url"
13
14
"strings"
14
15
"testing"
15
16
"time"
16
17
17
- "github.com/distribution/distribution/v3/configuration"
18
- "github.com/distribution/distribution/v3/registry/handlers"
19
18
"github.com/google/go-containerregistry/pkg/name"
19
+ "github.com/google/go-containerregistry/pkg/registry"
20
20
v1 "github.com/google/go-containerregistry/pkg/v1"
21
21
"github.com/google/go-containerregistry/pkg/v1/empty"
22
22
"github.com/google/go-containerregistry/pkg/v1/mutate"
23
23
"github.com/google/go-containerregistry/pkg/v1/partial"
24
24
"github.com/google/go-containerregistry/pkg/v1/remote"
25
25
"github.com/google/go-containerregistry/pkg/v1/types"
26
- "github.com/sirupsen/logrus"
27
26
"github.com/stretchr/testify/require"
28
27
29
28
// needed by the registry
30
29
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
31
30
)
32
31
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 )
39
40
}
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 ())
45
46
}
46
47
47
48
// WriteContainer uploads a container to the registry server.
@@ -96,11 +97,17 @@ func WriteContainer(t *testing.T, serverURL, containerRef, mediaType string, fil
96
97
})
97
98
require .NoError (t , err )
98
99
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
+ }
99
105
parsed , err := url .Parse (serverURL )
100
106
require .NoError (t , err )
101
107
parsed .Path = containerRef
108
+ parsedStr := parsed .String ()
102
109
103
- ref , err := name .ParseReference (strings .TrimPrefix (parsed . String () , "http://" ))
110
+ ref , err := name .ParseReference (strings .TrimPrefix (parsedStr , "http://" ))
104
111
require .NoError (t , err )
105
112
106
113
err = remote .Write (ref , image )
0 commit comments