framework.go 3.5 KB

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