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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.5 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package git
import (
2022-07-16 23:28:13 +05:30
"context"
"errors"
2021-02-22 17:27:13 +05:30
"fmt"
2022-07-16 23:28:13 +05:30
"io"
2021-02-22 17:27:13 +05:30
"io/ioutil"
"net"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
2022-07-16 23:28:13 +05:30
"gitlab.com/gitlab-org/gitaly/v14/client"
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"
2022-07-16 23:28:13 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
2021-02-22 17:27:13 +05:30
)
var (
originalUploadPackTimeout = uploadPackTimeout
)
2022-07-16 23:28:13 +05:30
type waitReader struct {
t time.Duration
2021-02-22 17:27:13 +05:30
}
2022-07-16 23:28:13 +05:30
func (f *waitReader) Read(b []byte) (int, error) {
time.Sleep(f.t)
return 0, io.EOF
2021-02-22 17:27:13 +05:30
}
type smartHTTPServiceServer struct {
gitalypb.UnimplementedSmartHTTPServiceServer
2022-07-16 23:28:13 +05:30
handler func(context.Context, *gitalypb.PostUploadPackWithSidechannelRequest) (*gitalypb.PostUploadPackWithSidechannelResponse, error)
2021-02-22 17:27:13 +05:30
}
2022-07-16 23:28:13 +05:30
func (srv *smartHTTPServiceServer) PostUploadPackWithSidechannel(ctx context.Context, req *gitalypb.PostUploadPackWithSidechannelRequest) (*gitalypb.PostUploadPackWithSidechannelResponse, error) {
return srv.handler(ctx, req)
2021-02-22 17:27:13 +05:30
}
func TestUploadPackTimesOut(t *testing.T) {
uploadPackTimeout = time.Millisecond
defer func() { uploadPackTimeout = originalUploadPackTimeout }()
2021-11-18 22:05:49 +05:30
addr := startSmartHTTPServer(t, &smartHTTPServiceServer{
2022-07-16 23:28:13 +05:30
handler: func(ctx context.Context, req *gitalypb.PostUploadPackWithSidechannelRequest) (*gitalypb.PostUploadPackWithSidechannelResponse, error) {
conn, err := client.OpenServerSidechannel(ctx)
if err != nil {
return nil, err
}
defer conn.Close()
_, _ = io.Copy(ioutil.Discard, conn)
return &gitalypb.PostUploadPackWithSidechannelResponse{}, nil
2021-02-22 17:27:13 +05:30
},
})
2022-07-16 23:28:13 +05:30
body := &waitReader{t: 10 * time.Millisecond}
2021-02-22 17:27:13 +05:30
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", body)
2022-07-16 23:28:13 +05:30
a := &api.Response{GitalyServer: api.GitalyServer{Address: addr}}
2021-02-22 17:27:13 +05:30
err := handleUploadPack(NewHttpResponseWriter(w), r, a)
2022-07-16 23:28:13 +05:30
require.True(t, errors.Is(err, context.DeadlineExceeded))
2021-02-22 17:27:13 +05:30
}
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)
2022-07-16 23:28:13 +05:30
srv := grpc.NewServer(testhelper.WithSidechannel())
2021-02-22 17:27:13 +05:30
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
}