[#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
This commit is contained in:
Alexander Hughes 2020-02-11 14:35:38 -05:00
parent 70daee6d3c
commit 8f27532ce0
9 changed files with 136 additions and 7 deletions

View File

@ -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 {

View File

@ -1,4 +1,4 @@
Error: shell not specified
Error: accepts 1 arg(s), received 0
Usage:
completion SHELL [flags]

View File

@ -1,4 +1,4 @@
Error: too many arguments, expected only the shell type
Error: accepts 1 arg(s), received 2
Usage:
completion SHELL [flags]

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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