debian-mirror-gitlab/workhorse/internal/git/upload-pack_test.go

89 lines
2.1 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package git
import (
"fmt"
"io/ioutil"
"net"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
2021-09-04 01:27:46 +05:30
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
2021-02-22 17:27:13 +05:30
2021-10-27 15:23:28 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
2021-02-22 17:27:13 +05:30
)
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 }()
2021-11-18 22:05:49 +05:30
addr := startSmartHTTPServer(t, &smartHTTPServiceServer{
2021-02-22 17:27:13 +05:30
PostUploadPackFunc: func(stream gitalypb.SmartHTTPService_PostUploadPackServer) error {
_, err := stream.Recv() // trigger a read on the client request body
require.NoError(t, err)
return nil
},
})
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")
}
2021-11-18 22:05:49 +05:30
func startSmartHTTPServer(t testing.TB, s gitalypb.SmartHTTPServiceServer) string {
t.Helper()
2021-02-22 17:27:13 +05:30
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))
}()
2021-11-18 22:05:49 +05:30
t.Cleanup(func() {
2021-02-22 17:27:13 +05:30
srv.GracefulStop()
require.NoError(t, os.RemoveAll(tmp), "error removing temp dir %q", tmp)
2021-11-18 22:05:49 +05:30
})
return fmt.Sprintf("%s://%s", ln.Addr().Network(), ln.Addr().String())
2021-02-22 17:27:13 +05:30
}