auth_test.go 3.0 KB

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