Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions api/v1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ package v1
import (
"log"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"

"github.com/operator-framework/operator-controller/test"
)

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

func TestMain(m *testing.M) {
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "helm", "olmv1", "base", "operator-controller", "crd", "experimental"),
},
ErrorIfCRDPathMissing: true,
}

// ENVTEST-based tests require specific binaries. By default, these binaries are located
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
// without using the Makefile targets.
//
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
// variable before each test execution. The following function simplifies this process
// by handling the configuration for you.
//
// To ensure the binaries are in the expected path without manual configuration, run:
// `make envtest-k8s-bins`
if getFirstFoundEnvTestBinaryDir() != "" {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}
testEnv := test.NewEnv()

var err error
config, err = testEnv.Start()
Expand All @@ -79,15 +59,3 @@ func TestMain(m *testing.M) {
utilruntime.Must(testEnv.Stop())
os.Exit(code)
}

// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
func getFirstFoundEnvTestBinaryDir() string {
basePath := filepath.Join("..", "..", "bin", "envtest-binaries", "k8s")
entries, _ := os.ReadDir(basePath)
for _, entry := range entries {
if entry.IsDir() {
return filepath.Join(basePath, entry.Name())
}
}
return ""
}
37 changes: 2 additions & 35 deletions internal/operator-controller/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ import (
"io/fs"
"log"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"

ocv1 "github.com/operator-framework/operator-controller/api/v1"
"github.com/operator-framework/operator-controller/internal/operator-controller/controllers"
"github.com/operator-framework/operator-controller/test"
)

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

func TestMain(m *testing.M) {
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "config", "base", "operator-controller", "crd", "experimental"),
},
ErrorIfCRDPathMissing: true,
}

// ENVTEST-based tests require specific binaries. By default, these binaries are located
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
// without using the Makefile targets.
//
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
// variable before each test execution. The following function simplifies this process
// by handling the configuration for you.
//
// To ensure the binaries are in the expected path without manual configuration, run:
// `make envtest-k8s-bins`
if getFirstFoundEnvTestBinaryDir() != "" {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}
testEnv := test.NewEnv()

var err error
config, err = testEnv.Start()
Expand All @@ -126,15 +105,3 @@ func TestMain(m *testing.M) {
utilruntime.Must(testEnv.Stop())
os.Exit(code)
}

// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
func getFirstFoundEnvTestBinaryDir() string {
basePath := filepath.Join("..", "..", "bin", "envtest-binaries", "k8s")
entries, _ := os.ReadDir(basePath)
for _, entry := range entries {
if entry.IsDir() {
return filepath.Join(basePath, entry.Name())
}
}
return ""
}
54 changes: 54 additions & 0 deletions test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package test

import (
"os"
"path"
"path/filepath"
"runtime"

"sigs.k8s.io/controller-runtime/pkg/envtest"
)

// NewEnv creates a new envtest.Environment instance.
func NewEnv() *envtest.Environment {
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
pathFromProjectRoot("helm/olmv1/base/operator-controller/crd/experimental"),
},
ErrorIfCRDPathMissing: true,
}
// ENVTEST-based tests require specific binaries. By default, these binaries are located
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
// without using the Makefile targets.
//
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
// variable before each test execution. The following function simplifies this process
// by handling the configuration for you.
//
// To ensure the binaries are in the expected path without manual configuration, run:
// `make envtest-k8s-bins`
if getFirstFoundEnvTestBinaryDir() != "" {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}
return testEnv
}

// pathFromProjectRoot returns the absolute path to the given relative path from the project root.
func pathFromProjectRoot(relativePath string) string {
_, filename, _, _ := runtime.Caller(0)
p := path.Join(path.Dir(path.Dir(filename)), relativePath)
return p
}

// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
func getFirstFoundEnvTestBinaryDir() string {
basePath := pathFromProjectRoot(filepath.Join("bin", "envtest-binaries", "k8s"))
entries, _ := os.ReadDir(basePath)
for _, entry := range entries {
if entry.IsDir() {
return filepath.Join(basePath, entry.Name())
}
}
return ""
}
Loading