mirror of https://github.com/knative/client.git
Added completion for route name (#1561)
* Added completion for route * Added tests for route completion
This commit is contained in:
parent
569e027d98
commit
07d0cd75c7
|
|
@ -32,6 +32,7 @@ var (
|
|||
"service": completeService,
|
||||
"revision": completeRevision,
|
||||
"broker": completeBroker,
|
||||
"route": completeRoute,
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -177,3 +178,29 @@ func completeRevision(config *completionConfig) (suggestions []string) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
func completeRoute(config *completionConfig) (suggestions []string) {
|
||||
suggestions = make([]string, 0)
|
||||
if len(config.args) != 0 {
|
||||
return
|
||||
}
|
||||
namespace, err := config.params.GetNamespace(config.command)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
client, err := config.params.NewServingClient(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
routeList, err := client.ListRoutes(config.command.Context())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, sug := range routeList.Items {
|
||||
if !strings.HasPrefix(sug.Name, config.toComplete) {
|
||||
continue
|
||||
}
|
||||
suggestions = append(suggestions, sug.Name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,22 +104,6 @@ var (
|
|||
fakeEventing = &fake.FakeEventingV1{Fake: &clienttesting.Fake{}}
|
||||
)
|
||||
|
||||
var knParams = initialiseKnParams()
|
||||
|
||||
func initialiseKnParams() *KnParams {
|
||||
return &KnParams{
|
||||
NewServingClient: func(namespace string) (v1.KnServingClient, error) {
|
||||
return v1.NewKnServingClient(fakeServing, namespace), nil
|
||||
},
|
||||
NewGitopsServingClient: func(namespace string, dir string) (v1.KnServingClient, error) {
|
||||
return v1.NewKnServingGitOpsClient(namespace, dir), nil
|
||||
},
|
||||
NewEventingClient: func(namespace string) (clienteventingv1.KnEventingClient, error) {
|
||||
return clienteventingv1.NewKnEventingClient(fakeEventing, namespace), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
testRev1 = servingv1.Revision{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
|
|
@ -145,6 +129,47 @@ var (
|
|||
testNsRevs = []servingv1.Revision{testRev1, testRev2, testRev3}
|
||||
)
|
||||
|
||||
var (
|
||||
testRoute1 = servingv1.Route{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "Route",
|
||||
APIVersion: "serving.knative.dev/v1",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "test-route-1", Namespace: testNs},
|
||||
}
|
||||
testRoute2 = servingv1.Route{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "Route",
|
||||
APIVersion: "serving.knative.dev/v1",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "test-route-2", Namespace: testNs},
|
||||
}
|
||||
testRoute3 = servingv1.Route{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "Route",
|
||||
APIVersion: "serving.knative.dev/v1",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "test-route-3", Namespace: testNs},
|
||||
}
|
||||
testNsRoutes = []servingv1.Route{testRoute1, testRoute2, testRoute3}
|
||||
)
|
||||
|
||||
var knParams = initialiseKnParams()
|
||||
|
||||
func initialiseKnParams() *KnParams {
|
||||
return &KnParams{
|
||||
NewServingClient: func(namespace string) (v1.KnServingClient, error) {
|
||||
return v1.NewKnServingClient(fakeServing, namespace), nil
|
||||
},
|
||||
NewGitopsServingClient: func(namespace string, dir string) (v1.KnServingClient, error) {
|
||||
return v1.NewKnServingGitOpsClient(namespace, dir), nil
|
||||
},
|
||||
NewEventingClient: func(namespace string) (clienteventingv1.KnEventingClient, error) {
|
||||
return clienteventingv1.NewKnEventingClient(fakeEventing, namespace), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceNameCompletionFuncService(t *testing.T) {
|
||||
completionFunc := ResourceNameCompletionFunc(knParams)
|
||||
|
||||
|
|
@ -466,6 +491,84 @@ func TestResourceNameCompletionFuncGitOps(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestResourceNameCompletionFuncRoute(t *testing.T) {
|
||||
completionFunc := ResourceNameCompletionFunc(knParams)
|
||||
|
||||
fakeServing.AddReactor("list", "routes",
|
||||
func(a clienttesting.Action) (bool, runtime.Object, error) {
|
||||
if a.GetNamespace() == errorNs {
|
||||
return true, nil, errors.NewInternalError(fmt.Errorf("unable to list services"))
|
||||
}
|
||||
return true, &servingv1.RouteList{Items: testNsRoutes}, nil
|
||||
})
|
||||
|
||||
tests := []testType{
|
||||
{
|
||||
"Empty suggestions when non-zero args",
|
||||
testNs,
|
||||
knParams,
|
||||
[]string{"xyz"},
|
||||
"",
|
||||
"route",
|
||||
},
|
||||
{
|
||||
"Empty suggestions when no namespace flag",
|
||||
"",
|
||||
knParams,
|
||||
nil,
|
||||
"",
|
||||
"route",
|
||||
},
|
||||
{
|
||||
"Suggestions when test-ns namespace set",
|
||||
testNs,
|
||||
knParams,
|
||||
nil,
|
||||
"",
|
||||
"route",
|
||||
},
|
||||
{
|
||||
"Empty suggestions when toComplete is not a prefix",
|
||||
testNs,
|
||||
knParams,
|
||||
nil,
|
||||
"xyz",
|
||||
"route",
|
||||
},
|
||||
{
|
||||
"Empty suggestions when error during list operation",
|
||||
errorNs,
|
||||
knParams,
|
||||
nil,
|
||||
"",
|
||||
"route",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
cmd := getResourceCommandWithTestSubcommand(tt.resource, tt.namespace != "", tt.resource != "no-parent")
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
config := &completionConfig{
|
||||
params: tt.p,
|
||||
command: cmd,
|
||||
args: tt.args,
|
||||
toComplete: tt.toComplete,
|
||||
}
|
||||
expectedFunc := resourceToFuncMap[tt.resource]
|
||||
if expectedFunc == nil {
|
||||
expectedFunc = func(config *completionConfig) []string {
|
||||
return []string{}
|
||||
}
|
||||
}
|
||||
cmd.Flags().Set("namespace", tt.namespace)
|
||||
actualSuggestions, actualDirective := completionFunc(cmd, tt.args, tt.toComplete)
|
||||
expectedSuggestions := expectedFunc(config)
|
||||
expectedDirective := cobra.ShellCompDirectiveNoFileComp
|
||||
assert.DeepEqual(t, actualSuggestions, expectedSuggestions)
|
||||
assert.Equal(t, actualDirective, expectedDirective)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func getResourceCommandWithTestSubcommand(resource string, addNamespace, addSubcommand bool) *cobra.Command {
|
||||
testCommand := &cobra.Command{
|
||||
Use: resource,
|
||||
|
|
|
|||
|
|
@ -32,8 +32,9 @@ func NewRouteDescribeCommand(p *commands.KnParams) *cobra.Command {
|
|||
// For machine readable output
|
||||
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")
|
||||
command := &cobra.Command{
|
||||
Use: "describe NAME",
|
||||
Short: "Show details of a route",
|
||||
Use: "describe NAME",
|
||||
Short: "Show details of a route",
|
||||
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return errors.New("'kn route describe' requires name of the route as single argument")
|
||||
|
|
|
|||
Loading…
Reference in New Issue