Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 5656042

Browse files
committed
test: Ensure defaults from JSON schema are respected
Without this, defaults declared in the JSON schema are not included in validation steps, which can lead to invalid failures, while also not allowing for tests that target defaults.
1 parent 0cd416f commit 5656042

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

common/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
6464
golang.org/x/net v0.19.0 // indirect
6565
golang.org/x/oauth2 v0.14.0 // indirect
66+
golang.org/x/sync v0.5.0 // indirect
6667
golang.org/x/sys v0.16.0 // indirect
6768
golang.org/x/term v0.15.0 // indirect
6869
golang.org/x/text v0.14.0 // indirect

common/pkg/testutils/openapi/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1515
)
1616

17-
// convertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
17+
// ConvertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
1818
// NOTE: This is used whenever we want to use one of the upstream libraries, as they use apiextensions.JSONSchemaProp.
1919
// NOTE: If new fields are added to clusterv1.JSONSchemaProps (e.g. to support complex types), the corresponding
2020
// schema validation must be added to validateRootSchema too.

common/pkg/testutils/openapi/validate.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
1212
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
13+
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting"
1314
structuralpruning "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning"
1415
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
1516
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -62,6 +63,18 @@ func ValidateClusterVariable(
6263
)}
6364
}
6465

66+
s, err := structuralschema.NewStructural(apiExtensionsSchema)
67+
if err != nil {
68+
return field.ErrorList{field.InternalError(fldPath,
69+
fmt.Errorf(
70+
"failed to create structural schema for variable %q; ClusterClass should be checked: %v",
71+
value.Name,
72+
err,
73+
),
74+
)}
75+
}
76+
defaulting.Default(variableValue, s)
77+
6578
// Validate variable against the schema.
6679
// NOTE: We're reusing a library func used in CRD validation.
6780
if err := validation.ValidateCustomResource(fldPath, variableValue, validator); err != nil {

0 commit comments

Comments
 (0)