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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
package hook
import (
"context"
"errors"
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/featureflag"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/transaction"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitlab"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg"
"gitlab.com/gitlab-org/gitaly/v16/internal/transaction/txinfo"
"gitlab.com/gitlab-org/gitaly/v16/internal/transaction/voting"
)
func TestHookManager_stopCalled(t *testing.T) {
ctx := testhelper.Context(t)
cfg := testcfg.Build(t)
repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
SkipCreationViaService: true,
})
expectedTx := txinfo.Transaction{
ID: 1234, Node: "primary", Primary: true,
}
var mockTxMgr transaction.MockManager
hookManager := NewManager(cfg, config.NewLocator(cfg), gittest.NewCommandFactory(t, cfg), &mockTxMgr, gitlab.NewMockClient(
t, gitlab.MockAllowed, gitlab.MockPreReceive, gitlab.MockPostReceive,
))
hooksPayload, err := git.NewHooksPayload(
cfg,
repo,
gittest.DefaultObjectHash,
&expectedTx,
&git.UserDetails{
UserID: "1234",
Username: "user",
Protocol: "web",
},
git.ReferenceTransactionHook,
featureflag.FromContext(ctx),
).Env()
require.NoError(t, err)
hookPaths := make([]string, 3)
for i, hook := range []string{"pre-receive", "update", "post-receive"} {
hookPaths[i] = gittest.WriteCustomHook(t, repoPath, hook, []byte("#!/bin/sh\nexit 1\n"))
}
preReceiveFunc := func(t *testing.T) error {
return hookManager.PreReceiveHook(ctx, repo, nil, []string{hooksPayload}, strings.NewReader("changes"), io.Discard, io.Discard)
}
updateFunc := func(t *testing.T) error {
return hookManager.UpdateHook(ctx, repo, "ref", gittest.DefaultObjectHash.ZeroOID.String(), gittest.DefaultObjectHash.ZeroOID.String(), []string{hooksPayload}, io.Discard, io.Discard)
}
postReceiveFunc := func(t *testing.T) error {
return hookManager.PostReceiveHook(ctx, repo, nil, []string{hooksPayload}, strings.NewReader("changes"), io.Discard, io.Discard)
}
for _, tc := range []struct {
desc string
hookFunc func(*testing.T) error
hookPath string
stopErr error
}{
{
desc: "pre-receive gets successfully stopped",
hookFunc: preReceiveFunc,
hookPath: hookPaths[0],
},
{
desc: "pre-receive with stop error does not clobber real error",
hookFunc: preReceiveFunc,
stopErr: errors.New("stop error"),
hookPath: hookPaths[0],
},
{
desc: "post-receive gets successfully stopped",
hookFunc: postReceiveFunc,
hookPath: hookPaths[2],
},
{
desc: "post-receive with stop error does not clobber real error",
hookFunc: postReceiveFunc,
stopErr: errors.New("stop error"),
hookPath: hookPaths[2],
},
{
desc: "update gets successfully stopped",
hookFunc: updateFunc,
hookPath: hookPaths[1],
},
{
desc: "update with stop error does not clobber real error",
hookFunc: updateFunc,
stopErr: errors.New("stop error"),
hookPath: hookPaths[1],
},
} {
t.Run(tc.desc, func(t *testing.T) {
wasInvoked := false
mockTxMgr.StopFn = func(ctx context.Context, tx txinfo.Transaction) error {
require.Equal(t, expectedTx, tx)
wasInvoked = true
return tc.stopErr
}
err := tc.hookFunc(t)
require.Equal(t, fmt.Sprintf("executing custom hooks: error executing \"%s\": exit status 1", tc.hookPath), err.Error())
require.True(t, wasInvoked, "expected stop to have been invoked")
})
}
}
func TestHookManager_contextCancellationCancelsVote(t *testing.T) {
ctx, cancel := context.WithCancel(testhelper.Context(t))
cfg := testcfg.Build(t)
repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
SkipCreationViaService: true,
})
commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))
mockTxMgr := transaction.MockManager{
VoteFn: func(ctx context.Context, _ txinfo.Transaction, _ voting.Vote, _ voting.Phase) error {
<-ctx.Done()
return fmt.Errorf("mock error: %w", ctx.Err())
},
}
hookManager := NewManager(cfg, config.NewLocator(cfg), gittest.NewCommandFactory(t, cfg), &mockTxMgr, gitlab.NewMockClient(
t, gitlab.MockAllowed, gitlab.MockPreReceive, gitlab.MockPostReceive,
))
hooksPayload, err := git.NewHooksPayload(
cfg,
repo,
gittest.DefaultObjectHash,
&txinfo.Transaction{
ID: 1234, Node: "primary", Primary: true,
},
nil,
git.ReferenceTransactionHook,
nil,
).Env()
require.NoError(t, err)
changes := fmt.Sprintf("%s %s refs/heads/master", commitID, gittest.DefaultObjectHash.ZeroOID)
cancel()
err = hookManager.ReferenceTransactionHook(ctx, ReferenceTransactionPrepared, []string{hooksPayload}, strings.NewReader(changes))
require.Equal(t, "error voting on transaction: mock error: context canceled", err.Error())
}
func TestIsForceDeletionsOnly(t *testing.T) {
anyOID := strings.Repeat("1", gittest.DefaultObjectHash.EncodedLen())
zeroOID := gittest.DefaultObjectHash.ZeroOID.String()
forceDeletion := fmt.Sprintf("%s %s refs/heads/force-delete", zeroOID, zeroOID)
forceUpdate := fmt.Sprintf("%s %s refs/heads/force-update", zeroOID, anyOID)
deletion := fmt.Sprintf("%s %s refs/heads/delete", anyOID, zeroOID)
for _, tc := range []struct {
desc string
changes string
expected bool
}{
{
desc: "single force deletion",
changes: forceDeletion + "\n",
expected: true,
},
{
desc: "single force deletion with missing newline",
changes: forceDeletion,
expected: true,
},
{
desc: "multiple force deletions",
changes: strings.Join([]string{forceDeletion, forceDeletion}, "\n"),
expected: true,
},
{
desc: "single non-force deletion",
changes: deletion + "\n",
expected: false,
},
{
desc: "single force update",
changes: forceUpdate + "\n",
expected: false,
},
{
desc: "mixed deletions and updates",
changes: strings.Join([]string{forceDeletion, forceUpdate}, "\n"),
expected: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
actual := isForceDeletionsOnly(gittest.DefaultObjectHash, strings.NewReader(tc.changes))
require.Equal(t, tc.expected, actual)
})
}
}
|