gitaly.go 7.3 KB
Newer Older
1 2 3
package testhelper

import (
4
	"fmt"
5 6 7 8
	"io"
	"io/ioutil"
	"log"
	"path"
9
	"strings"
10
	"sync"
11

12
	pb "gitlab.com/gitlab-org/gitaly-proto/go"
13

Ahmad Sherif's avatar
Ahmad Sherif committed
14
	"golang.org/x/net/context"
15 16
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
17 18
)

19
type GitalyTestServer struct {
Jacob Vosmaer's avatar
Jacob Vosmaer committed
20
	finalMessageCode codes.Code
21
	sync.WaitGroup
22
}
23

24 25
var (
	GitalyInfoRefsResponseMock    = strings.Repeat("Mock Gitaly InfoRefsResponse data", 100000)
26
	GitalyGetBlobResponseMock     = strings.Repeat("Mock Gitaly GetBlobResponse data", 100000)
27
	GitalyGetArchiveResponseMock  = strings.Repeat("Mock Gitaly GetArchiveResponse data", 100000)
28 29 30
	GitalyReceivePackResponseMock []byte
	GitalyUploadPackResponseMock  []byte
)
31 32 33 34 35 36 37 38 39 40 41

func init() {
	var err error
	if GitalyReceivePackResponseMock, err = ioutil.ReadFile(path.Join(RootDir(), "testdata/receive-pack-fixture.txt")); err != nil {
		log.Fatal(err)
	}
	if GitalyUploadPackResponseMock, err = ioutil.ReadFile(path.Join(RootDir(), "testdata/upload-pack-fixture.txt")); err != nil {
		log.Fatal(err)
	}
}

Jacob Vosmaer's avatar
Jacob Vosmaer committed
42 43
func NewGitalyServer(finalMessageCode codes.Code) *GitalyTestServer {
	return &GitalyTestServer{finalMessageCode: finalMessageCode}
44 45
}

46
func (s *GitalyTestServer) InfoRefsUploadPack(in *pb.InfoRefsRequest, stream pb.SmartHTTPService_InfoRefsUploadPackServer) error {
47 48 49
	s.WaitGroup.Add(1)
	defer s.WaitGroup.Done()

50 51 52 53
	if err := validateRepository(in.GetRepository()); err != nil {
		return err
	}

54 55 56 57
	nSends, err := sendBytes([]byte(GitalyInfoRefsResponseMock), 100, func(p []byte) error {
		return stream.Send(&pb.InfoRefsResponse{Data: p})
	})
	if err != nil {
Jacob Vosmaer's avatar
Jacob Vosmaer committed
58 59
		return err
	}
60 61 62
	if nSends <= 1 {
		panic("should have sent more than one message")
	}
Jacob Vosmaer's avatar
Jacob Vosmaer committed
63 64

	return s.finalError()
65 66
}

67
func (s *GitalyTestServer) InfoRefsReceivePack(in *pb.InfoRefsRequest, stream pb.SmartHTTPService_InfoRefsReceivePackServer) error {
68 69 70
	s.WaitGroup.Add(1)
	defer s.WaitGroup.Done()

71 72 73 74
	if err := validateRepository(in.GetRepository()); err != nil {
		return err
	}

75 76 77
	response := &pb.InfoRefsResponse{
		Data: []byte(GitalyInfoRefsResponseMock),
	}
Jacob Vosmaer's avatar
Jacob Vosmaer committed
78 79 80 81 82
	if err := stream.Send(response); err != nil {
		return err
	}

	return s.finalError()
83
}
84

85
func (s *GitalyTestServer) PostReceivePack(stream pb.SmartHTTPService_PostReceivePackServer) error {
86 87 88
	s.WaitGroup.Add(1)
	defer s.WaitGroup.Done()

89 90 91 92 93
	req, err := stream.Recv()
	if err != nil {
		return err
	}

94 95 96 97
	repo := req.GetRepository()
	if err := validateRepository(req.GetRepository()); err != nil {
		return err
	}
98 99 100 101 102

	data := []byte(strings.Join([]string{
		repo.GetStorageName(),
		repo.GetRelativePath(),
		req.GlId,
103
		req.GlUsername,
104
	}, "\000") + "\000")
105 106 107 108 109 110 111 112 113 114 115

	// The body of the request starts in the second message
	for {
		req, err := stream.Recv()
		if err != nil {
			if err != io.EOF {
				return err
			}
			break
		}

116 117 118 119 120 121 122 123 124 125
		// We want to echo the request data back
		data = append(data, req.GetData()...)
	}

	nSends, err := sendBytes(data, 100, func(p []byte) error {
		return stream.Send(&pb.PostReceivePackResponse{Data: p})
	})

	if nSends <= 1 {
		panic("should have sent more than one message")
126 127
	}

Jacob Vosmaer's avatar
Jacob Vosmaer committed
128
	return s.finalError()
129 130
}

