From c0b3a698fa15265eecdec0bd8776658c40d8d151 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 5 Mar 2021 15:44:30 -0500 Subject: [PATCH] Add Go tests for custom completions Signed-off-by: Marc Khouzam --- .../kubectl/pkg/cmd/annotate/annotate_test.go | 8 + .../cmd/apply/apply_edit_last_applied_test.go | 31 ++++ .../cmd/apply/apply_view_last_applied_test.go | 31 ++++ .../kubectl/pkg/cmd/attach/attach_test.go | 8 + .../pkg/cmd/autoscale/autoscale_test.go | 38 +++++ .../pkg/cmd/config/delete_cluster_test.go | 17 ++ .../pkg/cmd/config/delete_context_test.go | 17 ++ .../pkg/cmd/config/rename_context_test.go | 17 ++ .../pkg/cmd/config/use_context_test.go | 17 ++ .../kubectl/pkg/cmd/delete/delete_test.go | 15 ++ .../kubectl/pkg/cmd/describe/describe_test.go | 8 + .../kubectl/pkg/cmd/drain/drain_test.go | 21 +++ .../k8s.io/kubectl/pkg/cmd/edit/edit_test.go | 7 + .../k8s.io/kubectl/pkg/cmd/exec/exec_test.go | 8 + .../kubectl/pkg/cmd/expose/expose_test.go | 16 ++ .../k8s.io/kubectl/pkg/cmd/get/get_test.go | 8 + .../kubectl/pkg/cmd/label/label_test.go | 8 + .../k8s.io/kubectl/pkg/cmd/logs/logs_test.go | 8 + .../kubectl/pkg/cmd/patch/patch_test.go | 8 + .../pkg/cmd/portforward/portforward_test.go | 7 + .../pkg/cmd/rollout/rollout_history_test.go | 38 +++++ .../pkg/cmd/rollout/rollout_pause_test.go | 15 ++ .../pkg/cmd/rollout/rollout_restart_test.go | 38 +++++ .../pkg/cmd/rollout/rollout_resume_test.go | 38 +++++ .../pkg/cmd/rollout/rollout_status_test.go | 38 +++++ .../pkg/cmd/rollout/rollout_undo_test.go | 38 +++++ .../kubectl/pkg/cmd/scale/scale_test.go | 38 +++++ .../kubectl/pkg/cmd/taint/taint_test.go | 15 ++ .../kubectl/pkg/cmd/testing/completion.go | 158 ++++++++++++++++++ .../kubectl/pkg/cmd/top/top_node_test.go | 8 + .../kubectl/pkg/cmd/top/top_pod_test.go | 8 + 31 files changed, 730 insertions(+) create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_edit_last_applied_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_view_last_applied_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_resume_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_undo_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/scale/scale_test.go create mode 100644 staging/src/k8s.io/kubectl/pkg/cmd/testing/completion.go diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go index c1306f1fa44..4636b66b06d 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go @@ -24,6 +24,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -710,3 +711,10 @@ func TestAnnotateMultipleObjects(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } + +func TestCompletionAnnotateOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdAnnotate("kubectl", tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_edit_last_applied_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_edit_last_applied_test.go new file mode 100644 index 00000000000..e6b17629d3b --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_edit_last_applied_test.go @@ -0,0 +1,31 @@ +/* +Copyright 2021 The Kubernetes 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 apply + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionApplyEditLastAppliedOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdApplyEditLastApplied(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_view_last_applied_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_view_last_applied_test.go new file mode 100644 index 00000000000..e331acf0929 --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_view_last_applied_test.go @@ -0,0 +1,31 @@ +/* +Copyright 2021 The Kubernetes 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 apply + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionApplyViewLastAppliedOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdApplyViewLastApplied(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go index c9b809a441a..6f02f5ad121 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -447,6 +448,13 @@ func TestAttachWarnings(t *testing.T) { } } +func TestCompletionAttachNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdAttach(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + func attachPod() *corev1.Pod { return &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "10"}, diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale_test.go new file mode 100644 index 00000000000..e68e8321fbf --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 autoscale + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionAutoscaleNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdAutoscale(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment", "replicationcontroller", "replicaset", "statefulset"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionAutoscaleOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdAutoscale(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go index 3becba3fdf6..52149cfc2b6 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go @@ -24,8 +24,10 @@ import ( "reflect" "testing" + "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" ) type deleteClusterTest struct { @@ -95,3 +97,18 @@ func (test deleteClusterTest) run(t *testing.T) { t.Errorf("expected clusters %v, but found %v in kubeconfig", test.expectedClusters, clusters) } } + +func TestCompletionDeleteClusterNoArg(t *testing.T) { + // FIXME + t.Skip("Should not be skipeed") + + pathOptions, streams, factory := cmdtesting.PrepareConfigForCompletion(t) + configCmd := NewCmdConfig(factory, pathOptions, streams) + cmd, _, err := configCmd.Find([]string{"delete-cluster"}) + if err != nil { + t.Fatalf("unexpected error finding delete-cluster command: %v", err) + } + + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "my") + cmdtesting.CheckCompletion(t, comps, []string{"my-cluster"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go index 04bbe17cbb0..2d5de6af1d4 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go @@ -24,8 +24,10 @@ import ( "reflect" "testing" + "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" ) type deleteContextTest struct { @@ -96,3 +98,18 @@ func (test deleteContextTest) run(t *testing.T) { t.Errorf("expected contexts %v, but found %v in kubeconfig", test.expectedContexts, contexts) } } + +func TestCompletionDeleteContextNoArg(t *testing.T) { + // FIXME + t.Skip("Should not be skipeed") + + pathOptions, streams, factory := cmdtesting.PrepareConfigForCompletion(t) + configCmd := NewCmdConfig(factory, pathOptions, streams) + cmd, _, err := configCmd.Find([]string{"delete-context"}) + if err != nil { + t.Fatalf("unexpected error finding delete-context command: %v", err) + } + + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "my") + cmdtesting.CheckCompletion(t, comps, []string{"my-context"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go index cfacb90102a..ed3758e45a5 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go @@ -24,8 +24,10 @@ import ( "strings" "testing" + "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" ) const ( @@ -154,3 +156,18 @@ func (test renameContextTest) run(t *testing.T) { } } } + +func TestCompletionRenameContextNoArg(t *testing.T) { + // FIXME + t.Skip("Should not be skipeed") + + pathOptions, streams, factory := cmdtesting.PrepareConfigForCompletion(t) + configCmd := NewCmdConfig(factory, pathOptions, streams) + cmd, _, err := configCmd.Find([]string{"rename-context"}) + if err != nil { + t.Fatalf("unexpected error finding rename-context command: %v", err) + } + + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "my") + cmdtesting.CheckCompletion(t, comps, []string{"my-context"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go index 74424bc51b9..59488a13d4f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go @@ -22,8 +22,10 @@ import ( "os" "testing" + "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" ) type useContextTest struct { @@ -102,3 +104,18 @@ func (test useContextTest) run(t *testing.T) { t.Errorf("Failed in :%q\n expected config %v, but found %v\n in kubeconfig\n", test.description, test.expectedConfig, config) } } + +func TestCompletionUseContextNoArg(t *testing.T) { + // FIXME + t.Skip("Should not be skipeed") + + pathOptions, streams, factory := cmdtesting.PrepareConfigForCompletion(t) + configCmd := NewCmdConfig(factory, pathOptions, streams) + cmd, _, err := configCmd.Find([]string{"use-context"}) + if err != nil { + t.Fatalf("unexpected error finding use-context command: %v", err) + } + + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "my") + cmdtesting.CheckCompletion(t, comps, []string{"my-context"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go index dbe94cad3e7..e2781d03605 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go @@ -832,3 +832,18 @@ func TestResourceErrors(t *testing.T) { }) } } + +// TODO +// func TestCompletionDeleteNoArg(t *testing.T) { +// tf, streams := cmdtesting.PreparePodsForCompletion(t) +// cmd := NewCmdDelete(tf, streams) +// comps, directive := cmd.ValidArgsFunction(cmd, []string{""}, "po") +// cmdtesting.CheckCompletionOfResources(t, comps, directive) +// } + +func TestCompletionDeleteOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdDelete(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/describe/describe_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/describe/describe_test.go index b6fbfc444ba..c942bc1073a 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/describe/describe_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/describe/describe_test.go @@ -22,6 +22,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/resource" @@ -323,6 +324,13 @@ func TestDescribeNoResourcesFound(t *testing.T) { } } +func TestCompletionDescribeOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdDescribe("kubectl", tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + type testDescriber struct { Name, Namespace string Settings describe.DescriberSettings diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go index eacef7fd300..5569cc98a22 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go @@ -930,6 +930,27 @@ func TestDrain(t *testing.T) { } } +func TestCompletionCordonNoArg(t *testing.T) { + tf, streams := cmdtesting.PrepareNodesForCompletion(t) + cmd := NewCmdCordon(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "f") + cmdtesting.CheckCompletion(t, comps, []string{"firstnode"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionDrainNoArg(t *testing.T) { + tf, streams := cmdtesting.PrepareNodesForCompletion(t) + cmd := NewCmdDrain(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "f") + cmdtesting.CheckCompletion(t, comps, []string{"firstnode"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionUncordonNoArg(t *testing.T) { + tf, streams := cmdtesting.PrepareNodesForCompletion(t) + cmd := NewCmdUncordon(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "f") + cmdtesting.CheckCompletion(t, comps, []string{"firstnode"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + type MyReq struct { Request *http.Request } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go index df1d07ba4a1..dd89aac8fd2 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go @@ -283,6 +283,13 @@ func TestEdit(t *testing.T) { } } +func TestCompletionEditOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdEdit(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + func tryIndent(data []byte) []byte { indented := &bytes.Buffer{} if err := json.Indent(indented, data, "", "\t"); err == nil { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go index 814e3e87c44..7bd7f4c7a2d 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go @@ -27,6 +27,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" @@ -408,3 +409,10 @@ func TestSetupTTY(t *testing.T) { t.Errorf("attach stdin, TTY, is a terminal: tty.Out should equal o.Out") } } + +func TestCompletionExecNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdExec(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/expose/expose_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/expose/expose_test.go index 0cb40502012..b1027d8299e 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/expose/expose_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/expose/expose_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -676,3 +677,18 @@ func TestRunExposeService(t *testing.T) { }) } } + +func TestCompletionExposeNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdExposeService(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + expectedComps := []string{"deployment", "pod", "replicaset", "replicationcontroller", "service"} + cmdtesting.CheckCompletion(t, comps, expectedComps, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionExposeOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdExposeService(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go index ee0e39a7b66..6f1898b28a8 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go @@ -29,6 +29,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" batchv1 "k8s.io/api/batch/v1" @@ -2733,6 +2734,13 @@ foo 0/0 0 } } +func TestCompletionGetOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdGet("kubectl", tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + func watchBody(codec runtime.Codec, events []watch.Event) io.ReadCloser { buf := bytes.NewBuffer([]byte{}) enc := restclientwatch.NewEncoder(streaming.NewEncoder(buf, codec), codec) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go index 63176eedabe..b8e065c8684 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go @@ -24,6 +24,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -565,3 +566,10 @@ func TestLabelResourceVersion(t *testing.T) { t.Fatalf("unexpected error: %v", err) } } + +func TestCompletionLabelOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdLabel(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go index cb9b7cc9228..c40acff99bb 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go @@ -30,6 +30,7 @@ import ( "testing/iotest" "time" + "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -833,6 +834,13 @@ func TestNoResourceFoundMessage(t *testing.T) { } } +func TestCompletionLogsNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdLogs(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + type responseWrapperMock struct { data io.Reader err error diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch_test.go index 1fce8bfeda1..0d5ac1f6f71 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/resource" "k8s.io/client-go/rest/fake" @@ -190,3 +191,10 @@ func TestPatchObjectFromFileOutput(t *testing.T) { t.Errorf("unexpected output: %s", buf.String()) } } + +func TestCompletionPatchOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdPatch(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward_test.go index 06b99081ac6..058f0db7be7 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/portforward/portforward_test.go @@ -980,3 +980,10 @@ func TestCheckUDPPort(t *testing.T) { } } } + +func TestCompletionPortForwardNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdPortForward(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go new file mode 100644 index 00000000000..2d1c7b7d91a --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 rollout + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionRolloutHistoryNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutHistory(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment", "daemonset", "statefulset"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionRolloutHistoryOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutHistory(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go index 779ce0366c4..6f888f97567 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go @@ -22,6 +22,7 @@ import ( "net/http" "testing" + "github.com/spf13/cobra" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -72,6 +73,20 @@ func TestRolloutPause(t *testing.T) { } } +func TestCompletionRolloutPauseNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutPause(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionRolloutPauseOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutPause(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + type RolloutPauseRESTClient struct { *fake.RESTClient } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go new file mode 100644 index 00000000000..b2f1379f14c --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 rollout + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionRolloutRestartNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutRestart(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment", "daemonset", "statefulset"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionRolloutRestartOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutRestart(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_resume_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_resume_test.go new file mode 100644 index 00000000000..cf4be53cd4a --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_resume_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 rollout + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionRolloutResumeNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutResume(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionRolloutResumeOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutResume(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go new file mode 100644 index 00000000000..bcb0eef7627 --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 rollout + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionRolloutStatusNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutStatus(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment", "daemonset", "statefulset"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionRolloutStatusOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutStatus(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_undo_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_undo_test.go new file mode 100644 index 00000000000..c28d3767734 --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_undo_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 rollout + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionRolloutUndoNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutUndo(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment", "daemonset", "statefulset"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionRolloutUndoOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdRolloutUndo(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale_test.go new file mode 100644 index 00000000000..53d5aca3525 --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2021 The Kubernetes 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 scale + +import ( + "testing" + + "github.com/spf13/cobra" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" +) + +func TestCompletionScaleNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdScale(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"deployment", "replicaset", "replicationcontroller", "statefulset"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionScaleOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdScale(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go index a154c97dfd8..200bd96e0c5 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go @@ -24,6 +24,7 @@ import ( "testing" "time" + "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -388,6 +389,20 @@ func TestValidateFlags(t *testing.T) { } } +func TestCompletionTaintNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdTaint(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "") + cmdtesting.CheckCompletion(t, comps, []string{"node"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + +func TestCompletionTaintOneArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdTaint(tf, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{"pods"}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + type MyReq struct { Request *http.Request } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/testing/completion.go b/staging/src/k8s.io/kubectl/pkg/cmd/testing/completion.go new file mode 100644 index 00000000000..6ca72ac8975 --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/testing/completion.go @@ -0,0 +1,158 @@ +/* +Copyright 2021 The Kubernetes 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 testing + +import ( + "io/ioutil" + "net/http" + "os" + "sort" + "testing" + "time" + + "github.com/spf13/cobra" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/cli-runtime/pkg/resource" + "k8s.io/client-go/rest/fake" + "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + cmdutil "k8s.io/kubectl/pkg/cmd/util" + "k8s.io/kubectl/pkg/scheme" +) + +// PreparePodsForCompletion prepares the test factory and streams for pods +func PreparePodsForCompletion(t *testing.T) (cmdutil.Factory, genericclioptions.IOStreams) { + pods, _, _ := TestData() + + tf := NewTestFactory().WithNamespace("test") + defer tf.Cleanup() + codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) + + tf.UnstructuredClient = &fake.RESTClient{ + NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, + Resp: &http.Response{StatusCode: http.StatusOK, Header: DefaultHeader(), Body: ObjBody(codec, pods)}, + } + + streams, _, _, _ := genericclioptions.NewTestIOStreams() + return tf, streams +} + +// PrepareNodesForCompletion prepares the test factory and streams for pods +func PrepareNodesForCompletion(t *testing.T) (cmdutil.Factory, genericclioptions.IOStreams) { + // TODO create more than one node + // nodes := &corev1.NodeList{ + // ListMeta: metav1.ListMeta{ + // ResourceVersion: "1", + // }, + // Items: []corev1.Node{ + // { + // ObjectMeta: metav1.ObjectMeta{ + // Name: "firstnode", + // CreationTimestamp: metav1.Time{Time: time.Now()}, + // }, + // Status: corev1.NodeStatus{}, + // }, + // { + // ObjectMeta: metav1.ObjectMeta{ + // Name: "secondnode", + // CreationTimestamp: metav1.Time{Time: time.Now()}, + // }, + // Status: corev1.NodeStatus{}, + // }, + // }, + // } + + nodes := &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "firstnode", + CreationTimestamp: metav1.Time{Time: time.Now()}, + }, + Status: corev1.NodeStatus{}, + } + + tf := NewTestFactory() + defer tf.Cleanup() + + codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) + ns := scheme.Codecs.WithoutConversion() + + tf.UnstructuredClient = &fake.RESTClient{ + NegotiatedSerializer: ns, + Resp: &http.Response{StatusCode: http.StatusOK, Header: DefaultHeader(), Body: ObjBody(codec, nodes)}, + } + + streams, _, _, _ := genericclioptions.NewTestIOStreams() + return tf, streams +} + +// PrepareConfigForCompletion prepares some contexts for completion testing +func PrepareConfigForCompletion(t *testing.T) (*clientcmd.PathOptions, genericclioptions.IOStreams, cmdutil.Factory) { + conf := clientcmdapi.Config{ + Kind: "Config", + APIVersion: "v1", + Clusters: map[string]*clientcmdapi.Cluster{ + "minikube-cluster": {Server: "https://192.168.99.100:8443"}, + "my-cluster": {Server: "https://192.168.0.1:3434"}, + }, + Contexts: map[string]*clientcmdapi.Context{ + "minikube-context": {AuthInfo: "minikube", Cluster: "minikube"}, + "my-context": {AuthInfo: "my-context", Cluster: "my-context"}, + }, + CurrentContext: "minikube-context", + } + + fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + defer os.Remove(fakeKubeFile.Name()) + err = clientcmd.WriteToFile(conf, fakeKubeFile.Name()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + pathOptions := clientcmd.NewDefaultPathOptions() + pathOptions.GlobalFile = fakeKubeFile.Name() + pathOptions.EnvVar = "" + + streams, _, _, _ := genericclioptions.NewTestIOStreams() + factory := cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()) + + return pathOptions, streams, factory +} + +// CheckCompletion checks that the directive is correct and that each completion is present +func CheckCompletion(t *testing.T, comps, expectedComps []string, directive, expectedDirective cobra.ShellCompDirective) { + if e, d := expectedDirective, directive; e != d { + t.Errorf("expected directive\n%v\nbut got\n%v", e, d) + } + + sort.Strings(comps) + sort.Strings(expectedComps) + + if len(expectedComps) != len(comps) { + t.Fatalf("expected completions\n%v\nbut got\n%v", expectedComps, comps) + } + + for i := range comps { + if expectedComps[i] != comps[i] { + t.Errorf("expected completions\n%v\nbut got\n%v", expectedComps, comps) + break + } + } +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go index dca44b83f24..528b7358ed2 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go @@ -25,6 +25,7 @@ import ( "strings" "testing" + "github.com/spf13/cobra" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -424,3 +425,10 @@ func TestTopNodeWithSortByMemoryMetricsFrom(t *testing.T) { } } + +func TestCompletionTopNodeNoArg(t *testing.T) { + tf, streams := cmdtesting.PrepareNodesForCompletion(t) + cmd := NewCmdTopNode(tf, nil, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "f") + cmdtesting.CheckCompletion(t, comps, []string{"firstnode"}, directive, cobra.ShellCompDirectiveNoFileComp) +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go index d8b575cc79f..7986f77bdc0 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/spf13/cobra" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -393,6 +394,13 @@ func TestTopPodNoResourcesFound(t *testing.T) { } } +func TestCompletionTopPodNoArg(t *testing.T) { + tf, streams := cmdtesting.PreparePodsForCompletion(t) + cmd := NewCmdTopPod(tf, nil, streams) + comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "b") + cmdtesting.CheckCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp) +} + func testV1beta1PodMetricsData() []metricsv1beta1api.PodMetrics { return []metricsv1beta1api.PodMetrics{ {