testcase.go 2.8 KB

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