mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
Add unit tests for cluster, context, and user command line completion functions
This commit is contained in:
parent
31dba0a435
commit
996f79738e
@ -446,6 +446,12 @@ func (f *TestFactory) WithNamespace(ns string) *TestFactory {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithClientConfig sets the client config to use
|
||||||
|
func (f *TestFactory) WithClientConfig(clientConfig clientcmd.ClientConfig) *TestFactory {
|
||||||
|
f.kubeConfigFlags.WithClientConfig(clientConfig)
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanup cleans up TestFactory temp config file
|
// Cleanup cleans up TestFactory temp config file
|
||||||
func (f *TestFactory) Cleanup() {
|
func (f *TestFactory) Cleanup() {
|
||||||
if f.tempConfigFile == nil {
|
if f.tempConfigFile == nil {
|
||||||
|
@ -22,16 +22,92 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
|
"k8s.io/client-go/tools/clientcmd/api"
|
||||||
"k8s.io/kubectl/pkg/cmd/get"
|
"k8s.io/kubectl/pkg/cmd/get"
|
||||||
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||||
|
|
||||||
// cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
|
||||||
"k8s.io/kubectl/pkg/scheme"
|
"k8s.io/kubectl/pkg/scheme"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestClusterCompletionFunc(t *testing.T) {
|
||||||
|
setMockFactory(api.Config{
|
||||||
|
Clusters: map[string]*api.Cluster{
|
||||||
|
"bar": {},
|
||||||
|
"baz": {},
|
||||||
|
"foo": {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
comps, directive := ClusterCompletionFunc(nil, []string{}, "")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz", "foo"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ClusterCompletionFunc(nil, []string{}, "b")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ClusterCompletionFunc(nil, []string{}, "ba")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ClusterCompletionFunc(nil, []string{}, "bar")
|
||||||
|
checkCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ClusterCompletionFunc(nil, []string{}, "bart")
|
||||||
|
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextCompletionFunc(t *testing.T) {
|
||||||
|
setMockFactory(api.Config{
|
||||||
|
Contexts: map[string]*api.Context{
|
||||||
|
"bar": {},
|
||||||
|
"baz": {},
|
||||||
|
"foo": {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
comps, directive := ContextCompletionFunc(nil, []string{}, "")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz", "foo"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ContextCompletionFunc(nil, []string{}, "b")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ContextCompletionFunc(nil, []string{}, "ba")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ContextCompletionFunc(nil, []string{}, "bar")
|
||||||
|
checkCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = ContextCompletionFunc(nil, []string{}, "bart")
|
||||||
|
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUserCompletionFunc(t *testing.T) {
|
||||||
|
setMockFactory(api.Config{
|
||||||
|
AuthInfos: map[string]*api.AuthInfo{
|
||||||
|
"bar": {},
|
||||||
|
"baz": {},
|
||||||
|
"foo": {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
comps, directive := UserCompletionFunc(nil, []string{}, "")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz", "foo"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = UserCompletionFunc(nil, []string{}, "b")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = UserCompletionFunc(nil, []string{}, "ba")
|
||||||
|
checkCompletion(t, comps, []string{"bar", "baz"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = UserCompletionFunc(nil, []string{}, "bar")
|
||||||
|
checkCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
|
||||||
|
comps, directive = UserCompletionFunc(nil, []string{}, "bart")
|
||||||
|
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
|
}
|
||||||
|
|
||||||
func TestResourceTypeAndNameCompletionFuncOneArg(t *testing.T) {
|
func TestResourceTypeAndNameCompletionFuncOneArg(t *testing.T) {
|
||||||
tf, cmd := prepareCompletionTest()
|
tf, cmd := prepareCompletionTest()
|
||||||
addPodsToFactory(tf)
|
addPodsToFactory(tf)
|
||||||
@ -134,6 +210,12 @@ func TestPodResourceNameAndContainerCompletionFuncTooManyArgs(t *testing.T) {
|
|||||||
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setMockFactory(config api.Config) {
|
||||||
|
clientConfig := clientcmd.NewDefaultClientConfig(config, nil)
|
||||||
|
testFactory := cmdtesting.NewTestFactory().WithClientConfig(clientConfig)
|
||||||
|
SetFactoryForCompletion(testFactory)
|
||||||
|
}
|
||||||
|
|
||||||
func prepareCompletionTest() (*cmdtesting.TestFactory, *cobra.Command) {
|
func prepareCompletionTest() (*cmdtesting.TestFactory, *cobra.Command) {
|
||||||
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
||||||
defer tf.Cleanup()
|
defer tf.Cleanup()
|
||||||
|
Loading…
Reference in New Issue
Block a user