diff --git a/docker_auth_test.go b/docker_auth_test.go index b411568171..1f8df17c09 100644 --- a/docker_auth_test.go +++ b/docker_auth_test.go @@ -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"}, @@ -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"}, @@ -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"), @@ -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", @@ -345,8 +351,13 @@ 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())) @@ -354,6 +365,8 @@ func prepareLocalRegistryWithAuth(t *testing.T) { _, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) + + return mp } func prepareRedisImage(ctx context.Context, req ContainerRequest, t *testing.T) (Container, error) { diff --git a/testdata/auth.Dockerfile b/testdata/auth.Dockerfile index 1f57898cba..367e228a67 100644 --- a/testdata/auth.Dockerfile +++ b/testdata/auth.Dockerfile @@ -1 +1,3 @@ -FROM localhost:5001/redis:5.0-alpine +ARG REGISTRY_PORT=5001 + +FROM localhost:${REGISTRY_PORT}/redis:5.0-alpine