Skip to content

Commit 47f8d31

Browse files
authored
Load CRDs for envtests from chart directory (#2232)
Followup of #2145: given that `config` folder is going to be removed soon, `internal/operator-controller/controllers/suite_test.go` loads CRDs from `helm/olmv1/base/operator-controller/crd`. Creation of `envtest.Environment` moved and consolidated into `test/utils.go` so that it can be consumed by multiple test suites.
1 parent 16d1089 commit 47f8d31

File tree

3 files changed

+59
-70
lines changed

3 files changed

+59
-70
lines changed

api/v1/suite_test.go

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ package v1
1919
import (
2020
"log"
2121
"os"
22-
"path/filepath"
2322
"testing"
2423

2524
"github.com/stretchr/testify/require"
2625
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
2726
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2827
"k8s.io/client-go/rest"
2928
"sigs.k8s.io/controller-runtime/pkg/client"
30-
"sigs.k8s.io/controller-runtime/pkg/envtest"
29+
30+
"github.com/operator-framework/operator-controller/test"
3131
)
3232

3333
func newScheme(t *testing.T) *apimachineryruntime.Scheme {
@@ -46,27 +46,7 @@ func newClient(t *testing.T) client.Client {
4646
var config *rest.Config
4747

4848
func TestMain(m *testing.M) {
49-
testEnv := &envtest.Environment{
50-
CRDDirectoryPaths: []string{
51-
filepath.Join("..", "..", "helm", "olmv1", "base", "operator-controller", "crd", "experimental"),
52-
},
53-
ErrorIfCRDPathMissing: true,
54-
}
55-
56-
// ENVTEST-based tests require specific binaries. By default, these binaries are located
57-
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
58-
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
59-
// without using the Makefile targets.
60-
//
61-
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
62-
// variable before each test execution. The following function simplifies this process
63-
// by handling the configuration for you.
64-
//
65-
// To ensure the binaries are in the expected path without manual configuration, run:
66-
// `make envtest-k8s-bins`
67-
if getFirstFoundEnvTestBinaryDir() != "" {
68-
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
69-
}
49+
testEnv := test.NewEnv()
7050

7151
var err error
7252
config, err = testEnv.Start()
@@ -79,15 +59,3 @@ func TestMain(m *testing.M) {
7959
utilruntime.Must(testEnv.Stop())
8060
os.Exit(code)
8161
}
82-
83-
// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
84-
func getFirstFoundEnvTestBinaryDir() string {
85-
basePath := filepath.Join("..", "..", "bin", "envtest-binaries", "k8s")
86-
entries, _ := os.ReadDir(basePath)
87-
for _, entry := range entries {
88-
if entry.IsDir() {
89-
return filepath.Join(basePath, entry.Name())
90-
}
91-
}
92-
return ""
93-
}

internal/operator-controller/controllers/suite_test.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@ import (
2121
"io/fs"
2222
"log"
2323
"os"
24-
"path/filepath"
2524
"testing"
2625

2726
"github.com/stretchr/testify/require"
2827
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
2928
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3029
"k8s.io/client-go/rest"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
32-
"sigs.k8s.io/controller-runtime/pkg/envtest"
3331
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
3432

3533
ocv1 "github.com/operator-framework/operator-controller/api/v1"
3634
"github.com/operator-framework/operator-controller/internal/operator-controller/controllers"
35+
"github.com/operator-framework/operator-controller/test"
3736
)
3837

3938
func newScheme(t *testing.T) *apimachineryruntime.Scheme {
@@ -93,27 +92,7 @@ func newClientAndReconciler(t *testing.T) (client.Client, *controllers.ClusterEx
9392
var config *rest.Config
9493

9594
func TestMain(m *testing.M) {
96-
testEnv := &envtest.Environment{
97-
CRDDirectoryPaths: []string{
98-
filepath.Join("..", "..", "..", "config", "base", "operator-controller", "crd", "experimental"),
99-
},
100-
ErrorIfCRDPathMissing: true,
101-
}
102-
103-
// ENVTEST-based tests require specific binaries. By default, these binaries are located
104-
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
105-
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
106-
// without using the Makefile targets.
107-
//
108-
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
109-
// variable before each test execution. The following function simplifies this process
110-
// by handling the configuration for you.
111-
//
112-
// To ensure the binaries are in the expected path without manual configuration, run:
113-
// `make envtest-k8s-bins`
114-
if getFirstFoundEnvTestBinaryDir() != "" {
115-
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
116-
}
95+
testEnv := test.NewEnv()
11796

11897
var err error
11998
config, err = testEnv.Start()
@@ -126,15 +105,3 @@ func TestMain(m *testing.M) {
126105
utilruntime.Must(testEnv.Stop())
127106
os.Exit(code)
128107
}
129-
130-
// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
131-
func getFirstFoundEnvTestBinaryDir() string {
132-
basePath := filepath.Join("..", "..", "bin", "envtest-binaries", "k8s")
133-
entries, _ := os.ReadDir(basePath)
134-
for _, entry := range entries {
135-
if entry.IsDir() {
136-
return filepath.Join(basePath, entry.Name())
137-
}
138-
}
139-
return ""
140-
}

test/utils.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package test
2+
3+
import (
4+
"os"
5+
"path"
6+
"path/filepath"
7+
"runtime"
8+
9+
"sigs.k8s.io/controller-runtime/pkg/envtest"
10+
)
11+
12+
// NewEnv creates a new envtest.Environment instance.
13+
func NewEnv() *envtest.Environment {
14+
testEnv := &envtest.Environment{
15+
CRDDirectoryPaths: []string{
16+
pathFromProjectRoot("helm/olmv1/base/operator-controller/crd/experimental"),
17+
},
18+
ErrorIfCRDPathMissing: true,
19+
}
20+
// ENVTEST-based tests require specific binaries. By default, these binaries are located
21+
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
22+
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
23+
// without using the Makefile targets.
24+
//
25+
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
26+
// variable before each test execution. The following function simplifies this process
27+
// by handling the configuration for you.
28+
//
29+
// To ensure the binaries are in the expected path without manual configuration, run:
30+
// `make envtest-k8s-bins`
31+
if getFirstFoundEnvTestBinaryDir() != "" {
32+
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
33+
}
34+
return testEnv
35+
}
36+
37+
// pathFromProjectRoot returns the absolute path to the given relative path from the project root.
38+
func pathFromProjectRoot(relativePath string) string {
39+
_, filename, _, _ := runtime.Caller(0)
40+
p := path.Join(path.Dir(path.Dir(filename)), relativePath)
41+
return p
42+
}
43+
44+
// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
45+
func getFirstFoundEnvTestBinaryDir() string {
46+
basePath := pathFromProjectRoot(filepath.Join("bin", "envtest-binaries", "k8s"))
47+
entries, _ := os.ReadDir(basePath)
48+
for _, entry := range entries {
49+
if entry.IsDir() {
50+
return filepath.Join(basePath, entry.Name())
51+
}
52+
}
53+
return ""
54+
}

0 commit comments

Comments
 (0)