From 3e15e184ef5047e6beee3a9b5ddc70514ef61fa6 Mon Sep 17 00:00:00 2001 From: Matt Moyer Date: Thu, 17 Dec 2020 14:39:22 -0600 Subject: [PATCH] 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 --- cmd/pinniped/cmd/get.go | 6 +----- cmd/pinniped/cmd/kubeconfig_test.go | 30 +++++++++++++-------------- cmd/pinniped/cmd/login_oidc_test.go | 12 +++++------ cmd/pinniped/cmd/login_static_test.go | 10 ++++----- cmd/pinniped/cmd/version_test.go | 10 +++++---- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/cmd/pinniped/cmd/get.go b/cmd/pinniped/cmd/get.go index 27a027ae0..4b86d5fc3 100644 --- a/cmd/pinniped/cmd/get.go +++ b/cmd/pinniped/cmd/get.go @@ -8,11 +8,7 @@ import ( ) //nolint: gochecknoglobals -var getCmd = &cobra.Command{ - Use: "get", - Short: "get", - SilenceUsage: true, // do not print usage message when commands fail -} +var getCmd = &cobra.Command{Use: "get", Short: "get"} //nolint: gochecknoinits func init() { diff --git a/cmd/pinniped/cmd/kubeconfig_test.go b/cmd/pinniped/cmd/kubeconfig_test.go index e0fc8480a..b1317c5c1 100644 --- a/cmd/pinniped/cmd/kubeconfig_test.go +++ b/cmd/pinniped/cmd/kubeconfig_test.go @@ -81,7 +81,7 @@ func TestGetKubeconfig(t *testing.T) { args: []string{}, getPathToSelfErr: fmt.Errorf("some OS error"), wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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"), wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: could not configure Kubernetes client: some kube error `), }, @@ -135,7 +135,7 @@ func TestGetKubeconfig(t *testing.T) { "--concierge-authenticator-name", "test-authenticator", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: webhookauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found `), }, @@ -147,7 +147,7 @@ func TestGetKubeconfig(t *testing.T) { "--concierge-authenticator-name", "test-authenticator", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: jwtauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found `), }, @@ -159,7 +159,7 @@ func TestGetKubeconfig(t *testing.T) { "--concierge-authenticator-name", "test-authenticator", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: invalid authenticator type "invalid", supported values are "webhook" and "jwt" `), }, @@ -178,7 +178,7 @@ func TestGetKubeconfig(t *testing.T) { }, }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: failed to list JWTAuthenticator objects for autodiscovery: some list error `), }, @@ -197,7 +197,7 @@ func TestGetKubeconfig(t *testing.T) { }, }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: failed to list WebhookAuthenticator objects for autodiscovery: some list error `), }, @@ -207,7 +207,7 @@ func TestGetKubeconfig(t *testing.T) { "--kubeconfig", "./testdata/kubeconfig.yaml", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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"}}, }, 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 `), }, @@ -238,7 +238,7 @@ func TestGetKubeconfig(t *testing.T) { &conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}}, }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: could not autodiscover --oidc-issuer, and none was provided `), }, @@ -259,7 +259,7 @@ func TestGetKubeconfig(t *testing.T) { }, }, 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 `), }, @@ -275,7 +275,7 @@ func TestGetKubeconfig(t *testing.T) { &conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}}, }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: only one of --static-token and --static-token-env can be specified `), }, diff --git a/cmd/pinniped/cmd/login_oidc_test.go b/cmd/pinniped/cmd/login_oidc_test.go index 726264188..da7515a0d 100644 --- a/cmd/pinniped/cmd/login_oidc_test.go +++ b/cmd/pinniped/cmd/login_oidc_test.go @@ -79,7 +79,7 @@ func TestLoginOIDCCommand(t *testing.T) { name: "missing required flags", args: []string{}, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: required flag(s) "issuer" not set `), }, @@ -91,7 +91,7 @@ func TestLoginOIDCCommand(t *testing.T) { "--enable-concierge", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: invalid concierge parameters: endpoint must not be empty `), }, @@ -103,7 +103,7 @@ func TestLoginOIDCCommand(t *testing.T) { "--ca-bundle", "./does/not/exist", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` 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"), wantOptionsCount: 3, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: could not complete Pinniped login: some login error `), }, @@ -145,7 +145,7 @@ func TestLoginOIDCCommand(t *testing.T) { conciergeErr: fmt.Errorf("some concierge error"), wantOptionsCount: 3, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: could not complete concierge credential exchange: some concierge error `), }, diff --git a/cmd/pinniped/cmd/login_static_test.go b/cmd/pinniped/cmd/login_static_test.go index 2b98a76b3..ef5230209 100644 --- a/cmd/pinniped/cmd/login_static_test.go +++ b/cmd/pinniped/cmd/login_static_test.go @@ -66,7 +66,7 @@ func TestLoginStaticCommand(t *testing.T) { name: "missing required flags", args: []string{}, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: one of --token or --token-env must be set `), }, @@ -77,7 +77,7 @@ func TestLoginStaticCommand(t *testing.T) { "--enable-concierge", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: invalid concierge parameters: endpoint must not be empty `), }, @@ -87,7 +87,7 @@ func TestLoginStaticCommand(t *testing.T) { "--token-env", "TEST_TOKEN_ENV", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: --token-env variable "TEST_TOKEN_ENV" is not set `), }, @@ -100,7 +100,7 @@ func TestLoginStaticCommand(t *testing.T) { "TEST_TOKEN_ENV": "", }, wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: --token-env variable "TEST_TOKEN_ENV" is empty `), }, @@ -125,7 +125,7 @@ func TestLoginStaticCommand(t *testing.T) { }, conciergeErr: fmt.Errorf("some concierge error"), wantError: true, - wantStdout: here.Doc(` + wantStderr: here.Doc(` Error: could not complete concierge credential exchange: some concierge error `), }, diff --git a/cmd/pinniped/cmd/version_test.go b/cmd/pinniped/cmd/version_test.go index f5f9b5656..dc9c5d300 100644 --- a/cmd/pinniped/cmd/version_test.go +++ b/cmd/pinniped/cmd/version_test.go @@ -7,6 +7,7 @@ import ( "bytes" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.pinniped.dev/internal/here" @@ -41,7 +42,7 @@ func TestNewVersionCmd(t *testing.T) { args []string wantError bool wantStdoutRegexp string - wantStderr string + wantStderrRegexp string }{ { name: "no flags", @@ -57,7 +58,8 @@ func TestNewVersionCmd(t *testing.T) { name: "arg passed", args: []string{"tuna"}, wantError: true, - wantStdoutRegexp: `Error: unknown command "tuna" for "version"` + "\n" + knownGoodUsageRegexpForVersion, + wantStderrRegexp: `Error: unknown command "tuna" for "version"`, + wantStdoutRegexp: knownGoodUsageRegexpForVersion, }, } for _, tt := range tests { @@ -76,8 +78,8 @@ func TestNewVersionCmd(t *testing.T) { } else { require.NoError(t, err) } - require.Regexp(t, tt.wantStdoutRegexp, stdout.String(), "unexpected stdout") - require.Equal(t, tt.wantStderr, stderr.String(), "unexpected stderr") + assert.Regexp(t, tt.wantStdoutRegexp, stdout.String(), "unexpected stdout") + assert.Regexp(t, tt.wantStderrRegexp, stderr.String(), "unexpected stderr") }) } }