2023-09-08 16:20:14 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package apply
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-10-13 13:24:16 +00:00
|
|
|
"io"
|
2023-09-08 16:20:14 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-22 16:51:15 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
2023-10-13 13:24:16 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource/read"
|
2023-09-22 16:51:15 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2023-09-08 16:20:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceApplyCommand(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
2024-02-23 20:27:49 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
|
|
|
defer a.Shutdown()
|
2023-09-08 16:20:14 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
output string
|
|
|
|
args []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "sample output",
|
|
|
|
args: []string{"-f=../testdata/demo.hcl"},
|
|
|
|
output: "demo.v2.Artist 'korn' created.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nested data format",
|
|
|
|
args: []string{"-f=../testdata/nested_data.hcl"},
|
2023-09-25 18:03:45 +00:00
|
|
|
output: "mesh.v2beta1.Destinations 'api' created.",
|
2023-09-08 16:20:14 +00:00
|
|
|
},
|
2024-02-23 20:27:49 +00:00
|
|
|
{
|
|
|
|
name: "file path with no flag",
|
|
|
|
args: []string{"../testdata/nested_data.hcl"},
|
|
|
|
output: "mesh.v2beta1.Destinations 'api' created.",
|
|
|
|
},
|
2023-09-08 16:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
2024-02-23 20:27:49 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2023-09-08 16:20:14 +00:00
|
|
|
"-token=root",
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, tc.args...)
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), tc.output)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
func readResource(t *testing.T, a *agent.TestAgent, extraArgs []string) string {
|
|
|
|
readUi := cli.NewMockUi()
|
|
|
|
readCmd := read.New(readUi)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(extraArgs, args...)
|
|
|
|
|
|
|
|
code := readCmd.Run(args)
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Empty(t, readUi.ErrorWriter.String())
|
|
|
|
return readUi.OutputWriter.String()
|
|
|
|
}
|
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
func TestResourceApplyCommand_StdIn(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
|
|
|
defer a.Shutdown()
|
2023-10-13 13:24:16 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
t.Run("hcl", func(t *testing.T) {
|
|
|
|
stdinR, stdinW := io.Pipe()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.testStdin = stdinR
|
|
|
|
|
|
|
|
stdInput := `ID {
|
|
|
|
Type = gvk("demo.v2.Artist")
|
|
|
|
Name = "korn"
|
|
|
|
Tenancy {
|
2024-01-30 18:12:09 +00:00
|
|
|
Namespace = "default"
|
2024-02-23 20:27:49 +00:00
|
|
|
Partition = "default"
|
2023-10-13 13:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Data {
|
|
|
|
Name = "Korn"
|
|
|
|
Genre = "GENRE_METAL"
|
|
|
|
}
|
|
|
|
|
|
|
|
Metadata = {
|
|
|
|
"foo" = "bar"
|
|
|
|
}`
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.Write([]byte(stdInput))
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{
|
2024-02-23 20:27:49 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2023-10-13 13:24:16 +00:00
|
|
|
"-token=root",
|
|
|
|
"-",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
2024-01-29 20:08:31 +00:00
|
|
|
require.Equal(t, 0, code, ui.ErrorWriter.String())
|
2023-10-13 13:24:16 +00:00
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
2024-02-23 20:27:49 +00:00
|
|
|
expected := readResource(t, a, []string{"demo.v2.Artist", "korn"})
|
2023-10-13 13:24:16 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(), "demo.v2.Artist 'korn' created.")
|
2024-02-23 20:27:49 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(), expected)
|
2023-10-13 13:24:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
stdinR, stdinW := io.Pipe()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.testStdin = stdinR
|
|
|
|
|
|
|
|
stdInput := `{
|
|
|
|
"data": {
|
|
|
|
"genre": "GENRE_METAL",
|
|
|
|
"name": "Korn"
|
|
|
|
},
|
|
|
|
"id": {
|
|
|
|
"name": "korn",
|
|
|
|
"tenancy": {
|
2024-02-23 20:27:49 +00:00
|
|
|
"namespace": "default",
|
|
|
|
"partition": "default"
|
2023-10-13 13:24:16 +00:00
|
|
|
},
|
|
|
|
"type": {
|
|
|
|
"group": "demo",
|
|
|
|
"groupVersion": "v2",
|
|
|
|
"kind": "Artist"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"metadata": {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.Write([]byte(stdInput))
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{
|
2024-02-23 20:27:49 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2024-01-30 18:12:09 +00:00
|
|
|
"-token=root",
|
2024-02-23 20:27:49 +00:00
|
|
|
"-",
|
2023-10-13 13:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
2024-01-29 20:08:31 +00:00
|
|
|
require.Equal(t, 0, code, ui.ErrorWriter.String())
|
2023-10-13 13:24:16 +00:00
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
2024-02-23 20:27:49 +00:00
|
|
|
expected := readResource(t, a, []string{"demo.v2.Artist", "korn"})
|
2023-10-13 13:24:16 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(), "demo.v2.Artist 'korn' created.")
|
2024-02-23 20:27:49 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(), expected)
|
2023-10-13 13:24:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-08 16:20:14 +00:00
|
|
|
func TestResourceApplyInvalidArgs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type tc struct {
|
|
|
|
args []string
|
|
|
|
expectedCode int
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]tc{
|
|
|
|
"no file path": {
|
|
|
|
args: []string{"-f"},
|
|
|
|
expectedCode: 1,
|
|
|
|
expectedErr: errors.New("Failed to parse args: flag needs an argument: -f"),
|
|
|
|
},
|
|
|
|
"missing required flag": {
|
|
|
|
args: []string{},
|
|
|
|
expectedCode: 1,
|
2024-02-23 20:27:49 +00:00
|
|
|
expectedErr: errors.New("Incorrect argument format: Must provide exactly one positional argument to specify the resource to write"),
|
2023-09-08 16:20:14 +00:00
|
|
|
},
|
|
|
|
"file parsing failure": {
|
|
|
|
args: []string{"-f=../testdata/invalid.hcl"},
|
|
|
|
expectedCode: 1,
|
|
|
|
expectedErr: errors.New("Failed to decode resource from input file"),
|
|
|
|
},
|
|
|
|
"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"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|