mirror of https://github.com/hashicorp/consul
159 lines
4.4 KiB
Go
159 lines
4.4 KiB
Go
|
// Copyright (c) HashiCorp, Inc.
|
||
|
// SPDX-License-Identifier: BUSL-1.1
|
||
|
package delete
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/mitchellh/cli"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
|
||
|
"github.com/hashicorp/consul/agent"
|
||
|
"github.com/hashicorp/consul/command/resource/apply"
|
||
|
"github.com/hashicorp/consul/testrpc"
|
||
|
)
|
||
|
|
||
|
func TestResourceDeleteInvalidArgs(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
type tc struct {
|
||
|
args []string
|
||
|
expectedCode int
|
||
|
expectedErr error
|
||
|
}
|
||
|
|
||
|
cases := map[string]tc{
|
||
|
"nil args": {
|
||
|
args: nil,
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||
|
},
|
||
|
"empty args": {
|
||
|
args: []string{},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||
|
},
|
||
|
"missing file path": {
|
||
|
args: []string{"-f"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Failed to parse args: flag needs an argument: -f"),
|
||
|
},
|
||
|
"file not found": {
|
||
|
args: []string{"-f=../testdata/test.hcl"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Failed to load data: Failed to read file: open ../testdata/test.hcl: no such file or directory"),
|
||
|
},
|
||
|
"provide type and name": {
|
||
|
args: []string{"a.b.c"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||
|
},
|
||
|
"provide type and name with -f": {
|
||
|
args: []string{"a.b.c", "name", "-f", "test.hcl"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
|
||
|
},
|
||
|
"provide type and name with -f and other flags": {
|
||
|
args: []string{"a.b.c", "name", "-f", "test.hcl", "-namespace", "default"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
|
||
|
},
|
||
|
"does not provide resource name after type": {
|
||
|
args: []string{"a.b.c", "-namespace", "default"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Your argument format is incorrect: Must provide resource name right after type"),
|
||
|
},
|
||
|
"invalid resource type format": {
|
||
|
args: []string{"a.", "name", "-namespace", "default"},
|
||
|
expectedCode: 1,
|
||
|
expectedErr: errors.New("Your argument format is incorrect: Must include resource type argument in group.verion.kind format"),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for desc, tc := range cases {
|
||
|
t.Run(desc, func(t *testing.T) {
|
||
|
ui := cli.NewMockUi()
|
||
|
c := New(ui)
|
||
|
|
||
|
code := c.Run(tc.args)
|
||
|
|
||
|
require.Equal(t, tc.expectedCode, code)
|
||
|
require.Contains(t, ui.ErrorWriter.String(), tc.expectedErr.Error())
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func createResource(t *testing.T, a *agent.TestAgent) {
|
||
|
applyUi := cli.NewMockUi()
|
||
|
applyCmd := apply.New(applyUi)
|
||
|
|
||
|
args := []string{
|
||
|
"-http-addr=" + a.HTTPAddr(),
|
||
|
"-token=root",
|
||
|
}
|
||
|
|
||
|
args = append(args, []string{"-f=../testdata/demo.hcl"}...)
|
||
|
|
||
|
code := applyCmd.Run(args)
|
||
|
require.Equal(t, 0, code)
|
||
|
require.Empty(t, applyUi.ErrorWriter.String())
|
||
|
}
|
||
|
|
||
|
func TestResourceDelete(t *testing.T) {
|
||
|
if testing.Short() {
|
||
|
t.Skip("too slow for testing.Short")
|
||
|
}
|
||
|
|
||
|
t.Parallel()
|
||
|
|
||
|
a := agent.NewTestAgent(t, ``)
|
||
|
defer a.Shutdown()
|
||
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
||
|
|
||
|
defaultCmdArgs := []string{
|
||
|
"-http-addr=" + a.HTTPAddr(),
|
||
|
"-token=root",
|
||
|
}
|
||
|
cases := []struct {
|
||
|
name string
|
||
|
args []string
|
||
|
expectedCode int
|
||
|
createResource bool
|
||
|
}{
|
||
|
{
|
||
|
name: "delete resource in hcl format",
|
||
|
args: []string{"-f=../testdata/demo.hcl"},
|
||
|
expectedCode: 0,
|
||
|
createResource: true,
|
||
|
},
|
||
|
{
|
||
|
name: "delete resource in command line format",
|
||
|
args: []string{"demo.v2.Artist", "korn", "-partition=default", "-namespace=default", "-peer=local"},
|
||
|
expectedCode: 0,
|
||
|
createResource: true,
|
||
|
},
|
||
|
{
|
||
|
name: "delete resource that doesn't exist in command line format",
|
||
|
args: []string{"demo.v2.Artist", "korn", "-partition=default", "-namespace=default", "-peer=local"},
|
||
|
expectedCode: 0,
|
||
|
createResource: false,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tc := range cases {
|
||
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
ui := cli.NewMockUi()
|
||
|
c := New(ui)
|
||
|
cliArgs := append(tc.args, defaultCmdArgs...)
|
||
|
if tc.createResource {
|
||
|
createResource(t, a)
|
||
|
}
|
||
|
code := c.Run(cliArgs)
|
||
|
require.Empty(t, ui.ErrorWriter.String())
|
||
|
require.Equal(t, tc.expectedCode, code)
|
||
|
require.Contains(t, ui.OutputWriter.String(), "deleted")
|
||
|
})
|
||
|
}
|
||
|
}
|