Skip to content

Commit

Permalink
Added additional unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Salerno <[email protected]>
  • Loading branch information
davidesalerno committed Jun 28, 2023
1 parent 83aa322 commit f66f0b9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 79 additions & 0 deletions modules/dockerregistry/dockerregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/dockerregistry/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f66f0b9

Please sign in to comment.