@@ -180,6 +180,94 @@ func TestConfig_GetCredential_validConfig(t *testing.T) {
180
180
}
181
181
}
182
182
183
+ func TestConfig_GetCredential_legacyConfig (t * testing.T ) {
184
+ cfg , err := Load ("../../testdata/legacy_auths_config.json" )
185
+ if err != nil {
186
+ t .Fatal ("Load() error =" , err )
187
+ }
188
+
189
+ tests := []struct {
190
+ name string
191
+ serverAddress string
192
+ want auth.Credential
193
+ wantErr bool
194
+ }{
195
+ {
196
+ name : "Regular address matched" ,
197
+ serverAddress : "registry1.example.com" ,
198
+ want : auth.Credential {
199
+ Username : "username1" ,
200
+ Password : "password1" ,
201
+ },
202
+ },
203
+ {
204
+ name : "Another entry for the same address matched" ,
205
+ serverAddress : "https://registry1.example.com/" ,
206
+ want : auth.Credential {
207
+ Username : "foo" ,
208
+ Password : "bar" ,
209
+ },
210
+ },
211
+ {
212
+ name : "Address with different scheme unmached" ,
213
+ serverAddress : "http://registry1.example.com/" ,
214
+ want : auth .EmptyCredential ,
215
+ },
216
+ {
217
+ name : "Address with http prefix matched" ,
218
+ serverAddress : "registry2.example.com" ,
219
+ want : auth.Credential {
220
+ Username : "username2" ,
221
+ Password : "password2" ,
222
+ },
223
+ },
224
+ {
225
+ name : "Address with https prefix matched" ,
226
+ serverAddress : "registry3.example.com" ,
227
+ want : auth.Credential {
228
+ Username : "username3" ,
229
+ Password : "password3" ,
230
+ },
231
+ },
232
+ {
233
+ name : "Address with http prefix and / suffix matched" ,
234
+ serverAddress : "registry4.example.com" ,
235
+ want : auth.Credential {
236
+ Username : "username4" ,
237
+ Password : "password4" ,
238
+ },
239
+ },
240
+ {
241
+ name : "Address with https prefix and / suffix matched" ,
242
+ serverAddress : "registry5.example.com" ,
243
+ want : auth.Credential {
244
+ Username : "username5" ,
245
+ Password : "password5" ,
246
+ },
247
+ },
248
+ {
249
+ name : "Address with https prefix and path suffix matched" ,
250
+ serverAddress : "registry6.example.com" ,
251
+ want : auth.Credential {
252
+ Username : "username6" ,
253
+ Password : "password6" ,
254
+ },
255
+ },
256
+ }
257
+ for _ , tt := range tests {
258
+ t .Run (tt .name , func (t * testing.T ) {
259
+ got , err := cfg .GetCredential (tt .serverAddress )
260
+ if (err != nil ) != tt .wantErr {
261
+ t .Errorf ("Config.GetCredential() error = %v, wantErr %v" , err , tt .wantErr )
262
+ return
263
+ }
264
+ if ! reflect .DeepEqual (got , tt .want ) {
265
+ t .Errorf ("Config.GetCredential() = %v, want %v" , got , tt .want )
266
+ }
267
+ })
268
+ }
269
+ }
270
+
183
271
func TestConfig_GetCredential_invalidConfig (t * testing.T ) {
184
272
cfg , err := Load ("../../testdata/invalid_auths_entry_config.json" )
185
273
if err != nil {
@@ -1314,3 +1402,51 @@ func Test_decodeAuth(t *testing.T) {
1314
1402
})
1315
1403
}
1316
1404
}
1405
+
1406
+ func Test_toHostname (t * testing.T ) {
1407
+ tests := []struct {
1408
+ name string
1409
+ addr string
1410
+ want string
1411
+ }{
1412
+ {
1413
+ addr : "http://test.example.com" ,
1414
+ want : "test.example.com" ,
1415
+ },
1416
+ {
1417
+ addr : "http://test.example.com/" ,
1418
+ want : "test.example.com" ,
1419
+ },
1420
+ {
1421
+ addr : "http://test.example.com/foo/bar" ,
1422
+ want : "test.example.com" ,
1423
+ },
1424
+ {
1425
+ addr : "https://test.example.com" ,
1426
+ want : "test.example.com" ,
1427
+ },
1428
+ {
1429
+ addr : "https://test.example.com/" ,
1430
+ want : "test.example.com" ,
1431
+ },
1432
+ {
1433
+ addr : "http://test.example.com/foo/bar" ,
1434
+ want : "test.example.com" ,
1435
+ },
1436
+ {
1437
+ addr : "test.example.com" ,
1438
+ want : "test.example.com" ,
1439
+ },
1440
+ {
1441
+ addr : "test.example.com/foo/bar/" ,
1442
+ want : "test.example.com" ,
1443
+ },
1444
+ }
1445
+ for _ , tt := range tests {
1446
+ t .Run (tt .name , func (t * testing.T ) {
1447
+ if got := toHostname (tt .addr ); got != tt .want {
1448
+ t .Errorf ("toHostname() = %v, want %v" , got , tt .want )
1449
+ }
1450
+ })
1451
+ }
1452
+ }
0 commit comments