Browse code

Adding more tests around `auth.ResolveAuthConfig`

mostly because I've been failing at getting docker `0.6.2` working with
docker-registry `0.6.0` with regard to login and push. I suspect there
may be some mismatched expectations between `auth.ResolveAuthConfig` and
`registry.ResolveRepositoryName`, but I haven't done enough research or
experimentation to think it's anything more than user error. More tests
are good, no?

Dan Buch authored on 2013/09/25 01:26:17
Showing 1 changed files
... ...
@@ -75,20 +75,31 @@ func TestCreateAccount(t *testing.T) {
75 75
 	}
76 76
 }
77 77
 
78
-func TestSameAuthDataPostSave(t *testing.T) {
78
+func setupTempConfigFile() (*ConfigFile, error) {
79 79
 	root, err := ioutil.TempDir("", "docker-test")
80 80
 	if err != nil {
81
-		t.Fatal(err)
81
+		return nil, err
82 82
 	}
83 83
 	configFile := &ConfigFile{
84 84
 		rootPath: root,
85
-		Configs:  make(map[string]AuthConfig, 1),
85
+		Configs:  make(map[string]AuthConfig),
86 86
 	}
87 87
 
88
-	configFile.Configs["testIndex"] = AuthConfig{
89
-		Username: "docker-user",
90
-		Password: "docker-pass",
91
-		Email:    "docker@docker.io",
88
+	for _, registry := range []string{"testIndex", IndexServerAddress()} {
89
+		configFile.Configs[registry] = AuthConfig{
90
+			Username: "docker-user",
91
+			Password: "docker-pass",
92
+			Email:    "docker@docker.io",
93
+		}
94
+	}
95
+
96
+	return configFile, nil
97
+}
98
+
99
+func TestSameAuthDataPostSave(t *testing.T) {
100
+	configFile, err := setupTempConfigFile()
101
+	if err != nil {
102
+		t.Fatal(err)
92 103
 	}
93 104
 
94 105
 	err = SaveConfig(configFile)
... ...
@@ -110,3 +121,68 @@ func TestSameAuthDataPostSave(t *testing.T) {
110 110
 		t.Fail()
111 111
 	}
112 112
 }
113
+
114
+func TestResolveAuthConfigIndexServer(t *testing.T) {
115
+	configFile, err := setupTempConfigFile()
116
+	if err != nil {
117
+		t.Fatal(err)
118
+	}
119
+
120
+	for _, registry := range []string{"", IndexServerAddress()} {
121
+		resolved := configFile.ResolveAuthConfig(registry)
122
+		if resolved != configFile.Configs[IndexServerAddress()] {
123
+			t.Fail()
124
+		}
125
+	}
126
+}
127
+
128
+func TestResolveAuthConfigFullURL(t *testing.T) {
129
+	configFile, err := setupTempConfigFile()
130
+	if err != nil {
131
+		t.Fatal(err)
132
+	}
133
+
134
+	registryAuth := AuthConfig{
135
+		Username: "foo-user",
136
+		Password: "foo-pass",
137
+		Email:    "foo@example.com",
138
+	}
139
+	localAuth := AuthConfig{
140
+		Username: "bar-user",
141
+		Password: "bar-pass",
142
+		Email:    "bar@example.com",
143
+	}
144
+	configFile.Configs["https://registry.example.com/v1/"] = registryAuth
145
+	configFile.Configs["http://localhost:8000/v1/"] = localAuth
146
+
147
+	validRegistries := map[string][]string{
148
+		"https://registry.example.com/v1/": {
149
+			"https://registry.example.com/v1/",
150
+			"http://registry.example.com/v1/",
151
+			"registry.example.com",
152
+			"registry.example.com/v1/",
153
+		},
154
+		"http://localhost:8000/v1/": {
155
+			"https://localhost:8000/v1/",
156
+			"http://localhost:8000/v1/",
157
+			"localhost:8000",
158
+			"localhost:8000/v1/",
159
+		},
160
+	}
161
+
162
+	for configKey, registries := range validRegistries {
163
+		for _, registry := range registries {
164
+			var (
165
+				configured AuthConfig
166
+				ok         bool
167
+			)
168
+			resolved := configFile.ResolveAuthConfig(registry)
169
+			if configured, ok = configFile.Configs[configKey]; !ok {
170
+				t.Fail()
171
+			}
172
+			if resolved.Email != configured.Email {
173
+				t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
174
+			}
175
+		}
176
+	}
177
+}