Ensure that "null" can only be specified in "set-" commands (#2363)

This allows a check function to be set to determine whether it is
allowed to be set. This also ensures that if null is allowed it is
either the only value or not set.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2022-08-22 14:53:49 -04:00
1 parent a7bde08eee
commit a56d7aa4d0
2 files changed
+141 -7

No files matched your search

+101
View File
@@ -3,6 +3,7 @@ package common
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/hashicorp/boundary/internal/cmd/base"
@@ -422,3 +423,103 @@ func TestHandleAttributeFlags(t *testing.T) {
}
}
}
func TestNullableStringSlice(t *testing.T) {
makeStringSlicePointer := func(in ...string) *[]string {
return &in
}
tests := []struct {
name string
cmd string
args []string
expected base.StringSliceVar
expectedErr string
}{
{
name: "not-set-no-null",
cmd: "add-values",
args: []string{"-val", "foobar", "-val", "barfoo", "-val", "boobaz", "-val", "bazboo"},
expected: base.StringSliceVar{
Target: makeStringSlicePointer("foobar", "barfoo", "boobaz", "bazboo"),
},
},
{
name: "not-set-null",
cmd: "add-values",
args: []string{"-val", "foobar", "-val", "null", "-val", "boobaz", "-val", "bazboo"},
expectedErr: `"null" is not an allowed value`,
},
{
name: "set-no-null",
cmd: "set-values",
args: []string{"-val", "foobar", "-val", "barfoo", "-val", "boobaz", "-val", "bazboo"},
expected: base.StringSliceVar{
Target: makeStringSlicePointer("foobar", "barfoo", "boobaz", "bazboo"),
},
},
{
name: "set-only-null",
cmd: "set-values",
args: []string{"-val", "null"},
expected: base.StringSliceVar{
Target: makeStringSlicePointer("null"),
},
},
{
name: "set-null-and-others-beginning",
cmd: "set-values",
args: []string{"-val", "null", "-val", "barfoo", "-val", "boobaz", "-val", "bazboo"},
expectedErr: `"null" cannot be combined with other values`,
},
{
name: "set-null-and-others-middle",
cmd: "set-values",
args: []string{"-val", "foobar", "-val", "null", "-val", "boobaz", "-val", "bazboo"},
expectedErr: `"null" cannot be combined with other values`,
},
{
name: "set-null-and-others-end",
cmd: "set-values",
args: []string{"-val", "foobar", "-val", "barfoo", "-val", "boobaz", "-val", "null"},
expectedErr: `"null" cannot be combined with other values`,
},
{
name: "set-null-multiple",
cmd: "set-values",
args: []string{"-val", "null", "-val", "null"},
expected: base.StringSliceVar{
Target: makeStringSlicePointer("null"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
// Note: we do the setup on each run to make sure we aren't carrying
// state over; just like in the real CLI where each run would have
// pristine state.
c := new(base.Command)
flagSet := c.FlagSet(base.FlagSetNone)
f := flagSet.NewFlagSet("Stringsssss")
var target []string
ssVar := &base.StringSliceVar{
Name: "val",
Target: &target,
NullCheck: func() bool {
return strings.HasPrefix(tt.cmd, "set-")
},
}
f.StringSliceVar(ssVar)
err := flagSet.Parse(tt.args)
if tt.expectedErr != "" {
require.Error(err)
assert.Contains(err.Error(), tt.expectedErr)
return
}
require.NoError(err)
assert.Equal(*tt.expected.Target, *ssVar.Target)
})
}
}