Skip to content

Commit b1e5824

Browse files
committed
chore(testutil): refactor registrytest
1 parent 1ab3f69 commit b1e5824

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

devcontainer/devcontainer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func TestUserFrom(t *testing.T) {
231231
}})
232232
require.NoError(t, err)
233233

234-
parsed, err := url.Parse(registry)
234+
parsed, err := url.Parse("http://" + registry)
235235
require.NoError(t, err)
236236
parsed.Path = "coder/test:latest"
237237
ref, err := name.ParseReference(strings.TrimPrefix(parsed.String(), "http://"))
@@ -306,7 +306,7 @@ func TestUserFrom(t *testing.T) {
306306
},
307307
}})
308308
require.NoError(t, err)
309-
parsed, err := url.Parse(registry)
309+
parsed, err := url.Parse("http://" + registry)
310310
require.NoError(t, err)
311311
parsed.Path = "coder/test:" + tag
312312
ref, err := name.ParseReference(strings.TrimPrefix(parsed.String(), "http://"))

integration/integration_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import (
4747
"github.com/google/go-cmp/cmp"
4848
"github.com/google/go-containerregistry/pkg/authn"
4949
"github.com/google/go-containerregistry/pkg/name"
50-
"github.com/google/go-containerregistry/pkg/registry"
5150
v1 "github.com/google/go-containerregistry/pkg/v1"
5251
"github.com/google/go-containerregistry/pkg/v1/remote"
5352
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
@@ -2507,14 +2506,8 @@ type setupInMemoryRegistryOpts struct {
25072506

25082507
func setupInMemoryRegistry(t *testing.T, opts setupInMemoryRegistryOpts) string {
25092508
t.Helper()
2510-
tempDir := t.TempDir()
2511-
regHandler := registry.New(registry.WithBlobHandler(registry.NewDiskBlobHandler(tempDir)))
2512-
authHandler := mwtest.BasicAuthMW(opts.Username, opts.Password)(regHandler)
2513-
regSrv := httptest.NewServer(authHandler)
2514-
t.Cleanup(func() { regSrv.Close() })
2515-
regSrvURL, err := url.Parse(regSrv.URL)
2516-
require.NoError(t, err)
2517-
return fmt.Sprintf("localhost:%s", regSrvURL.Port())
2509+
regSrv := registrytest.New(t, mwtest.BasicAuthMW(opts.Username, opts.Password))
2510+
return regSrv
25182511
}
25192512

25202513
// TestMain runs before all tests to build the envbuilder image.

testutil/registrytest/registrytest.go

+23-16
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,46 @@ package registrytest
33
import (
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

Comments
 (0)