testcase.go 2.9 KB

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