auth_test.go 3.0 KB

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