2023-09-11 14:06:00 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package list
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2024-01-24 23:17:54 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
|
2023-09-11 14:06:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-01-24 23:17:54 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource/apply"
|
2023-09-11 14:06:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceListCommand(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")
|
|
|
|
|
|
|
|
applyCli := cli.NewMockUi()
|
|
|
|
|
|
|
|
applyCmd := apply.New(applyCli)
|
|
|
|
code := applyCmd.Run([]string{
|
|
|
|
"-f=../testdata/demo.hcl",
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
})
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Empty(t, applyCli.ErrorWriter.String())
|
|
|
|
require.Contains(t, applyCli.OutputWriter.String(), "demo.v2.Artist 'korn' created.")
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
output string
|
|
|
|
extraArgs []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "sample output",
|
|
|
|
output: "\"name\": \"korn\"",
|
|
|
|
extraArgs: []string{
|
|
|
|
"demo.v2.artist",
|
|
|
|
"-namespace=default",
|
|
|
|
"-partition=default",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "file input",
|
|
|
|
output: "\"name\": \"korn\"",
|
|
|
|
extraArgs: []string{
|
|
|
|
"-f=../testdata/demo.hcl",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, tc.extraArgs...)
|
|
|
|
|
|
|
|
actualCode := c.Run(args)
|
|
|
|
require.Equal(t, 0, actualCode)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), tc.output)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceListInvalidArgs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := agent.NewTestAgent(t, ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
type tc struct {
|
|
|
|
args []string
|
|
|
|
expectedCode int
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]tc{
|
|
|
|
"nil args": {
|
|
|
|
args: nil,
|
|
|
|
expectedCode: 1,
|
2023-09-18 13:09:31 +00:00
|
|
|
expectedErr: errors.New("Incorrect argument format: Must include resource type argument"),
|
2023-09-11 14:06:00 +00:00
|
|
|
},
|
|
|
|
"minimum args required": {
|
|
|
|
args: []string{},
|
|
|
|
expectedCode: 1,
|
2023-09-18 13:09:31 +00:00
|
|
|
expectedErr: errors.New("Incorrect argument format: Must include resource type argument"),
|
2023-09-11 14:06:00 +00:00
|
|
|
},
|
|
|
|
"no 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"),
|
|
|
|
},
|
|
|
|
"file parsing failure": {
|
|
|
|
args: []string{
|
|
|
|
"-f=../testdata/invalid_type.hcl",
|
|
|
|
},
|
|
|
|
expectedCode: 1,
|
|
|
|
expectedErr: errors.New("Failed to decode resource from input file"),
|
|
|
|
},
|
2023-09-18 13:09:31 +00:00
|
|
|
"file argument with resource type": {
|
2023-09-11 14:06:00 +00:00
|
|
|
args: []string{
|
|
|
|
"demo.v2.artist",
|
|
|
|
"-namespace=default",
|
|
|
|
"-partition=default",
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
"-f=demo.hcl",
|
|
|
|
},
|
2023-09-18 13:09:31 +00:00
|
|
|
expectedCode: 1,
|
|
|
|
expectedErr: errors.New("Incorrect argument format: File argument is not needed when resource information is provided with the command"),
|
2023-09-11 14:06:00 +00:00
|
|
|
},
|
|
|
|
"resource type invalid": {
|
|
|
|
args: []string{
|
|
|
|
"test",
|
|
|
|
"-namespace=default",
|
|
|
|
"-partition=default",
|
|
|
|
},
|
|
|
|
expectedCode: 1,
|
2024-01-24 23:17:54 +00:00
|
|
|
expectedErr: errors.New("Must include resource type argument in group.version.kind format"),
|
2023-09-11 14:06:00 +00:00
|
|
|
},
|
2023-09-18 13:09:31 +00:00
|
|
|
"resource name is provided": {
|
|
|
|
args: []string{
|
|
|
|
"demo.v2.artist",
|
|
|
|
"test",
|
|
|
|
"-namespace=default",
|
|
|
|
"-partition=default",
|
|
|
|
},
|
|
|
|
expectedCode: 1,
|
|
|
|
expectedErr: errors.New("Must include flag arguments after resource type"),
|
|
|
|
},
|
2023-09-11 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|