mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 13:55:41 +00:00
Add Go tests for custom completions
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit is contained in:
parent
879cdc5fa9
commit
c0b3a698fa
@ -24,6 +24,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@ -710,3 +711,10 @@ func TestAnnotateMultipleObjects(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
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)
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
@ -26,6 +26,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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 {
|
func attachPod() *corev1.Pod {
|
||||||
return &corev1.Pod{
|
return &corev1.Pod{
|
||||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "10"},
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "10"},
|
||||||
|
@ -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)
|
||||||
|
}
|
@ -24,8 +24,10 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
|
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type deleteClusterTest struct {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
@ -24,8 +24,10 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
|
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type deleteContextTest struct {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
@ -24,8 +24,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
|
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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)
|
||||||
|
}
|
||||||
|
@ -22,8 +22,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
|
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type useContextTest struct {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/cli-runtime/pkg/resource"
|
"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 {
|
type testDescriber struct {
|
||||||
Name, Namespace string
|
Name, Namespace string
|
||||||
Settings describe.DescriberSettings
|
Settings describe.DescriberSettings
|
||||||
|
@ -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 {
|
type MyReq struct {
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
func tryIndent(data []byte) []byte {
|
||||||
indented := &bytes.Buffer{}
|
indented := &bytes.Buffer{}
|
||||||
if err := json.Indent(indented, data, "", "\t"); err == nil {
|
if err := json.Indent(indented, data, "", "\t"); err == nil {
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"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")
|
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)
|
||||||
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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)
|
||||||
|
}
|
||||||
|
@ -29,6 +29,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||||
batchv1 "k8s.io/api/batch/v1"
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
@ -2733,6 +2734,13 @@ foo 0/0 0 <unknown>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
func watchBody(codec runtime.Codec, events []watch.Event) io.ReadCloser {
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
enc := restclientwatch.NewEncoder(streaming.NewEncoder(buf, codec), codec)
|
enc := restclientwatch.NewEncoder(streaming.NewEncoder(buf, codec), codec)
|
||||||
|
@ -24,6 +24,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@ -565,3 +566,10 @@ func TestLabelResourceVersion(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
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)
|
||||||
|
}
|
||||||
|
@ -30,6 +30,7 @@ import (
|
|||||||
"testing/iotest"
|
"testing/iotest"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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 {
|
type responseWrapperMock struct {
|
||||||
data io.Reader
|
data io.Reader
|
||||||
err error
|
err error
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/cli-runtime/pkg/resource"
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
"k8s.io/client-go/rest/fake"
|
"k8s.io/client-go/rest/fake"
|
||||||
@ -190,3 +191,10 @@ func TestPatchObjectFromFileOutput(t *testing.T) {
|
|||||||
t.Errorf("unexpected output: %s", buf.String())
|
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)
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
@ -22,6 +22,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"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 {
|
type RolloutPauseRESTClient struct {
|
||||||
*fake.RESTClient
|
*fake.RESTClient
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
38
staging/src/k8s.io/kubectl/pkg/cmd/scale/scale_test.go
Normal file
38
staging/src/k8s.io/kubectl/pkg/cmd/scale/scale_test.go
Normal file
@ -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)
|
||||||
|
}
|
@ -24,6 +24,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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 {
|
type MyReq struct {
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
}
|
}
|
||||||
|
158
staging/src/k8s.io/kubectl/pkg/cmd/testing/completion.go
Normal file
158
staging/src/k8s.io/kubectl/pkg/cmd/testing/completion.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -25,6 +25,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"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)
|
||||||
|
}
|
||||||
|
@ -26,6 +26,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
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 {
|
func testV1beta1PodMetricsData() []metricsv1beta1api.PodMetrics {
|
||||||
return []metricsv1beta1api.PodMetrics{
|
return []metricsv1beta1api.PodMetrics{
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user