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

update_branches_test.go « operations « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ea5701fa715f9c9e2ae5cff13341741073e4190 (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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package operations

import (
	"crypto/sha1"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v14/internal/git"
	"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
	"gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

var (
	updateBranchName = "feature"
	newrev           = []byte("1a35b5a77cf6af7edf6703f88e82f6aff613666f")
	oldrev           = []byte("0b4bc9a49b562e85de7cc9e834518ea6828729b9")
)

func TestSuccessfulUserUpdateBranchRequest(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)

	repo := localrepo.NewTestRepo(t, cfg, repoProto)

	testCases := []struct {
		desc             string
		updateBranchName string
		oldRev           []byte
		newRev           []byte
	}{
		{
			desc:             "short name fast-forward update",
			updateBranchName: updateBranchName,
			oldRev:           []byte("0b4bc9a49b562e85de7cc9e834518ea6828729b9"),
			newRev:           []byte("1a35b5a77cf6af7edf6703f88e82f6aff613666f"),
		},
		{
			desc:             "short name non-fast-forward update",
			updateBranchName: "fix",
			oldRev:           []byte("48f0be4bd10c1decee6fae52f9ae6d10f77b60f4"),
			newRev:           []byte("12d65c8dd2b2676fa3ac47d955accc085a37a9c1"),
		},
		{
			desc:             "short name branch creation",
			updateBranchName: "a-new-branch",
			oldRev:           []byte(git.ZeroOID.String()),
			newRev:           []byte("845009f4d7bdc9e0d8f26b1c6fb6e108aaff9314"),
		},
		// We create refs/heads/heads/BRANCH and
		// refs/heads/refs/heads/BRANCH here. See a similar
		// test for UserCreateBranch in
		// TestSuccessfulCreateBranchRequest()
		{
			desc:             "heads/* branch creation",
			updateBranchName: "heads/a-new-branch",
			oldRev:           []byte(git.ZeroOID.String()),
			newRev:           []byte("845009f4d7bdc9e0d8f26b1c6fb6e108aaff9314"),
		},
		{
			desc:             "refs/heads/* branch creation",
			updateBranchName: "refs/heads/a-new-branch",
			oldRev:           []byte(git.ZeroOID.String()),
			newRev:           []byte("845009f4d7bdc9e0d8f26b1c6fb6e108aaff9314"),
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			responseOk := &gitalypb.UserUpdateBranchResponse{}
			request := &gitalypb.UserUpdateBranchRequest{
				Repository: repoProto,
				BranchName: []byte(testCase.updateBranchName),
				Newrev:     testCase.newRev,
				Oldrev:     testCase.oldRev,
				User:       gittest.TestUser,
			}
			response, err := client.UserUpdateBranch(ctx, request)
			require.NoError(t, err)
			testhelper.ProtoEqual(t, responseOk, response)

			branchCommit, err := repo.ReadCommit(ctx, git.Revision(testCase.updateBranchName))

			require.NoError(t, err)
			require.Equal(t, string(testCase.newRev), branchCommit.Id)

			branches := gittest.Exec(t, cfg, "-C", repoPath, "for-each-ref", "--", "refs/heads/"+branchName)
			require.Contains(t, string(branches), "refs/heads/"+branchName)
		})
	}
}

func TestSuccessfulUserUpdateBranchRequestToDelete(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)

	repo := localrepo.NewTestRepo(t, cfg, repoProto)

	testCases := []struct {
		desc             string
		updateBranchName string
		oldRev           []byte
		newRev           []byte
		err              error
		createBranch     bool
	}{
		{
			desc:             "short name branch deletion",
			updateBranchName: "csv",
			oldRev:           []byte("3dd08961455abf80ef9115f4afdc1c6f968b503c"),
			newRev:           []byte(git.ZeroOID.String()),
			err:              status.Error(codes.InvalidArgument, "object not found"),
		},
		// We test for the failed heads/* and refs/heads/* cases below in TestFailedUserUpdateBranchRequest
		{
			desc:             "heads/* name branch deletion",
			updateBranchName: "heads/my-test-branch",
			createBranch:     true,
			oldRev:           []byte("689600b91aabec706e657e38ea706ece1ee8268f"),
			newRev:           []byte(git.ZeroOID.String()),
			err:              status.Error(codes.InvalidArgument, "object not found"),
		},
		{
			desc:             "refs/heads/* name branch deletion",
			updateBranchName: "refs/heads/my-other-test-branch",
			createBranch:     true,
			oldRev:           []byte("db46a1c5a5e474aa169b6cdb7a522d891bc4c5f9"),
			newRev:           []byte(git.ZeroOID.String()),
			err:              status.Error(codes.InvalidArgument, "object not found"),
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			if testCase.createBranch {
				gittest.Exec(t, cfg, "-C", repoPath, "branch", "--", testCase.updateBranchName, string(testCase.oldRev))
			}

			responseOk := &gitalypb.UserUpdateBranchResponse{}
			request := &gitalypb.UserUpdateBranchRequest{
				Repository: repoProto,
				BranchName: []byte(testCase.updateBranchName),
				Newrev:     testCase.newRev,
				Oldrev:     testCase.oldRev,
				User:       gittest.TestUser,
			}
			response, err := client.UserUpdateBranch(ctx, request)
			require.NoError(t, err)
			testhelper.ProtoEqual(t, responseOk, response)

			_, err = repo.ReadCommit(ctx, git.Revision(testCase.updateBranchName))
			require.Equal(t, localrepo.ErrObjectNotFound, err, "expected 'not found' error got %v", err)

			refs := gittest.Exec(t, cfg, "-C", repoPath, "for-each-ref", "--", "refs/heads/"+testCase.updateBranchName)
			require.NotContains(t, string(refs), testCase.oldRev, "branch deleted from refs")
		})
	}
}

