common_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 secretstore
  13. import (
  14. "context"
  15. "time"
  16. . "github.com/onsi/ginkgo/v2"
  17. . "github.com/onsi/gomega"
  18. corev1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. )
  23. type testCase struct {
  24. store esapi.GenericStore
  25. assert func()
  26. }
  27. var _ = Describe("SecretStore reconcile", func() {
  28. var test *testCase
  29. BeforeEach(func() {
  30. test = makeDefaultTestcase()
  31. })
  32. AfterEach(func() {
  33. Expect(k8sClient.Delete(context.Background(), test.store)).ToNot(HaveOccurred())
  34. })
  35. // a invalid provider config should be reflected
  36. // in the store status condition
  37. invalidProvider := func(tc *testCase) {
  38. tc.assert = func() {
  39. Eventually(func() bool {
  40. ss := tc.store.Copy()
  41. err := k8sClient.Get(context.Background(), types.NamespacedName{
  42. Name: defaultStoreName,
  43. Namespace: ss.GetObjectMeta().Namespace,
  44. }, ss)
  45. if err != nil {
  46. return false
  47. }
  48. status := ss.GetStatus()
  49. if len(status.Conditions) != 1 {
  50. return false
  51. }
  52. return status.Conditions[0].Reason == esapi.ReasonInvalidProviderConfig &&
  53. hasEvent(tc.store.GetTypeMeta().Kind, ss.GetName(), esapi.ReasonInvalidProviderConfig)
  54. }).
  55. WithTimeout(time.Second * 10).
  56. WithPolling(time.Second).
  57. Should(BeTrue())
  58. }
  59. }
  60. // if controllerClass does not match the controller
  61. // should not touch this store
  62. ignoreControllerClass := func(tc *testCase) {
  63. spc := tc.store.GetSpec()
  64. spc.Controller = "something-else"
  65. tc.assert = func() {
  66. Consistently(func() bool {
  67. ss := tc.store.Copy()
  68. err := k8sClient.Get(context.Background(), types.NamespacedName{
  69. Name: defaultStoreName,
  70. Namespace: ss.GetObjectMeta().Namespace,
  71. }, ss)
  72. if err != nil {
  73. return true
  74. }
  75. return len(ss.GetStatus().Conditions) == 0
  76. }).
  77. WithTimeout(time.Second * 3).
  78. WithPolling(time.Millisecond * 500).
  79. Should(BeTrue())
  80. }
  81. }
  82. validProvider := func(tc *testCase) {
  83. spc := tc.store.GetSpec()
  84. spc.Provider.Vault = nil
  85. spc.Provider.Fake = &esapi.FakeProvider{
  86. Data: []esapi.FakeProviderData{},
  87. }
  88. tc.assert = func() {
  89. Eventually(func() bool {
  90. ss := tc.store.Copy()
  91. err := k8sClient.Get(context.Background(), types.NamespacedName{
  92. Name: defaultStoreName,
  93. Namespace: ss.GetNamespace(),
  94. }, ss)
  95. if err != nil {
  96. return false
  97. }
  98. if len(ss.GetStatus().Conditions) != 1 {
  99. return false
  100. }
  101. return ss.GetStatus().Conditions[0].Reason == esapi.ReasonStoreValid &&
  102. ss.GetStatus().Conditions[0].Type == esapi.SecretStoreReady &&
  103. ss.GetStatus().Conditions[0].Status == corev1.ConditionTrue &&
  104. hasEvent(tc.store.GetTypeMeta().Kind, ss.GetName(), esapi.ReasonStoreValid)
  105. }).
  106. WithTimeout(time.Second * 10).
  107. WithPolling(time.Second).
  108. Should(BeTrue())
  109. }
  110. }
  111. DescribeTable("Controller Reconcile logic", func(muts ...func(tc *testCase)) {
  112. for _, mut := range muts {
  113. mut(test)
  114. }
  115. err := k8sClient.Create(context.Background(), test.store.Copy())
  116. Expect(err).ToNot(HaveOccurred())
  117. test.assert()
  118. },
  119. // namespaced store
  120. Entry("[namespace] invalid provider with secretStore should set InvalidStore condition", invalidProvider),
  121. Entry("[namespace] ignore stores with non-matching class", ignoreControllerClass),
  122. Entry("[namespace] valid provider has status=ready", validProvider),
  123. // cluster store
  124. Entry("[cluster] invalid provider with secretStore should set InvalidStore condition", invalidProvider, useClusterStore),
  125. Entry("[cluster] ignore stores with non-matching class", ignoreControllerClass, useClusterStore),
  126. Entry("[cluster] valid provider has status=ready", validProvider, useClusterStore),
  127. )
  128. })
  129. const (
  130. defaultStoreName = "default-store"
  131. defaultControllerClass = "test-ctrl"
  132. )
  133. func makeDefaultTestcase() *testCase {
  134. return &testCase{
  135. assert: func() {
  136. // this is a noop by default
  137. },
  138. store: &esapi.SecretStore{
  139. TypeMeta: metav1.TypeMeta{
  140. Kind: esapi.SecretStoreKind,
  141. APIVersion: esapi.SecretStoreKindAPIVersion,
  142. },
  143. ObjectMeta: metav1.ObjectMeta{
  144. Name: defaultStoreName,
  145. Namespace: "default",
  146. },
  147. Spec: esapi.SecretStoreSpec{
  148. Controller: defaultControllerClass,
  149. // empty provider
  150. // a testCase mutator must fill in the concrete provider
  151. Provider: &esapi.SecretStoreProvider{
  152. Vault: &esapi.VaultProvider{
  153. Version: esapi.VaultKVStoreV1,
  154. },
  155. },
  156. },
  157. },
  158. }
  159. }
  160. func useClusterStore(tc *testCase) {
  161. spc := tc.store.GetSpec()
  162. meta := tc.store.GetObjectMeta()
  163. tc.store = &esapi.ClusterSecretStore{
  164. TypeMeta: metav1.TypeMeta{
  165. Kind: esapi.ClusterSecretStoreKind,
  166. APIVersion: esapi.ClusterSecretStoreKindAPIVersion,
  167. },
  168. ObjectMeta: metav1.ObjectMeta{
  169. Name: meta.Name,
  170. },
  171. Spec: *spc,
  172. }
  173. }
  174. func hasEvent(involvedKind, name, reason string) bool {
  175. el := &corev1.EventList{}
  176. err := k8sClient.List(context.Background(), el)
  177. if err != nil {
  178. return false
  179. }
  180. for i := range el.Items {
  181. ev := el.Items[i]
  182. if ev.InvolvedObject.Kind == involvedKind && ev.InvolvedObject.Name == name {
  183. if ev.Reason == reason {
  184. return true
  185. }
  186. }
  187. }
  188. return false
  189. }