From d64c66a0948faf8aa92c02743fc1dde84dd0e9d4 Mon Sep 17 00:00:00 2001 From: Raghavendra Talur Date: Wed, 20 Nov 2024 01:14:13 -0500 Subject: [PATCH] tests: set the OPERATOR_NAMESPACE in the test suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows one to run the a specific test using the ginkgo cli or by using the vscode test decorators. For example: ``` $ ginkgo -focus "ClientProfile Controller" -r [1732083376] Controller Suite - 1/3 specs SS• SUCCESS! 5.797831s PASS Starting ceph-csi-operator suite [1732083376] e2e suite - 0/1 specs S SUCCESS! 59.25µs PASS Ginkgo ran 2 suites in 10.605577042s Test Suite Passed ``` Signed-off-by: Raghavendra Talur --- internal/controller/suite_test.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 7cd1ef8f..051d1954 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -18,6 +18,7 @@ package controller import ( "fmt" + "os" "path/filepath" "runtime" "testing" @@ -39,9 +40,12 @@ import ( // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. -var cfg *rest.Config -var k8sClient client.Client -var testEnv *envtest.Environment +var ( + cfg *rest.Config + k8sClient client.Client + testEnv *envtest.Environment + operatorNSUnsetNeeded bool +) func TestControllers(t *testing.T) { RegisterFailHandler(Fail) @@ -66,6 +70,12 @@ var _ = BeforeSuite(func() { fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)), } + _, set := os.LookupEnv("OPERATOR_NAMESPACE") + if !set { + Expect(os.Setenv("OPERATOR_NAMESPACE", "ceph-csi-operator-system")).To(Succeed()) + operatorNSUnsetNeeded = true + } + var err error // cfg is defined in this file globally. cfg, err = testEnv.Start() @@ -80,11 +90,14 @@ var _ = BeforeSuite(func() { k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) Expect(err).NotTo(HaveOccurred()) Expect(k8sClient).NotTo(BeNil()) - }) var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).NotTo(HaveOccurred()) + + if operatorNSUnsetNeeded { + Expect(os.Unsetenv("OPERATOR_NAMESPACE")).To(Succeed()) + } })