func TestSuccessfulGitHooksForUserUpdateBranchRequest(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	ctx, cfg, _, _, client := setupOperationsService(t, ctx)

	for _, hookName := range GitlabHooks {
		t.Run(hookName, func(t *testing.T) {
			testRepo, testRepoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{
				Seed: gittest.SeedGitLabTest,
			})

			hookOutputTempPath := gittest.WriteEnvToCustomHook(t, testRepoPath, hookName)

			request := &gitalypb.UserUpdateBranchRequest{
				Repository: testRepo,
				BranchName: []byte(updateBranchName),
				Newrev:     newrev,
				Oldrev:     oldrev,
				User:       gittest.TestUser,
			}

			responseOk := &gitalypb.UserUpdateBranchResponse{}
			response, err := client.UserUpdateBranch(ctx, request)
			require.NoError(t, err)
			require.Empty(t, response.PreReceiveError)

			testhelper.ProtoEqual(t, responseOk, response)
			output := string(testhelper.MustReadFile(t, hookOutputTempPath))
			require.Contains(t, output, "GL_USERNAME="+gittest.TestUser.GlUsername)
		})
	}
}

func TestFailedUserUpdateBranchDueToHooks(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	ctx, _, repoProto, repoPath, client := setupOperationsService(t, ctx)

	request := &gitalypb.UserUpdateBranchRequest{
		Repository: repoProto,
		BranchName: []byte(updateBranchName),
		Newrev:     newrev,
		Oldrev:     oldrev,
		User:       gittest.TestUser,
	}
	// Write a hook that will fail with the environment as the error message
	// so we can check that string for our env variables.
	hookContent := []byte("#!/bin/sh\nprintenv | paste -sd ' ' - >&2\nexit 1")

	for _, hookName := range gitlabPreHooks {
		gittest.WriteCustomHook(t, repoPath, hookName, hookContent)

		response, err := client.UserUpdateBranch(ctx, request)
		require.NoError(t, err)
		require.Contains(t, response.PreReceiveError, "GL_USERNAME="+gittest.TestUser.GlUsername)
		require.Contains(t, response.PreReceiveError, "PWD="+repoPath)

		responseOk := &gitalypb.UserUpdateBranchResponse{
			PreReceiveError: response.PreReceiveError,
		}
		testhelper.ProtoEqual(t, responseOk, response)
	}
}

func TestFailedUserUpdateBranchRequest(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	ctx, cfg, repoProto, _, client := setupOperationsService(t, ctx)

	revDoesntExist := fmt.Sprintf("%x", sha1.Sum([]byte("we need a non existent sha")))

	testCases := []struct {
		desc                string
		branchName          string
		newrev              []byte
		oldrev              []byte
		gotrev              []byte
		expectNotFoundError bool
		user                *gitalypb.User
		response            *gitalypb.UserUpdateBranchResponse
		err                 error
	}{
		{
			desc:                "empty branch name",
			branchName:          "",
			newrev:              newrev,
			oldrev:              oldrev,
			expectNotFoundError: true,
			user:                gittest.TestUser,
			err:                 status.Error(codes.InvalidArgument, "empty branch name"),
		},
		{
			desc:       "empty newrev",
			branchName: updateBranchName,
			newrev:     nil,
			oldrev:     oldrev,
			user:       gittest.TestUser,
			err:        status.Error(codes.InvalidArgument, "empty newrev"),
		},
		{
			desc:       "empty oldrev",
			branchName: updateBranchName,
			newrev:     newrev,
			oldrev:     nil,
			gotrev:     oldrev,
			user:       gittest.TestUser,
			err:        status.Error(codes.InvalidArgument, "empty oldrev"),
		},
		{
			desc:       "empty user",
			branchName: updateBranchName,
			newrev:     newrev,
			oldrev:     oldrev,
			user:       nil,
			err:        status.Error(codes.InvalidArgument, "empty user"),
		},
		{
			desc:                "non-existing branch",
			branchName:          "i-dont-exist",
			newrev:              newrev,
			oldrev:              oldrev,
			expectNotFoundError: true,
			user:                gittest.TestUser,
			err:                 status.Errorf(codes.FailedPrecondition, "Could not update %v. Please refresh and try again.", "i-dont-exist"),
		},
		{
			desc:       "existing branch failed deletion attempt",
			branchName: "csv",
			newrev:     []byte(git.ZeroOID.String()),
			oldrev:     oldrev,
			gotrev:     []byte("3dd08961455abf80ef9115f4afdc1c6f968b503c"),
			user:       gittest.TestUser,
			err:        status.Errorf(codes.FailedPrecondition, "Could not update %v. Please refresh and try again.", "csv"),
		},
		{
			desc:       "non-existing newrev",
			branchName: updateBranchName,
			newrev:     []byte(revDoesntExist),
			oldrev:     oldrev,
			user:       gittest.TestUser,
			err:        status.Errorf(codes.FailedPrecondition, "Could not update %v. Please refresh and try again.", updateBranchName),
		},
		{
			desc:       "non-existing oldrev",
			branchName: updateBranchName,
			newrev:     newrev,
			oldrev:     []byte(revDoesntExist),
			gotrev:     oldrev,
			user:       gittest.TestUser,
			err:        status.Errorf(codes.FailedPrecondition, "Could not update %v. Please refresh and try again.", updateBranchName),
		},
		{
			desc:       "existing branch, but unsupported heads/* name",
			branchName: "heads/feature",
			newrev:     []byte("1a35b5a77cf6af7edf6703f88e82f6aff613666f"),
			oldrev:     []byte("0b4bc9a49b562e85de7cc9e834518ea6828729b9"),
			user:       gittest.TestUser,
			err:        status.Errorf(codes.FailedPrecondition, "Could not update %v. Please refresh and try again.", "heads/feature"),
		},
		{
			desc:       "delete existing branch, but unsupported refs/heads/* name",
			branchName: "refs/heads/crlf-diff",
			newrev:     []byte(git.ZeroOID.String()),
			oldrev:     []byte("593890758a6f845c600f38ffa05be2749211caee"),
			user:       gittest.TestUser,
			err:        status.Errorf(codes.FailedPrecondition, "Could not update %v. Please refresh and try again.", "refs/heads/crlf-diff"),
		},
		{
			desc:                "short name branch deletion",
			branchName:          "csv",
			oldrev:              []byte("3dd08961455abf80ef9115f4afdc1c6f968b503c"),
			newrev:              []byte(git.ZeroOID.String()),
			expectNotFoundError: true,
			user:                gittest.TestUser,
			err:                 nil,
			response:            &gitalypb.UserUpdateBranchResponse{},
		},
	}

	repo := localrepo.NewTestRepo(t, cfg, repoProto)
	for _, testCase := range testCases {
		t.Run(testCase.desc, func(t *testing.T) {
			request := &gitalypb.UserUpdateBranchRequest{
				Repository: repoProto,
				BranchName: []byte(testCase.branchName),
				Newrev:     testCase.newrev,
				Oldrev:     testCase.oldrev,
				User:       testCase.user,
			}

			response, err := client.UserUpdateBranch(ctx, request)
			testhelper.ProtoEqual(t, testCase.response, response)
			testhelper.RequireGrpcError(t, testCase.err, err)

			branchCommit, err := repo.ReadCommit(ctx, git.Revision(testCase.branchName))
			if testCase.expectNotFoundError {
				require.Equal(t, localrepo.ErrObjectNotFound, err, "expected 'not found' error got %v", err)
				return
			}
			require.NoError(t, err)

			if len(testCase.gotrev) == 0 {
				// The common case is the update didn't succeed
				testCase.gotrev = testCase.oldrev
			}
			require.Equal(t, string(testCase.gotrev), branchCommit.Id)
		})
	}
}