testcase.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. //nolint
  16. . "github.com/onsi/gomega"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  20. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  21. "github.com/external-secrets/external-secrets/e2e/framework/log"
  22. )
  23. var TargetSecretName = "target-secret"
  24. // TestCase contains the test infra to run a table driven test.
  25. type TestCase struct {
  26. Framework *Framework
  27. ExternalSecret *esv1beta1.ExternalSecret
  28. ExternalSecretV1Alpha1 *esv1alpha1.ExternalSecret
  29. Secrets map[string]SecretEntry
  30. ExpectedSecret *v1.Secret
  31. AfterSync func(SecretStoreProvider, *v1.Secret)
  32. }
  33. type SecretEntry struct {
  34. Value string
  35. Tags map[string]string
  36. }
  37. // SecretStoreProvider is a interface that must be implemented
  38. // by a provider that runs the e2e test.
  39. type SecretStoreProvider interface {
  40. CreateSecret(key string, val SecretEntry)
  41. DeleteSecret(key string)
  42. }
  43. // TableFunc returns the main func that runs a TestCase in a table driven test.
  44. func TableFunc(f *Framework, prov SecretStoreProvider) func(...func(*TestCase)) {
  45. return func(tweaks ...func(*TestCase)) {
  46. var err error
  47. // make default test case
  48. // and apply customization to it
  49. tc := makeDefaultTestCase(f)
  50. for _, tweak := range tweaks {
  51. tweak(tc)
  52. }
  53. // create secrets & defer delete
  54. for k, v := range tc.Secrets {
  55. key := k
  56. prov.CreateSecret(key, v)
  57. defer func() {
  58. prov.DeleteSecret(key)
  59. }()
  60. }
  61. // create v1alpha1 external secret, if provided
  62. if tc.ExternalSecretV1Alpha1 != nil {
  63. err = tc.Framework.CRClient.Create(context.Background(), tc.ExternalSecretV1Alpha1)
  64. Expect(err).ToNot(HaveOccurred())
  65. } else {
  66. // create v1beta1 external secret otherwise
  67. err = tc.Framework.CRClient.Create(context.Background(), tc.ExternalSecret)
  68. Expect(err).ToNot(HaveOccurred())
  69. }
  70. // in case target name is empty
  71. if tc.ExternalSecret.Spec.Target.Name == "" {
  72. TargetSecretName = tc.ExternalSecret.ObjectMeta.Name
  73. }
  74. // wait for Kind=Secret to have the expected data
  75. secret, err := tc.Framework.WaitForSecretValue(tc.Framework.Namespace.Name, TargetSecretName, tc.ExpectedSecret)
  76. if err != nil {
  77. log.Logf("Did not match. Expected: %+v, Got: %+v", tc.ExpectedSecret, secret)
  78. }
  79. Expect(err).ToNot(HaveOccurred())
  80. tc.AfterSync(prov, secret)
  81. }
  82. }
  83. func makeDefaultTestCase(f *Framework) *TestCase {
  84. return &TestCase{
  85. AfterSync: func(ssp SecretStoreProvider, s *v1.Secret) {},
  86. Framework: f,
  87. ExternalSecret: &esv1beta1.ExternalSecret{
  88. ObjectMeta: metav1.ObjectMeta{
  89. Name: "e2e-es",
  90. Namespace: f.Namespace.Name,
  91. },
  92. Spec: esv1beta1.ExternalSecretSpec{
  93. SecretStoreRef: esv1beta1.SecretStoreRef{
  94. Name: f.Namespace.Name,
  95. },
  96. Target: esv1beta1.ExternalSecretTarget{
  97. Name: TargetSecretName,
  98. },
  99. },
  100. },
  101. }
  102. }