diff --git a/pkg/broker/broker_test.go b/pkg/broker/broker_test.go index b034b3c7b..eca5a17e6 100644 --- a/pkg/broker/broker_test.go +++ b/pkg/broker/broker_test.go @@ -230,6 +230,12 @@ func TestServiceDefinition_ProvisionVariables(t *testing.T) { GlobalDefaults string // 6 ExpectedError error ExpectedContext map[string]any + + // Some config values were historically expected to be provided as a string. + // That is because these values were sourced from ENV Vars. But these values can + // be provided via a config file. This flag is used to test the behavior of the + // service definition when the config values are provided as a JSON object instead of a string. + IsJSONFormat bool }{ "empty": { UserParams: "", @@ -396,16 +402,28 @@ func TestServiceDefinition_ProvisionVariables(t *testing.T) { }, ExpectedError: fmt.Errorf("failed unmarshaling config value provision.defaults"), }, + "provision_overrides override user params and global_defaults but not computed defaults using JSON objects": { + ServiceProperties: map[string]any{}, // 2 + ProvisionOverrides: map[string]any{"location": "eu"}, // 3 + UserParams: `{"location":"us"}`, // 4 + DefaultOverride: "{}", // 5 + GlobalDefaults: `{"location":"az"}`, // 6 + ExpectedContext: map[string]any{ + "location": "eu", + "name": "name-eu", + "maybe-missing": "default", + "osb_context": map[string]any{}, + "originatingIdentity": map[string]any{}, + }, + IsJSONFormat: true, + }, } for tn, tc := range cases { t.Run(tn, func(t *testing.T) { - if len(tc.DefaultOverride) > 0 { - viper.Set(service.ProvisionDefaultOverrideProperty(), tc.DefaultOverride) - } - if len(tc.GlobalDefaults) > 0 { - viper.Set(GlobalProvisionDefaults, tc.GlobalDefaults) - } + setViperConfig(service.ProvisionDefaultOverrideProperty(), tc.DefaultOverride, tc.IsJSONFormat) + setViperConfig(GlobalProvisionDefaults, tc.GlobalDefaults, tc.IsJSONFormat) + defer viper.Reset() details := paramparser.ProvisionDetails{ @@ -425,6 +443,19 @@ func TestServiceDefinition_ProvisionVariables(t *testing.T) { } } +// Function to set viper configuration based on JSON format flag. +func setViperConfig(propertyName string, value string, isJSONFormat bool) { + if len(value) == 0 { + return + } + + if isJSONFormat { + viper.Set(propertyName, mustUnmarshal(value)) + } else { + viper.Set(propertyName, value) + } +} + func TestServiceDefinition_UpdateVariables(t *testing.T) { service := ServiceDefinition{ ID: "00000000-0000-0000-0000-000000000000", diff --git a/pkg/broker/service_definition_test.go b/pkg/broker/service_definition_test.go index f2b1c20c7..ec99bb086 100644 --- a/pkg/broker/service_definition_test.go +++ b/pkg/broker/service_definition_test.go @@ -1,6 +1,7 @@ package broker_test import ( + "encoding/json" "fmt" "os" "sort" @@ -414,6 +415,47 @@ var _ = Describe("ServiceDefinition", func() { }) }) + When("a plan with provision defaults is provided in configuration as a string", func() { + BeforeEach(func() { + fakeServicePlanConfigObject := []map[string]interface{}{ + { + "name": fakePlanName, + "id": fakePlanID, + "description": fakePlanDescription, + "additional_property": fakePlanProperty, + }, + { + "name": fmt.Sprintf("fake-string-format-%s", fakePlanName), + "id": fmt.Sprintf("fake-string-format-%s", fakePlanID), + "description": fmt.Sprintf("fake-string-format-%s", fakePlanDescription), + "additional_property": fmt.Sprintf("fake-string-format-%s", fakePlanProperty), + }, + } + + viper.Set("service.fake-service.provision.defaults", `{"test": "value", "object": {"key": "value"}}`) + bytes, err := json.Marshal(fakeServicePlanConfigObject) + Expect(err).To(Not(HaveOccurred())) + viper.Set("service.fake-service.plans", string(bytes)) + }) + + It("should work", func() { + plan, err := service.UserDefinedPlans(maintenanceInfo) + Expect(err).To(Not(HaveOccurred())) + Expect(plan[0].Name).To(Equal(fakePlanName)) + Expect(plan[1].Name).To(Equal(fmt.Sprintf("fake-string-format-%s", fakePlanName))) + provisionOverrides, err := service.ProvisionDefaultOverrides() + Expect(err).To(Not(HaveOccurred())) + Expect(provisionOverrides).To( + Equal( + map[string]any{ + "test": `value`, + "object": map[string]any{"key": "value"}, + }, + ), + ) + }) + }) + }) When("plans are set as an environment variable", func() {