testcase.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2020 The cert-manager Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package generator
  14. import (
  15. "context"
  16. "time"
  17. //nolint
  18. . "github.com/onsi/gomega"
  19. // nolint
  20. v1 "k8s.io/api/core/v1"
  21. "github.com/external-secrets/external-secrets-e2e/framework"
  22. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. )
  27. type testCase struct {
  28. Framework *framework.Framework
  29. ExternalSecret *esv1beta1.ExternalSecret
  30. Generator client.Object
  31. AfterSync func(*v1.Secret)
  32. }
  33. var (
  34. generatorName = "myfake"
  35. )
  36. func generatorTableFunc(f *framework.Framework, tweaks ...func(*testCase)) {
  37. tc := &testCase{
  38. Framework: f,
  39. ExternalSecret: &esv1beta1.ExternalSecret{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "e2e-es",
  42. Namespace: f.Namespace.Name,
  43. },
  44. Spec: esv1beta1.ExternalSecretSpec{
  45. RefreshInterval: &metav1.Duration{Duration: time.Second * 5},
  46. Target: esv1beta1.ExternalSecretTarget{
  47. Name: "generated-secret",
  48. },
  49. },
  50. },
  51. }
  52. for _, t := range tweaks {
  53. t(tc)
  54. }
  55. err := f.CRClient.Create(context.Background(), tc.Generator)
  56. Expect(err).ToNot(HaveOccurred())
  57. err = f.CRClient.Create(context.Background(), tc.ExternalSecret)
  58. Expect(err).ToNot(HaveOccurred())
  59. Eventually(func() bool {
  60. var es esv1beta1.ExternalSecret
  61. err = f.CRClient.Get(context.Background(), types.NamespacedName{
  62. Namespace: tc.ExternalSecret.Namespace,
  63. Name: tc.ExternalSecret.Name,
  64. }, &es)
  65. if err != nil {
  66. return false
  67. }
  68. cond := getESCond(es.Status, esv1beta1.ExternalSecretReady)
  69. if cond == nil || cond.Status != v1.ConditionTrue {
  70. return false
  71. }
  72. return true
  73. }).WithTimeout(time.Second * 30).Should(BeTrue())
  74. var secret v1.Secret
  75. err = f.CRClient.Get(context.Background(), types.NamespacedName{
  76. Namespace: tc.ExternalSecret.Namespace,
  77. Name: tc.ExternalSecret.Spec.Target.Name,
  78. }, &secret)
  79. Expect(err).ToNot(HaveOccurred())
  80. tc.AfterSync(&secret)
  81. }
  82. // getESCond returns the condition with the provided type.
  83. func getESCond(status esv1beta1.ExternalSecretStatus, condType esv1beta1.ExternalSecretConditionType) *esv1beta1.ExternalSecretStatusCondition {
  84. for i := range status.Conditions {
  85. c := status.Conditions[i]
  86. if c.Type == condType {
  87. return &c
  88. }
  89. }
  90. return nil
  91. }