From e51bdfb8896e9838efe2114ed67f7d267c61a648 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Thu, 16 Nov 2023 10:38:35 +0100 Subject: [PATCH] Add additional test cases for nested variable defaulting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- .../cluster_variable_defaulting_test.go | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/internal/topology/variables/cluster_variable_defaulting_test.go b/internal/topology/variables/cluster_variable_defaulting_test.go index f441264e6bd2..b6a7e72e55a4 100644 --- a/internal/topology/variables/cluster_variable_defaulting_test.go +++ b/internal/topology/variables/cluster_variable_defaulting_test.go @@ -929,7 +929,6 @@ func Test_DefaultClusterVariable(t *testing.T) { Schema: clusterv1.VariableSchema{ OpenAPIV3Schema: clusterv1.JSONSchemaProps{ Type: "object", - Properties: map[string]clusterv1.JSONSchemaProps{ "enabled": { Type: "boolean", @@ -949,6 +948,76 @@ func Test_DefaultClusterVariable(t *testing.T) { createVariable: true, want: nil, }, + { + name: "Default new object variable if there is a top-level default", + clusterClassVariable: &statusVariableDefinition{ + Name: "httpProxy", + ClusterClassStatusVariableDefinition: &clusterv1.ClusterClassStatusVariableDefinition{ + Required: true, + Schema: clusterv1.VariableSchema{ + OpenAPIV3Schema: clusterv1.JSONSchemaProps{ + Type: "object", + Default: &apiextensionsv1.JSON{Raw: []byte(`{"url":"test-url"}`)}, + Properties: map[string]clusterv1.JSONSchemaProps{ + "enabled": { + Type: "boolean", + Default: &apiextensionsv1.JSON{Raw: []byte(`false`)}, + }, + "url": { + Type: "string", + }, + "noProxy": { + Type: "string", + }, + }, + }, + }, + }, + }, + createVariable: true, + want: &clusterv1.ClusterVariable{ + Name: "httpProxy", + Value: apiextensionsv1.JSON{ + // Defaulting first adds the top-level object and then the enabled field. + Raw: []byte(`{"enabled":false,"url":"test-url"}`), + }, + }, + }, + { + name: "Default new object variable if there is a top-level default (which is an empty object)", + clusterClassVariable: &statusVariableDefinition{ + Name: "httpProxy", + ClusterClassStatusVariableDefinition: &clusterv1.ClusterClassStatusVariableDefinition{ + Required: true, + Schema: clusterv1.VariableSchema{ + OpenAPIV3Schema: clusterv1.JSONSchemaProps{ + Type: "object", + Default: &apiextensionsv1.JSON{Raw: []byte(`{}`)}, + Properties: map[string]clusterv1.JSONSchemaProps{ + "enabled": { + Type: "boolean", + Default: &apiextensionsv1.JSON{Raw: []byte(`false`)}, + }, + "url": { + Type: "string", + }, + "noProxy": { + Type: "string", + }, + }, + }, + }, + }, + }, + createVariable: true, + want: &clusterv1.ClusterVariable{ + Name: "httpProxy", + Value: apiextensionsv1.JSON{ + // Defaulting first adds the top-level empty object and then the enabled field. + Raw: []byte(`{"enabled":false}`), + }, + }, + }, { name: "Don't default existing object variable", clusterClassVariable: &statusVariableDefinition{