From 8f27532ce0ee0438da66b08b23da330f59757d9c Mon Sep 17 00:00:00 2001 From: Alexander Hughes Date: Tue, 11 Feb 2020 14:35:38 -0500 Subject: [PATCH] [#34] Add tests to commands expecting N args Commands using cobra's ExactArgs feature previously did not have any validation that the ExactArgs was being enforced. This patchset adds tests to ensure that in the cases ExactArgs=n, that only n args are accepted, and other values such as x or y throw an appropriate error. Change-Id: I27b5774e79db51e0e9bc81d8c207be80811ba459 --- cmd/completion/completion_test.go | 21 ++++++++++++---- ...den => completion-cmd-too-few-args.golden} | 2 +- ...en => completion-cmd-too-many-args.golden} | 2 +- cmd/config/set_authinfo_test.go | 13 ++++++++++ cmd/config/set_context_test.go | 13 ++++++++++ ...onfig-cmd-set-authinfo-too-few-args.golden | 25 +++++++++++++++++++ ...nfig-cmd-set-authinfo-too-many-args.golden | 25 +++++++++++++++++++ ...config-cmd-set-context-too-few-args.golden | 21 ++++++++++++++++ ...onfig-cmd-set-context-too-many-args.golden | 21 ++++++++++++++++ 9 files changed, 136 insertions(+), 7 deletions(-) rename cmd/completion/testdata/TestCompletionGoldenOutput/{completion-no-args.golden => completion-cmd-too-few-args.golden} (68%) rename cmd/completion/testdata/TestCompletionGoldenOutput/{completion-too-many-args.golden => completion-cmd-too-many-args.golden} (58%) create mode 100644 cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-few-args.golden create mode 100644 cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-many-args.golden create mode 100644 cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden create mode 100644 cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden diff --git a/cmd/completion/completion_test.go b/cmd/completion/completion_test.go index 0e4791384..38e1b9309 100644 --- a/cmd/completion/completion_test.go +++ b/cmd/completion/completion_test.go @@ -2,6 +2,7 @@ package completion_test import ( "errors" + "fmt" "testing" "opendev.org/airship/airshipctl/cmd/completion" @@ -9,25 +10,35 @@ import ( ) func TestCompletion(t *testing.T) { - cmd := completion.NewCompletionCommand() - cmdTests := []*testutil.CmdTest{ { Name: "completion-bash", CmdLine: "bash", - Cmd: cmd, + Cmd: completion.NewCompletionCommand(), }, { Name: "completion-zsh", CmdLine: "zsh", - Cmd: cmd, + Cmd: completion.NewCompletionCommand(), }, { Name: "completion-unknown-shell", CmdLine: "fish", - Cmd: cmd, + Cmd: completion.NewCompletionCommand(), Error: errors.New("unsupported shell type \"fish\""), }, + { + Name: "completion-cmd-too-many-args", + CmdLine: "arg1 arg2", + Cmd: completion.NewCompletionCommand(), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2), + }, + { + Name: "completion-cmd-too-few-args", + CmdLine: "", + Cmd: completion.NewCompletionCommand(), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), + }, } for _, tt := range cmdTests { diff --git a/cmd/completion/testdata/TestCompletionGoldenOutput/completion-no-args.golden b/cmd/completion/testdata/TestCompletionGoldenOutput/completion-cmd-too-few-args.golden similarity index 68% rename from cmd/completion/testdata/TestCompletionGoldenOutput/completion-no-args.golden rename to cmd/completion/testdata/TestCompletionGoldenOutput/completion-cmd-too-few-args.golden index 5e588c0ca..79d8b2e10 100644 --- a/cmd/completion/testdata/TestCompletionGoldenOutput/completion-no-args.golden +++ b/cmd/completion/testdata/TestCompletionGoldenOutput/completion-cmd-too-few-args.golden @@ -1,4 +1,4 @@ -Error: shell not specified +Error: accepts 1 arg(s), received 0 Usage: completion SHELL [flags] diff --git a/cmd/completion/testdata/TestCompletionGoldenOutput/completion-too-many-args.golden b/cmd/completion/testdata/TestCompletionGoldenOutput/completion-cmd-too-many-args.golden similarity index 58% rename from cmd/completion/testdata/TestCompletionGoldenOutput/completion-too-many-args.golden rename to cmd/completion/testdata/TestCompletionGoldenOutput/completion-cmd-too-many-args.golden index 22323a2f4..acbc2a9de 100644 --- a/cmd/completion/testdata/TestCompletionGoldenOutput/completion-too-many-args.golden +++ b/cmd/completion/testdata/TestCompletionGoldenOutput/completion-cmd-too-many-args.golden @@ -1,4 +1,4 @@ -Error: too many arguments, expected only the shell type +Error: accepts 1 arg(s), received 2 Usage: completion SHELL [flags] diff --git a/cmd/config/set_authinfo_test.go b/cmd/config/set_authinfo_test.go index 99d2a1748..6c1a915fc 100644 --- a/cmd/config/set_authinfo_test.go +++ b/cmd/config/set_authinfo_test.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "fmt" "testing" kubeconfig "k8s.io/client-go/tools/clientcmd/api" @@ -54,6 +55,18 @@ func TestConfigSetAuthInfo(t *testing.T) { CmdLine: "--help", Cmd: NewCmdConfigSetAuthInfo(nil), }, + { + Name: "config-cmd-set-authinfo-too-many-args", + CmdLine: "arg1 arg2", + Cmd: NewCmdConfigSetAuthInfo(nil), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2), + }, + { + Name: "config-cmd-set-authinfo-too-few-args", + CmdLine: "", + Cmd: NewCmdConfigSetAuthInfo(nil), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), + }, } for _, tt := range cmdTests { diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index d7ba27e36..6fcad51f2 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "fmt" "testing" kubeconfig "k8s.io/client-go/tools/clientcmd/api" @@ -50,6 +51,18 @@ func TestConfigSetContext(t *testing.T) { CmdLine: "--help", Cmd: NewCmdConfigSetContext(nil), }, + { + Name: "config-cmd-set-context-too-many-args", + CmdLine: "arg1 arg2", + Cmd: NewCmdConfigSetContext(nil), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2), + }, + { + Name: "config-cmd-set-context-too-few-args", + CmdLine: "", + Cmd: NewCmdConfigSetContext(nil), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), + }, } for _, tt := range cmdTests { diff --git a/cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-few-args.golden b/cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-few-args.golden new file mode 100644 index 000000000..d5d12ad15 --- /dev/null +++ b/cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-few-args.golden @@ -0,0 +1,25 @@ +Error: accepts 1 arg(s), received 0 +Usage: + set-credentials NAME [flags] + +Examples: + +# Set only the "client-key" field on the "cluster-admin" +# entry, without touching other values: +airshipctl config set-credentials cluster-admin --username=~/.kube/admin.key + +# Set basic auth for the "cluster-admin" entry +airshipctl config set-credentials cluster-admin --username=admin --password=uXFGweU9l35qcif + +# Embed client certificate data in the "cluster-admin" entry +airshipctl config set-credentials cluster-admin --client-certificate=~/.kube/admin.crt --embed-certs=true + +Flags: + --client-certificate string Path to client-certificate file for the user entry in airshipctl + --client-key string Path to client-key file for the user entry in airshipctl + --embed-certs Embed client cert/key for the user entry in airshipctl + -h, --help help for set-credentials + --password string password for the user entry in airshipctl + --token string token for the user entry in airshipctl + --username string username for the user entry in airshipctl + diff --git a/cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-many-args.golden b/cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-many-args.golden new file mode 100644 index 000000000..04bd1edb6 --- /dev/null +++ b/cmd/config/testdata/TestConfigSetAuthInfoGoldenOutput/config-cmd-set-authinfo-too-many-args.golden @@ -0,0 +1,25 @@ +Error: accepts 1 arg(s), received 2 +Usage: + set-credentials NAME [flags] + +Examples: + +# Set only the "client-key" field on the "cluster-admin" +# entry, without touching other values: +airshipctl config set-credentials cluster-admin --username=~/.kube/admin.key + +# Set basic auth for the "cluster-admin" entry +airshipctl config set-credentials cluster-admin --username=admin --password=uXFGweU9l35qcif + +# Embed client certificate data in the "cluster-admin" entry +airshipctl config set-credentials cluster-admin --client-certificate=~/.kube/admin.crt --embed-certs=true + +Flags: + --client-certificate string Path to client-certificate file for the user entry in airshipctl + --client-key string Path to client-key file for the user entry in airshipctl + --embed-certs Embed client cert/key for the user entry in airshipctl + -h, --help help for set-credentials + --password string password for the user entry in airshipctl + --token string token for the user entry in airshipctl + --username string username for the user entry in airshipctl + diff --git a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden new file mode 100644 index 000000000..491364939 --- /dev/null +++ b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden @@ -0,0 +1,21 @@ +Error: accepts 1 arg(s), received 0 +Usage: + set-context NAME [flags] + +Examples: + +# Create a completely new e2e context entry +airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --user=auth-info --cluster-type=target + +# Update the current-context to e2e +airshipctl config set-context e2e --current-context=true + +Flags: + --cluster string cluster for the context entry in airshipctl config + --cluster-type string cluster-type for the context entry in airshipctl config + --current-context current-context for the context entry in airshipctl config + -h, --help help for set-context + --manifest string manifest for the context entry in airshipctl config + --namespace string namespace for the context entry in airshipctl config + --user string user for the context entry in airshipctl config + diff --git a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden new file mode 100644 index 000000000..67246757b --- /dev/null +++ b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden @@ -0,0 +1,21 @@ +Error: accepts 1 arg(s), received 2 +Usage: + set-context NAME [flags] + +Examples: + +# Create a completely new e2e context entry +airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --user=auth-info --cluster-type=target + +# Update the current-context to e2e +airshipctl config set-context e2e --current-context=true + +Flags: + --cluster string cluster for the context entry in airshipctl config + --cluster-type string cluster-type for the context entry in airshipctl config + --current-context current-context for the context entry in airshipctl config + -h, --help help for set-context + --manifest string manifest for the context entry in airshipctl config + --namespace string namespace for the context entry in airshipctl config + --user string user for the context entry in airshipctl config +