From f66f0b9451873edf2565039094c04210666ac57b Mon Sep 17 00:00:00 2001 From: Davide Salerno Date: Wed, 28 Jun 2023 21:36:59 +0200 Subject: [PATCH] Added additional unit tests Signed-off-by: Davide Salerno --- modules/dockerregistry/dockerregistry_test.go | 79 +++++++++++++++++++ modules/dockerregistry/go.mod | 2 +- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/modules/dockerregistry/dockerregistry_test.go b/modules/dockerregistry/dockerregistry_test.go index c6f0cf291b..b5352ebdd9 100644 --- a/modules/dockerregistry/dockerregistry_test.go +++ b/modules/dockerregistry/dockerregistry_test.go @@ -110,7 +110,38 @@ func TestDockerRegistryWithData(t *testing.T) { t.Cleanup(cancel) } +/**/ + func TestDockerRegistryWithAuth(t *testing.T) { + ctx := context.Background() + wd, err := os.Getwd() + assert.NoError(t, err) + container, err := RunContainer(ctx, WithAuthentication(wd+"/../../testdata/auth")) + + if err != nil { + t.Fatal(err) + } + + // Clean up the container after the test is complete + t.Cleanup(func() { + if err := container.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) + + port, ipAddress := getRegistryPortAndAddress(t, err, container, ctx) + + // Let's simply check that the registry is up and running with a GET to http://localhost:5000/v2/_catalog + h := http.Client{} + req, _ := http.NewRequest("GET", "http://"+ipAddress+":"+port.Port()+"/v2/_catalog", nil) + req.SetBasicAuth("testuser", "testpassword") + resp, _ := h.Do(req) + defer resp.Body.Close() + _, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) +} + +func TestDockerRegistryWithAuthAndData(t *testing.T) { t.Cleanup(func() { os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig) }) @@ -159,6 +190,54 @@ func TestDockerRegistryWithAuth(t *testing.T) { t.Cleanup(cancel) } +func TestDockerRegistryWithAuthDataAndImage(t *testing.T) { + t.Cleanup(func() { + os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig) + }) + os.Unsetenv("DOCKER_AUTH_CONFIG") + + // using the same credentials as in the Docker Registry + base64 := "dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" // testuser:testpassword + t.Setenv("DOCKER_AUTH_CONFIG", `{ + "auths": { + "localhost:5000": { "username": "testuser", "password": "testpassword", "auth": "`+base64+`" } + }, + "credsStore": "desktop" + }`) + ctx := context.Background() + wd, err := os.Getwd() + assert.NoError(t, err) + container, err := RunContainer(ctx, WithAuthentication(wd+"/../../testdata/auth"), WithData(wd+"/../../testdata/data"), WithImage("docker.io/registry:latest")) + + if err != nil { + t.Fatal(err) + } + + // Clean up the container after the test is complete + t.Cleanup(func() { + if err := container.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) + + // Let's check that we are able to start a container form image localhost:5000/redis:5.0-alpine + // using default username and password + req := testcontainers.ContainerRequest{ + Image: "localhost:5000/redis:5.0-alpine", + AlwaysPullImage: true, // make sure the authentication takes place + ExposedPorts: []string{"6379/tcp"}, + WaitingFor: wait.ForLog("Ready to accept connections"), + } + + redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + require.Nil(t, err) + terminateContainerOnEnd(t, ctx, redisContainer) + _, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) +} func getRegistryPortAndAddress(t *testing.T, err error, container *DockerRegistryContainer, ctx context.Context) (nat.Port, string) { port, err := container.MappedPort(ctx, "5000") if err != nil { diff --git a/modules/dockerregistry/go.mod b/modules/dockerregistry/go.mod index 0632c48889..5186aba9ff 100644 --- a/modules/dockerregistry/go.mod +++ b/modules/dockerregistry/go.mod @@ -3,6 +3,7 @@ module github.com/testcontainers/testcontainers-go/modules/dockerregistry go 1.19 require ( + github.com/docker/go-connections v0.4.0 github.com/stretchr/testify v1.8.4 github.com/testcontainers/testcontainers-go v0.20.0 gotest.tools/gotestsum v1.10.0 @@ -18,7 +19,6 @@ require ( github.com/dnephin/pflag v1.0.7 // indirect github.com/docker/distribution v2.8.2+incompatible // indirect github.com/docker/docker v23.0.5+incompatible // indirect - github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/fatih/color v1.13.0 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect