Skip to content

Commit 723c269

Browse files
authored
Merge pull request kubernetes#124916 from TessaIO/test-improve-unit-tests-for-container-name-autocompletion
test: improve unit tests for container name autocompletion
2 parents 2f0cf23 + fe81d0d commit 723c269

File tree

1 file changed

+64
-49
lines changed

1 file changed

+64
-49
lines changed

staging/src/k8s.io/kubectl/pkg/util/completion/completion_test.go

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -330,57 +330,69 @@ func TestPodResourceNameCompletionFuncJointFormTooManyArgs(t *testing.T) {
330330
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
331331
}
332332

333-
func TestPodResourceNameAndContainerCompletionFuncNoArgsPodName(t *testing.T) {
334-
tf, cmd := prepareCompletionTest()
335-
pods, _, _ := cmdtesting.TestData()
336-
addResourceToFactory(tf, pods)
337-
338-
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
339-
comps, directive := compFunc(cmd, []string{}, "b")
340-
checkCompletion(t, comps, []string{"bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
341-
}
342-
343-
func TestPodResourceNameAndContainerCompletionFuncNoArgsResources(t *testing.T) {
344-
tf, cmd := prepareCompletionTest()
345-
pods, _, _ := cmdtesting.TestData()
346-
addResourceToFactory(tf, pods)
347-
348-
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
349-
comps, directive := compFunc(cmd, []string{}, "s")
350-
checkCompletion(
351-
t, comps, []string{"services/", "statefulsets/"},
352-
directive, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveNoSpace)
353-
354-
}
355-
356-
func TestPodResourceNameAndContainerCompletionFuncTooManyArgs(t *testing.T) {
357-
tf, cmd := prepareCompletionTest()
358-
pods, _, _ := cmdtesting.TestData()
359-
addResourceToFactory(tf, pods)
360-
361-
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
362-
comps, directive := compFunc(cmd, []string{"pod-name", "container-name"}, "")
363-
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
364-
}
365-
366-
func TestPodResourceNameAndContainerCompletionFuncJointFormNoArgs(t *testing.T) {
367-
tf, cmd := prepareCompletionTest()
368-
pods, _, _ := cmdtesting.TestData()
369-
addResourceToFactory(tf, pods)
370-
371-
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
372-
comps, directive := compFunc(cmd, []string{}, "pod/b")
373-
checkCompletion(t, comps, []string{"pod/bar"}, directive, cobra.ShellCompDirectiveNoFileComp)
374-
}
333+
func TestResourceAndContainerNameCompletionFunc(t *testing.T) {
334+
barPod := getTestPod()
375335

376-
func TestPodResourceNameAndContainerCompletionFuncJointFormTooManyArgs(t *testing.T) {
377-
tf, cmd := prepareCompletionTest()
378-
pods, _, _ := cmdtesting.TestData()
379-
addResourceToFactory(tf, pods)
336+
testCases := []struct {
337+
name string
338+
args []string
339+
toComplete string
340+
expectedComps []string
341+
expectedDirective cobra.ShellCompDirective
342+
}{
343+
{
344+
name: "no args pod name",
345+
args: []string{},
346+
toComplete: "b",
347+
expectedComps: []string{"bar"},
348+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
349+
},
350+
{
351+
name: "no args resources",
352+
args: []string{},
353+
toComplete: "s",
354+
expectedComps: []string{"services/", "statefulsets/"},
355+
expectedDirective: cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace,
356+
},
357+
{
358+
name: "joint form no args",
359+
args: []string{},
360+
toComplete: "pod/b",
361+
expectedComps: []string{"pod/bar"},
362+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
363+
},
364+
{
365+
name: "joint form too many args",
366+
args: []string{"pod/pod-name", "container-name"},
367+
toComplete: "",
368+
expectedComps: []string{},
369+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
370+
},
371+
{
372+
name: "complete all containers' names",
373+
args: []string{"bar"},
374+
toComplete: "",
375+
expectedComps: []string{"bar", "foo"},
376+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
377+
},
378+
{
379+
name: "complete specific container name",
380+
args: []string{"bar"},
381+
toComplete: "b",
382+
expectedComps: []string{"bar"},
383+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
384+
},
385+
}
380386

381-
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
382-
comps, directive := compFunc(cmd, []string{"pod/pod-name", "container-name"}, "")
383-
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
387+
for _, tc := range testCases {
388+
t.Run(tc.name, func(t *testing.T) {
389+
tf, cmd := prepareCompletionTest()
390+
addResourceToFactory(tf, barPod)
391+
compFunc := PodResourceNameAndContainerCompletionFunc(tf)
392+
comps, directive := compFunc(cmd, tc.args, tc.toComplete)
393+
checkCompletion(t, comps, tc.expectedComps, directive, tc.expectedDirective)
394+
})
395+
}
384396
}
385397

386398
func TestResourceAndPortCompletionFunc(t *testing.T) {
@@ -539,6 +551,9 @@ func getTestPod() *corev1.Pod {
539551
},
540552
},
541553
},
554+
{
555+
Name: "foo",
556+
},
542557
},
543558
},
544559
}

0 commit comments

Comments
 (0)