fake_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "context"
  15. "testing"
  16. "github.com/onsi/gomega"
  17. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  18. )
  19. func TestNewClient(t *testing.T) {
  20. p := &Provider{}
  21. gomega.RegisterTestingT(t)
  22. // nil store
  23. _, err := p.NewClient(context.Background(), nil, nil, "")
  24. gomega.Expect(err).To(gomega.HaveOccurred())
  25. // missing provider
  26. _, err = p.NewClient(context.Background(), &esv1beta1.SecretStore{}, nil, "")
  27. gomega.Expect(err).To(gomega.HaveOccurred())
  28. }
  29. func TestClose(t *testing.T) {
  30. p := &Provider{}
  31. gomega.RegisterTestingT(t)
  32. err := p.Close(context.TODO())
  33. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  34. }
  35. type testCase struct {
  36. name string
  37. input []esv1beta1.FakeProviderData
  38. request esv1beta1.ExternalSecretDataRemoteRef
  39. expValue string
  40. expErr string
  41. }
  42. func TestGetSecret(t *testing.T) {
  43. gomega.RegisterTestingT(t)
  44. p := &Provider{}
  45. tbl := []testCase{
  46. {
  47. name: "return err when not found",
  48. input: []esv1beta1.FakeProviderData{},
  49. request: esv1beta1.ExternalSecretDataRemoteRef{
  50. Key: "/foo",
  51. Version: "v2",
  52. },
  53. expErr: "secret value not found",
  54. },
  55. {
  56. name: "get correct value from multiple versions",
  57. input: []esv1beta1.FakeProviderData{
  58. {
  59. Key: "/foo",
  60. Value: "bar2",
  61. Version: "v2",
  62. },
  63. {
  64. Key: "junk",
  65. Value: "xxxxx",
  66. },
  67. {
  68. Key: "/foo",
  69. Value: "bar1",
  70. Version: "v1",
  71. },
  72. },
  73. request: esv1beta1.ExternalSecretDataRemoteRef{
  74. Key: "/foo",
  75. Version: "v2",
  76. },
  77. expValue: "bar2",
  78. },
  79. }
  80. for _, row := range tbl {
  81. t.Run(row.name, func(t *testing.T) {
  82. cl, err := p.NewClient(context.Background(), &esv1beta1.SecretStore{
  83. Spec: esv1beta1.SecretStoreSpec{
  84. Provider: &esv1beta1.SecretStoreProvider{
  85. Fake: &esv1beta1.FakeProvider{
  86. Data: row.input,
  87. },
  88. },
  89. },
  90. }, nil, "")
  91. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  92. out, err := cl.GetSecret(context.Background(), row.request)
  93. if row.expErr != "" {
  94. gomega.Expect(err).To(gomega.MatchError(row.expErr))
  95. } else {
  96. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  97. }
  98. gomega.Expect(string(out)).To(gomega.Equal(row.expValue))
  99. })
  100. }
  101. }
  102. type testMapCase struct {
  103. name string
  104. input []esv1beta1.FakeProviderData
  105. request esv1beta1.ExternalSecretDataRemoteRef
  106. expValue map[string][]byte
  107. expErr string
  108. }
  109. func TestGetSecretMap(t *testing.T) {
  110. gomega.RegisterTestingT(t)
  111. p := &Provider{}
  112. tbl := []testMapCase{
  113. {
  114. name: "return err when not found",
  115. input: []esv1beta1.FakeProviderData{},
  116. request: esv1beta1.ExternalSecretDataRemoteRef{
  117. Key: "/foo",
  118. Version: "v2",
  119. },
  120. expErr: "secret value not found",
  121. },
  122. {
  123. name: "get correct value from multiple versions",
  124. input: []esv1beta1.FakeProviderData{
  125. {
  126. Key: "junk",
  127. ValueMap: map[string]string{
  128. "junk": "ok",
  129. },
  130. },
  131. {
  132. Key: "/foo",
  133. ValueMap: map[string]string{
  134. "foo": "bar",
  135. "baz": "bang",
  136. },
  137. Version: "v1",
  138. },
  139. {
  140. Key: "/foo",
  141. ValueMap: map[string]string{
  142. "foo": "bar",
  143. "baz": "bang",
  144. },
  145. Version: "v2",
  146. },
  147. },
  148. request: esv1beta1.ExternalSecretDataRemoteRef{
  149. Key: "/foo",
  150. Version: "v2",
  151. },
  152. expValue: map[string][]byte{
  153. "foo": []byte("bar"),
  154. "baz": []byte("bang"),
  155. },
  156. },
  157. }
  158. for _, row := range tbl {
  159. t.Run(row.name, func(t *testing.T) {
  160. cl, err := p.NewClient(context.Background(), &esv1beta1.SecretStore{
  161. Spec: esv1beta1.SecretStoreSpec{
  162. Provider: &esv1beta1.SecretStoreProvider{
  163. Fake: &esv1beta1.FakeProvider{
  164. Data: row.input,
  165. },
  166. },
  167. },
  168. }, nil, "")
  169. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  170. out, err := cl.GetSecretMap(context.Background(), row.request)
  171. if row.expErr != "" {
  172. gomega.Expect(err).To(gomega.MatchError(row.expErr))
  173. } else {
  174. gomega.Expect(err).ToNot(gomega.HaveOccurred())
  175. }
  176. gomega.Expect(out).To(gomega.Equal(row.expValue))
  177. })
  178. }
  179. }