file_store_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package credentials
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/docker/docker/cliconfig"
  6. "github.com/docker/docker/cliconfig/configfile"
  7. "github.com/docker/engine-api/types"
  8. )
  9. func newConfigFile(auths map[string]types.AuthConfig) *configfile.ConfigFile {
  10. tmp, _ := ioutil.TempFile("", "docker-test")
  11. name := tmp.Name()
  12. tmp.Close()
  13. c := cliconfig.NewConfigFile(name)
  14. c.AuthConfigs = auths
  15. return c
  16. }
  17. func TestFileStoreAddCredentials(t *testing.T) {
  18. f := newConfigFile(make(map[string]types.AuthConfig))
  19. s := NewFileStore(f)
  20. err := s.Store(types.AuthConfig{
  21. Auth: "super_secret_token",
  22. Email: "foo@example.com",
  23. ServerAddress: "https://example.com",
  24. })
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if len(f.AuthConfigs) != 1 {
  29. t.Fatalf("expected 1 auth config, got %d", len(f.AuthConfigs))
  30. }
  31. a, ok := f.AuthConfigs["https://example.com"]
  32. if !ok {
  33. t.Fatalf("expected auth for https://example.com, got %v", f.AuthConfigs)
  34. }
  35. if a.Auth != "super_secret_token" {
  36. t.Fatalf("expected auth `super_secret_token`, got %s", a.Auth)
  37. }
  38. if a.Email != "foo@example.com" {
  39. t.Fatalf("expected email `foo@example.com`, got %s", a.Email)
  40. }
  41. }
  42. func TestFileStoreGet(t *testing.T) {
  43. f := newConfigFile(map[string]types.AuthConfig{
  44. "https://example.com": {
  45. Auth: "super_secret_token",
  46. Email: "foo@example.com",
  47. ServerAddress: "https://example.com",
  48. },
  49. })
  50. s := NewFileStore(f)
  51. a, err := s.Get("https://example.com")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if a.Auth != "super_secret_token" {
  56. t.Fatalf("expected auth `super_secret_token`, got %s", a.Auth)
  57. }
  58. if a.Email != "foo@example.com" {
  59. t.Fatalf("expected email `foo@example.com`, got %s", a.Email)
  60. }
  61. }
  62. func TestFileStoreGetAll(t *testing.T) {
  63. s1 := "https://example.com"
  64. s2 := "https://example2.com"
  65. f := newConfigFile(map[string]types.AuthConfig{
  66. s1: {
  67. Auth: "super_secret_token",
  68. Email: "foo@example.com",
  69. ServerAddress: "https://example.com",
  70. },
  71. s2: {
  72. Auth: "super_secret_token2",
  73. Email: "foo@example2.com",
  74. ServerAddress: "https://example2.com",
  75. },
  76. })
  77. s := NewFileStore(f)
  78. as, err := s.GetAll()
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if len(as) != 2 {
  83. t.Fatalf("wanted 2, got %d", len(as))
  84. }
  85. if as[s1].Auth != "super_secret_token" {
  86. t.Fatalf("expected auth `super_secret_token`, got %s", as[s1].Auth)
  87. }
  88. if as[s1].Email != "foo@example.com" {
  89. t.Fatalf("expected email `foo@example.com`, got %s", as[s1].Email)
  90. }
  91. if as[s2].Auth != "super_secret_token2" {
  92. t.Fatalf("expected auth `super_secret_token2`, got %s", as[s2].Auth)
  93. }
  94. if as[s2].Email != "foo@example2.com" {
  95. t.Fatalf("expected email `foo@example2.com`, got %s", as[s2].Email)
  96. }
  97. }
  98. func TestFileStoreErase(t *testing.T) {
  99. f := newConfigFile(map[string]types.AuthConfig{
  100. "https://example.com": {
  101. Auth: "super_secret_token",
  102. Email: "foo@example.com",
  103. ServerAddress: "https://example.com",
  104. },
  105. })
  106. s := NewFileStore(f)
  107. err := s.Erase("https://example.com")
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. // file store never returns errors, check that the auth config is empty
  112. a, err := s.Get("https://example.com")
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if a.Auth != "" {
  117. t.Fatalf("expected empty auth token, got %s", a.Auth)
  118. }
  119. if a.Email != "" {
  120. t.Fatalf("expected empty email, got %s", a.Email)
  121. }
  122. }