summaryrefslogtreecommitdiff
path: root/workhorse/internal/git/upload-pack_test.go
blob: 9ee199a5402d26388473d62e311bca90da836169 (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
81
82
83
84
85
package git

import (
	"fmt"
	"io/ioutil"
	"net"
	"net/http/httptest"
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"google.golang.org/grpc"

	"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"

	"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
	"gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly"
)

var (
	originalUploadPackTimeout = uploadPackTimeout
)

type fakeReader struct {
	n   int
	err error
}

func (f *fakeReader) Read(b []byte) (int, error) {
	return f.n, f.err
}

type smartHTTPServiceServer struct {
	gitalypb.UnimplementedSmartHTTPServiceServer
	PostUploadPackFunc func(gitalypb.SmartHTTPService_PostUploadPackServer) error
}

func (srv *smartHTTPServiceServer) PostUploadPack(s gitalypb.SmartHTTPService_PostUploadPackServer) error {
	return srv.PostUploadPackFunc(s)
}

func TestUploadPackTimesOut(t *testing.T) {
	uploadPackTimeout = time.Millisecond
	defer func() { uploadPackTimeout = originalUploadPackTimeout }()

	addr, cleanUp := startSmartHTTPServer(t, &smartHTTPServiceServer{
		PostUploadPackFunc: func(stream gitalypb.SmartHTTPService_PostUploadPackServer) error {
			_, err := stream.Recv() // trigger a read on the client request body
			require.NoError(t, err)
			return nil
		},
	})
	defer cleanUp()

	body := &fakeReader{n: 0, err: nil}

	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "/", body)
	a := &api.Response{GitalyServer: gitaly.Server{Address: addr}}

	err := handleUploadPack(NewHttpResponseWriter(w), r, a)
	require.EqualError(t, err, "smarthttp.UploadPack: busyReader: context deadline exceeded")
}

func startSmartHTTPServer(t testing.TB, s gitalypb.SmartHTTPServiceServer) (string, func()) {
	tmp, err := ioutil.TempDir("", "")
	require.NoError(t, err)

	socket := filepath.Join(tmp, "gitaly.sock")
	ln, err := net.Listen("unix", socket)
	require.NoError(t, err)

	srv := grpc.NewServer()
	gitalypb.RegisterSmartHTTPServiceServer(srv, s)
	go func() {
		require.NoError(t, srv.Serve(ln))
	}()

	return fmt.Sprintf("%s://%s", ln.Addr().Network(), ln.Addr().String()), func() {
		srv.GracefulStop()
		require.NoError(t, os.RemoveAll(tmp), "error removing temp dir %q", tmp)
	}
}