Skip to content

Commit 295904c

Browse files
committed
util/test allow to pass in custom crd paths and scheme builders
1 parent f3a2d78 commit 295904c

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

internal/test/envtest/environment.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ import (
3232
"github.com/onsi/ginkgo/v2"
3333
"github.com/pkg/errors"
3434
admissionv1 "k8s.io/api/admissionregistration/v1"
35+
appsv1 "k8s.io/api/apps/v1"
3536
corev1 "k8s.io/api/core/v1"
37+
rbacv1 "k8s.io/api/rbac/v1"
3638
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3739
apierrors "k8s.io/apimachinery/pkg/api/errors"
3840
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41+
"k8s.io/apimachinery/pkg/runtime"
3942
kerrors "k8s.io/apimachinery/pkg/util/errors"
4043
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4144
"k8s.io/apimachinery/pkg/util/wait"
@@ -114,19 +117,20 @@ func init() {
114117
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
115118
utilruntime.Must(runtimev1.AddToScheme(scheme.Scheme))
116119
utilruntime.Must(ipamv1.AddToScheme(scheme.Scheme))
117-
utilruntime.Must(builder.AddTransitionV1Beta2ToScheme(scheme.Scheme))
118120
utilruntime.Must(addonsv1.AddToScheme(scheme.Scheme))
119121
}
120122

121123
// RunInput is the input for Run.
122124
type RunInput struct {
123-
M *testing.M
124-
ManagerUncachedObjs []client.Object
125-
ManagerCacheOptions cache.Options
126-
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
127-
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
128-
SetupEnv func(e *Environment)
129-
MinK8sVersion string
125+
M *testing.M
126+
ManagerUncachedObjs []client.Object
127+
ManagerCacheOptions cache.Options
128+
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
129+
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
130+
SetupEnv func(e *Environment)
131+
MinK8sVersion string
132+
AdditionalSchemeBuilder runtime.SchemeBuilder
133+
AdditionalCRDDirectoryPaths []string
130134
}
131135

132136
// Run executes the tests of the given testing.M in a test environment.
@@ -147,8 +151,28 @@ func Run(ctx context.Context, input RunInput) int {
147151
return input.M.Run()
148152
}
149153

154+
// Calculate the scheme.
155+
scheme := runtime.NewScheme()
156+
utilruntime.Must(appsv1.AddToScheme(scheme))
157+
utilruntime.Must(corev1.AddToScheme(scheme))
158+
utilruntime.Must(rbacv1.AddToScheme(scheme))
159+
160+
utilruntime.Must(addonsv1.AddToScheme(scheme))
161+
utilruntime.Must(admissionv1.AddToScheme(scheme))
162+
utilruntime.Must(admissionv1.AddToScheme(scheme))
163+
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
164+
utilruntime.Must(bootstrapv1.AddToScheme(scheme))
165+
utilruntime.Must(clusterv1.AddToScheme(scheme))
166+
utilruntime.Must(controlplanev1.AddToScheme(scheme))
167+
utilruntime.Must(expv1.AddToScheme(scheme))
168+
utilruntime.Must(ipamv1.AddToScheme(scheme))
169+
utilruntime.Must(runtimev1.AddToScheme(scheme))
170+
if input.AdditionalSchemeBuilder != nil {
171+
utilruntime.Must(input.AdditionalSchemeBuilder.AddToScheme(scheme))
172+
}
173+
150174
// Bootstrapping test environment
151-
env := newEnvironment(input.ManagerCacheOptions, input.ManagerUncachedObjs...)
175+
env := newEnvironment(scheme, input.AdditionalCRDDirectoryPaths, input.ManagerCacheOptions, input.ManagerUncachedObjs...)
152176

153177
ctx, cancel := context.WithCancelCause(ctx)
154178
env.cancelManager = cancel
@@ -233,20 +257,24 @@ type Environment struct {
233257
//
234258
// This function should be called only once for each package you're running tests within,
235259
// usually the environment is initialized in a suite_test.go file within a `BeforeSuite` ginkgo block.
236-
func newEnvironment(managerCacheOptions cache.Options, uncachedObjs ...client.Object) *Environment {
260+
func newEnvironment(scheme *runtime.Scheme, additionalCRDDirectoryPaths []string, managerCacheOptions cache.Options, uncachedObjs ...client.Object) *Environment {
237261
// Get the root of the current file to use in CRD paths.
238262
_, filename, _, _ := goruntime.Caller(0) //nolint:dogsled
239263
root := path.Join(path.Dir(filename), "..", "..", "..")
240264

265+
crdDirectoryPaths := []string{
266+
filepath.Join(root, "config", "crd", "bases"),
267+
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
268+
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
269+
}
270+
for _, path := range additionalCRDDirectoryPaths {
271+
crdDirectoryPaths = append(crdDirectoryPaths, filepath.Join(root, path))
272+
}
273+
241274
// Create the test environment.
242275
env := &envtest.Environment{
243276
ErrorIfCRDPathMissing: true,
244-
CRDDirectoryPaths: []string{
245-
filepath.Join(root, "config", "crd", "bases"),
246-
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
247-
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
248-
filepath.Join(root, "util", "test", "builder", "crd"),
249-
},
277+
CRDDirectoryPaths: crdDirectoryPaths,
250278
CRDs: []*apiextensionsv1.CustomResourceDefinition{
251279
builder.GenericBootstrapConfigCRD.DeepCopy(),
252280
builder.GenericBootstrapConfigTemplateCRD.DeepCopy(),
@@ -296,7 +324,7 @@ func newEnvironment(managerCacheOptions cache.Options, uncachedObjs ...client.Ob
296324
Controller: config.Controller{
297325
UsePriorityQueue: ptr.To[bool](feature.Gates.Enabled(feature.PriorityQueue)),
298326
},
299-
Scheme: scheme.Scheme,
327+
Scheme: scheme,
300328
Metrics: metricsserver.Options{
301329
BindAddress: "0",
302330
},

util/patch/suite_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ package patch
1818

1919
import (
2020
"os"
21+
"path/filepath"
2122
"testing"
2223
"time"
2324

25+
"k8s.io/apimachinery/pkg/runtime"
2426
ctrl "sigs.k8s.io/controller-runtime"
2527

2628
"sigs.k8s.io/cluster-api/internal/test/envtest"
29+
"sigs.k8s.io/cluster-api/util/test/builder"
2730
)
2831

2932
const (
@@ -39,5 +42,11 @@ func TestMain(m *testing.M) {
3942
os.Exit(envtest.Run(ctx, envtest.RunInput{
4043
M: m,
4144
SetupEnv: func(e *envtest.Environment) { env = e },
45+
AdditionalCRDDirectoryPaths: []string{
46+
filepath.Join("util", "test", "builder", "crd"),
47+
},
48+
AdditionalSchemeBuilder: runtime.NewSchemeBuilder(
49+
builder.AddTransitionV1Beta2ToScheme,
50+
),
4251
}))
4352
}

0 commit comments

Comments
 (0)