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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
//go:build !gitaly_test_sha256
package operations
import (
"fmt"
"io"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/transaction"
"gitlab.com/gitlab-org/gitaly/v16/internal/grpc/metadata"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testserver"
"gitlab.com/gitlab-org/gitaly/v16/internal/transaction/txinfo"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
var rebaseBranchName = "many_files"
func TestUserRebaseConfirmable_successful(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)
pushOptions := []string{"ci.skip", "test=value"}
cfg.Gitlab.URL = setupAndStartGitlabServer(t, gittest.GlID, "project-1", cfg, pushOptions...)
repo := localrepo.NewTestRepo(t, cfg, repoProto)
repoCopyProto, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
Seed: gittest.SeedGitLabTest,
})
branchOID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
preReceiveHookOutputPath := gittest.WriteEnvToCustomHook(t, repoPath, "pre-receive")
postReceiveHookOutputPath := gittest.WriteEnvToCustomHook(t, repoPath, "post-receive")
headerRequest := buildHeaderRequest(repoProto, gittest.TestUser, "1", rebaseBranchName, branchOID, repoCopyProto, "master")
headerRequest.GetHeader().GitPushOptions = pushOptions
require.NoError(t, rebaseStream.Send(headerRequest), "send header")
firstResponse, err := rebaseStream.Recv()
require.NoError(t, err, "receive first response")
_, err = repo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should not exist in the normal repo given that it is quarantined")
applyRequest := buildApplyRequest(true)
require.NoError(t, rebaseStream.Send(applyRequest), "apply rebase")
secondResponse, err := rebaseStream.Recv()
require.NoError(t, err, "receive second response")
_, err = rebaseStream.Recv()
require.Equal(t, io.EOF, err)
_, err = repo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.NoError(t, err)
newBranchCommit := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
require.NotEqual(t, newBranchCommit, branchOID)
require.Equal(t, newBranchCommit.String(), firstResponse.GetRebaseSha())
require.True(t, secondResponse.GetRebaseApplied(), "the second rebase is applied")
for _, outputPath := range []string{preReceiveHookOutputPath, postReceiveHookOutputPath} {
output := string(testhelper.MustReadFile(t, outputPath))
require.Contains(t, output, "GIT_PUSH_OPTION_COUNT=2")
require.Contains(t, output, "GIT_PUSH_OPTION_0=ci.skip")
require.Contains(t, output, "GIT_PUSH_OPTION_1=test=value")
}
}
func TestUserRebaseConfirmable_skipEmptyCommits(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)
// This is the base commit from which both "theirs" and "ours" branch from".
baseCommit := gittest.WriteCommit(t, cfg, repoPath,
gittest.WithTreeEntries(
gittest.TreeEntry{Mode: "100644", Path: "README", Content: "a\nb\nc\nd\ne\nf\n"},
),
)
// "theirs" changes the first line of the file to contain a "1".
theirs := gittest.WriteCommit(t, cfg, repoPath,
gittest.WithParents(baseCommit),
gittest.WithTreeEntries(
gittest.TreeEntry{Mode: "100644", Path: "README", Content: "1\nb\nc\nd\ne\nf\n"},
),
gittest.WithMessage("theirs"),
gittest.WithBranch("theirs"),
)
// We create two commits on "ours": the first one does the same changes as "theirs", but
// with a different commit ID. It is expected to become empty. And the second commit is an
// independent change which modifies the last line.
oursBecomingEmpty := gittest.WriteCommit(t, cfg, repoPath,
gittest.WithParents(baseCommit),
gittest.WithTreeEntries(
gittest.TreeEntry{Mode: "100644", Path: "README", Content: "1\nb\nc\nd\ne\nf\n"},
),
gittest.WithMessage("ours but same change as theirs"),
)
ours := gittest.WriteCommit(t, cfg, repoPath,
gittest.WithParents(oursBecomingEmpty),
gittest.WithTreeEntries(
gittest.TreeEntry{Mode: "100644", Path: "README", Content: "1\nb\nc\nd\ne\n6\n"},
),
gittest.WithMessage("ours with additional changes"),
gittest.WithBranch("ours"),
)
stream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(&gitalypb.UserRebaseConfirmableRequest{
UserRebaseConfirmableRequestPayload: &gitalypb.UserRebaseConfirmableRequest_Header_{
Header: &gitalypb.UserRebaseConfirmableRequest_Header{
Repository: repoProto,
User: gittest.TestUser,
RebaseId: "something",
// Note: the way UserRebaseConfirmable handles BranchSha and
// RemoteBranch is really weird. What we're doing is to rebase
// BranchSha on top of RemoteBranch, even though documentation of
// the protobuf says that we're doing it the other way round. I
// don't dare changing this now though, so I simply abide and write
// the test with those weird semantics.
Branch: []byte("ours"),
BranchSha: ours.String(),
RemoteRepository: repoProto,
RemoteBranch: []byte("theirs"),
Timestamp: ×tamppb.Timestamp{Seconds: 123456},
},
},
}))
response, err := stream.Recv()
require.NoError(t, err)
require.NoError(t, stream.Send(buildApplyRequest(true)))
rebaseOID := git.ObjectID(response.GetRebaseSha())
response, err = stream.Recv()
require.NoError(t, err)
require.True(t, response.GetRebaseApplied())
rebaseCommit, err := localrepo.NewTestRepo(t, cfg, repoProto).ReadCommit(ctx, rebaseOID.Revision())
require.NoError(t, err)
testhelper.ProtoEqual(t, &gitalypb.GitCommit{
Subject: []byte("ours with additional changes"),
Body: []byte("ours with additional changes"),
BodySize: 28,
Id: "ef7f98be1f753f1a9fa895d999a855611d691629",
ParentIds: []string{theirs.String()},
TreeId: "b68aeb18813d7f2e180f2cc0bccc128511438b29",
Author: gittest.DefaultCommitAuthor,
Committer: &gitalypb.CommitAuthor{
Name: gittest.TestUser.Name,
Email: gittest.TestUser.Email,
Date: ×tamppb.Timestamp{Seconds: 123456},
Timezone: []byte("+0000"),
},
}, rebaseCommit)
}
func TestUserRebaseConfirmable_transaction(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
txManager := transaction.NewTrackingManager()
ctx, cfg, repoProto, repoPath, client := setupOperationsService(
t, ctx,
// Praefect would intercept our call and inject its own transaction.
testserver.WithDisablePraefect(),
testserver.WithTransactionManager(txManager),
)
cfg.Gitlab.URL = setupAndStartGitlabServer(t, gittest.GlID, "project-1", cfg)
repo := localrepo.NewTestRepo(t, cfg, repoProto)
for _, tc := range []struct {
desc string
withTransaction bool
primary bool
expectedVotes int
expectPreReceiveHook bool
}{
{
desc: "non-transactional does not vote but executes hook",
expectedVotes: 0,
expectPreReceiveHook: true,
},
{
desc: "primary votes and executes hook",
withTransaction: true,
primary: true,
expectedVotes: 2,
expectPreReceiveHook: true,
},
{
desc: "secondary votes but does not execute hook",
withTransaction: true,
primary: false,
expectedVotes: 2,
expectPreReceiveHook: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
preReceiveHookOutputPath := gittest.WriteEnvToCustomHook(t, repoPath, "pre-receive")
txManager.Reset()
ctx := ctx
if tc.withTransaction {
ctx = metadata.OutgoingToIncoming(ctx)
var err error
ctx, err = txinfo.InjectTransaction(ctx, 1, "node", tc.primary)
require.NoError(t, err)
ctx = metadata.IncomingToOutgoing(ctx)
}
branchCommitID, err := repo.ResolveRevision(ctx, git.Revision(rebaseBranchName))
require.NoError(t, err)
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
headerRequest := buildHeaderRequest(repoProto, gittest.TestUser, "1", rebaseBranchName, branchCommitID, repoProto, "master")
require.NoError(t, rebaseStream.Send(headerRequest))
_, err = rebaseStream.Recv()
require.NoError(t, err)
require.NoError(t, rebaseStream.Send(buildApplyRequest(true)), "apply rebase")
secondResponse, err := rebaseStream.Recv()
require.NoError(t, err)
require.True(t, secondResponse.GetRebaseApplied(), "the second rebase is applied")
response, err := rebaseStream.Recv()
require.Nil(t, response)
require.Equal(t, io.EOF, err)
require.Equal(t, tc.expectedVotes, len(txManager.Votes()))
if tc.expectPreReceiveHook {
require.FileExists(t, preReceiveHookOutputPath)
} else {
require.NoFileExists(t, preReceiveHookOutputPath)
}
})
}
}
func TestUserRebaseConfirmable_stableCommitIDs(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)
cfg.Gitlab.URL = setupAndStartGitlabServer(t, gittest.GlID, "project-1", cfg)
repo := localrepo.NewTestRepo(t, cfg, repoProto)
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
committerDate := ×tamppb.Timestamp{Seconds: 100000000}
parentSha := gittest.ResolveRevision(t, cfg, repoPath, "master")
require.NoError(t, rebaseStream.Send(&gitalypb.UserRebaseConfirmableRequest{
UserRebaseConfirmableRequestPayload: &gitalypb.UserRebaseConfirmableRequest_Header_{
Header: &gitalypb.UserRebaseConfirmableRequest_Header{
Repository: repoProto,
User: gittest.TestUser,
RebaseId: "1",
Branch: []byte(rebaseBranchName),
BranchSha: gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName).String(),
RemoteRepository: repoProto,
RemoteBranch: []byte("master"),
Timestamp: committerDate,
},
},
}), "send header")
response, err := rebaseStream.Recv()
require.NoError(t, err, "receive first response")
require.Equal(t, "c52b98024db0d3af0ccb20ed2a3a93a21cfbba87", response.GetRebaseSha())
applyRequest := buildApplyRequest(true)
require.NoError(t, rebaseStream.Send(applyRequest), "apply rebase")
response, err = rebaseStream.Recv()
require.NoError(t, err, "receive second response")
require.True(t, response.GetRebaseApplied())
_, err = rebaseStream.Recv()
require.Equal(t, io.EOF, err)
commit, err := repo.ReadCommit(ctx, git.Revision(rebaseBranchName))
require.NoError(t, err, "look up git commit")
testhelper.ProtoEqual(t, &gitalypb.GitCommit{
Subject: []byte("Add a directory with many files to allow testing of default 1,000 entry limit"),
Body: []byte("Add a directory with many files to allow testing of default 1,000 entry limit\n\nFor performance reasons, GitLab will add a file viewer limit and only show\nthe first 1,000 entries in a directory. Having this directory with many\nempty files in the test project will make the test easy.\n"),
BodySize: 283,
Id: "c52b98024db0d3af0ccb20ed2a3a93a21cfbba87",
ParentIds: []string{parentSha.String()},
TreeId: "d0305132f880aa0ab4102e56a09cf1343ba34893",
Author: &gitalypb.CommitAuthor{
Name: []byte("Drew Blessing"),
Email: []byte("drew@gitlab.com"),
// Nanoseconds get ignored because commit timestamps aren't that granular.
Date: ×tamppb.Timestamp{Seconds: 1510610637},
Timezone: []byte("-0600"),
},
Committer: &gitalypb.CommitAuthor{
Name: gittest.TestUser.Name,
Email: gittest.TestUser.Email,
// Nanoseconds get ignored because commit timestamps aren't that granular.
Date: committerDate,
Timezone: []byte("+0000"),
},
}, commit)
}
func TestUserRebaseConfirmable_inputValidation(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, repo, repoPath, client := setupOperationsService(t, ctx)
repoCopy, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
Seed: gittest.SeedGitLabTest,
})
branchCommitID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
testCases := []struct {
desc string
req *gitalypb.UserRebaseConfirmableRequest
}{
{
desc: "empty Repository",
req: buildHeaderRequest(nil, gittest.TestUser, "1", rebaseBranchName, branchCommitID, repoCopy, "master"),
},
{
desc: "empty User",
req: buildHeaderRequest(repo, nil, "1", rebaseBranchName, branchCommitID, repoCopy, "master"),
},
{
desc: "empty Branch",
req: buildHeaderRequest(repo, gittest.TestUser, "1", "", branchCommitID, repoCopy, "master"),
},
{
desc: "empty BranchSha",
req: buildHeaderRequest(repo, gittest.TestUser, "1", rebaseBranchName, "", repoCopy, "master"),
},
{
desc: "empty RemoteRepository",
req: buildHeaderRequest(repo, gittest.TestUser, "1", rebaseBranchName, branchCommitID, nil, "master"),
},
{
desc: "empty RemoteBranch",
req: buildHeaderRequest(repo, gittest.TestUser, "1", rebaseBranchName, branchCommitID, repoCopy, ""),
},
{
desc: "invalid branch name",
req: buildHeaderRequest(repo, gittest.TestUser, "1", rebaseBranchName, branchCommitID, repoCopy, "+dev:master"),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
require.NoError(t, rebaseStream.Send(tc.req), "send request header")
firstResponse, err := rebaseStream.Recv()
testhelper.RequireGrpcCode(t, err, codes.InvalidArgument)
require.Contains(t, err.Error(), tc.desc)
require.Empty(t, firstResponse.GetRebaseSha(), "rebase sha on first response")
})
}
}
func TestUserRebaseConfirmable_abortViaClose(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, _, _, client := setupOperationsService(t, ctx)
testCases := []struct {
desc string
req *gitalypb.UserRebaseConfirmableRequest
closeSend bool
expectedErr error
}{
{
desc: "empty request, don't close",
req: &gitalypb.UserRebaseConfirmableRequest{
UserRebaseConfirmableRequestPayload: &gitalypb.UserRebaseConfirmableRequest_Header_{
Header: &gitalypb.UserRebaseConfirmableRequest_Header{
Repository: &gitalypb.Repository{},
},
},
},
expectedErr: structerr.NewFailedPrecondition("rebase aborted by client"),
},
{
desc: "empty request and close",
req: &gitalypb.UserRebaseConfirmableRequest{
UserRebaseConfirmableRequestPayload: &gitalypb.UserRebaseConfirmableRequest_Header_{
Header: &gitalypb.UserRebaseConfirmableRequest_Header{
Repository: &gitalypb.Repository{},
},
},
},
closeSend: true,
expectedErr: structerr.NewFailedPrecondition("rebase aborted by client"),
},
{
desc: "no request just close",
closeSend: true,
expectedErr: structerr.NewInternal("recv: EOF"),
},
}
for i, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
createRepoOpts := gittest.CreateRepositoryConfig{Seed: gittest.SeedGitLabTest}
testRepo, testRepoPath := gittest.CreateRepository(t, ctx, cfg, createRepoOpts)
testRepoCopy, _ := gittest.CreateRepository(t, ctx, cfg, createRepoOpts)
branchCommitID := gittest.ResolveRevision(t, cfg, testRepoPath, rebaseBranchName)
headerRequest := buildHeaderRequest(testRepo, gittest.TestUser, fmt.Sprintf("%v", i), rebaseBranchName, branchCommitID, testRepoCopy, "master")
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
require.NoError(t, rebaseStream.Send(headerRequest), "send first request")
firstResponse, err := rebaseStream.Recv()
require.NoError(t, err, "receive first response")
require.NotEmpty(t, firstResponse.GetRebaseSha(), "rebase sha on first response")
if tc.req != nil {
require.NoError(t, rebaseStream.Send(tc.req), "send second request")
}
if tc.closeSend {
require.NoError(t, rebaseStream.CloseSend(), "close request stream from client")
}
secondResponse, err := rebaseStream.Recv()
testhelper.RequireGrpcError(t, tc.expectedErr, err)
require.Nil(t, secondResponse)
newBranchCommitID := gittest.ResolveRevision(t, cfg, testRepoPath, rebaseBranchName)
require.Equal(t, newBranchCommitID, branchCommitID, "branch should not change when the rebase is aborted")
})
}
}
func TestUserRebaseConfirmable_abortViaApply(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)
repo := localrepo.NewTestRepo(t, cfg, repoProto)
testRepoCopy, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
Seed: gittest.SeedGitLabTest,
})
branchCommitID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
headerRequest := buildHeaderRequest(repoProto, gittest.TestUser, "1", rebaseBranchName, branchCommitID, testRepoCopy, "master")
require.NoError(t, rebaseStream.Send(headerRequest), "send header")
firstResponse, err := rebaseStream.Recv()
require.NoError(t, err, "receive first response")
_, err = repo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should not exist in the normal repo given that it is quarantined")
applyRequest := buildApplyRequest(false)
require.NoError(t, rebaseStream.Send(applyRequest), "apply rebase")
secondResponse, err := rebaseStream.Recv()
require.Error(t, err, "second response should have error")
testhelper.RequireGrpcCode(t, err, codes.FailedPrecondition)
require.False(t, secondResponse.GetRebaseApplied(), "the second rebase is not applied")
_, err = repo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should have been discarded")
newBranchCommitID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
require.Equal(t, branchCommitID, newBranchCommitID, "branch should not change when the rebase is not applied")
require.NotEqual(t, newBranchCommitID, firstResponse.GetRebaseSha(), "branch should not be the sha returned when the rebase is not applied")
}
func TestUserRebaseConfirmable_preReceiveError(t *testing.T) {
t.Parallel()
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, testhelper.Context(t))
repo := localrepo.NewTestRepo(t, cfg, repoProto)
repoCopyProto, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
Seed: gittest.SeedGitLabTest,
})
branchCommitID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
hookContent := []byte("#!/bin/sh\necho 'failure'\nexit 1")
for i, hookName := range GitlabPreHooks {
t.Run(hookName, func(t *testing.T) {
gittest.WriteCustomHook(t, repoPath, hookName, hookContent)
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
headerRequest := buildHeaderRequest(repoProto, gittest.TestUser, fmt.Sprintf("%v", i), rebaseBranchName, branchCommitID, repoCopyProto, "master")
require.NoError(t, rebaseStream.Send(headerRequest), "send header")
firstResponse, err := rebaseStream.Recv()
require.NoError(t, err, "receive first response")
_, err = repo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should not exist in the normal repo given that it is quarantined")
applyRequest := buildApplyRequest(true)
require.NoError(t, rebaseStream.Send(applyRequest), "apply rebase")
secondResponse, err := rebaseStream.Recv()
require.Nil(t, secondResponse)
testhelper.RequireGrpcError(t, structerr.NewPermissionDenied(`access check: "running %s hooks: failure\n"`, hookName).WithDetail(
&gitalypb.UserRebaseConfirmableError{
Error: &gitalypb.UserRebaseConfirmableError_AccessCheck{
AccessCheck: &gitalypb.AccessCheckError{
ErrorMessage: "failure\n",
},
},
},
), err)
_, err = repo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
if hookName == "pre-receive" {
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should have been discarded")
} else {
require.NoError(t, err)
}
newBranchCommitID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
require.Equal(t, branchCommitID, newBranchCommitID, "branch should not change when the rebase fails due to PreReceiveError")
require.NotEqual(t, newBranchCommitID, firstResponse.GetRebaseSha(), "branch should not be the sha returned when the rebase fails due to PreReceiveError")
})
}
}
func TestUserRebaseConfirmable_gitError(t *testing.T) {
t.Parallel()
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, testhelper.Context(t))
repoCopyProto, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
Seed: gittest.SeedGitLabTest,
})
targetBranch := "rebase-encoding-failure-trigger"
targetBranchCommitID := gittest.ResolveRevision(t, cfg, repoPath, targetBranch)
sourceBranchCommitID := gittest.ResolveRevision(t, cfg, repoPath, "master")
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
headerRequest := buildHeaderRequest(repoProto, gittest.TestUser, "1", targetBranch, targetBranchCommitID, repoCopyProto, "master")
require.NoError(t, rebaseStream.Send(headerRequest), "send header")
response, err := rebaseStream.Recv()
require.Nil(t, response)
testhelper.RequireGrpcError(t, structerr.NewFailedPrecondition(`rebasing commits: rebase: commit "eb8f5fb9523b868cef583e09d4bf70b99d2dd404": there are conflicting files`).WithDetail(
&gitalypb.UserRebaseConfirmableError{
Error: &gitalypb.UserRebaseConfirmableError_RebaseConflict{
RebaseConflict: &gitalypb.MergeConflictError{
ConflictingFiles: [][]byte{
[]byte("README.md"),
},
ConflictingCommitIds: []string{
sourceBranchCommitID.String(),
targetBranchCommitID.String(),
},
},
},
},
), err)
newBranchCommitID := gittest.ResolveRevision(t, cfg, repoPath, targetBranch)
require.Equal(t, targetBranchCommitID, newBranchCommitID, "branch should not change when the rebase fails due to GitError")
}
func TestUserRebaseConfirmable_deletedFileInLocalRepo(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, client := setupOperationsServiceWithoutRepo(t, ctx)
localRepoProto, localRepoPath := gittest.CreateRepository(t, ctx, cfg)
localRepo := localrepo.NewTestRepo(t, cfg, localRepoProto)
remoteRepoProto, remoteRepoPath := gittest.CreateRepository(t, ctx, cfg)
// Write the root commit into both repositories as common history.
var rootCommitID git.ObjectID
for _, path := range []string{localRepoPath, remoteRepoPath} {
rootCommitID = gittest.WriteCommit(t, cfg, path,
gittest.WithTreeEntries(
gittest.TreeEntry{Path: "change-me", Mode: "100644", Content: "unchanged contents"},
gittest.TreeEntry{Path: "delete-me", Mode: "100644", Content: "useless stuff"},
),
)
}
// Write a commit into the local repository that deletes a single file.
localCommitID := gittest.WriteCommit(t, cfg, localRepoPath,
gittest.WithParents(rootCommitID),
gittest.WithTreeEntries(
gittest.TreeEntry{Path: "change-me", Mode: "100644", Content: "unchanged contents"},
),
gittest.WithBranch("local"),
)
// And then finally write a commit into the remote repository that changes a different file.
gittest.WriteCommit(t, cfg, remoteRepoPath,
gittest.WithParents(rootCommitID),
gittest.WithTreeEntries(
gittest.TreeEntry{Path: "change-me", Mode: "100644", Content: "modified contents"},
gittest.TreeEntry{Path: "delete-me", Mode: "100644", Content: "useless stuff"},
),
gittest.WithBranch("remote"),
)
// Send the first request to tell the server to perform the rebase.
stream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(buildHeaderRequest(
localRepoProto, gittest.TestUser, "1", "local", localCommitID, remoteRepoProto, "remote",
)))
firstResponse, err := stream.Recv()
require.NoError(t, err)
_, err = localRepo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should not exist in the normal repo given that it is quarantined")
// Send the second request to tell the server to apply the rebase.
require.NoError(t, stream.Send(buildApplyRequest(true)))
secondResponse, err := stream.Recv()
require.NoError(t, err)
require.True(t, secondResponse.GetRebaseApplied())
_, err = stream.Recv()
require.Equal(t, io.EOF, err)
newBranchCommitID := gittest.ResolveRevision(t, cfg, localRepoPath, "local")
_, err = localRepo.ReadCommit(ctx, git.Revision(firstResponse.GetRebaseSha()))
require.NoError(t, err)
require.NotEqual(t, newBranchCommitID, localCommitID)
require.Equal(t, newBranchCommitID.String(), firstResponse.GetRebaseSha())
}
func TestUserRebaseConfirmable_deletedFileInRemoteRepo(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, client := setupOperationsServiceWithoutRepo(t, ctx)
localRepoProto, localRepoPath := gittest.CreateRepository(t, ctx, cfg)
localRepo := localrepo.NewTestRepo(t, cfg, localRepoProto)
remoteRepoProto, remoteRepoPath := gittest.CreateRepository(t, ctx, cfg)
// Write the root commit into both repositories as common history.
var rootCommitID git.ObjectID
for _, path := range []string{localRepoPath, remoteRepoPath} {
rootCommitID = gittest.WriteCommit(t, cfg, path,
gittest.WithTreeEntries(
gittest.TreeEntry{Path: "unchanged", Mode: "100644", Content: "unchanged contents"},
gittest.TreeEntry{Path: "delete-me", Mode: "100644", Content: "useless stuff"},
),
gittest.WithBranch("local"),
)
}
// Write a commit into the remote repository that deletes a file.
remoteCommitID := gittest.WriteCommit(t, cfg, remoteRepoPath,
gittest.WithParents(rootCommitID),
gittest.WithTreeEntries(
gittest.TreeEntry{Path: "unchanged", Mode: "100644", Content: "unchanged contents"},
),
gittest.WithBranch("remote"),
)
_, err := localRepo.ReadCommit(ctx, remoteCommitID.Revision())
require.Equal(t, localrepo.ErrObjectNotFound, err, "remote commit should not yet exist in local repository")
// Send the first request to tell the server to perform the rebase.
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
require.NoError(t, rebaseStream.Send(buildHeaderRequest(
localRepoProto, gittest.TestUser, "1", "local", rootCommitID, remoteRepoProto, "remote",
)))
firstResponse, err := rebaseStream.Recv()
require.NoError(t, err)
_, err = localRepo.ReadCommit(ctx, remoteCommitID.Revision())
require.Equal(t, localrepo.ErrObjectNotFound, err, "commit should not exist in the normal repo given that it is quarantined")
// Send the second request to tell the server to apply the rebase.
require.NoError(t, rebaseStream.Send(buildApplyRequest(true)))
secondResponse, err := rebaseStream.Recv()
require.NoError(t, err)
require.True(t, secondResponse.GetRebaseApplied(), "the second rebase is applied")
_, err = rebaseStream.Recv()
require.Equal(t, io.EOF, err)
_, err = localRepo.ReadCommit(ctx, remoteCommitID.Revision())
require.NoError(t, err)
rebasedBranchCommitID := gittest.ResolveRevision(t, cfg, localRepoPath, "local")
require.NotEqual(t, rebasedBranchCommitID, rootCommitID)
require.Equal(t, rebasedBranchCommitID.String(), firstResponse.GetRebaseSha())
}
func TestUserRebaseConfirmable_failedWithCode(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
ctx, cfg, repoProto, repoPath, client := setupOperationsService(t, ctx)
branchCommitID := gittest.ResolveRevision(t, cfg, repoPath, rebaseBranchName)
testCases := []struct {
desc string
buildHeaderRequest func() *gitalypb.UserRebaseConfirmableRequest
expectedErr error
}{
{
desc: "no repository provided",
buildHeaderRequest: func() *gitalypb.UserRebaseConfirmableRequest {
return buildHeaderRequest(nil, gittest.TestUser, "1", rebaseBranchName, branchCommitID, nil, "master")
},
expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect(
"empty Repository",
"repo scoped: empty Repository",
)),
},
{
desc: "non-existing storage",
buildHeaderRequest: func() *gitalypb.UserRebaseConfirmableRequest {
repo := proto.Clone(repoProto).(*gitalypb.Repository)
repo.StorageName = "@this-storage-does-not-exist"
return buildHeaderRequest(repo, gittest.TestUser, "1", rebaseBranchName, branchCommitID, repo, "master")
},
expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect(
`creating repo quarantine: creating object quarantine: getting repo path: GetStorageByName: no such storage: "@this-storage-does-not-exist"`,
"repo scoped: invalid Repository",
)),
},
{
desc: "missing repository path",
buildHeaderRequest: func() *gitalypb.UserRebaseConfirmableRequest {
repo := proto.Clone(repoProto).(*gitalypb.Repository)
repo.RelativePath = ""
return buildHeaderRequest(repo, gittest.TestUser, "1", rebaseBranchName, branchCommitID, repo, "master")
},
expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect(
"empty RelativePath",
"repo scoped: invalid Repository",
)),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
rebaseStream, err := client.UserRebaseConfirmable(ctx)
require.NoError(t, err)
headerRequest := tc.buildHeaderRequest()
require.NoError(t, rebaseStream.Send(headerRequest), "send header")
_, err = rebaseStream.Recv()
testhelper.RequireGrpcError(t, tc.expectedErr, err)
})
}
}
func buildHeaderRequest(repo *gitalypb.Repository, user *gitalypb.User, rebaseID string, branchName string, commitID git.ObjectID, remoteRepo *gitalypb.Repository, remoteBranch string) *gitalypb.UserRebaseConfirmableRequest {
return &gitalypb.UserRebaseConfirmableRequest{
UserRebaseConfirmableRequestPayload: &gitalypb.UserRebaseConfirmableRequest_Header_{
Header: &gitalypb.UserRebaseConfirmableRequest_Header{
Repository: repo,
User: user,
RebaseId: rebaseID,
Branch: []byte(branchName),
BranchSha: commitID.String(),
RemoteRepository: remoteRepo,
RemoteBranch: []byte(remoteBranch),
},
},
}
}
func buildApplyRequest(apply bool) *gitalypb.UserRebaseConfirmableRequest {
return &gitalypb.UserRebaseConfirmableRequest{
UserRebaseConfirmableRequestPayload: &gitalypb.UserRebaseConfirmableRequest_Apply{
Apply: apply,
},
}
}
|