Provide full path to Docker config file

Add tests.  The tests assume a POSIX file system, but it seems many
other tests assume Linux, so perhaps this is not a problem, or at
least does not add a new problem.

Fixes #1235
This commit is contained in:
David Dooling 2020-05-06 09:38:07 -05:00
parent c0618a06c1
commit 8bfd370ef9
No known key found for this signature in database
GPG Key ID: 9558581EBB90A121
2 changed files with 50 additions and 6 deletions

View File

@ -55,14 +55,16 @@ const (
UpstreamClientUaKey = "UPSTREAM_CLIENT_TYPE"
)
// DockerConfLocation returns the file system location of the Docker configuration
// from the DOCKER_CONFIG environment variable. If that variable is not set, it
// returns "/kaniko/.docker/config.json".
// DockerConfLocation returns the file system location of the Docker
// configuration file under the directory set in the DOCKER_CONFIG environment
// variable. If that variable is not set, it returns the OS-equivalent of
// "/kaniko/.docker/config.json".
func DockerConfLocation() string {
if dockerConfLocation := os.Getenv("DOCKER_CONFIG"); dockerConfLocation != "" {
return dockerConfLocation
configFile := "config.json"
if dockerConfDir := os.Getenv("DOCKER_CONFIG"); dockerConfDir != "" {
return filepath.Join(dockerConfDir, configFile)
}
return "/kaniko/.docker/config.json"
return string(os.PathSeparator) + filepath.Join("kaniko", ".docker", configFile)
}
func (w *withUserAgent) RoundTrip(r *http.Request) (*http.Response, error) {

View File

@ -46,6 +46,48 @@ func mustTag(t *testing.T, s string) name.Tag {
return tag
}
func TestDockerConfLocation(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)
}
unset := DockerConfLocation()
unsetExpected := "/kaniko/.docker/config.json" // will fail on Windows
if unset != unsetExpected {
t.Errorf("Unexpected default Docker configuration file location: expected:'%s' got:'%s'", unsetExpected, unset)
}
if err := os.Setenv(dcfg, "/kaniko/.docker"); err != nil {
t.Fatalf("Failed to set DOCKER_CONFIG: %v", err)
}
kanikoDefault := DockerConfLocation()
kanikoDefaultExpected := "/kaniko/.docker/config.json" // will fail on Windows
if kanikoDefault != kanikoDefaultExpected {
t.Errorf("Unexpected kaniko default Docker conf file location: expected:'%s' got:'%s'", kanikoDefaultExpected, kanikoDefault)
}
if err := os.Setenv(dcfg, "/a/different/path"); err != nil {
t.Fatalf("Failed to set DOCKER_CONFIG: %v", err)
}
set := DockerConfLocation()
setExpected := "/a/different/path/config.json" // will fail on Windows
if set != setExpected {
t.Errorf("Unexpected DOCKER_CONF-based file location: expected:'%s' got:'%s'", setExpected, set)
}
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)
}
} else {
if err := os.Unsetenv(dcfg); err != nil {
t.Fatalf("Failed to unset DOCKER_CONFIG after testing: %v", err)
}
}
}
func TestWriteImageOutputs(t *testing.T) {
img, err := random.Image(1024, 3)
if err != nil {