Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testhelper_test.go « storage « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aeb405ca6a556b480cc59359cf91aee4b81072c2 (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
package storage

import (
	"net"
	"os"
	"path/filepath"
	"testing"
	"time"

	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/config"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var testStorage config.Storage

func TestMain(m *testing.M) {
	configureTestStorage()
	os.Exit(m.Run())
}

func configureTestStorage() {
	storagePath, err := filepath.Abs("testdata/repositories/storage1")
	if err != nil {
		panic(err)
	}

	if err := os.RemoveAll(storagePath); err != nil {
		panic(err)
	}

	if err := os.MkdirAll(storagePath, 0755); err != nil {
		panic(err)
	}

	testStorage = config.Storage{Name: "storage-will-be-deleted", Path: storagePath}

	config.Config.Storages = []config.Storage{testStorage}
}

func runStorageServer(t *testing.T) (*grpc.Server, string) {
	server := testhelper.NewTestGrpcServer(t, nil, nil)
	serverSocketPath := testhelper.GetTemporaryGitalySocketFileName()

	listener, err := net.Listen("unix", serverSocketPath)
	if err != nil {
		t.Fatal(err)
	}

	gitalypb.RegisterStorageServiceServer(server, NewServer())
	reflection.Register(server)

	go server.Serve(listener)

	return server, serverSocketPath
}

func newStorageClient(t *testing.T, serverSocketPath string) (gitalypb.StorageServiceClient, *grpc.ClientConn) {
	connOpts := []grpc.DialOption{
		grpc.WithInsecure(),
		grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
			return net.DialTimeout("unix", addr, timeout)
		}),
	}
	conn, err := grpc.Dial(serverSocketPath, connOpts...)
	if err != nil {
		t.Fatal(err)
	}

	return gitalypb.NewStorageServiceClient(conn), conn
}