Skip to content

Commit

Permalink
Added completion for route name (#1561)
Browse files Browse the repository at this point in the history
* Added completion for route

* Added tests for route completion
  • Loading branch information
vyasgun authored Jan 10, 2022
1 parent 569e027 commit 07d0cd7
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 18 deletions.
27 changes: 27 additions & 0 deletions pkg/kn/commands/completion_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
"service": completeService,
"revision": completeRevision,
"broker": completeBroker,
"route": completeRoute,
}
)

Expand Down Expand Up @@ -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
}
135 changes: 119 additions & 16 deletions pkg/kn/commands/completion_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions pkg/kn/commands/route/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 07d0cd7

Please sign in to comment.