command/services/deregister: tests for flag validation

This commit is contained in:
Mitchell Hashimoto 2018-10-01 08:55:32 -07:00
parent 0f82c9570b
commit 6459387cd8
No known key found for this signature in database
GPG Key ID: A3A9A8F4F25C3E56
2 changed files with 44 additions and 0 deletions

View File

@ -45,6 +45,9 @@ func (c *cmd) Run(args []string) int {
if len(args) == 0 && c.flagId == "" { if len(args) == 0 && c.flagId == "" {
c.UI.Error("Service deregistration requires at least one argument or -id.") c.UI.Error("Service deregistration requires at least one argument or -id.")
return 1 return 1
} else if len(args) > 0 && c.flagId != "" {
c.UI.Error("Service deregistration requires arguments or -id, not both.")
return 1
} }
svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{ svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{

View File

@ -19,6 +19,47 @@ func TestCommand_noTabs(t *testing.T) {
} }
} }
func TestCommand_Validation(t *testing.T) {
t.Parallel()
ui := cli.NewMockUi()
c := New(ui)
cases := map[string]struct {
args []string
output string
}{
"no args or id": {
[]string{},
"at least one",
},
"args and -id": {
[]string{"-id", "web", "foo.json"},
"not both",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
require := require.New(t)
c.init()
// Ensure our buffer is always clear
if ui.ErrorWriter != nil {
ui.ErrorWriter.Reset()
}
if ui.OutputWriter != nil {
ui.OutputWriter.Reset()
}
require.Equal(1, c.Run(tc.args))
output := ui.ErrorWriter.String()
require.Contains(output, tc.output)
})
}
}
func TestCommand_File_id(t *testing.T) { func TestCommand_File_id(t *testing.T) {
t.Parallel() t.Parallel()