Skip to content

Commit

Permalink
Add test and better logs
Browse files Browse the repository at this point in the history
This commit adds a test to retrieving auth configurations, use either
the auth block in the config or specified via the agent config and adds
a log if lookup fails.
  • Loading branch information
dadgar committed Jan 23, 2017
1 parent 67bec60 commit 77b19f5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
9 changes: 6 additions & 3 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,15 +986,18 @@ func (d *DockerDriver) pullImage(driverConfig *DockerDriverConfig, client *docke
Email: driverConfig.Auth[0].Email,
ServerAddress: driverConfig.Auth[0].ServerAddress,
}
}
if authConfigFile := d.config.Read("docker.auth.config"); authConfigFile != "" {
} else if authConfigFile := d.config.Read("docker.auth.config"); authConfigFile != "" {
authOptionsPtr, err := authOptionFrom(authConfigFile, repo)
if err != nil {
d.logger.Printf("[INFO] driver.docker: failed to find docker auth for repo %q: %v", repo, err)
return fmt.Errorf("Failed to find docker auth for repo %q: %v", repo, err)
}

authOptions = *authOptionsPtr
if authOptions.Email == "" && authOptions.Password == "" &&
authOptions.ServerAddress == "" && authOptions.Username == "" {
d.logger.Printf("[DEBUG] driver.docker: did not find docker auth for repo %q", repo)
}
}

d.emitEvent("Downloading image %s:%s", repo, tag)
Expand Down Expand Up @@ -1391,7 +1394,7 @@ func calculatePercent(newSample, oldSample, newTotal, oldTotal uint64, cores int
return (float64(numerator) / float64(denom)) * float64(cores) * 100.0
}

// authOptionsFrom takes the Docker auth config file and the repo being pulled
// authOptionFrom takes the Docker auth config file and the repo being pulled
// and returns an AuthConfiguration or an error if the file/repo could not be
// parsed or looked up.
func authOptionFrom(file, repo string) (*docker.AuthConfiguration, error) {
Expand Down
51 changes: 51 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,3 +1330,54 @@ func copyImage(t *testing.T, taskDir *allocdir.TaskDir, image string) {
dst := filepath.Join(taskDir.LocalDir, image)
copyFile(filepath.Join("./test-resources/docker", image), dst, t)
}

func TestDockerDriver_AuthConfiguration(t *testing.T) {
path := "./test-resources/docker/auth.json"
cases := []struct {
Repo string
AuthConfig *docker.AuthConfiguration
}{
{
Repo: "lolwhat.com/what:1337",
AuthConfig: &docker.AuthConfiguration{},
},
{
Repo: "redis:3.2",
AuthConfig: &docker.AuthConfiguration{
Username: "test",
Password: "1234",
Email: "",
ServerAddress: "https://index.docker.io/v1/",
},
},
{
Repo: "quay.io/redis:3.2",
AuthConfig: &docker.AuthConfiguration{
Username: "test",
Password: "5678",
Email: "",
ServerAddress: "quay.io",
},
},
{
Repo: "other.io/redis:3.2",
AuthConfig: &docker.AuthConfiguration{
Username: "test",
Password: "abcd",
Email: "",
ServerAddress: "https://other.io/v1/",
},
},
}

for i, c := range cases {
act, err := authOptionFrom(path, c.Repo)
if err != nil {
t.Fatalf("Test %d failed: %v", i+1, err)
}

if !reflect.DeepEqual(act, c.AuthConfig) {
t.Fatalf("Test %d failed: Unexpected auth config: got %+v; want %+v", i+1, act, c.AuthConfig)
}
}
}
13 changes: 13 additions & 0 deletions client/driver/test-resources/docker/auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "dGVzdDoxMjM0"
},
"quay.io": {
"auth": "dGVzdDo1Njc4"
},
"https://other.io/v1/": {
"auth": "dGVzdDphYmNk"
}
}
}

0 comments on commit 77b19f5

Please sign in to comment.