Skip to content

Commit

Permalink
Added unit tests for completion helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasgun committed Dec 21, 2021
1 parent 558d864 commit 088b3c2
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
gopkg.in/yaml.v2 v2.4.0
gotest.tools/v3 v3.0.3
k8s.io/api v0.21.4
k8s.io/apiextensions-apiserver v0.21.4
Expand Down
2 changes: 2 additions & 0 deletions pkg/kn/commands/completion_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/spf13/cobra"
)

// ResourceNameCompletionFunc will return a function that will autocomplete the name of
// the resource based on the subcommand
func ResourceNameCompletionFunc(p *KnParams) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

Expand Down
197 changes: 197 additions & 0 deletions pkg/kn/commands/completion_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright © 2021 The Knative 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 commands

import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"

"github.com/spf13/cobra"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
clienttesting "k8s.io/client-go/testing"
v1 "knative.dev/client/pkg/serving/v1"
v12 "knative.dev/serving/pkg/apis/serving/v1"
servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake"
)

const (
testNs = "test-ns"
)

var (
testSvc1 = v12.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-1", Namespace: testNs},
}
testSvc2 = v12.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-2", Namespace: testNs},
}
testSvc3 = v12.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-svc-3", Namespace: testNs},
}
testNsServices = []v12.Service{testSvc1, testSvc2, testSvc3}

fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}}
knParams = &KnParams{
NewServingClient: func(namespace string) (v1.KnServingClient, error) {
return v1.NewKnServingClient(fakeServing, namespace), nil
},
NewGitopsServingClient: func(namespace string, dir string) (v1.KnServingClient, error) {
return v1.NewKnServingGitOpsClient(namespace, dir), nil
},
}
)

func Test_completeService(t *testing.T) {

fakeServing.AddReactor("list", "services",
func(a clienttesting.Action) (bool, runtime.Object, error) {
return true, &v12.ServiceList{Items: testNsServices}, nil
})
tests := []struct {
name string
namespace string
args []string
p *KnParams
toComplete string
expectedSuggestions []string
}{
{
"Suggestions when test-ns namespace set",
testNs,
nil,
knParams,
"",
[]string{"test-svc-1", "test-svc-2", "test-svc-3"},
},
{
"Empty suggestions when non zero args",
testNs,
[]string{"xyz"},
knParams,
"",
[]string{},
},
{
"Empty suggestions when toComplete is not valid for completion",
testNs,
nil,
knParams,
"xyz",
[]string{},
},
}
for _, tt := range tests {
testCmd := testCommandGenerator(false)
testCmd.SetArgs([]string{"--namespace", tt.namespace})
testCmd.Execute()
actualSuggestions, directive := completeService(tt.p, testCmd, tt.args, tt.toComplete)
assert.DeepEqual(t, tt.expectedSuggestions, actualSuggestions)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
}
}

func Test_completeGitOps(t *testing.T) {
tempDir := setupTempDir(t)
assert.Assert(t, tempDir != "")
defer os.RemoveAll(tempDir)

tests := []struct {
name string
namespace string
args []string
p *KnParams
toComplete string
expectedSuggestions []string
}{
{
"Suggestions when test-ns namespace set",
testNs,
nil,
knParams,
"",
[]string{"test-svc-1", "test-svc-2", "test-svc-3"},
},
{
"Empty suggestions when non zero args",
testNs,
[]string{"xyz"},
knParams,
"",
[]string{},
},
{
"Empty suggestions when toComplete is not valid for completion",
testNs,
nil,
knParams,
"xyz",
[]string{},
},
}
for _, tt := range tests {
testCmd := testCommandGenerator(false)
testCmd.Flags().String("target", "", "")
testCmd.SetArgs([]string{"--namespace", tt.namespace, "--target", tempDir})
testCmd.Execute()
actualSuggestions, directive := completeGitOps(tt.p, testCmd, tt.args, tt.toComplete, tempDir)
assert.DeepEqual(t, tt.expectedSuggestions, actualSuggestions)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
}
}

func setupTempDir(t *testing.T) string {
tempDir, err := ioutil.TempDir("", "test-dir")
assert.NilError(t, err)

svcPath := path.Join(tempDir, "test-ns", "ksvc")
err = os.MkdirAll(svcPath, 0700)
assert.NilError(t, err)

for i, testSvc := range []v12.Service{testSvc1, testSvc2, testSvc3} {
tempFile, err := os.Create(path.Join(svcPath, fmt.Sprintf("test-svc-%d.yaml", i+1)))
assert.NilError(t, err)
writeToFile(t, testSvc, tempFile)
}

return tempDir
}

func writeToFile(t *testing.T, testSvc v12.Service, tempFile *os.File) {
yamlPrinter, err := genericclioptions.NewJSONYamlPrintFlags().ToPrinter("yaml")
assert.NilError(t, err)

err = yamlPrinter.PrintObj(&testSvc, tempFile)
assert.NilError(t, err)

defer tempFile.Close()
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ gopkg.in/inf.v0
# gopkg.in/ini.v1 v1.62.0
gopkg.in/ini.v1
# gopkg.in/yaml.v2 v2.4.0
## explicit
gopkg.in/yaml.v2
# gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gopkg.in/yaml.v3
Expand Down

0 comments on commit 088b3c2

Please sign in to comment.