Add a test for "failed to marshal response to stdout" error case.

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer 2020-07-28 08:42:25 -05:00
parent 1e8463ac2d
commit ebe39c8663

View File

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