testcase.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. )
  21. const TargetSecretName = "target-secret"
  22. // TestCase contains the test infra to run a table driven test.
  23. type TestCase struct {
  24. Framework *Framework
  25. ExternalSecret *esv1alpha1.ExternalSecret
  26. Secrets map[string]string
  27. ExpectedSecret *v1.Secret
  28. }
  29. // SecretStoreProvider is a interface that must be implemented
  30. // by a provider that runs the e2e test.
  31. type SecretStoreProvider interface {
  32. CreateSecret(key string, val string)
  33. DeleteSecret(key string)
  34. }
  35. // TableFunc returns the main func that runs a TestCase in a table driven test.
  36. func TableFunc(f *Framework, prov SecretStoreProvider) func(...func(*TestCase)) {
  37. return func(tweaks ...func(*TestCase)) {
  38. var err error
  39. // make default test case
  40. // and apply customization to it
  41. tc := makeDefaultTestCase(f)
  42. for _, tweak := range tweaks {
  43. tweak(tc)
  44. }
  45. // create secrets & defer delete
  46. for k, v := range tc.Secrets {
  47. key := k
  48. prov.CreateSecret(key, v)
  49. defer func() {
  50. prov.DeleteSecret(key)
  51. }()
  52. }
  53. // create external secret
  54. err = tc.Framework.CRClient.Create(context.Background(), tc.ExternalSecret)
  55. Expect(err).ToNot(HaveOccurred())
  56. // wait for Kind=Secret to have the expected data
  57. _, err = tc.Framework.WaitForSecretValue(tc.Framework.Namespace.Name, TargetSecretName, tc.ExpectedSecret)
  58. Expect(err).ToNot(HaveOccurred())
  59. }
  60. }
  61. func makeDefaultTestCase(f *Framework) *TestCase {
  62. return &TestCase{
  63. Framework: f,
  64. ExternalSecret: &esv1alpha1.ExternalSecret{
  65. ObjectMeta: metav1.ObjectMeta{
  66. Name: "e2e-es",
  67. Namespace: f.Namespace.Name,
  68. },
  69. Spec: esv1alpha1.ExternalSecretSpec{
  70. SecretStoreRef: esv1alpha1.SecretStoreRef{
  71. Name: f.Namespace.Name,
  72. },
  73. Target: esv1alpha1.ExternalSecretTarget{
  74. Name: TargetSecretName,
  75. },
  76. },
  77. },
  78. }
  79. }