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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package operations
import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/internal/config"
"gitlab.com/gitlab-org/gitaly/internal/git/hooks"
"gitlab.com/gitlab-org/gitaly/internal/rubyserver"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
var (
gitlabPreHooks = []string{"pre-receive", "update"}
gitlabPostHooks = []string{"post-receive"}
GitlabPreHooks = gitlabPreHooks
GitlabHooks []string
RubyServer *rubyserver.Server
user = &gitalypb.User{
Name: []byte("Jane Doe"),
Email: []byte("janedoe@gitlab.com"),
GlId: "user-123",
GlUsername: "janedoe",
}
)
func init() {
copy(GitlabHooks, gitlabPreHooks)
GitlabHooks = append(GitlabHooks, gitlabPostHooks...)
}
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
defer testhelper.MustHaveNoChildProcess()
hookDir, err := ioutil.TempDir("", "gitaly-tmp-hooks")
if err != nil {
log.Fatal(err)
}
defer func(oldDir string) {
config.Config.GitlabShell.Dir = oldDir
}(config.Config.GitlabShell.Dir)
config.Config.GitlabShell.Dir = hookDir
if err := os.MkdirAll(hooks.Path(), 0755); err != nil {
log.Fatal(err)
}
defer os.RemoveAll(hookDir)
testhelper.ConfigureGitalySSH()
testhelper.ConfigureRuby()
RubyServer, err = rubyserver.Start()
if err != nil {
log.Fatal(err)
}
defer RubyServer.Stop()
return m.Run()
}
func runOperationServiceServer(t *testing.T) (*grpc.Server, string) {
grpcServer := testhelper.NewTestGrpcServer(t, nil, nil)
serverSocketPath := testhelper.GetTemporaryGitalySocketFileName()
listener, err := net.Listen("unix", serverSocketPath)
if err != nil {
t.Fatal(err)
}
gitalypb.RegisterOperationServiceServer(grpcServer, &server{RubyServer})
reflection.Register(grpcServer)
go grpcServer.Serve(listener)
return grpcServer, "unix://" + serverSocketPath
}
func newOperationClient(t *testing.T, serverSocketPath string) (gitalypb.OperationServiceClient, *grpc.ClientConn) {
connOpts := []grpc.DialOption{
grpc.WithInsecure(),
}
conn, err := grpc.Dial(serverSocketPath, connOpts...)
if err != nil {
t.Fatal(err)
}
return gitalypb.NewOperationServiceClient(conn), conn
}
var NewOperationClient = newOperationClient
// The callee is responsible for clean up of the specific hook, testMain removes
// the hook dir
func WriteEnvToHook(t *testing.T, repoPath, hookName string) string {
hookOutputTemp, err := ioutil.TempFile("", "")
require.NoError(t, err)
require.NoError(t, hookOutputTemp.Close())
hookContent := fmt.Sprintf("#!/bin/sh\n/usr/bin/env > %s\n", hookOutputTemp.Name())
_, err = OverrideHooks(hookName, []byte(hookContent))
require.NoError(t, err)
return hookOutputTemp.Name()
}
// When testing hooks, the content for one specific hook can be defined, to simulate
// behaviours on different hook content
func OverrideHooks(name string, content []byte) (func(), error) {
fullPath := path.Join(hooks.Path(), name)
return func() { os.Remove(fullPath) }, ioutil.WriteFile(fullPath, content, 0755)
}
|