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.

94 lines
2.4 KiB
Go
Raw Permalink 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
"net"
"net/http/httptest"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
2022-08-13 15:12:31 +05:30
"gitlab.com/gitlab-org/gitaly/v15/client"
"gitlab.com/gitlab-org/gitaly/v15/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()
2022-07-23 23:45:48 +05:30
_, _ = io.Copy(io.Discard, conn)
2022-07-16 23:28:13 +05:30
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()
2022-07-23 23:45:48 +05:30
tmp := t.TempDir()
2021-02-22 17:27:13 +05:30
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()
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
}