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