suite_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2023.
  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 controller
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "runtime"
  18. "testing"
  19. . "github.com/onsi/ginkgo/v2"
  20. . "github.com/onsi/gomega"
  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. logf "sigs.k8s.io/controller-runtime/pkg/log"
  26. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  27. databasev1 "github.com/iwanhae/nodb/api/v1"
  28. //+kubebuilder:scaffold:imports
  29. )
  30. // These tests use Ginkgo (BDD-style Go testing framework). Refer to
  31. // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
  32. var cfg *rest.Config
  33. var k8sClient client.Client
  34. var testEnv *envtest.Environment
  35. func TestControllers(t *testing.T) {
  36. RegisterFailHandler(Fail)
  37. RunSpecs(t, "Controller Suite")
  38. }
  39. var _ = BeforeSuite(func() {
  40. logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
  41. By("bootstrapping test environment")
  42. testEnv = &envtest.Environment{
  43. CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
  44. ErrorIfCRDPathMissing: true,
  45. // The BinaryAssetsDirectory is only required if you want to run the tests directly
  46. // without call the makefile target test. If not informed it will look for the
  47. // default path defined in controller-runtime which is /usr/local/kubebuilder/.
  48. // Note that you must have the required binaries setup under the bin directory to perform
  49. // the tests directly. When we run make test it will be setup and used automatically.
  50. BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
  51. fmt.Sprintf("1.28.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
  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 = databasev1.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. })
  65. var _ = AfterSuite(func() {
  66. By("tearing down the test environment")
  67. err := testEnv.Stop()
  68. Expect(err).NotTo(HaveOccurred())
  69. })