Skip to content

Commit

Permalink
Testing for RequireResourceTypeAndName
Browse files Browse the repository at this point in the history
Added new clivalidation_test.go with a test for RequireResrouceType
Also Updated delete_test, list_test, and show_test in cli/resources so that the arguments tests for those commands would include testing for ambigous types

Signed-off-by: Josh <[email protected]>
  • Loading branch information
jhandel committed Feb 8, 2024
1 parent 845e67f commit 420a8ba
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/cli/clivalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func RequireResource(cmd *cobra.Command, args []string) (resourceType string, re
// are present. If either is missing, an error is returned.
func RequireResourceTypeAndName(args []string) (string, string, error) {
if len(args) < 2 {
return "", "", errors.New("No resource type or name provided")
return "", "", errors.New("no resource type or name provided")
}
resourceType, err := RequireResourceType(args)
if err != nil {
Expand Down
82 changes: 82 additions & 0 deletions pkg/cli/clivalidation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2024 The Radius Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"errors"
"fmt"
"strings"
"testing"

"github.com/radius-project/radius/pkg/cli/clients"
"github.com/stretchr/testify/require"
)

func Test_RequireResourceType(t *testing.T) {

supportedTypes := []string{}

for _, resourceType := range clients.ResourceTypesList {
supportedType := strings.Split(resourceType, "/")[1]
supportedTypes = append(supportedTypes, supportedType)
}

resourceTypesErrorString := strings.Join(supportedTypes, "\n")

tests := []struct {
name string
args []string
want string
wantErr error
}{
{
name: "No arguments",
args: []string{},
want: "",
wantErr: errors.New("no resource type provided"),
},
{
name: "Supported resource type",
args: []string{"mongoDatabases"},
want: "Applications.Datastores/mongoDatabases",
wantErr: nil,
},
{
name: "Multiple resource types",
args: []string{"secretStores"},
want: "",
wantErr: fmt.Errorf("multiple resource types match 'secretStores'. Please specify the full resource type and try again:\n\nApplications.Dapr/secretStores\nApplications.Core/secretStores\n"),
},
{
name: "Unsupported resource type",
args: []string{"unsupported"},
want: "",
wantErr: fmt.Errorf("'unsupported' is not a valid resource type. Available Types are: \n\n" + resourceTypesErrorString + "\n"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RequireResourceType(tt.args)
if len(tt.want) > 0 {
require.Equal(t, tt.want, got)
} else {
require.Equal(t, tt.wantErr, err)
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/cli/cmd/resource/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "List Command with ambiguous args",
Input: []string{"secretStores"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cli/cmd/resource/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "List Command with ambiguous args",
Input: []string{"secretStores"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cli/cmd/resource/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "List Command with ambiguous args",
Input: []string{"secretStores"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)
}
Expand Down

0 comments on commit 420a8ba

Please sign in to comment.