Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error_test.go « git « internal « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 11c9baa7d08fd4506e84c6c55fef39747b68e472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package git

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
	"google.golang.org/protobuf/types/known/durationpb"
)

func TestHandleLimitErr(t *testing.T) {
	testCases := []struct {
		desc          string
		errWriter     func(io.Writer) error
		expectedBytes []byte
	}{
		{
			desc:      "upload pack",
			errWriter: writeUploadPackError,
			expectedBytes: bytes.Join([][]byte{
				[]byte{'0', '0', '4', '7'},
				[]byte("ERR GitLab is currently unable to handle this request due to load.\n"),
			}, []byte{}),
		},
		{
			desc:      "receive pack",
			errWriter: writeReceivePackError,
			expectedBytes: bytes.Join([][]byte{
				{'0', '0', '2', '3', 1, '0', '0', '1', 'a'},
				[]byte("unpack server is busy\n"),
				{'0', '0', '0', '0', '0', '0', '4', '4', 2},
				[]byte("GitLab is currently unable to handle this request due to load.\n"),
				{'0', '0', '0', '0'},
			}, []byte{}),
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			var body bytes.Buffer
			err := errWithDetail(t, &gitalypb.LimitError{
				ErrorMessage: "concurrency queue wait time reached",
				RetryAfter:   durationpb.New(0)})

			handleLimitErr(fmt.Errorf("wrapped error: %w", err), &body, tc.errWriter)
			require.Equal(t, tc.expectedBytes, body.Bytes())
		})
	}

	t.Run("non LimitError", func(t *testing.T) {
		var body bytes.Buffer
		err := status.Error(codes.Internal, "some internal error")
		handleLimitErr(fmt.Errorf("wrapped error: %w", err), &body, writeUploadPackError)
		require.Equal(t, []byte(nil), body.Bytes())

		handleLimitErr(fmt.Errorf("wrapped error: %w", err), &body, writeReceivePackError)
		require.Equal(t, []byte(nil), body.Bytes())

	})
}

// errWithDetail adds the given details to the error if it is a gRPC status whose code is not OK.
func errWithDetail(t *testing.T, detail proto.Message) error {
	st := status.New(codes.Unavailable, "too busy")

	proto := st.Proto()
	marshaled, err := anypb.New(detail)
	require.NoError(t, err)

	proto.Details = append(proto.Details, marshaled)

	return status.ErrorProto(proto)
}