|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + apimachineryruntime "k8s.io/apimachinery/pkg/runtime" |
| 27 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 28 | + "k8s.io/client-go/rest" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 31 | +) |
| 32 | + |
| 33 | +func newScheme(t *testing.T) *apimachineryruntime.Scheme { |
| 34 | + sch := apimachineryruntime.NewScheme() |
| 35 | + require.NoError(t, AddToScheme(sch)) |
| 36 | + return sch |
| 37 | +} |
| 38 | + |
| 39 | +func newClient(t *testing.T) client.Client { |
| 40 | + cl, err := client.New(config, client.Options{Scheme: newScheme(t)}) |
| 41 | + require.NoError(t, err) |
| 42 | + require.NotNil(t, cl) |
| 43 | + return cl |
| 44 | +} |
| 45 | + |
| 46 | +var config *rest.Config |
| 47 | + |
| 48 | +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 | + } |
| 70 | + |
| 71 | + var err error |
| 72 | + config, err = testEnv.Start() |
| 73 | + utilruntime.Must(err) |
| 74 | + if config == nil { |
| 75 | + log.Panic("expected cfg to not be nil") |
| 76 | + } |
| 77 | + |
| 78 | + code := m.Run() |
| 79 | + utilruntime.Must(testEnv.Stop()) |
| 80 | + os.Exit(code) |
| 81 | +} |
| 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 | +} |
0 commit comments