From 6c87c793db7099f93b94c0e26d57b230c106a6a9 Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Thu, 23 Jul 2020 09:50:23 -0700 Subject: [PATCH] Extract test helper for asserting API errors in rest_test.go Signed-off-by: Aram Price --- pkg/registry/loginrequest/rest_test.go | 45 ++++++++++---------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/pkg/registry/loginrequest/rest_test.go b/pkg/registry/loginrequest/rest_test.go index 624a3834..47ba6164 100644 --- a/pkg/registry/loginrequest/rest_test.go +++ b/pkg/registry/loginrequest/rest_test.go @@ -11,9 +11,8 @@ import ( "testing" "time" - apierrors "k8s.io/apimachinery/pkg/api/errors" - "github.com/stretchr/testify/require" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apiserver/pkg/authentication/authenticator" @@ -78,6 +77,15 @@ func loginRequest(spec placeholderapi.LoginRequestSpec) *placeholderapi.LoginReq } } +func requireAPIError(t *testing.T, response runtime.Object, err error, expectedErrorTypeChecker func(err error) bool, expectedErrorMessage string) { + t.Helper() + require.Nil(t, response) + require.True(t, expectedErrorTypeChecker(err)) + var status apierrors.APIStatus + errors.As(err, &status) + require.Contains(t, status.Status().Message, expectedErrorMessage) +} + func TestCreateSucceedsWhenGivenAToken(t *testing.T) { webhook := FakeToken{} storage := NewREST(&webhook) @@ -182,11 +190,7 @@ func TestCreateFailsWhenGivenTheWrongInputType(t *testing.T) { rest.ValidateAllObjectFunc, &metav1.CreateOptions{}) - require.Nil(t, response) - require.True(t, apierrors.IsBadRequest(err)) - var status apierrors.APIStatus - errors.As(err, &status) - require.Contains(t, status.Status().Message, "not a LoginRequest") + requireAPIError(t, response, err, apierrors.IsBadRequest, "not a LoginRequest") } func TestCreateFailsWhenTokenIsNilInRequest(t *testing.T) { @@ -196,13 +200,8 @@ func TestCreateFailsWhenTokenIsNilInRequest(t *testing.T) { Token: nil, })) - require.Nil(t, response) - require.True(t, apierrors.IsInvalid(err)) - var status apierrors.APIStatus - errors.As(err, &status) - require.Equal(t, - `.placeholder.suzerain-io.github.io "request name" is invalid: spec.token.value: Required value: token must be supplied`, - status.Status().Message) + requireAPIError(t, response, err, apierrors.IsInvalid, + `.placeholder.suzerain-io.github.io "request name" is invalid: spec.token.value: Required value: token must be supplied`) } func TestCreateFailsWhenTokenValueIsEmptyInRequest(t *testing.T) { @@ -212,13 +211,8 @@ func TestCreateFailsWhenTokenValueIsEmptyInRequest(t *testing.T) { Token: &placeholderapi.LoginRequestTokenCredential{Value: ""}, })) - require.Nil(t, response) - require.True(t, apierrors.IsInvalid(err)) - var status apierrors.APIStatus - errors.As(err, &status) - require.Equal(t, - `.placeholder.suzerain-io.github.io "request name" is invalid: spec.token.value: Required value: token must be supplied`, - status.Status().Message) + requireAPIError(t, response, err, apierrors.IsInvalid, + `.placeholder.suzerain-io.github.io "request name" is invalid: spec.token.value: Required value: token must be supplied`) } func TestCreateFailsWhenRequestOptionsDryRunIsNotEmpty(t *testing.T) { @@ -230,11 +224,6 @@ func TestCreateFailsWhenRequestOptionsDryRunIsNotEmpty(t *testing.T) { DryRun: []string{"some dry run flag"}, }) - require.Nil(t, response) - require.True(t, apierrors.IsInvalid(err)) - var status apierrors.APIStatus - errors.As(err, &status) - require.Equal(t, - `.placeholder.suzerain-io.github.io "request name" is invalid: dryRun: Unsupported value: []string{"some dry run flag"}`, - status.Status().Message) + requireAPIError(t, response, err, apierrors.IsInvalid, + `.placeholder.suzerain-io.github.io "request name" is invalid: dryRun: Unsupported value: []string{"some dry run flag"}`) }