Skip to content

refactor(testutil): refactor registrytest #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading