diff --git a/pkg/executor/push_test.go b/pkg/executor/push_test.go index 41b5c5708d..3f3b844604 100644 --- a/pkg/executor/push_test.go +++ b/pkg/executor/push_test.go @@ -37,6 +37,9 @@ import ( "github.com/spf13/afero" ) +const DefaultKanikoDockerConfigJson = "/kaniko/.docker/config.json" + + func mustTag(t *testing.T, s string) name.Tag { tag, err := name.NewTag(s, name.StrictValidation) if err != nil { @@ -45,6 +48,60 @@ func mustTag(t *testing.T, s string) name.Tag { return tag } + +func TestDockerConfLocationWithFileLocation(t *testing.T) { + dcfg := "DOCKER_CONFIG" + originalDockerConfig := os.Getenv(dcfg) + if err := os.Unsetenv(dcfg); err != nil { + t.Fatalf("Failed to unset DOCKER_CONFIG: %v", err) + } + tmpDir, err := ioutil.TempDir("", "*") + if err != nil { + t.Fatalf("could not create temp dir: %s", err) + } + defer os.RemoveAll(tmpDir) + file, err := ioutil.TempFile(tmpDir, "docker.conf") + if err != nil { + t.Fatalf("could not create temp file: %s", err) + } + defer os.Remove(file.Name()) + if err := os.Setenv(dcfg, file.Name()); err != nil { + t.Fatalf("Failed to unset DOCKER_CONFIG: %v", err) + } + unset := DockerConfLocation() + unsetExpected := file.Name() + if unset != unsetExpected { + t.Errorf("Unexpected default Docker configuration file location: expected:'%s' got:'%s'", unsetExpected, unset) + } + restoreOriginalDockerConfigEnv(t, originalDockerConfig) +} + + +func TestDockerConfLocationWithInvalidFileLocation(t *testing.T) { + dcfg := "DOCKER_CONFIG" + originalDockerConfig := os.Getenv(dcfg) + if err := os.Unsetenv(dcfg); err != nil { + t.Fatalf("Failed to unset DOCKER_CONFIG: %v", err) + } + tmpDir, err := ioutil.TempDir("", "*") + if err != nil { + t.Fatalf("could not create temp dir: %s", err) + } + defer os.RemoveAll(tmpDir) + random := "fdgdsfrdfgdf-fdfsf-24dsgfd" //replace with a really random string + file := filepath.Join(tmpDir, random) // an random file name, shouldn't exist + if err := os.Setenv(dcfg, file); err != nil { + t.Fatalf("Failed to unset DOCKER_CONFIG: %v", err) + } + unset := DockerConfLocation() + unsetExpected := DefaultKanikoDockerConfigJson + if unset != unsetExpected { + t.Errorf("Unexpected default Docker configuration file location: expected:'%s' got:'%s'", unsetExpected, unset) + } + restoreOriginalDockerConfigEnv(t, originalDockerConfig) +} + + func TestDockerConfLocation(t *testing.T) { dcfg := "DOCKER_CONFIG" originalDockerConfig := os.Getenv(dcfg) @@ -53,7 +110,7 @@ func TestDockerConfLocation(t *testing.T) { t.Fatalf("Failed to unset DOCKER_CONFIG: %v", err) } unset := DockerConfLocation() - unsetExpected := "/kaniko/.docker/config.json" // will fail on Windows + unsetExpected := DefaultKanikoDockerConfigJson // will fail on Windows if unset != unsetExpected { t.Errorf("Unexpected default Docker configuration file location: expected:'%s' got:'%s'", unsetExpected, unset) } @@ -69,7 +126,7 @@ func TestDockerConfLocation(t *testing.T) { t.Fatalf("Failed to set DOCKER_CONFIG: %v", err) } kanikoDefault := DockerConfLocation() - kanikoDefaultExpected := filepath.Join(tmpDir, "/kaniko/.docker/config.json") // will fail on Windows + kanikoDefaultExpected := filepath.Join(tmpDir, DefaultKanikoDockerConfigJson) // will fail on Windows if kanikoDefault != kanikoDefaultExpected { t.Errorf("Unexpected kaniko default Docker conf file location: expected:'%s' got:'%s'", kanikoDefaultExpected, kanikoDefault) } @@ -87,7 +144,11 @@ func TestDockerConfLocation(t *testing.T) { if set != setExpected { t.Errorf("Unexpected DOCKER_CONF-based file location: expected:'%s' got:'%s'", setExpected, set) } + restoreOriginalDockerConfigEnv(t, originalDockerConfig) +} +func restoreOriginalDockerConfigEnv(t *testing.T, originalDockerConfig string) { + dcfg := "DOCKER_CONFIG" if originalDockerConfig != "" { if err := os.Setenv(dcfg, originalDockerConfig); err != nil { t.Fatalf("Failed to set DOCKER_CONFIG back to original value '%s': %v", originalDockerConfig, err)