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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package hook
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/config"
"gitlab.com/gitlab-org/gitaly/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
"google.golang.org/grpc/codes"
)
func TestPreReceiveInvalidArgument(t *testing.T) {
server, serverSocketPath := runHooksServer(t)
defer server.Stop()
client, conn := newHooksClient(t, serverSocketPath)
defer conn.Close()
ctx, cancel := testhelper.Context()
defer cancel()
stream, err := client.PreReceiveHook(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(&gitalypb.PreReceiveHookRequest{}))
_, err = stream.Recv()
testhelper.RequireGrpcError(t, err, codes.InvalidArgument)
}
func TestPreReceive(t *testing.T) {
rubyDir := config.Config.Ruby.Dir
defer func(rubyDir string) {
config.Config.Ruby.Dir = rubyDir
}(rubyDir)
cwd, err := os.Getwd()
require.NoError(t, err)
config.Config.Ruby.Dir = filepath.Join(cwd, "testdata")
server, serverSocketPath := runHooksServer(t)
defer server.Stop()
testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
gitObjectDir := "/some/dir"
gitAlternateObjectDirs := []string{"a", "b", "c"}
testRepo.GitObjectDirectory = gitObjectDir
testRepo.GitAlternateObjectDirectories = gitAlternateObjectDirs
client, conn := newHooksClient(t, serverSocketPath)
defer conn.Close()
ctx, cancel := testhelper.Context()
defer cancel()
testCases := []struct {
desc string
stdin io.Reader
req gitalypb.PreReceiveHookRequest
status int32
stdout, stderr string
}{
{
desc: "valid stdin",
stdin: bytes.NewBufferString("a\nb\nc\nd\ne\nf\ng"),
req: gitalypb.PreReceiveHookRequest{Repository: testRepo, KeyId: "key_id", Protocol: "protocol"},
status: 0,
stdout: "OK",
stderr: "",
},
{
desc: "missing stdin",
stdin: bytes.NewBuffer(nil),
req: gitalypb.PreReceiveHookRequest{Repository: testRepo, KeyId: "key_id", Protocol: "protocol"},
status: 1,
stdout: "",
stderr: "FAIL",
},
{
desc: "missing protocol",
stdin: bytes.NewBufferString("a\nb\nc\nd\ne\nf\ng"),
req: gitalypb.PreReceiveHookRequest{Repository: testRepo, KeyId: "key_id"},
status: 1,
stdout: "",
stderr: "FAIL",
},
{
desc: "missing key_id",
stdin: bytes.NewBufferString("a\nb\nc\nd\ne\nf\ng"),
req: gitalypb.PreReceiveHookRequest{Repository: testRepo, Protocol: "protocol"},
status: 1,
stdout: "",
stderr: "FAIL",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
stream, err := client.PreReceiveHook(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(&tc.req))
go func() {
writer := streamio.NewWriter(func(p []byte) error {
return stream.Send(&gitalypb.PreReceiveHookRequest{Stdin: p})
})
_, err := io.Copy(writer, tc.stdin)
require.NoError(t, err)
require.NoError(t, stream.CloseSend(), "close send")
}()
var status int32
var stdout, stderr bytes.Buffer
for {
resp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
t.Errorf("error when receiving stream: %v", err)
}
_, err = stdout.Write(resp.GetStdout())
require.NoError(t, err)
_, err = stderr.Write(resp.GetStderr())
require.NoError(t, err)
status = resp.GetExitStatus().GetValue()
require.NoError(t, err)
}
require.Equal(t, tc.status, status)
assert.Equal(t, tc.stderr, text.ChompBytes(stderr.Bytes()), "hook stderr")
assert.Equal(t, tc.stdout, text.ChompBytes(stdout.Bytes()), "hook stdout")
})
}
}
|