testcase.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "time"
  16. //nolint
  17. "github.com/external-secrets/external-secrets-e2e/framework/log"
  18. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. )
  26. var TargetSecretName = "target-secret"
  27. // TestCase contains the test infra to run a table driven test.
  28. type TestCase struct {
  29. Framework *Framework
  30. ExternalSecret *esv1.ExternalSecret
  31. PushSecret *esv1alpha1.PushSecret
  32. PushSecretSource *v1.Secret
  33. AdditionalObjects []client.Object
  34. Secrets map[string]SecretEntry
  35. ExpectedSecret *v1.Secret
  36. AfterSync func(SecretStoreProvider, *v1.Secret)
  37. VerifyPushSecretOutcome func(ps *esv1alpha1.PushSecret, pushClient esv1.SecretsClient)
  38. }
  39. type SecretEntry struct {
  40. Value string
  41. Tags map[string]string
  42. }
  43. // SecretStoreProvider is a interface that must be implemented
  44. // by a provider that runs the e2e test.
  45. type SecretStoreProvider interface {
  46. CreateSecret(key string, val SecretEntry)
  47. DeleteSecret(key string)
  48. }
  49. // TableFuncWithExternalSecret returns the main func that runs a TestCase in a table driven test.
  50. func TableFuncWithExternalSecret(f *Framework, prov SecretStoreProvider) func(...func(*TestCase)) {
  51. return func(tweaks ...func(*TestCase)) {
  52. // make default test case
  53. // and apply customization to it
  54. tc := makeDefaultExternalSecretTestCase(f)
  55. for _, tweak := range tweaks {
  56. tweak(tc)
  57. }
  58. // create secrets & defer delete
  59. var deferRemoveKeys []string
  60. for k, v := range tc.Secrets {
  61. key := k
  62. prov.CreateSecret(key, v)
  63. deferRemoveKeys = append(deferRemoveKeys, key)
  64. }
  65. defer func() {
  66. for _, k := range deferRemoveKeys {
  67. prov.DeleteSecret(k)
  68. }
  69. }()
  70. // create v1alpha1 external secret, if provided
  71. createProvidedExternalSecret(tc)
  72. // create additional objects
  73. generateAdditionalObjects(tc)
  74. // in case target name is empty
  75. if tc.ExternalSecret != nil && tc.ExternalSecret.Spec.Target.Name == "" {
  76. TargetSecretName = tc.ExternalSecret.ObjectMeta.Name
  77. }
  78. // wait for Kind=Secret to have the expected data
  79. executeAfterSync(tc, f, prov)
  80. }
  81. }
  82. func executeAfterSync(tc *TestCase, f *Framework, prov SecretStoreProvider) {
  83. if tc.ExpectedSecret != nil {
  84. secret, err := tc.Framework.WaitForSecretValue(tc.Framework.Namespace.Name, TargetSecretName, tc.ExpectedSecret)
  85. if err != nil {
  86. f.printESDebugLogs(tc.ExternalSecret.Name, tc.ExternalSecret.Namespace)
  87. log.Logf("Did not match. Expected: %+v, Got: %+v", tc.ExpectedSecret, secret)
  88. }
  89. Expect(err).ToNot(HaveOccurred())
  90. tc.AfterSync(prov, secret)
  91. } else {
  92. tc.AfterSync(prov, nil)
  93. }
  94. }
  95. func generateAdditionalObjects(tc *TestCase) {
  96. if tc.AdditionalObjects != nil {
  97. for _, obj := range tc.AdditionalObjects {
  98. err := tc.Framework.CRClient.Create(GinkgoT().Context(), obj)
  99. Expect(err).ToNot(HaveOccurred())
  100. }
  101. }
  102. }
  103. func createProvidedExternalSecret(tc *TestCase) {
  104. if tc.ExternalSecret == nil {
  105. return
  106. }
  107. err := tc.Framework.CRClient.Create(GinkgoT().Context(), tc.ExternalSecret)
  108. Expect(err).ToNot(HaveOccurred())
  109. }
  110. // TableFuncWithPushSecret returns the main func that runs a TestCase in a table driven test for push secrets.
  111. func TableFuncWithPushSecret(f *Framework, prov SecretStoreProvider, pushClient esv1.SecretsClient) func(...func(*TestCase)) {
  112. return func(tweaks ...func(*TestCase)) {
  113. var err error
  114. // make default test case
  115. // and apply customization to it
  116. tc := makeDefaultPushSecretTestCase(f)
  117. for _, tweak := range tweaks {
  118. tweak(tc)
  119. }
  120. if tc.PushSecretSource != nil {
  121. err := tc.Framework.CRClient.Create(GinkgoT().Context(), tc.PushSecretSource)
  122. Expect(err).ToNot(HaveOccurred())
  123. }
  124. // create v1alpha1 push secret, if provided
  125. if tc.PushSecret != nil {
  126. // create v1beta1 external secret otherwise
  127. err = tc.Framework.CRClient.Create(GinkgoT().Context(), tc.PushSecret)
  128. Expect(err).ToNot(HaveOccurred())
  129. }
  130. // additional objects
  131. generateAdditionalObjects(tc)
  132. // Run verification on the secret that push secret created or not.
  133. tc.VerifyPushSecretOutcome(tc.PushSecret, pushClient)
  134. }
  135. }
  136. func makeDefaultExternalSecretTestCase(f *Framework) *TestCase {
  137. return &TestCase{
  138. AfterSync: func(ssp SecretStoreProvider, s *v1.Secret) {},
  139. Framework: f,
  140. ExternalSecret: &esv1.ExternalSecret{
  141. ObjectMeta: metav1.ObjectMeta{
  142. Name: "e2e-es",
  143. Namespace: f.Namespace.Name,
  144. },
  145. Spec: esv1.ExternalSecretSpec{
  146. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  147. SecretStoreRef: esv1.SecretStoreRef{
  148. Name: f.Namespace.Name,
  149. },
  150. Target: esv1.ExternalSecretTarget{
  151. Name: TargetSecretName,
  152. },
  153. },
  154. },
  155. }
  156. }
  157. func makeDefaultPushSecretTestCase(f *Framework) *TestCase {
  158. return &TestCase{
  159. Framework: f,
  160. PushSecret: &esv1alpha1.PushSecret{
  161. ObjectMeta: metav1.ObjectMeta{
  162. Name: "e2e-ps",
  163. Namespace: f.Namespace.Name,
  164. },
  165. Spec: esv1alpha1.PushSecretSpec{
  166. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  167. SecretStoreRefs: []esv1alpha1.PushSecretStoreRef{
  168. {
  169. Name: f.Namespace.Name,
  170. },
  171. },
  172. },
  173. },
  174. }
  175. }