Update test assertions related to spf13/cobra.

It now correctly prints errors to stderr (https://github.com/spf13/cobra/pull/894).

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer 2020-12-17 14:39:22 -06:00
parent 6a457466df
commit 3e15e184ef
No known key found for this signature in database
GPG Key ID: EAE88AD172C5AE2D
5 changed files with 33 additions and 35 deletions

View File

@ -8,11 +8,7 @@ import (
) )
//nolint: gochecknoglobals //nolint: gochecknoglobals
var getCmd = &cobra.Command{ var getCmd = &cobra.Command{Use: "get", Short: "get"}
Use: "get",
Short: "get",
SilenceUsage: true, // do not print usage message when commands fail
}
//nolint: gochecknoinits //nolint: gochecknoinits
func init() { func init() {

View File

@ -81,7 +81,7 @@ func TestGetKubeconfig(t *testing.T) {
args: []string{}, args: []string{},
getPathToSelfErr: fmt.Errorf("some OS error"), getPathToSelfErr: fmt.Errorf("some OS error"),
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not determine the Pinniped executable path: some OS error Error: could not determine the Pinniped executable path: some OS error
`), `),
}, },
@ -91,7 +91,7 @@ func TestGetKubeconfig(t *testing.T) {
"--oidc-ca-bundle", "./does/not/exist", "--oidc-ca-bundle", "./does/not/exist",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not read --oidc-ca-bundle: open ./does/not/exist: no such file or directory Error: could not read --oidc-ca-bundle: open ./does/not/exist: no such file or directory
`), `),
}, },
@ -101,7 +101,7 @@ func TestGetKubeconfig(t *testing.T) {
"--kubeconfig", "./does/not/exist", "--kubeconfig", "./does/not/exist",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not load --kubeconfig: stat ./does/not/exist: no such file or directory Error: could not load --kubeconfig: stat ./does/not/exist: no such file or directory
`), `),
}, },
@ -112,7 +112,7 @@ func TestGetKubeconfig(t *testing.T) {
"--kubeconfig-context", "invalid", "--kubeconfig-context", "invalid",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not load --kubeconfig/--kubeconfig-context: no such context "invalid" Error: could not load --kubeconfig/--kubeconfig-context: no such context "invalid"
`), `),
}, },
@ -123,7 +123,7 @@ func TestGetKubeconfig(t *testing.T) {
}, },
getClientsetErr: fmt.Errorf("some kube error"), getClientsetErr: fmt.Errorf("some kube error"),
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not configure Kubernetes client: some kube error Error: could not configure Kubernetes client: some kube error
`), `),
}, },
@ -135,7 +135,7 @@ func TestGetKubeconfig(t *testing.T) {
"--concierge-authenticator-name", "test-authenticator", "--concierge-authenticator-name", "test-authenticator",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: webhookauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found Error: webhookauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found
`), `),
}, },
@ -147,7 +147,7 @@ func TestGetKubeconfig(t *testing.T) {
"--concierge-authenticator-name", "test-authenticator", "--concierge-authenticator-name", "test-authenticator",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: jwtauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found Error: jwtauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found
`), `),
}, },
@ -159,7 +159,7 @@ func TestGetKubeconfig(t *testing.T) {
"--concierge-authenticator-name", "test-authenticator", "--concierge-authenticator-name", "test-authenticator",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: invalid authenticator type "invalid", supported values are "webhook" and "jwt" Error: invalid authenticator type "invalid", supported values are "webhook" and "jwt"
`), `),
}, },
@ -178,7 +178,7 @@ func TestGetKubeconfig(t *testing.T) {
}, },
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: failed to list JWTAuthenticator objects for autodiscovery: some list error Error: failed to list JWTAuthenticator objects for autodiscovery: some list error
`), `),
}, },
@ -197,7 +197,7 @@ func TestGetKubeconfig(t *testing.T) {
}, },
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: failed to list WebhookAuthenticator objects for autodiscovery: some list error Error: failed to list WebhookAuthenticator objects for autodiscovery: some list error
`), `),
}, },
@ -207,7 +207,7 @@ func TestGetKubeconfig(t *testing.T) {
"--kubeconfig", "./testdata/kubeconfig.yaml", "--kubeconfig", "./testdata/kubeconfig.yaml",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: no authenticators were found in namespace "pinniped-concierge" (try setting --concierge-namespace) Error: no authenticators were found in namespace "pinniped-concierge" (try setting --concierge-namespace)
`), `),
}, },
@ -224,7 +224,7 @@ func TestGetKubeconfig(t *testing.T) {
&conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator-4", Namespace: "test-namespace"}}, &conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator-4", Namespace: "test-namespace"}},
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: multiple authenticators were found in namespace "test-namespace", so the --concierge-authenticator-type/--concierge-authenticator-name flags must be specified Error: multiple authenticators were found in namespace "test-namespace", so the --concierge-authenticator-type/--concierge-authenticator-name flags must be specified
`), `),
}, },
@ -238,7 +238,7 @@ func TestGetKubeconfig(t *testing.T) {
&conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}}, &conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}},
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not autodiscover --oidc-issuer, and none was provided Error: could not autodiscover --oidc-issuer, and none was provided
`), `),
}, },
@ -259,7 +259,7 @@ func TestGetKubeconfig(t *testing.T) {
}, },
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: tried to autodiscover --oidc-ca-bundle, but JWTAuthenticator test-namespace/test-authenticator has invalid spec.tls.certificateAuthorityData: illegal base64 data at input byte 7 Error: tried to autodiscover --oidc-ca-bundle, but JWTAuthenticator test-namespace/test-authenticator has invalid spec.tls.certificateAuthorityData: illegal base64 data at input byte 7
`), `),
}, },
@ -275,7 +275,7 @@ func TestGetKubeconfig(t *testing.T) {
&conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}}, &conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}},
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: only one of --static-token and --static-token-env can be specified Error: only one of --static-token and --static-token-env can be specified
`), `),
}, },

