Skip to content

Commit

Permalink
chore(testutil): refactor registrytest
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstcn committed Dec 19, 2024
1 parent 1ab3f69 commit b1e5824
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions devcontainer/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestUserFrom(t *testing.T) {
}})
require.NoError(t, err)

parsed, err := url.Parse(registry)
parsed, err := url.Parse("http://" + registry)
require.NoError(t, err)
parsed.Path = "coder/test:latest"
ref, err := name.ParseReference(strings.TrimPrefix(parsed.String(), "http://"))
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestUserFrom(t *testing.T) {
},
}})
require.NoError(t, err)
parsed, err := url.Parse(registry)
parsed, err := url.Parse("http://" + registry)
require.NoError(t, err)
parsed.Path = "coder/test:" + tag
ref, err := name.ParseReference(strings.TrimPrefix(parsed.String(), "http://"))
Expand Down
11 changes: 2 additions & 9 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/registry"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
Expand Down Expand Up @@ -2507,14 +2506,8 @@ type setupInMemoryRegistryOpts struct {

func setupInMemoryRegistry(t *testing.T, opts setupInMemoryRegistryOpts) string {
t.Helper()
tempDir := t.TempDir()
regHandler := registry.New(registry.WithBlobHandler(registry.NewDiskBlobHandler(tempDir)))
authHandler := mwtest.BasicAuthMW(opts.Username, opts.Password)(regHandler)
regSrv := httptest.NewServer(authHandler)
t.Cleanup(func() { regSrv.Close() })
regSrvURL, err := url.Parse(regSrv.URL)
require.NoError(t, err)
return fmt.Sprintf("localhost:%s", regSrvURL.Port())
regSrv := registrytest.New(t, mwtest.BasicAuthMW(opts.Username, opts.Password))
return regSrv
}

// TestMain runs before all tests to build the envbuilder image.
Expand Down
39 changes: 23 additions & 16 deletions testutil/registrytest/registrytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,46 @@ package registrytest
import (
"archive/tar"
"bytes"
"context"
"crypto"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

"github.com/distribution/distribution/v3/configuration"
"github.com/distribution/distribution/v3/registry/handlers"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/registry"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

// needed by the registry
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
)

// New creates a new registry server that discards all logs.
func New(t *testing.T) string {
cfg := &configuration.Configuration{
Storage: configuration.Storage{
"inmemory": configuration.Parameters{},
},
// New starts a new Docker registry listening on localhost.
// It will automatically shut down when the test finishes.
// It will store data in memory.
func New(t testing.TB, mws ...func(http.Handler) http.Handler) string {
t.Helper()
regHandler := registry.New(registry.WithBlobHandler(registry.NewInMemoryBlobHandler()))
for _, mw := range mws {
regHandler = mw(regHandler)
}
logrus.SetOutput(io.Discard)
app := handlers.NewApp(context.Background(), cfg)
srv := httptest.NewServer(app)
t.Cleanup(srv.Close)
return srv.URL
regSrv := httptest.NewServer(regHandler)
t.Cleanup(func() { regSrv.Close() })
regSrvURL, err := url.Parse(regSrv.URL)
require.NoError(t, err)
return fmt.Sprintf("localhost:%s", regSrvURL.Port())
}

// WriteContainer uploads a container to the registry server.
Expand Down Expand Up @@ -96,11 +97,17 @@ func WriteContainer(t *testing.T, serverURL, containerRef, mediaType string, fil
})
require.NoError(t, err)

// url.Parse will interpret localhost:12345 as scheme localhost and host 12345
// so we need to add a scheme to the URL
if !strings.HasPrefix(serverURL, "http://") {
serverURL = "http://" + serverURL
}
parsed, err := url.Parse(serverURL)
require.NoError(t, err)
parsed.Path = containerRef
parsedStr := parsed.String()

ref, err := name.ParseReference(strings.TrimPrefix(parsed.String(), "http://"))
ref, err := name.ParseReference(strings.TrimPrefix(parsedStr, "http://"))
require.NoError(t, err)

err = remote.Write(ref, image)
Expand Down

0 comments on commit b1e5824

Please sign in to comment.