2023-09-05 16:17:19 +00:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package read
import (
2023-09-13 18:48:40 +00:00
"errors"
2024-01-30 18:12:09 +00:00
"fmt"
2023-09-05 16:17:19 +00:00
"testing"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
2023-09-13 18:48:40 +00:00
"github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/command/resource/apply"
2024-01-30 18:12:09 +00:00
"github.com/hashicorp/consul/sdk/freeport"
2023-09-13 18:48:40 +00:00
"github.com/hashicorp/consul/testrpc"
2023-09-05 16:17:19 +00:00
)
func TestResourceReadInvalidArgs ( t * testing . T ) {
t . Parallel ( )
type tc struct {
2023-09-13 18:48:40 +00:00
args [ ] string
expectedCode int
expectedErr error
2023-09-05 16:17:19 +00:00
}
cases := map [ string ] tc {
"nil args" : {
2023-09-13 18:48:40 +00:00
args : nil ,
expectedCode : 1 ,
2023-09-18 13:09:31 +00:00
expectedErr : errors . New ( "Incorrect argument format: Must specify two arguments: resource type and resource name" ) ,
2023-09-05 16:17:19 +00:00
} ,
"empty args" : {
2023-09-13 18:48:40 +00:00
args : [ ] string { } ,
expectedCode : 1 ,
2023-09-18 13:09:31 +00:00
expectedErr : errors . New ( "Incorrect argument format: Must specify two arguments: resource type and resource name" ) ,
2023-09-05 16:17:19 +00:00
} ,
"missing file path" : {
2023-09-13 18:48:40 +00:00
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" ) ,
2023-09-05 16:17:19 +00:00
} ,
"provide type and name" : {
2023-09-13 18:48:40 +00:00
args : [ ] string { "a.b.c" } ,
expectedCode : 1 ,
2023-09-18 13:09:31 +00:00
expectedErr : errors . New ( "Incorrect argument format: Must specify two arguments: resource type and resource name" ) ,
2023-09-05 16:17:19 +00:00
} ,
"provide type and name with -f" : {
2023-09-13 18:48:40 +00:00
args : [ ] string { "a.b.c" , "name" , "-f" , "test.hcl" } ,
expectedCode : 1 ,
2023-09-18 13:09:31 +00:00
expectedErr : errors . New ( "Incorrect argument format: File argument is not needed when resource information is provided with the command" ) ,
2023-09-05 16:17:19 +00:00
} ,
"provide type and name with -f and other flags" : {
2023-09-13 18:48:40 +00:00
args : [ ] string { "a.b.c" , "name" , "-f" , "test.hcl" , "-namespace" , "default" } ,
expectedCode : 1 ,
2023-09-18 13:09:31 +00:00
expectedErr : errors . New ( "Incorrect argument format: File argument is not needed when resource information is provided with the command" ) ,
2023-09-05 16:17:19 +00:00
} ,
"does not provide resource name after type" : {
2023-09-13 18:48:40 +00:00
args : [ ] string { "a.b.c" , "-namespace" , "default" } ,
expectedCode : 1 ,
2023-09-18 13:09:31 +00:00
expectedErr : errors . New ( "Incorrect argument format: Must provide resource name right after type" ) ,
2023-09-13 18:48:40 +00:00
} ,
"invalid resource type format" : {
args : [ ] string { "a." , "name" , "-namespace" , "default" } ,
expectedCode : 1 ,
2024-01-24 23:17:54 +00:00
expectedErr : errors . New ( "Incorrect argument format: Must provide resource type argument with either in group.version.kind format or its shorthand name" ) ,
2023-09-05 16:17:19 +00:00
} ,
}
for desc , tc := range cases {
t . Run ( desc , func ( t * testing . T ) {
ui := cli . NewMockUi ( )
c := New ( ui )
2023-09-13 18:48:40 +00:00
code := c . Run ( tc . args )
require . Equal ( t , tc . expectedCode , code )
require . Contains ( t , ui . ErrorWriter . String ( ) , tc . expectedErr . Error ( ) )
2023-09-05 16:17:19 +00:00
} )
}
}
2024-01-30 18:12:09 +00:00
func createResource ( t * testing . T , port int ) {
2023-09-13 18:48:40 +00:00
applyUi := cli . NewMockUi ( )
applyCmd := apply . New ( applyUi )
args := [ ] string {
2024-01-30 18:12:09 +00:00
fmt . Sprintf ( "-grpc-addr=127.0.0.1:%d" , port ) ,
2023-09-13 18:48:40 +00:00
"-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 ( ) )
}
2023-09-05 16:17:19 +00:00
func TestResourceRead ( t * testing . T ) {
2023-09-13 18:48:40 +00:00
if testing . Short ( ) {
t . Skip ( "too slow for testing.Short" )
}
t . Parallel ( )
2024-01-30 18:12:09 +00:00
availablePort := freeport . GetOne ( t )
a := agent . NewTestAgent ( t , fmt . Sprintf ( "ports { grpc = %d }" , availablePort ) )
2023-09-13 18:48:40 +00:00
testrpc . WaitForTestAgent ( t , a . RPC , "dc1" )
2024-01-30 18:12:09 +00:00
t . Cleanup ( func ( ) {
a . Shutdown ( )
} )
2023-09-13 18:48:40 +00:00
defaultCmdArgs := [ ] string {
2024-01-30 18:12:09 +00:00
fmt . Sprintf ( "-grpc-addr=127.0.0.1:%d" , availablePort ) ,
2023-09-13 18:48:40 +00:00
"-token=root" ,
}
2024-01-30 18:12:09 +00:00
createResource ( t , availablePort )
2023-09-13 18:48:40 +00:00
cases := [ ] struct {
name string
args [ ] string
expectedCode int
errMsg string
} {
{
name : "read resource in hcl format" ,
args : [ ] string { "-f=../testdata/demo.hcl" } ,
expectedCode : 0 ,
errMsg : "" ,
} ,
{
name : "read resource in command line format" ,
2024-01-29 20:08:31 +00:00
args : [ ] string { "demo.v2.Artist" , "korn" , "-partition=default" , "-namespace=default" } ,
2023-09-13 18:48:40 +00:00
expectedCode : 0 ,
errMsg : "" ,
} ,
{
name : "read resource that doesn't exist" ,
2024-01-29 20:08:31 +00:00
args : [ ] string { "demo.v2.Artist" , "fake-korn" , "-partition=default" , "-namespace=default" } ,
2023-09-13 18:48:40 +00:00
expectedCode : 1 ,
2024-01-30 18:12:09 +00:00
errMsg : "error reading resource: rpc error: code = NotFound desc = resource not found\n" ,
2023-09-13 18:48:40 +00:00
} ,
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
ui := cli . NewMockUi ( )
c := New ( ui )
cliArgs := append ( tc . args , defaultCmdArgs ... )
code := c . Run ( cliArgs )
2024-01-30 18:12:09 +00:00
require . Contains ( t , ui . ErrorWriter . String ( ) , tc . errMsg )
2023-09-13 18:48:40 +00:00
require . Equal ( t , tc . expectedCode , code )
} )
}
2023-09-05 16:17:19 +00:00
}