Skip to content

Commit

Permalink
UnitTest: DockerConfLocation must return a file if it is existent and…
Browse files Browse the repository at this point in the history
… passed using env, or default if the file is incorrect
  • Loading branch information
akram committed Sep 29, 2020
1 parent 1e0c260 commit 87f259f
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions pkg/executor/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit 87f259f

Please sign in to comment.