From 5656042199b1a5b23a830b1e6ee8c19077d7ed30 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Thu, 28 Mar 2024 10:05:04 +0000 Subject: [PATCH] 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. --- common/go.mod | 1 + common/pkg/testutils/openapi/convert.go | 2 +- common/pkg/testutils/openapi/validate.go | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/common/go.mod b/common/go.mod index 7e97d6d90..d65da65ec 100644 --- a/common/go.mod +++ b/common/go.mod @@ -63,6 +63,7 @@ require ( golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/oauth2 v0.14.0 // indirect + golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/common/pkg/testutils/openapi/convert.go b/common/pkg/testutils/openapi/convert.go index 922e4f0b5..5eb248617 100644 --- a/common/pkg/testutils/openapi/convert.go +++ b/common/pkg/testutils/openapi/convert.go @@ -14,7 +14,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ) -// convertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp. +// ConvertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp. // NOTE: This is used whenever we want to use one of the upstream libraries, as they use apiextensions.JSONSchemaProp. // NOTE: If new fields are added to clusterv1.JSONSchemaProps (e.g. to support complex types), the corresponding // schema validation must be added to validateRootSchema too. diff --git a/common/pkg/testutils/openapi/validate.go b/common/pkg/testutils/openapi/validate.go index 9ef8f4ea8..bf776db6d 100644 --- a/common/pkg/testutils/openapi/validate.go +++ b/common/pkg/testutils/openapi/validate.go @@ -10,6 +10,7 @@ import ( "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" + "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting" structuralpruning "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning" "k8s.io/apiextensions-apiserver/pkg/apiserver/validation" "k8s.io/apimachinery/pkg/util/validation/field" @@ -62,6 +63,18 @@ func ValidateClusterVariable( )} } + s, err := structuralschema.NewStructural(apiExtensionsSchema) + if err != nil { + return field.ErrorList{field.InternalError(fldPath, + fmt.Errorf( + "failed to create structural schema for variable %q; ClusterClass should be checked: %v", + value.Name, + err, + ), + )} + } + defaulting.Default(variableValue, s) + // Validate variable against the schema. // NOTE: We're reusing a library func used in CRD validation. if err := validation.ValidateCustomResource(fldPath, variableValue, validator); err != nil {