Skip to content

Commit 6ff34d5

Browse files
committed
Fix CEL unit tests
Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent 9cddf40 commit 6ff34d5

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

internal/controllers/clusterclass/clusterclass_controller_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/types"
3232
"k8s.io/apimachinery/pkg/util/version"
33-
"k8s.io/component-base/featuregate"
3433
utilfeature "k8s.io/component-base/featuregate/testing"
35-
utilversion "k8s.io/component-base/version"
3634
"k8s.io/klog/v2"
3735
"k8s.io/utils/ptr"
3836
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -47,6 +45,7 @@ import (
4745
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
4846
"sigs.k8s.io/cluster-api/feature"
4947
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
48+
"sigs.k8s.io/cluster-api/internal/topology/variables"
5049
"sigs.k8s.io/cluster-api/util/cache"
5150
"sigs.k8s.io/cluster-api/util/test/builder"
5251
)
@@ -1164,14 +1163,11 @@ func TestReconciler_reconcileVariables(t *testing.T) {
11641163

11651164
// Pin the compatibility version used in variable CEL validation to 1.29, so we don't have to continuously refactor
11661165
// the unit tests that verify that compatibility is handled correctly.
1167-
effectiveVer := featuregate.DefaultComponentGlobalsRegistry.EffectiveVersionFor(featuregate.DefaultKubeComponent)
1168-
if effectiveVer != nil {
1169-
g.Expect(effectiveVer.MinCompatibilityVersion()).To(Equal(version.MustParse("v1.29")))
1170-
} else {
1171-
v := utilversion.DefaultKubeEffectiveVersion()
1172-
v.SetMinCompatibilityVersion(version.MustParse("v1.29"))
1173-
g.Expect(featuregate.DefaultComponentGlobalsRegistry.Register(featuregate.DefaultKubeComponent, v, nil)).To(Succeed())
1174-
}
1166+
backupEnvSetVersion := variables.GetEnvSetVersion()
1167+
defer func() {
1168+
variables.SetEnvSetVersion(backupEnvSetVersion)
1169+
}()
1170+
variables.SetEnvSetVersion(version.MustParse("1.29"))
11751171

11761172
s := &scope{
11771173
clusterClass: tt.clusterClass,

internal/topology/variables/clusterclass_variable_validation.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
3535
"k8s.io/apimachinery/pkg/util/sets"
3636
"k8s.io/apimachinery/pkg/util/validation/field"
37+
"k8s.io/apimachinery/pkg/util/version"
3738
celconfig "k8s.io/apiserver/pkg/apis/cel"
3839
apiservercel "k8s.io/apiserver/pkg/cel"
3940
"k8s.io/apiserver/pkg/cel/environment"
@@ -183,6 +184,22 @@ func validateClusterClassXVariableMetadata(schema *clusterv1.JSONSchemaProps, fl
183184

184185
var validVariableTypes = sets.Set[string]{}.Insert("object", "array", "string", "number", "integer", "boolean")
185186

187+
// envSetVersion can be overwritten in unit tests to use a different version for the CEL EnvSet, so we don't have to
188+
// continuously refactor the unit tests that verify that compatibility is handled correctly.
189+
var envSetVersion = environment.DefaultCompatibilityVersion()
190+
191+
// SetEnvSetVersion sets the CEL EnvSet version. Should be only used for unit tests, so we don't have to
192+
// continuously refactor the unit tests that verify that compatibility is handled correctly.
193+
func SetEnvSetVersion(v *version.Version) {
194+
envSetVersion = v
195+
}
196+
197+
// GetEnvSetVersion gets the CEL EnvSet version. Should be only used for unit tests, so we don't have to
198+
// continuously refactor the unit tests that verify that compatibility is handled correctly.
199+
func GetEnvSetVersion() *version.Version {
200+
return envSetVersion
201+
}
202+
186203
// validateRootSchema validates the schema.
187204
func validateRootSchema(ctx context.Context, oldClusterClassVariables, clusterClassVariable *clusterv1.ClusterClassVariable, fldPath *field.Path) field.ErrorList {
188205
var allErrs field.ErrorList
@@ -274,7 +291,7 @@ func validateRootSchema(ctx context.Context, oldClusterClassVariables, clusterCl
274291
// * The Kubernetes CEL library assumes this behavior and it's baked into cel.NewValidator (they use n to validate)
275292
// * If the DefaultCompatibilityVersion is an earlier version than "n-1", it means we could roll back even more than 1 version.
276293
opts := &validationOptions{
277-
celEnvironmentSet: environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), true),
294+
celEnvironmentSet: environment.MustBaseEnvSet(envSetVersion, true),
278295
}
279296
if oldAPIExtensionsSchema != nil {
280297
opts.preexistingExpressions = findPreexistingExpressions(oldAPIExtensionsSchema)

internal/topology/variables/clusterclass_variable_validation_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ import (
2121
"strings"
2222
"testing"
2323

24-
. "github.com/onsi/gomega"
2524
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2625
"k8s.io/apimachinery/pkg/util/validation/field"
2726
"k8s.io/apimachinery/pkg/util/version"
28-
"k8s.io/component-base/featuregate"
29-
utilversion "k8s.io/component-base/version"
3027
"k8s.io/utils/ptr"
3128

3229
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
@@ -343,18 +340,13 @@ func Test_ValidateClusterClassVariables(t *testing.T) {
343340

344341
for _, tt := range tests {
345342
t.Run(tt.name, func(t *testing.T) {
346-
g := NewWithT(t)
347-
348343
// Pin the compatibility version used in variable CEL validation to 1.29, so we don't have to continuously refactor
349344
// the unit tests that verify that compatibility is handled correctly.
350-
effectiveVer := featuregate.DefaultComponentGlobalsRegistry.EffectiveVersionFor(featuregate.DefaultKubeComponent)
351-
if effectiveVer != nil {
352-
g.Expect(effectiveVer.MinCompatibilityVersion()).To(Equal(version.MustParse("v1.29")))
353-
} else {
354-
v := utilversion.DefaultKubeEffectiveVersion()
355-
v.SetMinCompatibilityVersion(version.MustParse("v1.29"))
356-
g.Expect(featuregate.DefaultComponentGlobalsRegistry.Register(featuregate.DefaultKubeComponent, v, nil)).To(Succeed())
357-
}
345+
backupEnvSetVersion := GetEnvSetVersion()
346+
defer func() {
347+
SetEnvSetVersion(backupEnvSetVersion)
348+
}()
349+
SetEnvSetVersion(version.MustParse("1.29"))
358350

359351
gotErrs := ValidateClusterClassVariables(ctx,
360352
tt.oldClusterClassVariables,

internal/webhooks/clusterclass_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/apimachinery/pkg/util/version"
29-
"k8s.io/component-base/featuregate"
3029
utilfeature "k8s.io/component-base/featuregate/testing"
31-
utilversion "k8s.io/component-base/version"
3230
"k8s.io/utils/ptr"
3331
ctrl "sigs.k8s.io/controller-runtime"
3432
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,6 +35,7 @@ import (
3735
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
3836
"sigs.k8s.io/cluster-api/api/core/v1beta2/index"
3937
"sigs.k8s.io/cluster-api/feature"
38+
"sigs.k8s.io/cluster-api/internal/topology/variables"
4039
"sigs.k8s.io/cluster-api/internal/webhooks/util"
4140
"sigs.k8s.io/cluster-api/util/test/builder"
4241
)
@@ -1900,14 +1899,11 @@ func TestClusterClassValidation(t *testing.T) {
19001899

19011900
// Pin the compatibility version used in variable CEL validation to 1.29, so we don't have to continuously refactor
19021901
// the unit tests that verify that compatibility is handled correctly.
1903-
effectiveVer := featuregate.DefaultComponentGlobalsRegistry.EffectiveVersionFor(featuregate.DefaultKubeComponent)
1904-
if effectiveVer != nil {
1905-
g.Expect(effectiveVer.MinCompatibilityVersion()).To(Equal(version.MustParse("v1.29")))
1906-
} else {
1907-
v := utilversion.DefaultKubeEffectiveVersion()
1908-
v.SetMinCompatibilityVersion(version.MustParse("v1.29"))
1909-
g.Expect(featuregate.DefaultComponentGlobalsRegistry.Register(featuregate.DefaultKubeComponent, v, nil)).To(Succeed())
1910-
}
1902+
backupEnvSetVersion := variables.GetEnvSetVersion()
1903+
defer func() {
1904+
variables.SetEnvSetVersion(backupEnvSetVersion)
1905+
}()
1906+
variables.SetEnvSetVersion(version.MustParse("1.29"))
19111907

19121908
// Create the webhook and add the fakeClient as its client.
19131909
webhook := &ClusterClass{Client: fakeClient}

0 commit comments

Comments
 (0)