suite_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2022.
  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. http://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 controllers
  14. import (
  15. "path/filepath"
  16. "testing"
  17. . "github.com/onsi/ginkgo"
  18. . "github.com/onsi/gomega"
  19. "k8s.io/client-go/kubernetes/scheme"
  20. "k8s.io/client-go/rest"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. "sigs.k8s.io/controller-runtime/pkg/envtest"
  23. "sigs.k8s.io/controller-runtime/pkg/envtest/printer"
  24. logf "sigs.k8s.io/controller-runtime/pkg/log"
  25. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  26. appv1 "github.com/kakao/bluegreen/api/v1"
  27. //+kubebuilder:scaffold:imports
  28. )
  29. // These tests use Ginkgo (BDD-style Go testing framework). Refer to
  30. // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
  31. var cfg *rest.Config
  32. var k8sClient client.Client
  33. var testEnv *envtest.Environment
  34. func TestAPIs(t *testing.T) {
  35. RegisterFailHandler(Fail)
  36. RunSpecsWithDefaultAndCustomReporters(t,
  37. "Controller Suite",
  38. []Reporter{printer.NewlineReporter{}})
  39. }
  40. var _ = BeforeSuite(func() {
  41. logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
  42. By("bootstrapping test environment")
  43. testEnv = &envtest.Environment{
  44. CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
  45. ErrorIfCRDPathMissing: true,
  46. }
  47. var err error
  48. // cfg is defined in this file globally.
  49. cfg, err = testEnv.Start()
  50. Expect(err).NotTo(HaveOccurred())
  51. Expect(cfg).NotTo(BeNil())
  52. err = appv1.AddToScheme(scheme.Scheme)
  53. Expect(err).NotTo(HaveOccurred())
  54. //+kubebuilder:scaffold:scheme
  55. k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
  56. Expect(err).NotTo(HaveOccurred())
  57. Expect(k8sClient).NotTo(BeNil())
  58. }, 60)
  59. var _ = AfterSuite(func() {
  60. By("tearing down the test environment")
  61. err := testEnv.Stop()
  62. Expect(err).NotTo(HaveOccurred())
  63. })