131
func (s *GitalyTestServer) PostUploadPack(stream pb.SmartHTTPService_PostUploadPackServer) error {
132 133 134
	s.WaitGroup.Add(1)
	defer s.WaitGroup.Done()

135 136 137 138 139
	req, err := stream.Recv()
	if err != nil {
		return err
	}

140 141 142 143
	repo := req.GetRepository()
	if err := validateRepository(req.GetRepository()); err != nil {
		return err
	}
144 145 146 147 148

	data := []byte(strings.Join([]string{
		repo.GetStorageName(),
		repo.GetRelativePath(),
	}, "\000") + "\000")
149 150 151 152 153 154 155 156 157 158 159

	// The body of the request starts in the second message
	for {
		req, err := stream.Recv()
		if err != nil {
			if err != io.EOF {
				return err
			}
			break
		}

160 161 162 163 164 165 166 167 168 169 170 171 172 173
		data = append(data, req.GetData()...)
	}

	nSends, err := sendBytes(data, 100, func(p []byte) error {
		return stream.Send(&pb.PostUploadPackResponse{Data: p})
	})

	if nSends <= 1 {
		panic("should have sent more than one message")
	}

	return s.finalError()
}

Ahmad Sherif's avatar
Ahmad Sherif committed
174 175 176 177
func (s *GitalyTestServer) CommitIsAncestor(ctx context.Context, in *pb.CommitIsAncestorRequest) (*pb.CommitIsAncestorResponse, error) {
	return nil, nil
}

178
func (s *GitalyTestServer) GetBlob(in *pb.GetBlobRequest, stream pb.BlobService_GetBlobServer) error {
Ahmad Sherif's avatar
Ahmad Sherif committed
179 180 181 182 183 184 185
	s.WaitGroup.Add(1)
	defer s.WaitGroup.Done()

	if err := validateRepository(in.GetRepository()); err != nil {
		return err
	}

186 187 188
	response := &pb.GetBlobResponse{
		Oid:  in.GetOid(),
		Size: int64(len(GitalyGetBlobResponseMock)),
Ahmad Sherif's avatar
Ahmad Sherif committed
189
	}
190
	nSends, err := sendBytes([]byte(GitalyGetBlobResponseMock), 100, func(p []byte) error {
Ahmad Sherif's avatar
Ahmad Sherif committed
191 192 193 194 195 196 197
		response.Data = p

		if err := stream.Send(response); err != nil {
			return err
		}

		// Use a new response so we don't send other fields (Size, ...) over and over
198
		response = &pb.GetBlobResponse{}
Ahmad Sherif's avatar
Ahmad Sherif committed
199 200 201 202 203 204 205 206 207 208 209 210 211

		return nil
	})
	if err != nil {
		return err
	}
	if nSends <= 1 {
		panic("should have sent more than one message")
	}

	return s.finalError()
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
func (s *GitalyTestServer) GetArchive(in *pb.GetArchiveRequest, stream pb.RepositoryService_GetArchiveServer) error {
	s.WaitGroup.Add(1)
	defer s.WaitGroup.Done()

	if err := validateRepository(in.GetRepository()); err != nil {
		return err
	}

	nSends, err := sendBytes([]byte(GitalyGetArchiveResponseMock), 100, func(p []byte) error {
		return stream.Send(&pb.GetArchiveResponse{Data: p})
	})
	if err != nil {
		return err
	}
	if nSends <= 1 {
		panic("should have sent more than one message")
	}

	return s.finalError()
}

func (s *GitalyTestServer) RepositoryExists(context.Context, *pb.RepositoryExistsRequest) (*pb.RepositoryExistsResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) RepackIncremental(context.Context, *pb.RepackIncrementalRequest) (*pb.RepackIncrementalResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) RepackFull(context.Context, *pb.RepackFullRequest) (*pb.RepackFullResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) GarbageCollect(context.Context, *pb.GarbageCollectRequest) (*pb.GarbageCollectResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) RepositorySize(context.Context, *pb.RepositorySizeRequest) (*pb.RepositorySizeResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) ApplyGitattributes(context.Context, *pb.ApplyGitattributesRequest) (*pb.ApplyGitattributesResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) FetchRemote(context.Context, *pb.FetchRemoteRequest) (*pb.FetchRemoteResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) CreateRepository(context.Context, *pb.CreateRepositoryRequest) (*pb.CreateRepositoryResponse, error) {
	return nil, nil
}

func (s *GitalyTestServer) Exists(context.Context, *pb.RepositoryExistsRequest) (*pb.RepositoryExistsResponse, error) {
	return nil, nil
}

269 270 271 272 273 274 275
// sendBytes returns the number of times the 'sender' function was called and an error.
func sendBytes(data []byte, chunkSize int, sender func([]byte) error) (int, error) {
	i := 0
	for ; len(data) > 0; i++ {
		n := chunkSize
		if n > len(data) {
			n = len(data)
276
		}
277 278 279

		if err := sender(data[:n]); err != nil {
			return i, err
280
		}
281
		data = data[n:]
282 283
	}

284
	return i, nil
Jacob Vosmaer's avatar
Jacob Vosmaer committed
285 286 287 288 289
}

func (s *GitalyTestServer) finalError() error {
	if code := s.finalMessageCode; code != codes.OK {
		return grpc.Errorf(code, "error as specified by test")
290 291
	}

292 293
	return nil
}
294 295 296 297 298 299 300 301 302 303

func validateRepository(repo *pb.Repository) error {
	if len(repo.GetStorageName()) == 0 {
		return fmt.Errorf("missing storage_name: %v", repo)
	}
	if len(repo.GetRelativePath()) == 0 {
		return fmt.Errorf("missing relative_path: %v", repo)
	}
	return nil
}