Skip to content

Commit

Permalink
tests: set the OPERATOR_NAMESPACE in the test suite
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
raghavendra-talur committed Nov 22, 2024
1 parent 44cd32f commit 74c2c6e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
Expand All @@ -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)
Expand All @@ -66,6 +70,15 @@ 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
}

// init the controller config
InitConfig()

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expand All @@ -80,11 +93,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())
}
})

0 comments on commit 74c2c6e

Please sign in to comment.