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
|
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 TestPostReceiveInvalidArgument(t *testing.T) {
stop, serverSocketPath := runHooksServer(t)
defer stop()
client, conn := newHooksClient(t, serverSocketPath)
defer conn.Close()
ctx, cancel := testhelper.Context()
defer cancel()
stream, err := client.PostReceiveHook(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(&gitalypb.PostReceiveHookRequest{}), "empty repository should result in an error")
_, err = stream.Recv()
testhelper.RequireGrpcError(t, err, codes.InvalidArgument)
}
func TestPostReceive(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")
stop, serverSocketPath := runHooksServer(t)
defer stop()
testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
client, conn := newHooksClient(t, serverSocketPath)
defer conn.Close()
testCases := []struct {
desc string
stdin io.Reader
req gitalypb.PostReceiveHookRequest
status int32
stdout string
stderr string
}{
{
desc: "valid stdin",
stdin: bytes.NewBufferString("a\nb\nc\nd\ne\nf\ng"),
req: gitalypb.PostReceiveHookRequest{Repository: testRepo, KeyId: "key_id", GitPushOptions: []string{"option0", "option1"}},
status: 0,
stdout: "OK",
stderr: "",
},
{
desc: "missing stdin",
stdin: bytes.NewBuffer(nil),
req: gitalypb.PostReceiveHookRequest{Repository: testRepo, KeyId: "key_id", GitPushOptions: []string{"option0"}},
status: 1,
stdout: "",
stderr: "FAIL",
},
{
desc: "missing key_id",
stdin: bytes.NewBuffer(nil),
req: gitalypb.PostReceiveHookRequest{Repository: testRepo, GitPushOptions: []string{"option0"}},
status: 1,
stdout: "",
stderr: "FAIL",
},
{
desc: "missing git push option",
stdin: bytes.NewBuffer(nil),
req: gitalypb.PostReceiveHookRequest{Repository: testRepo, KeyId: "key_id"},
status: 1,
stdout: "",
stderr: "FAIL",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
stream, err := client.PostReceiveHook(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.PostReceiveHookRequest{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
}
_, err = stdout.Write(resp.GetStdout())
require.NoError(t, err)
stderr.Write(resp.GetStderr())
status = resp.GetExitStatus().GetValue()
}
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")
})
}
}
|