suite_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "context"
  16. "path/filepath"
  17. "testing"
  18. . "github.com/onsi/ginkgo"
  19. . "github.com/onsi/gomega"
  20. ctrl "sigs.k8s.io/controller-runtime"
  21. "k8s.io/client-go/kubernetes/scheme"
  22. "k8s.io/client-go/rest"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. "sigs.k8s.io/controller-runtime/pkg/envtest"
  25. "sigs.k8s.io/controller-runtime/pkg/envtest/printer"
  26. logf "sigs.k8s.io/controller-runtime/pkg/log"
  27. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  28. appv1 "github.com/kakao/bluegreen/api/v1"
  29. "github.com/kakao/bluegreen/controllers/bluegreen"
  30. //+kubebuilder:scaffold:imports
  31. )
  32. // These tests use Ginkgo (BDD-style Go testing framework). Refer to
  33. // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
  34. var cfg *rest.Config
  35. var k8sClient client.Client
  36. var testEnv *envtest.Environment
  37. var ctx context.Context
  38. var cancel context.CancelFunc
  39. func TestAPIs(t *testing.T) {
  40. RegisterFailHandler(Fail)
  41. RunSpecsWithDefaultAndCustomReporters(t,
  42. "Controller Suite",
  43. []Reporter{printer.NewlineReporter{}})
  44. }
  45. var _ = BeforeSuite(func() {
  46. logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
  47. ctx, cancel = context.WithCancel(context.TODO())
  48. By("bootstrapping test environment")
  49. testEnv = &envtest.Environment{
  50. CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
  51. ErrorIfCRDPathMissing: true,
  52. }
  53. var err error
  54. // cfg is defined in this file globally.
  55. cfg, err = testEnv.Start()
  56. Expect(err).NotTo(HaveOccurred())
  57. Expect(cfg).NotTo(BeNil())
  58. err = appv1.AddToScheme(scheme.Scheme)
  59. Expect(err).NotTo(HaveOccurred())
  60. //+kubebuilder:scaffold:scheme
  61. k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
  62. Expect(err).NotTo(HaveOccurred())
  63. Expect(k8sClient).NotTo(BeNil())
  64. k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
  65. Scheme: scheme.Scheme,
  66. })
  67. Expect(err).ToNot(HaveOccurred())
  68. err = (&BlueGreenReconciler{
  69. Client: &bluegreen.BlueGreenClient{
  70. Client: k8sManager.GetClient(),
  71. },
  72. Scheme: k8sManager.GetScheme(),
  73. }).SetupWithManager(k8sManager)
  74. Expect(err).ToNot(HaveOccurred())
  75. go func() {
  76. defer GinkgoRecover()
  77. err = k8sManager.Start(ctx)
  78. Expect(err).ToNot(HaveOccurred(), "failed to run manager")
  79. }()
  80. }, 60)
  81. var _ = AfterSuite(func() {
  82. cancel()
  83. By("tearing down the test environment")
  84. err := testEnv.Stop()
  85. Expect(err).NotTo(HaveOccurred())
  86. })