From ebe39c8663509e0058cc02fb3cff12f427ec23db Mon Sep 17 00:00:00 2001 From: Matt Moyer Date: Tue, 28 Jul 2020 08:42:25 -0500 Subject: [PATCH] Add a test for "failed to marshal response to stdout" error case. Signed-off-by: Matt Moyer --- cmd/placeholder-name/main_test.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/placeholder-name/main_test.go b/cmd/placeholder-name/main_test.go index 55320064..30adbe51 100644 --- a/cmd/placeholder-name/main_test.go +++ b/cmd/placeholder-name/main_test.go @@ -10,14 +10,16 @@ import ( "fmt" "testing" - "k8s.io/client-go/pkg/apis/clientauthentication" - - "github.com/stretchr/testify/require" - "github.com/sclevine/spec" "github.com/sclevine/spec/report" + "github.com/stretchr/testify/require" + "k8s.io/client-go/pkg/apis/clientauthentication" ) +type errWriter struct{ returnErr error } + +func (e *errWriter) Write([]byte) (int, error) { return 0, e.returnErr } + func TestRun(t *testing.T) { spec.Run(t, "Run", func(t *testing.T, when spec.G, it spec.S) { var buffer *bytes.Buffer @@ -83,6 +85,21 @@ func TestRun(t *testing.T) { }) }, spec.Parallel()) + when("the JSON encoder fails", func() { + it.Before(func() { + tokenExchanger = func(token, caBundle, apiEndpoint string) (*clientauthentication.ExecCredential, error) { + return &clientauthentication.ExecCredential{ + Status: &clientauthentication.ExecCredentialStatus{Token: "some token"}, + }, nil + } + }) + + it("returns an error", func() { + err := run(envGetter, tokenExchanger, &errWriter{returnErr: fmt.Errorf("some IO error")}) + require.EqualError(t, err, "failed to marshal response to stdout: some IO error") + }) + }, spec.Parallel()) + when("the token exchange succeeds", func() { var actualToken, actualCaBundle, actualAPIEndpoint string