secretsink_controller_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 secretsink
  13. import (
  14. "context"
  15. "errors"
  16. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  17. v1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  18. "github.com/external-secrets/external-secrets/pkg/controllers/secretsink/internal/fakes"
  19. "github.com/go-logr/logr"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. ctrl "sigs.k8s.io/controller-runtime"
  25. )
  26. var _ = Describe("secretsink", func() {
  27. var (
  28. reconciler *Reconciler
  29. client *fakes.Client
  30. )
  31. BeforeEach(func() {
  32. client = new(fakes.Client)
  33. reconciler = &Reconciler{client, logr.Discard(), nil, nil, 0, ""}
  34. })
  35. Describe("#Reconcile", func() {
  36. var (
  37. statusWriter *fakes.StatusWriter
  38. )
  39. BeforeEach(func() {
  40. statusWriter = new(fakes.StatusWriter)
  41. client.StatusReturns(statusWriter)
  42. })
  43. It("succeeds", func() {
  44. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  45. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  46. Expect(err).NotTo(HaveOccurred())
  47. Expect(client.GetCallCount()).To(Equal(2))
  48. Expect(client.StatusCallCount()).To(Equal(1))
  49. _, gotNamespacedName, _ := client.GetArgsForCall(0)
  50. Expect(gotNamespacedName).To(Equal(namspacedName))
  51. Expect(statusWriter.PatchCallCount()).To(Equal(1))
  52. _, _, patch, _ := statusWriter.PatchArgsForCall(0)
  53. Expect(patch.Type()).To(Equal(types.MergePatchType))
  54. })
  55. When("an error returns in get", func() {
  56. BeforeEach(func() {
  57. client.GetReturns(errors.New("UnknownError"))
  58. })
  59. It("returns the error", func() {
  60. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  61. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  62. Expect(err).To(MatchError("get resource: UnknownError"))
  63. Expect(client.GetCallCount()).To(Equal(1))
  64. Expect(client.StatusCallCount()).To(Equal(0))
  65. })
  66. })
  67. When("an object is not found", func() {
  68. BeforeEach(func() {
  69. client.GetReturns(statusErrorNotFound{})
  70. })
  71. It("returns an empty result without error", func() {
  72. namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
  73. _, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
  74. Expect(err).NotTo(HaveOccurred())
  75. })
  76. })
  77. })
  78. Describe("#GetSecretSinkCondition", func() {
  79. It("returns nil for empty secret sink status", func() {
  80. secretSinkStatus := new(esapi.SecretSinkStatus)
  81. secretSinkConditionType := new(esapi.SecretSinkConditionType)
  82. Expect(GetSecretSinkCondition(*secretSinkStatus, *secretSinkConditionType)).To(BeNil())
  83. })
  84. It("returns correct condition for secret sink status", func() {
  85. secretSinkStatusCondition := esapi.SecretSinkStatusCondition{Type: esapi.SecretSinkReady}
  86. secretSinkStatus := esapi.SecretSinkStatus{Conditions: []esapi.SecretSinkStatusCondition{secretSinkStatusCondition}}
  87. secretSinkConditionType := esapi.SecretSinkReady
  88. Expect(GetSecretSinkCondition(secretSinkStatus, secretSinkConditionType)).To(Equal(&secretSinkStatusCondition))
  89. })
  90. })
  91. Describe("#SetSecretSinkCondition", func() {
  92. It("appends a condition", func() {
  93. secretSink := esapi.SecretSink{}
  94. secretSinkStatusCondition := esapi.SecretSinkStatusCondition{}
  95. secretSinkStatus := esapi.SecretSinkStatus{Conditions: []esapi.SecretSinkStatusCondition{secretSinkStatusCondition}}
  96. expected := esapi.SecretSink{Status: secretSinkStatus}
  97. Expect(SetSecretSinkCondition(secretSink, secretSinkStatusCondition)).To(Equal(expected))
  98. })
  99. It("changes an existing condition", func() {
  100. conditionStatusTrue := v1.ConditionTrue
  101. secretSinkWithCondition := esapi.SecretSink{Status: esapi.SecretSinkStatus{Conditions: []esapi.SecretSinkStatusCondition{
  102. {
  103. Status: conditionStatusTrue,
  104. Type: esapi.SecretSinkReady,
  105. },
  106. }},
  107. }
  108. secretSinkStatusConditionTrue := esapi.SecretSinkStatusCondition{Status: conditionStatusTrue,
  109. Type: esapi.SecretSinkReady,
  110. Message: "Update status",
  111. }
  112. got := SetSecretSinkCondition(secretSinkWithCondition, secretSinkStatusConditionTrue)
  113. Expect(len(got.Status.Conditions)).To(Equal(1))
  114. Expect(got.Status.Conditions[0]).To(Equal(secretSinkStatusConditionTrue))
  115. })
  116. })
  117. Describe("#GetSecret", func() {
  118. It("returns a secret if it exists", func() {
  119. sink := esapi.SecretSink{
  120. Spec: esapi.SecretSinkSpec{
  121. Selector: esapi.SecretSinkSelector{
  122. Secret: esapi.SecretSinkSecret{
  123. Name: "foo",
  124. },
  125. },
  126. },
  127. }
  128. sink.Namespace = "bar"
  129. _, err := reconciler.GetSecret(context.TODO(), sink)
  130. Expect(err).To(BeNil())
  131. _, name, _ := client.GetArgsForCall(0)
  132. Expect(name.Namespace).To(Equal("bar"))
  133. Expect(name.Name).To(Equal("foo"))
  134. })
  135. It("returns an error if it doesn't exist", func() {
  136. client.GetReturns(errors.New("secret not found"))
  137. _, err := reconciler.GetSecret(context.TODO(), esapi.SecretSink{})
  138. Expect(err).To(HaveOccurred())
  139. })
  140. })
  141. Describe("#GetSecretStore", func() {
  142. sink := esapi.SecretSink{
  143. Spec: esapi.SecretSinkSpec{
  144. SecretStoreRefs: []esapi.SecretSinkStoreRef{
  145. {
  146. Name: "foo",
  147. },
  148. },
  149. },
  150. }
  151. sink.Namespace = "bar"
  152. clusterSink := esapi.SecretSink{
  153. Spec: esapi.SecretSinkSpec{
  154. SecretStoreRefs: []esapi.SecretSinkStoreRef{
  155. {
  156. Name: "foo",
  157. Kind: "ClusterSecretStore",
  158. },
  159. },
  160. },
  161. }
  162. It("returns a secretstore if it exists", func() {
  163. _, err := reconciler.GetSecretStore(context.TODO(), sink)
  164. Expect(err).To(BeNil())
  165. Expect(client.GetCallCount()).To(Equal(1))
  166. _, name, store := client.GetArgsForCall(0)
  167. Expect(name.Namespace).To(Equal("bar"))
  168. Expect(name.Name).To(Equal("foo"))
  169. Expect(store).To(BeAssignableToTypeOf(&v1beta1.SecretStore{}))
  170. })
  171. It("returns an error if it doesn't exist", func() {
  172. client.GetReturns(errors.New("secretstore not found"))
  173. _, err := reconciler.GetSecretStore(context.TODO(), sink)
  174. Expect(err).To(HaveOccurred())
  175. })
  176. It("returns a clustersecretstore if it exists", func() {
  177. _, err := reconciler.GetSecretStore(context.TODO(), clusterSink)
  178. Expect(err).To(BeNil())
  179. Expect(client.GetCallCount()).To(Equal(1))
  180. _, name, store := client.GetArgsForCall(0)
  181. Expect(store).To(BeAssignableToTypeOf(&v1beta1.ClusterSecretStore{}))
  182. Expect(name.Name).To(Equal("foo"))
  183. })
  184. })
  185. })