View File

@ -79,7 +79,7 @@ func TestLoginOIDCCommand(t *testing.T) {
name: "missing required flags", name: "missing required flags",
args: []string{}, args: []string{},
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: required flag(s) "issuer" not set Error: required flag(s) "issuer" not set
`), `),
}, },
@ -91,7 +91,7 @@ func TestLoginOIDCCommand(t *testing.T) {
"--enable-concierge", "--enable-concierge",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: invalid concierge parameters: endpoint must not be empty Error: invalid concierge parameters: endpoint must not be empty
`), `),
}, },
@ -103,7 +103,7 @@ func TestLoginOIDCCommand(t *testing.T) {
"--ca-bundle", "./does/not/exist", "--ca-bundle", "./does/not/exist",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not read --ca-bundle: open ./does/not/exist: no such file or directory Error: could not read --ca-bundle: open ./does/not/exist: no such file or directory
`), `),
}, },
@ -115,7 +115,7 @@ func TestLoginOIDCCommand(t *testing.T) {
"--ca-bundle-data", "invalid-base64", "--ca-bundle-data", "invalid-base64",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not read --ca-bundle-data: illegal base64 data at input byte 7 Error: could not read --ca-bundle-data: illegal base64 data at input byte 7
`), `),
}, },
@ -128,7 +128,7 @@ func TestLoginOIDCCommand(t *testing.T) {
loginErr: fmt.Errorf("some login error"), loginErr: fmt.Errorf("some login error"),
wantOptionsCount: 3, wantOptionsCount: 3,
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not complete Pinniped login: some login error Error: could not complete Pinniped login: some login error
`), `),
}, },
@ -145,7 +145,7 @@ func TestLoginOIDCCommand(t *testing.T) {
conciergeErr: fmt.Errorf("some concierge error"), conciergeErr: fmt.Errorf("some concierge error"),
wantOptionsCount: 3, wantOptionsCount: 3,
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not complete concierge credential exchange: some concierge error Error: could not complete concierge credential exchange: some concierge error
`), `),
}, },

View File

@ -66,7 +66,7 @@ func TestLoginStaticCommand(t *testing.T) {
name: "missing required flags", name: "missing required flags",
args: []string{}, args: []string{},
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: one of --token or --token-env must be set Error: one of --token or --token-env must be set
`), `),
}, },
@ -77,7 +77,7 @@ func TestLoginStaticCommand(t *testing.T) {
"--enable-concierge", "--enable-concierge",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: invalid concierge parameters: endpoint must not be empty Error: invalid concierge parameters: endpoint must not be empty
`), `),
}, },
@ -87,7 +87,7 @@ func TestLoginStaticCommand(t *testing.T) {
"--token-env", "TEST_TOKEN_ENV", "--token-env", "TEST_TOKEN_ENV",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: --token-env variable "TEST_TOKEN_ENV" is not set Error: --token-env variable "TEST_TOKEN_ENV" is not set
`), `),
}, },
@ -100,7 +100,7 @@ func TestLoginStaticCommand(t *testing.T) {
"TEST_TOKEN_ENV": "", "TEST_TOKEN_ENV": "",
}, },
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: --token-env variable "TEST_TOKEN_ENV" is empty Error: --token-env variable "TEST_TOKEN_ENV" is empty
`), `),
}, },
@ -125,7 +125,7 @@ func TestLoginStaticCommand(t *testing.T) {
}, },
conciergeErr: fmt.Errorf("some concierge error"), conciergeErr: fmt.Errorf("some concierge error"),
wantError: true, wantError: true,
wantStdout: here.Doc(` wantStderr: here.Doc(`
Error: could not complete concierge credential exchange: some concierge error Error: could not complete concierge credential exchange: some concierge error
`), `),
}, },

View File

@ -7,6 +7,7 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.pinniped.dev/internal/here" "go.pinniped.dev/internal/here"
@ -41,7 +42,7 @@ func TestNewVersionCmd(t *testing.T) {
args []string args []string
wantError bool wantError bool
wantStdoutRegexp string wantStdoutRegexp string
wantStderr string wantStderrRegexp string
}{ }{
{ {
name: "no flags", name: "no flags",
@ -57,7 +58,8 @@ func TestNewVersionCmd(t *testing.T) {
name: "arg passed", name: "arg passed",
args: []string{"tuna"}, args: []string{"tuna"},
wantError: true, wantError: true,
wantStdoutRegexp: `Error: unknown command "tuna" for "version"` + "\n" + knownGoodUsageRegexpForVersion, wantStderrRegexp: `Error: unknown command "tuna" for "version"`,
wantStdoutRegexp: knownGoodUsageRegexpForVersion,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -76,8 +78,8 @@ func TestNewVersionCmd(t *testing.T) {
} else { } else {
require.NoError(t, err) require.NoError(t, err)
} }
require.Regexp(t, tt.wantStdoutRegexp, stdout.String(), "unexpected stdout") assert.Regexp(t, tt.wantStdoutRegexp, stdout.String(), "unexpected stdout")
require.Equal(t, tt.wantStderr, stderr.String(), "unexpected stderr") assert.Regexp(t, tt.wantStderrRegexp, stderr.String(), "unexpected stderr")
}) })
} }
} }