Skip to content

Commit

Permalink
fix: make test deterministic
Browse files Browse the repository at this point in the history
Not using fixed ports is good
  • Loading branch information
mdelapenya committed Jun 26, 2024
1 parent e343395 commit 5d13313
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
39 changes: 26 additions & 13 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,26 @@ func removeImageFromLocalCache(t *testing.T, img string) {
}

func TestBuildContainerFromDockerfileWithDockerAuthConfig(t *testing.T) {
mappedPort := prepareLocalRegistryWithAuth(t)

// using the same credentials as in the Docker Registry
base64 := "dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" // testuser:testpassword
t.Setenv("DOCKER_AUTH_CONFIG", `{
"auths": {
"localhost:5001": { "username": "testuser", "password": "testpassword", "auth": "`+base64+`" }
"localhost:`+mappedPort+`": { "username": "testuser", "password": "testpassword", "auth": "`+base64+`" }
},
"credsStore": "desktop"
}`)

prepareLocalRegistryWithAuth(t)

ctx := context.Background()

req := ContainerRequest{
FromDockerfile: FromDockerfile{
Context: "./testdata",
Dockerfile: "auth.Dockerfile",
BuildArgs: map[string]*string{
"REGISTRY_PORT": &mappedPort,
},
},
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
Expand All @@ -252,23 +255,26 @@ func TestBuildContainerFromDockerfileWithDockerAuthConfig(t *testing.T) {
}

func TestBuildContainerFromDockerfileShouldFailWithWrongDockerAuthConfig(t *testing.T) {
mappedPort := prepareLocalRegistryWithAuth(t)

// using different credentials than in the Docker Registry
base64 := "Zm9vOmJhcg==" // foo:bar
t.Setenv("DOCKER_AUTH_CONFIG", `{
"auths": {
"localhost:5001": { "username": "foo", "password": "bar", "auth": "`+base64+`" }
"localhost:`+mappedPort+`": { "username": "foo", "password": "bar", "auth": "`+base64+`" }
},
"credsStore": "desktop"
}`)

prepareLocalRegistryWithAuth(t)

ctx := context.Background()

req := ContainerRequest{
FromDockerfile: FromDockerfile{
Context: "./testdata",
Dockerfile: "auth.Dockerfile",
BuildArgs: map[string]*string{
"REGISTRY_PORT": &mappedPort,
},
},
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
Expand All @@ -281,20 +287,20 @@ func TestBuildContainerFromDockerfileShouldFailWithWrongDockerAuthConfig(t *test
}

func TestCreateContainerFromPrivateRegistry(t *testing.T) {
mappedPort := prepareLocalRegistryWithAuth(t)

// using the same credentials as in the Docker Registry
base64 := "dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" // testuser:testpassword
t.Setenv("DOCKER_AUTH_CONFIG", `{
"auths": {
"localhost:5001": { "username": "testuser", "password": "testpassword", "auth": "`+base64+`" }
"localhost:`+mappedPort+`": { "username": "testuser", "password": "testpassword", "auth": "`+base64+`" }
},
"credsStore": "desktop"
}`)

prepareLocalRegistryWithAuth(t)

ctx := context.Background()
req := ContainerRequest{
Image: "localhost:5001/redis:5.0-alpine",
Image: "localhost:" + mappedPort + "/redis:5.0-alpine",
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
Expand All @@ -308,14 +314,14 @@ func TestCreateContainerFromPrivateRegistry(t *testing.T) {
terminateContainerOnEnd(t, ctx, redisContainer)
}

func prepareLocalRegistryWithAuth(t *testing.T) {
func prepareLocalRegistryWithAuth(t *testing.T) string {
ctx := context.Background()
wd, err := os.Getwd()
require.NoError(t, err)
// copyDirectoryToContainer {
req := ContainerRequest{
Image: "registry:2",
ExposedPorts: []string{"5001:5000/tcp"},
ExposedPorts: []string{"5000/tcp"},
Env: map[string]string{
"REGISTRY_AUTH": "htpasswd",
"REGISTRY_AUTH_HTPASSWD_REALM": "Registry",
Expand Down Expand Up @@ -345,15 +351,22 @@ func prepareLocalRegistryWithAuth(t *testing.T) {
registryC, err := GenericContainer(ctx, genContainerReq)
require.NoError(t, err)

mappedPort, err := registryC.MappedPort(ctx, "5000/tcp")
require.NoError(t, err)

mp := mappedPort.Port()

t.Cleanup(func() {
removeImageFromLocalCache(t, "localhost:5001/redis:5.0-alpine")
removeImageFromLocalCache(t, "localhost:"+mp+"/redis:5.0-alpine")
})
t.Cleanup(func() {
require.NoError(t, registryC.Terminate(context.Background()))
})

_, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

return mp
}

func prepareRedisImage(ctx context.Context, req ContainerRequest, t *testing.T) (Container, error) {
Expand Down
4 changes: 3 additions & 1 deletion testdata/auth.Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
FROM localhost:5001/redis:5.0-alpine
ARG REGISTRY_PORT=5001

FROM localhost:${REGISTRY_PORT}/redis:5.0-alpine

0 comments on commit 5d13313

Please sign in to comment.