github.com/gondor/docker@v1.9.0-rc1/registry/auth_test.go (about) 1 package registry 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/docker/docker/cliconfig" 10 ) 11 12 func TestEncodeAuth(t *testing.T) { 13 newAuthConfig := &cliconfig.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"} 14 authStr := cliconfig.EncodeAuth(newAuthConfig) 15 decAuthConfig := &cliconfig.AuthConfig{} 16 var err error 17 decAuthConfig.Username, decAuthConfig.Password, err = cliconfig.DecodeAuth(authStr) 18 if err != nil { 19 t.Fatal(err) 20 } 21 if newAuthConfig.Username != decAuthConfig.Username { 22 t.Fatal("Encode Username doesn't match decoded Username") 23 } 24 if newAuthConfig.Password != decAuthConfig.Password { 25 t.Fatal("Encode Password doesn't match decoded Password") 26 } 27 if authStr != "a2VuOnRlc3Q=" { 28 t.Fatal("AuthString encoding isn't correct.") 29 } 30 } 31 32 func setupTempConfigFile() (*cliconfig.ConfigFile, error) { 33 root, err := ioutil.TempDir("", "docker-test-auth") 34 if err != nil { 35 return nil, err 36 } 37 root = filepath.Join(root, cliconfig.ConfigFileName) 38 configFile := cliconfig.NewConfigFile(root) 39 40 for _, registry := range []string{"testIndex", IndexServer} { 41 configFile.AuthConfigs[registry] = cliconfig.AuthConfig{ 42 Username: "docker-user", 43 Password: "docker-pass", 44 Email: "docker@docker.io", 45 } 46 } 47 48 return configFile, nil 49 } 50 51 func TestSameAuthDataPostSave(t *testing.T) { 52 configFile, err := setupTempConfigFile() 53 if err != nil { 54 t.Fatal(err) 55 } 56 defer os.RemoveAll(configFile.Filename()) 57 58 err = configFile.Save() 59 if err != nil { 60 t.Fatal(err) 61 } 62 63 authConfig := configFile.AuthConfigs["testIndex"] 64 if authConfig.Username != "docker-user" { 65 t.Fail() 66 } 67 if authConfig.Password != "docker-pass" { 68 t.Fail() 69 } 70 if authConfig.Email != "docker@docker.io" { 71 t.Fail() 72 } 73 if authConfig.Auth != "" { 74 t.Fail() 75 } 76 } 77 78 func TestResolveAuthConfigIndexServer(t *testing.T) { 79 configFile, err := setupTempConfigFile() 80 if err != nil { 81 t.Fatal(err) 82 } 83 defer os.RemoveAll(configFile.Filename()) 84 85 indexConfig := configFile.AuthConfigs[IndexServer] 86 87 officialIndex := &IndexInfo{ 88 Official: true, 89 } 90 privateIndex := &IndexInfo{ 91 Official: false, 92 } 93 94 resolved := ResolveAuthConfig(configFile, officialIndex) 95 assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer") 96 97 resolved = ResolveAuthConfig(configFile, privateIndex) 98 assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer") 99 } 100 101 func TestResolveAuthConfigFullURL(t *testing.T) { 102 configFile, err := setupTempConfigFile() 103 if err != nil { 104 t.Fatal(err) 105 } 106 defer os.RemoveAll(configFile.Filename()) 107 108 registryAuth := cliconfig.AuthConfig{ 109 Username: "foo-user", 110 Password: "foo-pass", 111 Email: "foo@example.com", 112 } 113 localAuth := cliconfig.AuthConfig{ 114 Username: "bar-user", 115 Password: "bar-pass", 116 Email: "bar@example.com", 117 } 118 officialAuth := cliconfig.AuthConfig{ 119 Username: "baz-user", 120 Password: "baz-pass", 121 Email: "baz@example.com", 122 } 123 configFile.AuthConfigs[IndexServer] = officialAuth 124 125 expectedAuths := map[string]cliconfig.AuthConfig{ 126 "registry.example.com": registryAuth, 127 "localhost:8000": localAuth, 128 "registry.com": localAuth, 129 } 130 131 validRegistries := map[string][]string{ 132 "registry.example.com": { 133 "https://registry.example.com/v1/", 134 "http://registry.example.com/v1/", 135 "registry.example.com", 136 "registry.example.com/v1/", 137 }, 138 "localhost:8000": { 139 "https://localhost:8000/v1/", 140 "http://localhost:8000/v1/", 141 "localhost:8000", 142 "localhost:8000/v1/", 143 }, 144 "registry.com": { 145 "https://registry.com/v1/", 146 "http://registry.com/v1/", 147 "registry.com", 148 "registry.com/v1/", 149 }, 150 } 151 152 for configKey, registries := range validRegistries { 153 configured, ok := expectedAuths[configKey] 154 if !ok || configured.Email == "" { 155 t.Fail() 156 } 157 index := &IndexInfo{ 158 Name: configKey, 159 } 160 for _, registry := range registries { 161 configFile.AuthConfigs[registry] = configured 162 resolved := ResolveAuthConfig(configFile, index) 163 if resolved.Email != configured.Email { 164 t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email) 165 } 166 delete(configFile.AuthConfigs, registry) 167 resolved = ResolveAuthConfig(configFile, index) 168 if resolved.Email == configured.Email { 169 t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email) 170 } 171 } 172 } 173 }