diff options
author | Mikhail Mazurskiy <mmazurskiy@gitlab.com> | 2021-04-23 14:26:26 +0300 |
---|---|---|
committer | Mikhail Mazurskiy <mmazurskiy@gitlab.com> | 2021-05-01 03:33:44 +0300 |
commit | bf61e6c33c42ecdf3e20dcfbc7adbcdfa4a629ba (patch) | |
tree | 8081a43cf506443bb8cdd8c198148472480f2f51 | |
parent | 545cdd3427d74548937a9ab98ca580bbf27339d2 (diff) |
Fix issues in tests
23 files changed, 119 insertions, 87 deletions
diff --git a/client/dial_test.go b/client/dial_test.go index 8909f2482..f4bc4c050 100644 --- a/client/dial_test.go +++ b/client/dial_test.go @@ -18,6 +18,7 @@ import ( gitalyauth "gitlab.com/gitlab-org/gitaly/auth" proxytestdata "gitlab.com/gitlab-org/gitaly/internal/praefect/grpc-proxy/testdata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" gitaly_x509 "gitlab.com/gitlab-org/gitaly/internal/x509" "gitlab.com/gitlab-org/labkit/correlation" grpccorrelation "gitlab.com/gitlab-org/labkit/correlation/grpc" @@ -293,6 +294,7 @@ func TestDial_Tracing(t *testing.T) { t.Run("unary", func(t *testing.T) { reporter := jaeger.NewInMemoryReporter() tracer, tracerCloser := jaeger.NewTracer("", jaeger.NewConstSampler(true), reporter) + defer tracerCloser.Close() defer func(old opentracing.Tracer) { opentracing.SetGlobalTracer(old) }(opentracing.GlobalTracer()) opentracing.SetGlobalTracer(tracer) @@ -318,7 +320,6 @@ func TestDial_Tracing(t *testing.T) { require.NoError(t, err) span.Finish() - tracerCloser.Close() spans := reporter.GetSpans() require.Len(t, spans, 3) @@ -350,6 +351,7 @@ func TestDial_Tracing(t *testing.T) { t.Run("stream", func(t *testing.T) { reporter := jaeger.NewInMemoryReporter() tracer, tracerCloser := jaeger.NewTracer("", jaeger.NewConstSampler(true), reporter) + defer tracerCloser.Close() defer func(old opentracing.Tracer) { opentracing.SetGlobalTracer(old) }(opentracing.GlobalTracer()) opentracing.SetGlobalTracer(tracer) @@ -380,7 +382,6 @@ func TestDial_Tracing(t *testing.T) { require.Nil(t, resp) span.Finish() - tracerCloser.Close() spans := reporter.GetSpans() require.Len(t, spans, 3) @@ -526,9 +527,9 @@ func TestHealthCheckDialer(t *testing.T) { defer cancel() _, err := HealthCheckDialer(DialContext)(ctx, addr, nil) - require.Equal(t, status.Error(codes.Unauthenticated, "authentication required"), err, "should fail without token configured") + testassert.GrpcEqualErr(t, status.Error(codes.Unauthenticated, "authentication required"), err) cc, err := HealthCheckDialer(DialContext)(ctx, addr, []grpc.DialOption{grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2("token"))}) require.NoError(t, err) - cc.Close() + require.NoError(t, cc.Close()) } diff --git a/internal/backchannel/backchannel_test.go b/internal/backchannel/backchannel_test.go index d86fe84b4..f68ed16a2 100644 --- a/internal/backchannel/backchannel_test.go +++ b/internal/backchannel/backchannel_test.go @@ -13,6 +13,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -97,7 +98,7 @@ func TestBackchannel_concurrentRequestsFromMultipleClients(t *testing.T) { } resp, err := gitalypb.NewRefTransactionClient(client).VoteTransaction(ctx, &gitalypb.VoteTransactionRequest{}) - assert.Equal(t, err, errNonMultiplexed) + testassert.GrpcEqualErr(t, errNonMultiplexed, err) assert.Nil(t, resp) assert.NoError(t, client.Close()) @@ -112,7 +113,7 @@ func TestBackchannel_concurrentRequestsFromMultipleClients(t *testing.T) { srv := grpc.NewServer() gitalypb.RegisterRefTransactionServer(srv, mockTransactionServer{ voteTransactionFunc: func(ctx context.Context, req *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) { - assert.Equal(t, &gitalypb.VoteTransactionRequest{TransactionId: i}, req) + testassert.ProtoEqual(t, &gitalypb.VoteTransactionRequest{TransactionId: i}, req) return nil, expectedErr }, }) @@ -136,7 +137,7 @@ func TestBackchannel_concurrentRequestsFromMultipleClients(t *testing.T) { go func() { defer invocations.Done() resp, err := gitalypb.NewRefTransactionClient(client).VoteTransaction(ctx, &gitalypb.VoteTransactionRequest{TransactionId: i}) - assert.Equal(t, err, expectedErr) + testassert.GrpcEqualErr(t, expectedErr, err) assert.Nil(t, resp) }() } diff --git a/internal/gitaly/service/blob/lfs_pointers_test.go b/internal/gitaly/service/blob/lfs_pointers_test.go index 213a75bd0..0da6befc3 100644 --- a/internal/gitaly/service/blob/lfs_pointers_test.go +++ b/internal/gitaly/service/blob/lfs_pointers_test.go @@ -16,6 +16,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -163,7 +164,7 @@ func TestListLFSPointers(t *testing.T) { if err == io.EOF { break } - require.Equal(t, err, tc.expectedErr) + testassert.GrpcEqualErr(t, tc.expectedErr, err) if err != nil { break } diff --git a/internal/gitaly/service/conflicts/resolve_conflicts_test.go b/internal/gitaly/service/conflicts/resolve_conflicts_test.go index e451ce448..ee7a2264c 100644 --- a/internal/gitaly/service/conflicts/resolve_conflicts_test.go +++ b/internal/gitaly/service/conflicts/resolve_conflicts_test.go @@ -17,7 +17,6 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/conflicts" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" - "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" @@ -387,7 +386,7 @@ func TestResolveConflictsNonOIDRequests(t *testing.T) { })) _, err = stream.CloseAndRecv() - require.Equal(t, status.Errorf(codes.Unknown, "Rugged::InvalidError: unable to parse OID - contains invalid characters"), err) + testassert.GrpcEqualErr(t, status.Errorf(codes.Unknown, "Rugged::InvalidError: unable to parse OID - contains invalid characters"), err) } func TestResolveConflictsIdenticalContent(t *testing.T) { diff --git a/internal/gitaly/service/diff/find_changed_paths_test.go b/internal/gitaly/service/diff/find_changed_paths_test.go index b1c699fc9..8864a47b9 100644 --- a/internal/gitaly/service/diff/find_changed_paths_test.go +++ b/internal/gitaly/service/diff/find_changed_paths_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -160,7 +161,7 @@ func TestFindChangedPathsRequest_failing(t *testing.T) { t.Run(tc.desc, func(t *testing.T) { _, err := stream.Recv() - require.Equal(t, tc.err, err) + testassert.GrpcEqualErr(t, tc.err, err) }) } } diff --git a/internal/gitaly/service/objectpool/create_test.go b/internal/gitaly/service/objectpool/create_test.go index 8171546a8..ee2f81e63 100644 --- a/internal/gitaly/service/objectpool/create_test.go +++ b/internal/gitaly/service/objectpool/create_test.go @@ -12,8 +12,8 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/git/objectpool" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" - "google.golang.org/grpc/status" ) func TestCreate(t *testing.T) { @@ -143,7 +143,7 @@ func TestUnsuccessfulCreate(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { _, err := client.CreateObjectPool(ctx, tc.request) - require.Equal(t, status.Convert(tc.error).Err(), err) + testassert.GrpcEqualErr(t, tc.error, err) }) } } @@ -210,7 +210,7 @@ func TestDelete(t *testing.T) { RelativePath: tc.relativePath, }, }}) - require.Equal(t, status.Convert(tc.error).Err(), err) + testassert.GrpcEqualErr(t, tc.error, err) }) } } diff --git a/internal/gitaly/service/objectpool/fetch_into_object_pool_test.go b/internal/gitaly/service/objectpool/fetch_into_object_pool_test.go index 3df38142a..39e1035ff 100644 --- a/internal/gitaly/service/objectpool/fetch_into_object_pool_test.go +++ b/internal/gitaly/service/objectpool/fetch_into_object_pool_test.go @@ -20,6 +20,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git/objectpool" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/labkit/log" @@ -114,7 +115,7 @@ func TestFetchIntoObjectPool_hooks(t *testing.T) { } _, err = client.FetchIntoObjectPool(ctx, req) - require.Equal(t, status.Error(codes.Internal, "exit status 128"), err) + testassert.GrpcEqualErr(t, status.Error(codes.Internal, "exit status 128"), err) } func TestFetchIntoObjectPool_CollectLogStatistics(t *testing.T) { diff --git a/internal/gitaly/service/operations/branches_test.go b/internal/gitaly/service/operations/branches_test.go index 648e392a9..5efc75d05 100644 --- a/internal/gitaly/service/operations/branches_test.go +++ b/internal/gitaly/service/operations/branches_test.go @@ -310,7 +310,7 @@ func TestSuccessfulCreateBranchRequestWithStartPointRefPrefix(t *testing.T) { TargetCommit: targetCommitOK, }, } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) branches := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "for-each-ref", "--", "refs/heads/"+testCase.branchName) require.Contains(t, string(branches), "refs/heads/"+testCase.branchName) }) @@ -396,7 +396,7 @@ func TestFailedUserCreateBranchRequest(t *testing.T) { } response, err := client.UserCreateBranch(ctx, request) - require.Equal(t, testCase.err, err) + testassert.GrpcEqualErr(t, testCase.err, err) require.Empty(t, response) }) } @@ -595,7 +595,7 @@ func TestFailedUserDeleteBranchDueToValidation(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.desc, func(t *testing.T) { response, err := client.UserDeleteBranch(ctx, testCase.request) - require.Equal(t, testCase.err, err) + testassert.GrpcEqualErr(t, testCase.err, err) testassert.ProtoEqual(t, testCase.response, response) }) } diff --git a/internal/gitaly/service/operations/commit_files_test.go b/internal/gitaly/service/operations/commit_files_test.go index db448dcc5..9377152ea 100644 --- a/internal/gitaly/service/operations/commit_files_test.go +++ b/internal/gitaly/service/operations/commit_files_test.go @@ -18,6 +18,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" @@ -917,7 +918,7 @@ func TestUserCommitFiles(t *testing.T) { } resp, err := stream.CloseAndRecv() - require.Equal(t, step.error, err) + testassert.GrpcEqualErr(t, step.error, err) if step.error != nil { continue } diff --git a/internal/gitaly/service/operations/merge_test.go b/internal/gitaly/service/operations/merge_test.go index 0d4bbf7ac..8469dbdf0 100644 --- a/internal/gitaly/service/operations/merge_test.go +++ b/internal/gitaly/service/operations/merge_test.go @@ -744,7 +744,7 @@ func TestConflictsOnUserMergeToRefRequest(t *testing.T) { request.AllowConflicts = false _, err := client.UserMergeToRef(ctx, request) - require.Equal(t, status.Error(codes.FailedPrecondition, "Failed to create merge commit for source_sha 1450cd639e0bc6721eb02800169e464f212cde06 and target_sha 824be604a34828eb682305f0d963056cfac87b2d at refs/merge-requests/x/written"), err) + testassert.GrpcEqualErr(t, status.Error(codes.FailedPrecondition, "Failed to create merge commit for source_sha 1450cd639e0bc6721eb02800169e464f212cde06 and target_sha 824be604a34828eb682305f0d963056cfac87b2d at refs/merge-requests/x/written"), err) }) } diff --git a/internal/gitaly/service/operations/tags_test.go b/internal/gitaly/service/operations/tags_test.go index 6b74f3a43..e16452317 100644 --- a/internal/gitaly/service/operations/tags_test.go +++ b/internal/gitaly/service/operations/tags_test.go @@ -247,7 +247,7 @@ func testSuccessfulUserCreateTagRequest(t *testing.T, ctx context.Context) { responseOk.Tag.Id = text.ChompBytes(id) } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) tag := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "tag") require.Contains(t, string(tag), inputTagName) @@ -489,7 +489,7 @@ func testSuccessfulUserCreateTagRequestAnnotatedLightweightDisambiguation(t *tes response, err := client.UserCreateTag(ctx, request) if testCase.err != nil { - require.Equal(t, testCase.err, err) + testassert.GrpcEqualErr(t, testCase.err, err) } else { defer testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "tag", "-d", tagName) require.NoError(t, err) @@ -681,7 +681,7 @@ func TestSuccessfulUserCreateTagRequestToNonCommit(t *testing.T) { tagID := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "rev-parse", inputTagName) responseOk.Tag.Id = text.ChompBytes(tagID) } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) peeledID := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "rev-parse", inputTagName+"^{}") require.Equal(t, testCase.targetRevision, text.ChompBytes(peeledID)) @@ -773,7 +773,7 @@ func TestSuccessfulUserCreateTagNestedTags(t *testing.T) { responseOk.Tag.TargetCommit, err = repo.ReadCommit(ctx, git.Revision(testCase.targetObject)) require.NoError(t, err) } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) peeledID := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "rev-parse", tagName+"^{}") peeledIDStr := text.ChompBytes(peeledID) @@ -808,7 +808,7 @@ func TestSuccessfulUserCreateTagNestedTags(t *testing.T) { MessageSize: 0, }, } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) createdIDLight := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "rev-parse", tagNameLight) createdIDLightStr := text.ChompBytes(createdIDLight) @@ -883,8 +883,8 @@ func testUserDeleteTagsuccessfulDeletionOfPrefixedTag(t *testing.T, ctx context. } response, err := client.UserDeleteTag(ctx, request) - require.Equal(t, testCase.err, err) - require.Equal(t, testCase.response, response) + testassert.GrpcEqualErr(t, testCase.err, err) + testassert.ProtoEqual(t, testCase.response, response) refs := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "for-each-ref", "--", "refs/tags/"+testCase.tagNameInput) require.NotContains(t, string(refs), testCase.tagCommit, "tag kept because we stripped off refs/tags/*") @@ -928,7 +928,7 @@ func TestUserCreateTagsuccessfulCreationOfPrefixedTag(t *testing.T) { } response, err := client.UserCreateTag(ctx, request) - require.Equal(t, testCase.err, err) + testassert.GrpcEqualErr(t, testCase.err, err) commitOk, err := repo.ReadCommit(ctx, git.Revision(testCase.tagTargetRevisionInput)) require.NoError(t, err) @@ -940,7 +940,7 @@ func TestUserCreateTagsuccessfulCreationOfPrefixedTag(t *testing.T) { }, } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) refs := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "for-each-ref", "--", "refs/tags/"+testCase.tagNameInput) require.Contains(t, string(refs), testCase.tagTargetRevisionInput, "tag created, we did not strip off refs/tags/*") @@ -1048,7 +1048,7 @@ func TestFailedUserDeleteTagRequestDueToValidation(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.desc, func(t *testing.T) { response, err := client.UserDeleteTag(ctx, testCase.request) - require.Equal(t, testCase.err, err) + testassert.GrpcEqualErr(t, testCase.err, err) testassert.ProtoEqual(t, testCase.response, response) }) } @@ -1080,7 +1080,7 @@ func testFailedUserDeleteTagDueToHooks(t *testing.T, ctx context.Context) { gittest.WriteCustomHook(t, repoPath, hookName, hookContent) response, err := client.UserDeleteTag(ctx, request) - require.Nil(t, err) + require.NoError(t, err) require.Contains(t, response.PreReceiveError, "GL_ID="+testhelper.TestUser.GlId) tags := testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "tag") @@ -1108,7 +1108,7 @@ func TestFailedUserCreateTagDueToHooks(t *testing.T) { gittest.WriteCustomHook(t, repoPath, hookName, hookContent) response, err := client.UserCreateTag(ctx, request) - require.Nil(t, err) + require.NoError(t, err) require.Contains(t, response.PreReceiveError, "GL_ID="+testhelper.TestUser.GlId) } } @@ -1158,8 +1158,8 @@ func TestFailedUserCreateTagRequestDueToTagExistence(t *testing.T) { } response, err := client.UserCreateTag(ctx, request) - require.Equal(t, testCase.err, err) - require.Equal(t, testCase.response, response) + testassert.GrpcEqualErr(t, testCase.err, err) + testassert.ProtoEqual(t, testCase.response, response) }) } } @@ -1276,8 +1276,8 @@ func TestFailedUserCreateTagRequestDueToValidation(t *testing.T) { } response, err := client.UserCreateTag(ctx, request) - require.Equal(t, testCase.err, err) - require.Equal(t, testCase.response, response) + testassert.GrpcEqualErr(t, testCase.err, err) + testassert.ProtoEqual(t, testCase.response, response) }) } } @@ -1354,7 +1354,7 @@ func testTagHookOutput(t *testing.T, ctx context.Context) { Exists: false, PreReceiveError: testCase.output, } - require.Equal(t, createResponseOk, createResponse) + testassert.ProtoEqual(t, createResponseOk, createResponse) defer testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "tag", "-d", tagNameInput) testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "tag", tagNameInput) @@ -1364,7 +1364,7 @@ func testTagHookOutput(t *testing.T, ctx context.Context) { deleteResponseOk := &gitalypb.UserDeleteTagResponse{ PreReceiveError: testCase.output, } - require.Equal(t, deleteResponseOk, deleteResponse) + testassert.ProtoEqual(t, deleteResponseOk, deleteResponse) }) } } diff --git a/internal/gitaly/service/operations/update_branches_test.go b/internal/gitaly/service/operations/update_branches_test.go index 2db6e757f..47824ba05 100644 --- a/internal/gitaly/service/operations/update_branches_test.go +++ b/internal/gitaly/service/operations/update_branches_test.go @@ -14,6 +14,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -93,7 +94,7 @@ func testSuccessfulUserUpdateBranchRequestFeatured(t *testing.T, ctx context.Con } response, err := client.UserUpdateBranch(ctx, request) require.NoError(t, err) - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) branchCommit, err := repo.ReadCommit(ctx, git.Revision(testCase.updateBranchName)) @@ -164,8 +165,8 @@ func testSuccessfulUserUpdateBranchRequestToDeleteFeatured(t *testing.T, ctx con User: testhelper.TestUser, } response, err := client.UserUpdateBranch(ctx, request) - require.Nil(t, err) - require.Equal(t, responseOk, response) + require.NoError(t, err) + testassert.ProtoEqual(t, responseOk, response) _, err = repo.ReadCommit(ctx, git.Revision(testCase.updateBranchName)) require.Equal(t, localrepo.ErrObjectNotFound, err, "expected 'not found' error got %v", err) @@ -203,7 +204,7 @@ func testSuccessfulGitHooksForUserUpdateBranchRequestFeatured(t *testing.T, ctx require.NoError(t, err) require.Empty(t, response.PreReceiveError) - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) output := string(testhelper.MustReadFile(t, hookOutputTempPath)) require.Contains(t, output, "GL_USERNAME="+testhelper.TestUser.GlUsername) }) @@ -232,14 +233,14 @@ func testFailedUserUpdateBranchDueToHooksFeatured(t *testing.T, ctx context.Cont gittest.WriteCustomHook(t, repoPath, hookName, hookContent) response, err := client.UserUpdateBranch(ctx, request) - require.Nil(t, err) + require.NoError(t, err) require.Contains(t, response.PreReceiveError, "GL_USERNAME="+testhelper.TestUser.GlUsername) require.Contains(t, response.PreReceiveError, "PWD="+repoPath) responseOk := &gitalypb.UserUpdateBranchResponse{ PreReceiveError: response.PreReceiveError, } - require.Equal(t, responseOk, response) + testassert.ProtoEqual(t, responseOk, response) } } @@ -374,7 +375,7 @@ func testFailedUserUpdateBranchRequestFeatured(t *testing.T, ctx context.Context response, err := client.UserUpdateBranch(ctx, request) require.Equal(t, testCase.response, response) - require.Equal(t, testCase.err, err) + testassert.GrpcEqualErr(t, testCase.err, err) branchCommit, err := repo.ReadCommit(ctx, git.Revision(testCase.branchName)) if testCase.expectNotFoundError { diff --git a/internal/gitaly/service/remote/update_remote_mirror_test.go b/internal/gitaly/service/remote/update_remote_mirror_test.go index 029b71943..0b6c9f167 100644 --- a/internal/gitaly/service/remote/update_remote_mirror_test.go +++ b/internal/gitaly/service/remote/update_remote_mirror_test.go @@ -18,6 +18,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" @@ -418,7 +419,7 @@ func testUpdateRemoteMirrorFeatured(t *testing.T, ctx context.Context, cfg confi } require.NoError(t, err) - require.Equal(t, tc.response, resp) + testassert.ProtoEqual(t, tc.response, resp) // Check that the refs on the mirror now refer to the correct commits. // This is done by checking the commit messages as the commits are otherwise diff --git a/internal/gitaly/service/repository/apply_gitattributes_test.go b/internal/gitaly/service/repository/apply_gitattributes_test.go index b383e5bb2..49e683d24 100644 --- a/internal/gitaly/service/repository/apply_gitattributes_test.go +++ b/internal/gitaly/service/repository/apply_gitattributes_test.go @@ -19,6 +19,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -193,7 +194,7 @@ func TestApplyGitattributesWithTransaction(t *testing.T) { Repository: repo, Revision: tc.revision, }) - require.Equal(t, tc.expectedErr, err) + testassert.GrpcEqualErr(t, tc.expectedErr, err) path := filepath.Join(infoPath, "attributes") if tc.shouldExist { diff --git a/internal/gitaly/service/repository/fetch_remote_test.go b/internal/gitaly/service/repository/fetch_remote_test.go index 2b88f1c7f..09dceb8d3 100644 --- a/internal/gitaly/service/repository/fetch_remote_test.go +++ b/internal/gitaly/service/repository/fetch_remote_test.go @@ -25,6 +25,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -508,7 +509,7 @@ func TestFetchRemote_force(t *testing.T) { tc.request.Repository = targetRepoProto _, err := client.FetchRemote(ctx, tc.request) - require.Equal(t, tc.expectedErr, err) + testassert.GrpcEqualErr(t, tc.expectedErr, err) updatedBranchOID, err := targetRepo.ResolveRevision(ctx, "refs/heads/master") require.NoError(t, err) diff --git a/internal/gitaly/transaction/manager_test.go b/internal/gitaly/transaction/manager_test.go index 05dc5fe11..3e7e1c03e 100644 --- a/internal/gitaly/transaction/manager_test.go +++ b/internal/gitaly/transaction/manager_test.go @@ -14,6 +14,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -116,7 +117,7 @@ func TestPoolManager_Vote(t *testing.T) { } err := manager.Vote(ctx, tc.transaction, praefect, tc.vote) - require.Equal(t, tc.expectedErr, err) + testassert.GrpcEqualErr(t, tc.expectedErr, err) }) } }) @@ -171,7 +172,7 @@ func TestPoolManager_Stop(t *testing.T) { } err := manager.Stop(ctx, tc.transaction, praefect) - require.Equal(t, tc.expectedErr, err) + testassert.GrpcEqualErr(t, tc.expectedErr, err) }) } }) diff --git a/internal/middleware/cache/cache_test.go b/internal/middleware/cache/cache_test.go index ad0c8648b..7d55f13af 100644 --- a/internal/middleware/cache/cache_test.go +++ b/internal/middleware/cache/cache_test.go @@ -17,6 +17,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/middleware/cache/testdata" "gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -122,11 +123,11 @@ func TestInvalidators(t *testing.T) { require.Equal(t, 0, cache.MethodErrCount.Method["/grpc.health.v1.Health/Check"]) _, err = testdata.NewInterceptedServiceClient(cc).IgnoredMethod(ctx, &testdata.Request{}) - require.Equal(t, status.Error(codes.Unimplemented, "method IgnoredMethod not implemented"), err) + testassert.GrpcEqualErr(t, status.Error(codes.Unimplemented, "method IgnoredMethod not implemented"), err) require.Equal(t, 0, cache.MethodErrCount.Method["/testdata.InterceptedService/IgnoredMethod"]) - require.Equal(t, expectedInvalidations, mCache.(*mockCache).invalidatedRepos) - require.Equal(t, expectedSvcRequests, svc.repoRequests) + testassert.ProtoEqual(t, expectedInvalidations, mCache.(*mockCache).invalidatedRepos) + testassert.ProtoEqual(t, expectedSvcRequests, svc.repoRequests) require.Equal(t, 3, mCache.(*mockCache).endedLeases.count) } diff --git a/internal/middleware/limithandler/limithandler_test.go b/internal/middleware/limithandler/limithandler_test.go index 3f22cd9a2..4bdc10a71 100644 --- a/internal/middleware/limithandler/limithandler_test.go +++ b/internal/middleware/limithandler/limithandler_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/middleware/limithandler" pb "gitlab.com/gitlab-org/gitaly/internal/middleware/limithandler/testdata" @@ -52,9 +53,13 @@ func TestUnaryLimitHandler(t *testing.T) { defer wg.Done() resp, err := client.Unary(ctx, &pb.UnaryRequest{}) - require.NotNil(t, resp) - require.NoError(t, err) - require.True(t, resp.Ok) + if !assert.NoError(t, err) { + return + } + if !assert.NotNil(t, resp) { + return + } + assert.True(t, resp.Ok) }() } @@ -79,12 +84,12 @@ func TestStreamLimitHandler(t *testing.T) { fullname: "/test.Test/StreamOutput", f: func(t *testing.T, ctx context.Context, client pb.TestClient) { stream, err := client.StreamOutput(ctx, &pb.StreamOutputRequest{}) - require.NotNil(t, stream) require.NoError(t, err) + require.NotNil(t, stream) r, err := stream.Recv() - require.NotNil(t, r) require.NoError(t, err) + require.NotNil(t, r) require.True(t, r.Ok) }, maxConcurrency: 3, @@ -95,13 +100,13 @@ func TestStreamLimitHandler(t *testing.T) { fullname: "/test.Test/StreamInput", f: func(t *testing.T, ctx context.Context, client pb.TestClient) { stream, err := client.StreamInput(ctx) - require.NotNil(t, stream) require.NoError(t, err) + require.NotNil(t, stream) require.NoError(t, stream.Send(&pb.StreamInputRequest{})) r, err := stream.CloseAndRecv() - require.NotNil(t, r) require.NoError(t, err) + require.NotNil(t, r) require.True(t, r.Ok) }, maxConcurrency: 3, @@ -112,15 +117,15 @@ func TestStreamLimitHandler(t *testing.T) { fullname: "/test.Test/Bidirectional", f: func(t *testing.T, ctx context.Context, client pb.TestClient) { stream, err := client.Bidirectional(ctx) - require.NotNil(t, stream) require.NoError(t, err) + require.NotNil(t, stream) require.NoError(t, stream.Send(&pb.BidirectionalRequest{})) require.NoError(t, stream.CloseSend()) r, err := stream.Recv() - require.NotNil(t, r) require.NoError(t, err) + require.NotNil(t, r) require.True(t, r.Ok) }, maxConcurrency: 3, @@ -133,8 +138,8 @@ func TestStreamLimitHandler(t *testing.T) { fullname: "/test.Test/Bidirectional", f: func(t *testing.T, ctx context.Context, client pb.TestClient) { stream, err := client.Bidirectional(ctx) - require.NotNil(t, stream) require.NoError(t, err) + require.NotNil(t, stream) // Since the concurrency id is fixed all requests have the same // id, but subsequent requests in a stream, even with the same @@ -145,8 +150,8 @@ func TestStreamLimitHandler(t *testing.T) { require.NoError(t, stream.CloseSend()) r, err := stream.Recv() - require.NotNil(t, r) require.NoError(t, err) + require.NotNil(t, r) require.True(t, r.Ok) }, maxConcurrency: 3, @@ -158,12 +163,12 @@ func TestStreamLimitHandler(t *testing.T) { fullname: "/test.Test/StreamOutput", f: func(t *testing.T, ctx context.Context, client pb.TestClient) { stream, err := client.StreamOutput(ctx, &pb.StreamOutputRequest{}) - require.NotNil(t, stream) require.NoError(t, err) + require.NotNil(t, stream) r, err := stream.Recv() - require.NotNil(t, r) require.NoError(t, err) + require.NotNil(t, r) require.True(t, r.Ok) }, maxConcurrency: 0, diff --git a/internal/praefect/coordinator_test.go b/internal/praefect/coordinator_test.go index d1377685c..f07a5d6af 100644 --- a/internal/praefect/coordinator_test.go +++ b/internal/praefect/coordinator_test.go @@ -32,6 +32,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/praefect/transactions" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/promtest" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/labkit/correlation" "google.golang.org/grpc" @@ -1518,7 +1519,7 @@ func TestCoordinator_grpcErrorHandling(t *testing.T) { &gitalypb.UserCreateBranchRequest{ Repository: repoProto, }) - require.Equal(t, tc.expectedErr, err) + testassert.GrpcEqualErr(t, tc.expectedErr, err) for _, node := range gitalies { require.True(t, node.operationServer.called, "expected gitaly %q to have been called", node.mock.GetStorage()) diff --git a/internal/praefect/grpc-proxy/proxy/handler_test.go b/internal/praefect/grpc-proxy/proxy/handler_test.go index f472090a4..96934c07a 100644 --- a/internal/praefect/grpc-proxy/proxy/handler_test.go +++ b/internal/praefect/grpc-proxy/proxy/handler_test.go @@ -30,6 +30,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/praefect/grpc-proxy/proxy" pb "gitlab.com/gitlab-org/gitaly/internal/praefect/grpc-proxy/testdata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "go.uber.org/goleak" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -138,7 +139,7 @@ func (s *ProxyHappySuite) TestPingEmptyCarriesClientMetadata() { ctx := metadata.NewOutgoingContext(s.ctx(), metadata.Pairs(clientMdKey, "true")) out, err := s.testClient.PingEmpty(ctx, &pb.Empty{}) require.NoError(s.T(), err, "PingEmpty should succeed without errors") - require.Equal(s.T(), &pb.PingResponse{Value: pingDefaultValue, Counter: 42}, out) + testassert.ProtoEqual(s.T(), &pb.PingResponse{Value: pingDefaultValue, Counter: 42}, out) } func (s *ProxyHappySuite) TestPingEmpty_StressTest() { @@ -153,7 +154,7 @@ func (s *ProxyHappySuite) TestPingCarriesServerHeadersAndTrailers() { // This is an awkward calling convention... but meh. out, err := s.testClient.Ping(s.ctx(), &pb.PingRequest{Value: "foo"}, grpc.Header(&headerMd), grpc.Trailer(&trailerMd)) require.NoError(s.T(), err, "Ping should succeed without errors") - require.Equal(s.T(), &pb.PingResponse{Value: "foo", Counter: 42}, out) + testassert.ProtoEqual(s.T(), &pb.PingResponse{Value: "foo", Counter: 42}, out) assert.Contains(s.T(), headerMd, serverHeaderMdKey, "server response headers must contain server data") assert.Len(s.T(), trailerMd, 1, "server response trailers must contain server data") } diff --git a/internal/praefect/server_test.go b/internal/praefect/server_test.go index c7dba6eb3..0555cc860 100644 --- a/internal/praefect/server_test.go +++ b/internal/praefect/server_test.go @@ -39,6 +39,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/praefect/transactions" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/promtest" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/internal/version" @@ -97,7 +98,7 @@ func TestNewBackchannelServerFactory(t *testing.T) { defer nodeSet.Close() clientConn := nodeSet["default"]["gitaly-1"].Connection - require.Equal(t, + testassert.GrpcEqualErr(t, status.Error(codes.NotFound, "transaction not found: 0"), clientConn.Invoke(ctx, "/Service/Method", &gitalypb.CreateBranchRequest{}, &gitalypb.CreateBranchResponse{}), ) @@ -978,7 +979,7 @@ func TestErrorThreshold(t *testing.T) { require.True(t, healthy) _, err = handler(ctx, &mock.RepoRequest{Repo: repo}) - require.Equal(t, status.Error(codes.Internal, "something went wrong"), err) + testassert.GrpcEqualErr(t, status.Error(codes.Internal, "something went wrong"), err) } healthy, err := node.CheckHealth(ctx) diff --git a/internal/praefect/service/info/consistencycheck_test.go b/internal/praefect/service/info/consistencycheck_test.go index 169adbd6f..baefabac8 100644 --- a/internal/praefect/service/info/consistencycheck_test.go +++ b/internal/praefect/service/info/consistencycheck_test.go @@ -19,6 +19,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/praefect/datastore" "gitlab.com/gitlab-org/gitaly/internal/praefect/nodes" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testassert" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -156,7 +157,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { execAndVerify(t, req, func(t *testing.T, responses []*gitalypb.ConsistencyCheckResponse, err error) { require.NoError(t, err) - require.Equal(t, []*gitalypb.ConsistencyCheckResponse{ + testassert.ProtoEqual(t, []*gitalypb.ConsistencyCheckResponse{ { RepoRelativePath: firstRepoPath, ReferenceStorage: referenceStorageName, @@ -191,8 +192,8 @@ func TestServer_ConsistencyCheck(t *testing.T) { DisableReconcilliation: false, }, verify: func(t *testing.T, resp []*gitalypb.ConsistencyCheckResponse, err error) { - require.Equal(t, expErrStatus, err) - require.Equal(t, []*gitalypb.ConsistencyCheckResponse{ + testassert.GrpcEqualErr(t, expErrStatus, err) + testassert.ProtoEqual(t, []*gitalypb.ConsistencyCheckResponse{ { RepoRelativePath: firstRepoPath, TargetChecksum: checksum, @@ -228,7 +229,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { }, verify: func(t *testing.T, resp []*gitalypb.ConsistencyCheckResponse, err error) { require.NoError(t, err) - require.Equal(t, []*gitalypb.ConsistencyCheckResponse{ + testassert.ProtoEqual(t, []*gitalypb.ConsistencyCheckResponse{ { RepoRelativePath: firstRepoPath, TargetChecksum: checksum, @@ -261,7 +262,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { ReferenceStorage: targetStorageName, }, verify: func(t *testing.T, resp []*gitalypb.ConsistencyCheckResponse, err error) { - require.Equal(t, status.Error(codes.InvalidArgument, "missing target storage"), err) + testassert.GrpcEqualErr(t, status.Error(codes.InvalidArgument, "missing target storage"), err) }, }, { @@ -272,7 +273,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { ReferenceStorage: targetStorageName, }, verify: func(t *testing.T, resp []*gitalypb.ConsistencyCheckResponse, err error) { - require.Equal(t, status.Error(codes.NotFound, `unable to find target storage "unknown"`), err) + testassert.GrpcEqualErr(t, status.Error(codes.NotFound, `unable to find target storage "unknown"`), err) }, }, { @@ -287,7 +288,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { codes.InvalidArgument, fmt.Sprintf(`target storage %q is same as current primary, must provide alternate reference`, referenceStorageName), ) - require.Equal(t, expErr, err) + testassert.GrpcEqualErr(t, expErr, err) }, }, { @@ -302,7 +303,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { codes.NotFound, fmt.Sprintf(`unable to find reference storage "unknown" in nodes for shard %q`, virtualStorage), ) - require.Equal(t, expErr, err) + testassert.GrpcEqualErr(t, expErr, err) }, }, { @@ -317,7 +318,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { codes.InvalidArgument, fmt.Sprintf(`target storage %q cannot match reference storage %q`, referenceStorageName, referenceStorageName), ) - require.Equal(t, expErr, err) + testassert.GrpcEqualErr(t, expErr, err) }, }, { @@ -328,7 +329,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { ReferenceStorage: targetStorageName, }, verify: func(t *testing.T, resp []*gitalypb.ConsistencyCheckResponse, err error) { - require.Equal(t, status.Error(codes.InvalidArgument, "missing virtual storage"), err) + testassert.GrpcEqualErr(t, status.Error(codes.InvalidArgument, "missing virtual storage"), err) }, }, { @@ -339,7 +340,7 @@ func TestServer_ConsistencyCheck(t *testing.T) { ReferenceStorage: "unknown", }, verify: func(t *testing.T, resp []*gitalypb.ConsistencyCheckResponse, err error) { - require.Equal(t, status.Error(codes.NotFound, "virtual storage does not exist"), err) + testassert.GrpcEqualErr(t, status.Error(codes.NotFound, "virtual storage does not exist"), err) }, }, } { @@ -361,12 +362,12 @@ func TestServer_ConsistencyCheck(t *testing.T) { execAndVerify(t, req, func(t *testing.T, responses []*gitalypb.ConsistencyCheckResponse, err error) { t.Helper() - require.Equal(t, expErrStatus, err) + testassert.GrpcEqualErr(t, expErrStatus, err) errs := []string{ fmt.Sprintf("rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial unix //%s: connect: no such file or directory\"", strings.TrimPrefix(targetGitaly.Address(), "unix://")), "rpc error: code = Canceled desc = context canceled", } - require.Equal(t, []*gitalypb.ConsistencyCheckResponse{ + testassert.ProtoEqual(t, []*gitalypb.ConsistencyCheckResponse{ { RepoRelativePath: firstRepoPath, ReferenceStorage: referenceStorageName, diff --git a/internal/testhelper/testassert/assert.go b/internal/testhelper/testassert/assert.go index 50a473082..39a291339 100644 --- a/internal/testhelper/testassert/assert.go +++ b/internal/testhelper/testassert/assert.go @@ -3,13 +3,25 @@ package testassert import ( "testing" - "github.com/golang/protobuf/proto" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/testing/protocmp" ) // ProtoEqual asserts that expected and actual protobuf messages are equal. +// It can accept not only proto.Message, but slices, maps, and structs too. // This is required as comparing messages directly with `require.Equal` doesn't // work. -func ProtoEqual(t testing.TB, expected proto.Message, actual proto.Message) { - require.True(t, proto.Equal(expected, actual), "proto messages not equal\nexpected: %v\ngot: %v", expected, actual) +func ProtoEqual(t testing.TB, expected, actual interface{}) { + require.Empty(t, cmp.Diff(expected, actual, protocmp.Transform())) +} + +// GrpcEqualErr asserts that expected and actual gRPC errors are equal. +// This is required as comparing messages directly with `require.Equal` doesn't +// work. +func GrpcEqualErr(t testing.TB, expected, actual error) { + t.Helper() + // .Proto() handles nil receiver + ProtoEqual(t, status.Convert(expected).Proto(), status.Convert(actual).Proto()) } |