Added completion for domain command (#1562)

* Added completion for domain command

* Added tests for domain completion
This commit is contained in:
Gunjan Vyas 2022-01-12 19:49:51 +05:30 committed by GitHub
parent 1fcab77a08
commit d8670f5762
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 146 additions and 4 deletions

View File

@ -29,10 +29,11 @@ type completionConfig struct {
var ( var (
resourceToFuncMap = map[string]func(config *completionConfig) []string{ resourceToFuncMap = map[string]func(config *completionConfig) []string{
"service": completeService,
"revision": completeRevision,
"broker": completeBroker, "broker": completeBroker,
"domain": completeDomain,
"revision": completeRevision,
"route": completeRoute, "route": completeRoute,
"service": completeService,
} }
) )
@ -43,7 +44,7 @@ func ResourceNameCompletionFunc(p *KnParams) func(cmd *cobra.Command, args []str
var use string var use string
if cmd.Parent() != nil { if cmd.Parent() != nil {
use = cmd.Parent().Use use = cmd.Parent().Name()
} }
config := completionConfig{ config := completionConfig{
p, p,
@ -204,3 +205,29 @@ func completeRoute(config *completionConfig) (suggestions []string) {
} }
return return
} }
func completeDomain(config *completionConfig) (suggestions []string) {
suggestions = make([]string, 0)
if len(config.args) != 0 {
return
}
namespace, err := config.params.GetNamespace(config.command)
if err != nil {
return
}
client, err := config.params.NewServingV1alpha1Client(namespace)
if err != nil {
return
}
domainMappingList, err := client.ListDomainMappings(config.command.Context())
if err != nil {
return
}
for _, sug := range domainMappingList.Items {
if !strings.HasPrefix(sug.Name, config.toComplete) {
continue
}
suggestions = append(suggestions, sug.Name)
}
return
}

View File

@ -25,15 +25,20 @@ import (
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientv1alpha1 "knative.dev/client/pkg/serving/v1alpha1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
clienttesting "k8s.io/client-go/testing" clienttesting "k8s.io/client-go/testing"
clienteventingv1 "knative.dev/client/pkg/eventing/v1" clienteventingv1 "knative.dev/client/pkg/eventing/v1"
v1 "knative.dev/client/pkg/serving/v1" v1 "knative.dev/client/pkg/serving/v1"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
"knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1/fake" "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1/fake"
servingv1 "knative.dev/serving/pkg/apis/serving/v1" servingv1 "knative.dev/serving/pkg/apis/serving/v1"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake" servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake"
servingv1alpha1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"
) )
type testType struct { type testType struct {
@ -74,7 +79,8 @@ var (
} }
testNsServices = []servingv1.Service{testSvc1, testSvc2, testSvc3} testNsServices = []servingv1.Service{testSvc1, testSvc2, testSvc3}
fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}} fakeServing = &servingv1fake.FakeServingV1{Fake: &clienttesting.Fake{}}
fakeServingAlpha = &servingv1alpha1fake.FakeServingV1alpha1{Fake: &clienttesting.Fake{}}
) )
var ( var (
@ -154,6 +160,31 @@ var (
testNsRoutes = []servingv1.Route{testRoute1, testRoute2, testRoute3} testNsRoutes = []servingv1.Route{testRoute1, testRoute2, testRoute3}
) )
var (
testDomain1 = v1alpha1.DomainMapping{
TypeMeta: metav1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-domain-1", Namespace: testNs},
}
testDomain2 = v1alpha1.DomainMapping{
TypeMeta: metav1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-domain-2", Namespace: testNs},
}
testDomain3 = v1alpha1.DomainMapping{
TypeMeta: metav1.TypeMeta{
Kind: "DomainMapping",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-domain-3", Namespace: testNs},
}
testNsDomains = []v1alpha1.DomainMapping{testDomain1, testDomain2, testDomain3}
)
var knParams = initialiseKnParams() var knParams = initialiseKnParams()
func initialiseKnParams() *KnParams { func initialiseKnParams() *KnParams {
@ -167,6 +198,9 @@ func initialiseKnParams() *KnParams {
NewEventingClient: func(namespace string) (clienteventingv1.KnEventingClient, error) { NewEventingClient: func(namespace string) (clienteventingv1.KnEventingClient, error) {
return clienteventingv1.NewKnEventingClient(fakeEventing, namespace), nil return clienteventingv1.NewKnEventingClient(fakeEventing, namespace), nil
}, },
NewServingV1alpha1Client: func(namespace string) (clientv1alpha1.KnServingClient, error) {
return clientv1alpha1.NewKnServingClient(fakeServingAlpha, namespace), nil
},
} }
} }
@ -569,6 +603,84 @@ func TestResourceNameCompletionFuncRoute(t *testing.T) {
} }
} }
func TestResourceNameCompletionFuncDomain(t *testing.T) {
completionFunc := ResourceNameCompletionFunc(knParams)
fakeServingAlpha.AddReactor("list", "domainmappings",
func(a clienttesting.Action) (bool, runtime.Object, error) {
if a.GetNamespace() == errorNs {
return true, nil, errors.NewInternalError(fmt.Errorf("unable to list services"))
}
return true, &v1alpha1.DomainMappingList{Items: testNsDomains}, nil
})
tests := []testType{
{
"Empty suggestions when non-zero args",
testNs,
knParams,
[]string{"xyz"},
"",
"domain",
},
{
"Empty suggestions when no namespace flag",
"",
knParams,
nil,
"",
"domain",
},
{
"Suggestions when test-ns namespace set",
testNs,
knParams,
nil,
"",
"domain",
},
{
"Empty suggestions when toComplete is not a prefix",
testNs,
knParams,
nil,
"xyz",
"domain",
},
{
"Empty suggestions when error during list operation",
errorNs,
knParams,
nil,
"",
"domain",
},
}
for _, tt := range tests {
cmd := getResourceCommandWithTestSubcommand(tt.resource, tt.namespace != "", tt.resource != "no-parent")
t.Run(tt.name, func(t *testing.T) {
config := &completionConfig{
params: tt.p,
command: cmd,
args: tt.args,
toComplete: tt.toComplete,
}
expectedFunc := resourceToFuncMap[tt.resource]
if expectedFunc == nil {
expectedFunc = func(config *completionConfig) []string {
return []string{}
}
}
cmd.Flags().Set("namespace", tt.namespace)
actualSuggestions, actualDirective := completionFunc(cmd, tt.args, tt.toComplete)
expectedSuggestions := expectedFunc(config)
expectedDirective := cobra.ShellCompDirectiveNoFileComp
assert.DeepEqual(t, actualSuggestions, expectedSuggestions)
assert.Equal(t, actualDirective, expectedDirective)
})
}
}
func getResourceCommandWithTestSubcommand(resource string, addNamespace, addSubcommand bool) *cobra.Command { func getResourceCommandWithTestSubcommand(resource string, addNamespace, addSubcommand bool) *cobra.Command {
testCommand := &cobra.Command{ testCommand := &cobra.Command{
Use: resource, Use: resource,

View File

@ -32,6 +32,7 @@ func NewDomainMappingDeleteCommand(p *commands.KnParams) *cobra.Command {
Example: ` Example: `
# Delete domain mappings 'hello.example.com' # Delete domain mappings 'hello.example.com'
kn domain delete hello.example.com`, kn domain delete hello.example.com`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 { if len(args) != 1 {
return errors.New("'kn domain delete' requires the domain name given as single argument") return errors.New("'kn domain delete' requires the domain name given as single argument")

View File

@ -38,6 +38,7 @@ func NewDomainMappingDescribeCommand(p *commands.KnParams) *cobra.Command {
Example: ` Example: `
# Show details of for the domain 'hello.example.com' # Show details of for the domain 'hello.example.com'
kn domain describe hello.example.com`, kn domain describe hello.example.com`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 { if len(args) != 1 {
return errors.New("'kn domain describe' requires name of the domain mapping as single argument") return errors.New("'kn domain describe' requires name of the domain mapping as single argument")

View File

@ -36,6 +36,7 @@ func NewDomainMappingUpdateCommand(p *commands.KnParams) *cobra.Command {
Example: ` Example: `
# Update a domain mappings 'hello.example.com' for Knative service 'hello' # Update a domain mappings 'hello.example.com' for Knative service 'hello'
kn domain update hello.example.com --refFlags hello`, kn domain update hello.example.com --refFlags hello`,
ValidArgsFunction: commands.ResourceNameCompletionFunc(p),
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 { if len(args) != 1 {
return errors.New("'kn domain create' requires the domain name given as single argument") return errors.New("'kn domain create' requires the domain name given as single argument")