Skip to content

Commit

Permalink
ref(bundle.go): only validate/return value if param applies to provid…
Browse files Browse the repository at this point in the history
…ed action (#204)

* ref(bundle.go): only validate/return value if param applies to provided action

Signed-off-by: Vaughn Dice <vadice@microsoft.com>

* incorporate review feedback

Signed-off-by: Vaughn Dice <vadice@microsoft.com>

* fix typo in tests per review feedback

Signed-off-by: Vaughn Dice <vadice@microsoft.com>
  • Loading branch information
vdice authored Apr 20, 2020
1 parent 919bc67 commit d6962e8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
9 changes: 7 additions & 2 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,16 @@ type Action struct {
}

// ValuesOrDefaults returns parameter values or the default parameter values. An error is returned when the parameter value does not pass
// the schema validation or a required parameter is missing.
func ValuesOrDefaults(vals map[string]interface{}, b *Bundle) (map[string]interface{}, error) {
// the schema validation or a required parameter is missing, assuming the parameter applies to the provided action.
func ValuesOrDefaults(vals map[string]interface{}, b *Bundle, action string) (map[string]interface{}, error) {
res := map[string]interface{}{}

for name, param := range b.Parameters {
// If the parameter doesn't apply to the provided action,
// skip validation and do not attempt to include in the returned list
if !param.AppliesTo(action) {
continue
}
s, ok := b.Definitions[param.Definition]
if !ok {
return res, fmt.Errorf("unable to find definition for %s", name)
Expand Down
69 changes: 63 additions & 6 deletions bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestValuesOrDefaults(t *testing.T) {
},
}

vod, err := ValuesOrDefaults(vals, b)
vod, err := ValuesOrDefaults(vals, b, "install")

is.NoError(err)
is.True(vod["enabled"].(bool))
Expand All @@ -154,19 +154,19 @@ func TestValuesOrDefaults(t *testing.T) {

// This should err out because of type problem
vals["replicaCount"] = "banana"
_, err = ValuesOrDefaults(vals, b)
_, err = ValuesOrDefaults(vals, b, "install")
is.Error(err)

// Check for panic when zero value Bundle is passed
_, err = ValuesOrDefaults(vals, &Bundle{})
_, err = ValuesOrDefaults(vals, &Bundle{}, "install")
is.NoError(err)
}

func TestValuesOrDefaults_NoParameter(t *testing.T) {
is := assert.New(t)
vals := map[string]interface{}{}
b := &Bundle{}
vod, err := ValuesOrDefaults(vals, b)
vod, err := ValuesOrDefaults(vals, b, "install")
is.NoError(err)
is.Len(vod, 0)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestValuesOrDefaults_Required(t *testing.T) {
},
}

_, err := ValuesOrDefaults(vals, b)
_, err := ValuesOrDefaults(vals, b, "install")
is.Error(err)

// It is unclear what the outcome should be when the user supplies
Expand All @@ -208,11 +208,68 @@ func TestValuesOrDefaults_Required(t *testing.T) {
// Example: It makes perfect sense for a user to specify --set minimum=0
// and in so doing meet the requirement that a value be specified.
vals["minimum"] = 0
res, err := ValuesOrDefaults(vals, b)
res, err := ValuesOrDefaults(vals, b, "install")
is.NoError(err)
is.Equal(0, res["minimum"])
}

func TestValuesOrDefaults_NotApplicableToAction(t *testing.T) {
// vals represent user-supplied parameter values
vals := map[string]interface{}{
"param-with-default-and-override": true,
}

b := &Bundle{
Definitions: map[string]*definition.Schema{
"param-with-default-not-applicable": {
Type: "string",
Default: "foo",
},
"required-param-not-applicable": {
Type: "string",
},
"param-with-default": {
Type: "boolean",
Default: false,
},
"param-with-default-and-override": {
Type: "boolean",
Default: false,
},
},
Parameters: map[string]Parameter{
"param-with-default-not-applicable": {
Definition: "param-with-default-not-applicable",
ApplyTo: []string{
"uninstall",
},
},
"required-param-not-applicable": {
Definition: "required-param-not-applicable",
Required: true,
ApplyTo: []string{
"uninstall",
},
},
"param-with-default": {
Definition: "param-with-default",
},
"param-with-default-and-override": {
Definition: "param-with-default-and-override",
},
},
}

res, err := ValuesOrDefaults(vals, b, "install")
require.NoError(t, err)

expected := map[string]interface{}{
"param-with-default": false,
"param-with-default-and-override": true,
}
require.Equal(t, expected, res)
}

func TestValidateVersionTag(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit d6962e8

Please sign in to comment.