suite_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2026 LocoStack.
  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. "context"
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "time"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. "k8s.io/client-go/kubernetes/scheme"
  23. "k8s.io/client-go/rest"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. "sigs.k8s.io/controller-runtime/pkg/envtest"
  26. logf "sigs.k8s.io/controller-runtime/pkg/log"
  27. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  28. locostackcomv1alpha1 "github.com/LocoStack/loco-operator/api/v1alpha1"
  29. // +kubebuilder:scaffold:imports
  30. )
  31. // These tests use Ginkgo (BDD-style Go testing framework). Refer to
  32. // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
  33. var (
  34. ctx context.Context
  35. cancel context.CancelFunc
  36. testEnv *envtest.Environment
  37. cfg *rest.Config
  38. k8sClient client.Client
  39. )
  40. func TestControllers(t *testing.T) {
  41. RegisterFailHandler(Fail)
  42. RunSpecs(t, "Controller Suite")
  43. }
  44. var _ = BeforeSuite(func() {
  45. logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
  46. ctx, cancel = context.WithCancel(context.TODO())
  47. var err error
  48. err = locostackcomv1alpha1.AddToScheme(scheme.Scheme)
  49. Expect(err).NotTo(HaveOccurred())
  50. // +kubebuilder:scaffold:scheme
  51. By("bootstrapping test environment")
  52. testEnv = &envtest.Environment{
  53. CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
  54. ErrorIfCRDPathMissing: true,
  55. }
  56. // Retrieve the first found binary directory to allow running tests from IDEs
  57. if getFirstFoundEnvTestBinaryDir() != "" {
  58. testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
  59. }
  60. // cfg is defined in this file globally.
  61. cfg, err = testEnv.Start()
  62. Expect(err).NotTo(HaveOccurred())
  63. Expect(cfg).NotTo(BeNil())
  64. k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
  65. Expect(err).NotTo(HaveOccurred())
  66. Expect(k8sClient).NotTo(BeNil())
  67. })
  68. var _ = AfterSuite(func() {
  69. By("tearing down the test environment")
  70. cancel()
  71. Eventually(func() error {
  72. return testEnv.Stop()
  73. }, time.Minute, time.Second).Should(Succeed())
  74. })
  75. // getFirstFoundEnvTestBinaryDir locates the first binary in the specified path.
  76. // ENVTEST-based tests depend on specific binaries, usually located in paths set by
  77. // controller-runtime. When running tests directly (e.g., via an IDE) without using
  78. // Makefile targets, the 'BinaryAssetsDirectory' must be explicitly configured.
  79. //
  80. // This function streamlines the process by finding the required binaries, similar to
  81. // setting the 'KUBEBUILDER_ASSETS' environment variable. To ensure the binaries are
  82. // properly set up, run 'make setup-envtest' beforehand.
  83. func getFirstFoundEnvTestBinaryDir() string {
  84. basePath := filepath.Join("..", "..", "bin", "k8s")
  85. entries, err := os.ReadDir(basePath)
  86. if err != nil {
  87. logf.Log.Error(err, "Failed to read directory", "path", basePath)
  88. return ""
  89. }
  90. for _, entry := range entries {
  91. if entry.IsDir() {
  92. return filepath.Join(basePath, entry.Name())
  93. }
  94. }
  95. return ""
  96. }