Skip to content

Commit

Permalink
Add supported arguments check in phase.Exec
Browse files Browse the repository at this point in the history
  • Loading branch information
viveksinghggits committed Feb 22, 2022
1 parent 477487b commit 0e9fce2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/Masterminds/semver"
"github.com/pkg/errors"
"k8s.io/utils/strings/slices"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/field"
Expand Down Expand Up @@ -65,13 +66,27 @@ func (p *Phase) Exec(ctx context.Context, bp crv1alpha1.Blueprint, action string
if err = checkRequiredArgs(p.f.RequiredArgs(), args); err != nil {
return nil, errors.Wrapf(err, "Required args missing for function %s", p.f.Name())
}

if err = checkUnsupportedArgs(p.f.Arguments(), args); err != nil {
return nil, errors.Wrapf(err, "Checking supported args for function %s.", p.f.Name())
}

p.args = args
}
}
// Execute the function
return p.f.Exec(ctx, tp, p.args)
}

func checkUnsupportedArgs(supportedArgs []string, args map[string]interface{}) error {
for a := range args {
if !slices.Contains(supportedArgs, a) {
return errors.Errorf("argument %s is not supported", a)
}
}
return nil
}

// GetPhases renders the returns a list of Phases with pre-rendered arguments.
func GetPhases(bp crv1alpha1.Blueprint, action, version string, tp param.TemplateParams) ([]*Phase, error) {
a, ok := bp.Actions[action]
Expand Down
40 changes: 40 additions & 0 deletions pkg/phase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,43 @@ func (s *PhaseSuite) TestExec(c *C) {
c.Assert(output, Equals, tc.expected)
}
}

func (s *PhaseSuite) TestCheckSupportedArgs(c *C) {
for _, tc := range []struct {
supprtedArgs []string
providedArgs map[string]interface{}
err Checker
expErr string
}{
{
supprtedArgs: []string{"a", "b", "c"},
providedArgs: map[string]interface{}{
"a": "val",
"b": "val",
"c": "val",
},
err: IsNil,
},
{
supprtedArgs: []string{"a", "b", "c"},
providedArgs: map[string]interface{}{
"a": "val",
"b": "val",
"c": "val",
"d": "val",
},
err: NotNil,
expErr: "argument d is not supported",
},
{
supprtedArgs: []string{"a", "b", "c"},
providedArgs: map[string]interface{}{},
err: IsNil},
} {
err := checkUnsupportedArgs(tc.supprtedArgs, tc.providedArgs)
if err != nil {
c.Assert(err.Error(), Equals, tc.expErr)
}
c.Assert(err, tc.err)
}
}

0 comments on commit 0e9fce2

Please sign in to comment.