testcase.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 framework
  13. import (
  14. "context"
  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/gomega"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. )
  25. var TargetSecretName = "target-secret"
  26. // TestCase contains the test infra to run a table driven test.
  27. type TestCase struct {
  28. Framework *Framework
  29. ExternalSecret *esv1.ExternalSecret
  30. PushSecret *esv1alpha1.PushSecret
  31. PushSecretSource *v1.Secret
  32. AdditionalObjects []client.Object
  33. Secrets map[string]SecretEntry
  34. ExpectedSecret *v1.Secret
  35. AfterSync func(SecretStoreProvider, *v1.Secret)
  36. VerifyPushSecretOutcome func(ps *esv1alpha1.PushSecret, pushClient esv1.SecretsClient)
  37. }
  38. type SecretEntry struct {
  39. Value string
  40. Tags map[string]string
  41. }
  42. // SecretStoreProvider is a interface that must be implemented
  43. // by a provider that runs the e2e test.
  44. type SecretStoreProvider interface {
  45. CreateSecret(key string, val SecretEntry)
  46. DeleteSecret(key string)
  47. }
  48. // TableFuncWithExternalSecret returns the main func that runs a TestCase in a table driven test.
  49. func TableFuncWithExternalSecret(f *Framework, prov SecretStoreProvider) func(...func(*TestCase)) {
  50. return func(tweaks ...func(*TestCase)) {
  51. // make default test case
  52. // and apply customization to it
  53. tc := makeDefaultExternalSecretTestCase(f)
  54. for _, tweak := range tweaks {
  55. tweak(tc)
  56. }
  57. // create secrets & defer delete
  58. var deferRemoveKeys []string
  59. for k, v := range tc.Secrets {
  60. key := k
  61. prov.CreateSecret(key, v)
  62. deferRemoveKeys = append(deferRemoveKeys, key)
  63. }
  64. defer func() {
  65. for _, k := range deferRemoveKeys {
  66. prov.DeleteSecret(k)
  67. }
  68. }()
  69. // create v1alpha1 external secret, if provided
  70. createProvidedExternalSecret(tc)
  71. // create additional objects
  72. generateAdditionalObjects(tc)
  73. // in case target name is empty
  74. if tc.ExternalSecret != nil && tc.ExternalSecret.Spec.Target.Name == "" {
  75. TargetSecretName = tc.ExternalSecret.ObjectMeta.Name
  76. }
  77. // wait for Kind=Secret to have the expected data
  78. executeAfterSync(tc, f, prov)
  79. }
  80. }
  81. func executeAfterSync(tc *TestCase, f *Framework, prov SecretStoreProvider) {
  82. if tc.ExpectedSecret != nil {
  83. secret, err := tc.Framework.WaitForSecretValue(tc.Framework.Namespace.Name, TargetSecretName, tc.ExpectedSecret)
  84. if err != nil {
  85. f.printESDebugLogs(tc.ExternalSecret.Name, tc.ExternalSecret.Namespace)
  86. log.Logf("Did not match. Expected: %+v, Got: %+v", tc.ExpectedSecret, secret)
  87. }
  88. Expect(err).ToNot(HaveOccurred())
  89. tc.AfterSync(prov, secret)
  90. } else {
  91. tc.AfterSync(prov, nil)
  92. }
  93. }
  94. func generateAdditionalObjects(tc *TestCase) {
  95. if tc.AdditionalObjects != nil {
  96. for _, obj := range tc.AdditionalObjects {
  97. err := tc.Framework.CRClient.Create(context.Background(), obj)
  98. Expect(err).ToNot(HaveOccurred())
  99. }
  100. }
  101. }
  102. func createProvidedExternalSecret(tc *TestCase) {
  103. if tc.ExternalSecret == nil {
  104. return
  105. }
  106. err := tc.Framework.CRClient.Create(context.Background(), tc.ExternalSecret)
  107. Expect(err).ToNot(HaveOccurred())
  108. }
  109. // TableFuncWithPushSecret returns the main func that runs a TestCase in a table driven test for push secrets.
  110. func TableFuncWithPushSecret(f *Framework, prov SecretStoreProvider, pushClient esv1.SecretsClient) func(...func(*TestCase)) {
  111. return func(tweaks ...func(*TestCase)) {
  112. var err error
  113. // make default test case
  114. // and apply customization to it
  115. tc := makeDefaultPushSecretTestCase(f)
  116. for _, tweak := range tweaks {
  117. tweak(tc)
  118. }
  119. if tc.PushSecretSource != nil {
  120. err := tc.Framework.CRClient.Create(context.Background(), tc.PushSecretSource)
  121. Expect(err).ToNot(HaveOccurred())
  122. }
  123. // create v1alpha1 push secret, if provided
  124. if tc.PushSecret != nil {
  125. // create v1beta1 external secret otherwise
  126. err = tc.Framework.CRClient.Create(context.Background(), tc.PushSecret)
  127. Expect(err).ToNot(HaveOccurred())
  128. }
  129. // additional objects
  130. generateAdditionalObjects(tc)
  131. // Run verification on the secret that push secret created or not.
  132. tc.VerifyPushSecretOutcome(tc.PushSecret, pushClient)
  133. }
  134. }
  135. func makeDefaultExternalSecretTestCase(f *Framework) *TestCase {
  136. return &TestCase{
  137. AfterSync: func(ssp SecretStoreProvider, s *v1.Secret) {},
  138. Framework: f,
  139. ExternalSecret: &esv1.ExternalSecret{
  140. ObjectMeta: metav1.ObjectMeta{
  141. Name: "e2e-es",
  142. Namespace: f.Namespace.Name,
  143. },
  144. Spec: esv1.ExternalSecretSpec{
  145. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  146. SecretStoreRef: esv1.SecretStoreRef{
  147. Name: f.Namespace.Name,
  148. },
  149. Target: esv1.ExternalSecretTarget{
  150. Name: TargetSecretName,
  151. },
  152. },
  153. },
  154. }
  155. }
  156. func makeDefaultPushSecretTestCase(f *Framework) *TestCase {
  157. return &TestCase{
  158. Framework: f,
  159. PushSecret: &esv1alpha1.PushSecret{
  160. ObjectMeta: metav1.ObjectMeta{
  161. Name: "e2e-ps",
  162. Namespace: f.Namespace.Name,
  163. },
  164. Spec: esv1alpha1.PushSecretSpec{
  165. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  166. SecretStoreRefs: []esv1alpha1.PushSecretStoreRef{
  167. {
  168. Name: f.Namespace.Name,
  169. },
  170. },
  171. },
  172. },
  173. }
  174. }