framework.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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"
  16. // nolint
  17. . "github.com/onsi/gomega"
  18. // nolint
  19. . "github.com/onsi/ginkgo/extensions/table"
  20. api "k8s.io/api/core/v1"
  21. "k8s.io/client-go/kubernetes"
  22. kscheme "k8s.io/client-go/kubernetes/scheme"
  23. "k8s.io/client-go/rest"
  24. crclient "sigs.k8s.io/controller-runtime/pkg/client"
  25. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  26. "github.com/external-secrets/external-secrets/e2e/framework/addon"
  27. "github.com/external-secrets/external-secrets/e2e/framework/util"
  28. )
  29. func init() {
  30. _ = kscheme.AddToScheme(util.Scheme)
  31. _ = esv1alpha1.AddToScheme(util.Scheme)
  32. }
  33. type Framework struct {
  34. BaseName string
  35. // KubeConfig which was used to create the connection.
  36. KubeConfig *rest.Config
  37. // Kubernetes API clientsets
  38. KubeClientSet kubernetes.Interface
  39. // controller-runtime client for newer controllers
  40. CRClient crclient.Client
  41. // Namespace in which all test resources should reside
  42. Namespace *api.Namespace
  43. Addons []addon.Addon
  44. }
  45. // New returns a new framework instance with defaults.
  46. func New(baseName string) *Framework {
  47. f := &Framework{
  48. BaseName: baseName,
  49. }
  50. f.KubeConfig, f.KubeClientSet, f.CRClient = util.NewConfig()
  51. BeforeEach(f.BeforeEach)
  52. AfterEach(f.AfterEach)
  53. return f
  54. }
  55. // BeforeEach creates a namespace.
  56. func (f *Framework) BeforeEach() {
  57. var err error
  58. By("Building a namespace api object")
  59. f.Namespace, err = util.CreateKubeNamespace(f.BaseName, f.KubeClientSet)
  60. Expect(err).NotTo(HaveOccurred())
  61. By("Using the namespace " + f.Namespace.Name)
  62. }
  63. // AfterEach deletes the namespace and cleans up the registered addons.
  64. func (f *Framework) AfterEach() {
  65. for _, a := range f.Addons {
  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. By("deleting test namespace")
  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. func Compose(descAppend string, f *Framework, fn func(f *Framework) (string, func(*TestCase)), tweaks ...func(*TestCase)) TableEntry {
  89. desc, tfn := fn(f)
  90. tweaks = append(tweaks, tfn)
  91. te := Entry(desc + " " + descAppend)
  92. // need to convert []func to []interface{}
  93. ifs := make([]interface{}, len(tweaks))
  94. for i := 0; i < len(tweaks); i++ {
  95. ifs[i] = tweaks[i]
  96. }
  97. te.Parameters = ifs
  98. return te
  99. }