framework.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://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 framework
  14. import (
  15. // nolint
  16. . "github.com/onsi/ginkgo/v2"
  17. // nolint
  18. . "github.com/onsi/gomega"
  19. api "k8s.io/api/core/v1"
  20. "k8s.io/client-go/kubernetes"
  21. "k8s.io/client-go/rest"
  22. crclient "sigs.k8s.io/controller-runtime/pkg/client"
  23. "github.com/external-secrets/external-secrets-e2e/framework/addon"
  24. "github.com/external-secrets/external-secrets-e2e/framework/log"
  25. "github.com/external-secrets/external-secrets-e2e/framework/util"
  26. )
  27. type Framework struct {
  28. BaseName string
  29. // KubeConfig which was used to create the connection.
  30. KubeConfig *rest.Config
  31. // Kubernetes API clientsets
  32. KubeClientSet kubernetes.Interface
  33. // controller-runtime client for newer controllers
  34. CRClient crclient.Client
  35. // Namespace in which all test resources should reside
  36. Namespace *api.Namespace
  37. Addons []addon.Addon
  38. MakeRemoteRefKey func(base string) string
  39. }
  40. // New returns a new framework instance with defaults.
  41. func New(baseName string) *Framework {
  42. f := &Framework{
  43. BaseName: baseName,
  44. MakeRemoteRefKey: func(base string) string { return base },
  45. }
  46. f.KubeConfig, f.KubeClientSet, f.CRClient = util.NewConfig()
  47. BeforeEach(f.BeforeEach)
  48. AfterEach(f.AfterEach)
  49. return f
  50. }
  51. // BeforeEach creates a namespace.
  52. func (f *Framework) BeforeEach() {
  53. var err error
  54. f.Namespace, err = util.CreateKubeNamespace(f.BaseName, f.KubeClientSet)
  55. log.Logf("created test namespace %s", f.Namespace.Name)
  56. Expect(err).ToNot(HaveOccurred())
  57. }
  58. // AfterEach deletes the namespace and cleans up the registered addons.
  59. func (f *Framework) AfterEach() {
  60. addon.PrintLogs()
  61. for _, a := range f.Addons {
  62. if CurrentSpecReport().Failed() {
  63. err := a.Logs()
  64. Expect(err).ToNot(HaveOccurred())
  65. }
  66. err := a.Uninstall()
  67. Expect(err).ToNot(HaveOccurred())
  68. }
  69. // reset addons to default once the run is done
  70. f.Addons = []addon.Addon{}
  71. log.Logf("deleting test namespace %s", f.Namespace.Name)
  72. err := util.DeleteKubeNamespace(f.Namespace.Name, f.KubeClientSet)
  73. Expect(err).NotTo(HaveOccurred())
  74. }
  75. func (f *Framework) Install(a addon.Addon) {
  76. f.Addons = append(f.Addons, a)
  77. By("installing addon")
  78. err := a.Setup(&addon.Config{
  79. KubeConfig: f.KubeConfig,
  80. KubeClientSet: f.KubeClientSet,
  81. CRClient: f.CRClient,
  82. })
  83. Expect(err).NotTo(HaveOccurred())
  84. err = a.Install()
  85. Expect(err).NotTo(HaveOccurred())
  86. }
  87. // Compose helps define multiple testcases with same/different auth methods.
  88. // This is a factory function that returns a TableEntry.
  89. func Compose(descAppend string, f *Framework, fn func(f *Framework) (string, func(*TestCase)), tweaks ...func(*TestCase)) TableEntry {
  90. // prepend common fn to tweaks
  91. desc, cfn := fn(f)
  92. tweaks = append([]func(*TestCase){cfn}, tweaks...)
  93. // need to convert []func to []any
  94. ifs := make([]any, len(tweaks))
  95. for i := 0; i < len(tweaks); i++ {
  96. ifs[i] = tweaks[i]
  97. }
  98. te := Entry(desc+" "+descAppend, ifs...)
  99. return te
  100. }