framework.go 3.3 KB

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