Skip to content

Commit

Permalink
test: configuration is expected in string or JSON format (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
zucchinidev authored Jul 8, 2024
1 parent 2dd77b4 commit 05c21de
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
43 changes: 37 additions & 6 deletions pkg/broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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{
Expand All @@ -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",
Expand Down
42 changes: 42 additions & 0 deletions pkg/broker/service_definition_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package broker_test

import (
"encoding/json"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 05c21de

Please sign in to comment.