Merge pull request #124916 from TessaIO/test-improve-unit-tests-for-container-name-autocompletion
test: improve unit tests for container name autocompletion Kubernetes-commit: 723c2696411a282f5d4441b6bf61cc5173cd0e45
This commit is contained in:
commit
21275cf4d0
|
|
@ -330,57 +330,69 @@ func TestPodResourceNameCompletionFuncJointFormTooManyArgs(t *testing.T) {
|
||||||
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPodResourceNameAndContainerCompletionFuncNoArgsPodName(t *testing.T) {
|
func TestResourceAndContainerNameCompletionFunc(t *testing.T) {
|
||||||
tf, cmd := prepareCompletionTest()
|
barPod := getTestPod()
|
||||||
pods, _, _ := cmdtesting.TestData()
|
|
||||||
addResourceToFactory(tf, pods)
|
|
||||||
|
|
||||||
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
|
testCases := []struct {
|
||||||
comps, directive := compFunc(cmd, []string{}, "b")
|
name string
|
||||||
checkCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
args []string
|
||||||
}
|
toComplete string
|
||||||
|
expectedComps []string
|
||||||
|
expectedDirective cobra.ShellCompDirective
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no args pod name",
|
||||||
|
args: []string{},
|
||||||
|
toComplete: "b",
|
||||||
|
expectedComps: []string{"bar"},
|
||||||
|
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no args resources",
|
||||||
|
args: []string{},
|
||||||
|
toComplete: "s",
|
||||||
|
expectedComps: []string{"services/", "statefulsets/"},
|
||||||
|
expectedDirective: cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "joint form no args",
|
||||||
|
args: []string{},
|
||||||
|
toComplete: "pod/b",
|
||||||
|
expectedComps: []string{"pod/bar"},
|
||||||
|
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "joint form too many args",
|
||||||
|
args: []string{"pod/pod-name", "container-name"},
|
||||||
|
toComplete: "",
|
||||||
|
expectedComps: []string{},
|
||||||
|
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "complete all containers' names",
|
||||||
|
args: []string{"bar"},
|
||||||
|
toComplete: "",
|
||||||
|
expectedComps: []string{"bar", "foo"},
|
||||||
|
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "complete specific container name",
|
||||||
|
args: []string{"bar"},
|
||||||
|
toComplete: "b",
|
||||||
|
expectedComps: []string{"bar"},
|
||||||
|
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func TestPodResourceNameAndContainerCompletionFuncNoArgsResources(t *testing.T) {
|
for _, tc := range testCases {
|
||||||
tf, cmd := prepareCompletionTest()
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
pods, _, _ := cmdtesting.TestData()
|
tf, cmd := prepareCompletionTest()
|
||||||
addResourceToFactory(tf, pods)
|
addResourceToFactory(tf, barPod)
|
||||||
|
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
|
||||||
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
|
comps, directive := compFunc(cmd, tc.args, tc.toComplete)
|
||||||
comps, directive := compFunc(cmd, []string{}, "s")
|
checkCompletion(t, comps, tc.expectedComps, directive, tc.expectedDirective)
|
||||||
checkCompletion(
|
})
|
||||||
t, comps, []string{"services/", "statefulsets/"},
|
}
|
||||||
directive, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveNoSpace)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPodResourceNameAndContainerCompletionFuncTooManyArgs(t *testing.T) {
|
|
||||||
tf, cmd := prepareCompletionTest()
|
|
||||||
pods, _, _ := cmdtesting.TestData()
|
|
||||||
addResourceToFactory(tf, pods)
|
|
||||||
|
|
||||||
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
|
|
||||||
comps, directive := compFunc(cmd, []string{"pod-name", "container-name"}, "")
|
|
||||||
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPodResourceNameAndContainerCompletionFuncJointFormNoArgs(t *testing.T) {
|
|
||||||
tf, cmd := prepareCompletionTest()
|
|
||||||
pods, _, _ := cmdtesting.TestData()
|
|
||||||
addResourceToFactory(tf, pods)
|
|
||||||
|
|
||||||
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
|
|
||||||
comps, directive := compFunc(cmd, []string{}, "pod/b")
|
|
||||||
checkCompletion(t, comps, []string{"pod/bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPodResourceNameAndContainerCompletionFuncJointFormTooManyArgs(t *testing.T) {
|
|
||||||
tf, cmd := prepareCompletionTest()
|
|
||||||
pods, _, _ := cmdtesting.TestData()
|
|
||||||
addResourceToFactory(tf, pods)
|
|
||||||
|
|
||||||
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
|
|
||||||
comps, directive := compFunc(cmd, []string{"pod/pod-name", "container-name"}, "")
|
|
||||||
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceAndPortCompletionFunc(t *testing.T) {
|
func TestResourceAndPortCompletionFunc(t *testing.T) {
|
||||||
|
|
@ -539,6 +551,9 @@ func getTestPod() *corev1.Pod {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue