github.com/rumpl/bof@v23.0.0-rc.2+incompatible/registry/auth_test.go (about) 1 package registry // import "github.com/docker/docker/registry" 2 3 import ( 4 "testing" 5 6 "github.com/docker/docker/api/types" 7 "github.com/docker/docker/api/types/registry" 8 "gotest.tools/v3/assert" 9 ) 10 11 func buildAuthConfigs() map[string]types.AuthConfig { 12 authConfigs := map[string]types.AuthConfig{} 13 14 for _, reg := range []string{"testIndex", IndexServer} { 15 authConfigs[reg] = types.AuthConfig{ 16 Username: "docker-user", 17 Password: "docker-pass", 18 } 19 } 20 21 return authConfigs 22 } 23 24 func TestResolveAuthConfigIndexServer(t *testing.T) { 25 authConfigs := buildAuthConfigs() 26 indexConfig := authConfigs[IndexServer] 27 28 officialIndex := ®istry.IndexInfo{ 29 Official: true, 30 } 31 privateIndex := ®istry.IndexInfo{ 32 Official: false, 33 } 34 35 resolved := ResolveAuthConfig(authConfigs, officialIndex) 36 assert.Equal(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer") 37 38 resolved = ResolveAuthConfig(authConfigs, privateIndex) 39 assert.Check(t, resolved != indexConfig, "Expected ResolveAuthConfig to not return IndexServer") 40 } 41 42 func TestResolveAuthConfigFullURL(t *testing.T) { 43 authConfigs := buildAuthConfigs() 44 45 registryAuth := types.AuthConfig{ 46 Username: "foo-user", 47 Password: "foo-pass", 48 } 49 localAuth := types.AuthConfig{ 50 Username: "bar-user", 51 Password: "bar-pass", 52 } 53 officialAuth := types.AuthConfig{ 54 Username: "baz-user", 55 Password: "baz-pass", 56 } 57 authConfigs[IndexServer] = officialAuth 58 59 expectedAuths := map[string]types.AuthConfig{ 60 "registry.example.com": registryAuth, 61 "localhost:8000": localAuth, 62 "example.com": localAuth, 63 } 64 65 validRegistries := map[string][]string{ 66 "registry.example.com": { 67 "https://registry.example.com/v1/", 68 "http://registry.example.com/v1/", 69 "registry.example.com", 70 "registry.example.com/v1/", 71 }, 72 "localhost:8000": { 73 "https://localhost:8000/v1/", 74 "http://localhost:8000/v1/", 75 "localhost:8000", 76 "localhost:8000/v1/", 77 }, 78 "example.com": { 79 "https://example.com/v1/", 80 "http://example.com/v1/", 81 "example.com", 82 "example.com/v1/", 83 }, 84 } 85 86 for configKey, registries := range validRegistries { 87 configured, ok := expectedAuths[configKey] 88 if !ok { 89 t.Fail() 90 } 91 index := ®istry.IndexInfo{ 92 Name: configKey, 93 } 94 for _, reg := range registries { 95 authConfigs[reg] = configured 96 resolved := ResolveAuthConfig(authConfigs, index) 97 if resolved.Username != configured.Username || resolved.Password != configured.Password { 98 t.Errorf("%s -> %v != %v\n", reg, resolved, configured) 99 } 100 delete(authConfigs, reg) 101 resolved = ResolveAuthConfig(authConfigs, index) 102 if resolved.Username == configured.Username || resolved.Password == configured.Password { 103 t.Errorf("%s -> %v == %v\n", reg, resolved, configured) 104 } 105 } 106 } 107 }