test: improve unit tests for container name autocompletion

Signed-off-by: TessaIO <ahmedgrati1999@gmail.com>
This commit is contained in:
TessaIO 2024-05-16 21:40:50 +02:00
parent 0aa01be424
commit fe81d0d5ac

View File

@ -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 {
t.Run(tc.name, func(t *testing.T) {
tf, cmd := prepareCompletionTest() tf, cmd := prepareCompletionTest()
pods, _, _ := cmdtesting.TestData() addResourceToFactory(tf, barPod)
addResourceToFactory(tf, pods)
compFunc := PodResourceNameAndContainerCompletionFunc(tf) compFunc := PodResourceNameAndContainerCompletionFunc(tf)
comps, directive := compFunc(cmd, []string{}, "s") comps, directive := compFunc(cmd, tc.args, tc.toComplete)
checkCompletion( checkCompletion(t, comps, tc.expectedComps, directive, tc.expectedDirective)
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",
},
}, },
}, },
} }