framework.go 3.7 KB

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