-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathcheckout_git_mirrors_integration_test.go
More file actions
835 lines (675 loc) · 24.8 KB
/
Copy pathcheckout_git_mirrors_integration_test.go
File metadata and controls
835 lines (675 loc) · 24.8 KB
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
package integration
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
"github.com/buildkite/agent/v3/internal/experiments"
"github.com/buildkite/agent/v3/internal/job"
"github.com/buildkite/bintest/v3"
)
func TestCheckingOutGitHubPullRequests_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
env := []string{
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS=--bare",
"BUILDKITE_PULL_REQUEST=123",
"BUILDKITE_PIPELINE_PROVIDER=github",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "--bare", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-ffxdq"},
{"fetch", "--", "origin", "refs/pull/123/head"},
{"rev-parse", "FETCH_HEAD"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-ffxdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
}
func TestWithResolvingCommitExperiment_WithGitMirrors(t *testing.T) {
t.Parallel()
ctx, _ := experiments.Enable(mainCtx, experiments.ResolveCommitAfterCheckout)
tester, err := NewExecutorTester(ctx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
env := []string{
"BUILDKITE_GIT_CLONE_FLAGS=-v",
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS=--bare",
"BUILDKITE_GIT_CLEAN_FLAGS=-fdq",
"BUILDKITE_GIT_FETCH_FLAGS=-v",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "--bare", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-fdq"},
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"rev-parse", "HEAD"},
})
tester.RunAndCheck(t, env...)
}
func TestCheckingOutLocalGitProject_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
env := []string{
"BUILDKITE_GIT_CLONE_FLAGS=-v",
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS=--config pack.threads=35",
"BUILDKITE_GIT_CLEAN_FLAGS=-fdq",
"BUILDKITE_GIT_FETCH_FLAGS=-v",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "--config", "pack.threads=35", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-fdq"},
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
}
func TestCheckingOutLocalGitProjectWithSubmodules_WithGitMirrors(t *testing.T) {
t.Parallel()
// Git for windows seems to struggle with local submodules in the temp dir
if runtime.GOOS == "windows" {
t.Skip()
}
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
submoduleRepo, err := createTestGitRespository()
if err != nil {
t.Fatalf("createTestGitRepository() error = %v", err)
}
defer submoduleRepo.Close()
out, err := tester.Repo.Execute("-c", "protocol.file.allow=always", "submodule", "add", submoduleRepo.Path)
if err != nil {
t.Fatalf("tester.Repo.Execute(submodule, add, %q) error = %v\nout = %s", submoduleRepo.Path, err, out)
}
out, err = tester.Repo.Execute("commit", "-am", "Add example submodule")
if err != nil {
t.Fatalf(`tester.Repo.Execute(commit, -am, "Add example submodule") error = %v\nout = %s`, err, out)
}
env := []string{
"BUILDKITE_GIT_CLONE_FLAGS=-v",
"BUILDKITE_GIT_CLEAN_FLAGS=-fdq",
"BUILDKITE_GIT_FETCH_FLAGS=-v",
"BUILDKITE_GIT_SUBMODULE_CLONE_CONFIG=protocol.file.allow=always",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "-v", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "--mirror", "-v", "--", submoduleRepo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-fdq"},
{"submodule", "foreach", "--recursive", "git clean -fdq"},
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"submodule", "sync", "--recursive"},
{"config", "--file", ".gitmodules", "--null", "--get-regexp", "submodule\\..+\\.url"},
{"-c", "protocol.file.allow=always", "submodule", "update", "--init", "--recursive", "--force", "--reference", submoduleRepo.Path},
{"submodule", "foreach", "--recursive", "git reset --hard"},
{"clean", "-fdq"},
{"submodule", "foreach", "--recursive", "git clean -fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
}
func TestCheckingOutLocalGitProjectWithSubmodulesDisabled_WithGitMirrors(t *testing.T) {
t.Parallel()
// Git for windows seems to struggle with local submodules in the temp dir
if runtime.GOOS == "windows" {
t.Skip()
}
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
submoduleRepo, err := createTestGitRespository()
if err != nil {
t.Fatalf("createTestGitRespository() error = %v", err)
}
defer submoduleRepo.Close()
out, err := tester.Repo.Execute("-c", "protocol.file.allow=always", "submodule", "add", submoduleRepo.Path)
if err != nil {
t.Fatalf("tester.Repo.Execute(submodule, add, %q) error = %v\nout = %s", submoduleRepo.Path, err, out)
}
out, err = tester.Repo.Execute("commit", "-am", "Add example submodule")
if err != nil {
t.Fatalf(`tester.Repo.Execute(commit, -am, "Add example submodule") error = %v\nout = %s`, err, out)
}
env := []string{
"BUILDKITE_GIT_CLONE_FLAGS=-v",
"BUILDKITE_GIT_CLEAN_FLAGS=-fdq",
"BUILDKITE_GIT_FETCH_FLAGS=-v",
"BUILDKITE_GIT_SUBMODULES=false",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "-v", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-fdq"},
{"submodule", "foreach", "--recursive", "git clean -fdq"},
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
}
func TestCheckingOutShallowCloneOfLocalGitProject_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
env := []string{
"BUILDKITE_GIT_CLONE_FLAGS=--depth=1",
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS=--bare",
"BUILDKITE_GIT_CLEAN_FLAGS=-fdq",
"BUILDKITE_GIT_FETCH_FLAGS=--depth=1",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "--bare", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "--depth=1", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-fdq"},
{"fetch", "--depth=1", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
}
func TestCheckingOutSetsCorrectGitMetadataAndSendsItToBuildkite_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t)
}
func TestCheckingOutWithSSHKeyscan_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
var gitSSHCommand string
git := tester.MustMock(t, "git")
git.IgnoreUnexpectedInvocations()
git.Expect("clone", "--mirror", "-v", "--", "git@github.com:buildkite/agent.git", bintest.MatchAny()).
AndCallFunc(func(c *bintest.Call) {
// Capture GIT_SSH_COMMAND for verification
for _, env := range c.Env {
if strings.HasPrefix(env, "GIT_SSH_COMMAND=") {
gitSSHCommand = env
break
}
}
c.Exit(0)
})
env := []string{
"BUILDKITE_REPO=git@github.com:buildkite/agent.git",
"BUILDKITE_SSH_KEYSCAN=true",
}
tester.RunAndCheck(t, env...)
// Verify GIT_SSH_COMMAND was set with accept-new
if !strings.Contains(gitSSHCommand, "StrictHostKeyChecking=accept-new") {
t.Errorf("Expected GIT_SSH_COMMAND to contain 'StrictHostKeyChecking=accept-new', got: %q", gitSSHCommand)
}
}
func TestCheckingOutWithoutSSHKeyscan_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
var gitSSHCommand string
git := tester.MustMock(t, "git")
git.IgnoreUnexpectedInvocations()
git.Expect("clone", "--mirror", "-v", "--", "https://github.com/buildkite/bash-example.git", bintest.MatchAny()).
AndCallFunc(func(c *bintest.Call) {
// Capture GIT_SSH_COMMAND for verification
for _, env := range c.Env {
if strings.HasPrefix(env, "GIT_SSH_COMMAND=") {
gitSSHCommand = env
break
}
}
c.Exit(0)
})
env := []string{
"BUILDKITE_REPO=https://github.com/buildkite/bash-example.git",
"BUILDKITE_SSH_KEYSCAN=false",
}
tester.RunAndCheck(t, env...)
// Verify GIT_SSH_COMMAND does NOT contain accept-new when SSHKeyscan is disabled
if strings.Contains(gitSSHCommand, "StrictHostKeyChecking=accept-new") {
t.Errorf("Expected GIT_SSH_COMMAND to NOT contain 'StrictHostKeyChecking=accept-new', got: %q", gitSSHCommand)
}
}
func TestCheckingOutWithSSHKeyscanAndHTTPSRepo_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
var gitSSHCommand string
git := tester.MustMock(t, "git")
git.IgnoreUnexpectedInvocations()
// Even with HTTPS repos, GIT_SSH_COMMAND is set (it just won't be used)
git.Expect("clone", "--mirror", "-v", "--", "https://github.com/buildkite/bash-example.git", bintest.MatchAny()).
AndCallFunc(func(c *bintest.Call) {
// Capture GIT_SSH_COMMAND for verification
for _, env := range c.Env {
if strings.HasPrefix(env, "GIT_SSH_COMMAND=") {
gitSSHCommand = env
break
}
}
c.Exit(0)
})
env := []string{
"BUILDKITE_REPO=https://github.com/buildkite/bash-example.git",
"BUILDKITE_SSH_KEYSCAN=true",
}
tester.RunAndCheck(t, env...)
// Verify GIT_SSH_COMMAND was set with accept-new even for HTTPS repos
if !strings.Contains(gitSSHCommand, "StrictHostKeyChecking=accept-new") {
t.Errorf("Expected GIT_SSH_COMMAND to contain 'StrictHostKeyChecking=accept-new', got: %q", gitSSHCommand)
}
}
func TestCleaningAnExistingCheckout_WithGitMirrors(t *testing.T) {
t.Skip("TODO: Figure out what this test is actually supposed to be testing")
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
// Create an existing checkout
out, err := tester.Repo.Execute("clone", "-v", "--", tester.Repo.Path, tester.CheckoutDir())
if err != nil {
t.Fatalf(`tester.Repo.Execute(clone, -v, --, %q, %q) error = %v\nout = %s`, tester.Repo.Path, tester.CheckoutDir(), err, out)
}
testpath := filepath.Join(tester.CheckoutDir(), "test.txt")
if err := os.WriteFile(testpath, []byte("llamas"), 0o700); err != nil {
t.Fatalf("os.WriteFile(test.txt, llamas, 0o700) = %v", err)
}
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.
Expect("meta-data", "exists", job.CommitMetadataKey).
AndExitWith(0)
tester.RunAndCheck(t)
// This used to check if os.IsExist(err) == true.
// Unfortunately, os.IsExist(err) is not the same as !os.IsNotExist(err)
// (https://go.dev/play/p/j8z6jsF5qJs) and the code under test isn't
// removing the file.
if _, err := os.Stat(testpath); !os.IsNotExist(err) {
t.Errorf("os.Stat(test.txt) error = nil, want no such file or directory")
}
}
func TestForcingACleanCheckout_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.
Expect("meta-data", "exists", job.CommitMetadataKey).
AndExitWith(0)
tester.RunAndCheck(t, "BUILDKITE_CLEAN_CHECKOUT=true")
if !strings.Contains(tester.Output, "Cleaning pipeline checkout") {
t.Fatal(`tester.Output does not contain "Cleaning pipeline checkout"`)
}
}
func TestCheckoutOnAnExistingRepositoryWithoutAGitFolder_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
// Create an existing checkout
out, err := tester.Repo.Execute("clone", "-v", "--", tester.Repo.Path, tester.CheckoutDir())
if err != nil {
t.Fatalf(`tester.Repo.Execute(clone, -v, --, %q, %q) error = %v\nout = %s`, tester.Repo.Path, tester.CheckoutDir(), err, out)
}
if err := os.RemoveAll(filepath.Join(tester.CheckoutDir(), ".git", "refs")); err != nil {
t.Fatalf("os.RemoveAll(.git/refs) = %v", err)
}
agent := tester.MockAgent(t)
agent.
Expect("meta-data", "exists", job.CommitMetadataKey).
AndExitWith(0)
tester.RunAndCheck(t)
}
func TestCheckoutRetriesOnCleanFailure_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
var cleanCounter int32
// Mock out all git commands, passing them through to the real thing unless it's a checkout
git := tester.MustMock(t, "git").PassthroughToLocalCommand().Before(func(i bintest.Invocation) error {
if i.Args[0] == "clean" {
c := atomic.AddInt32(&cleanCounter, 1)
// NB: clean gets run twice per checkout
if c == 1 {
return errors.New("Sunspots have caused git clean to fail")
}
}
return nil
})
git.Expect().AtLeastOnce().WithAnyArguments()
tester.RunAndCheck(t)
}
func TestCheckoutRetriesOnCloneFailure_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
var cloneCounter int32
// Mock out all git commands, passing them through to the real thing unless it's a checkout
git := tester.MustMock(t, "git").PassthroughToLocalCommand().Before(func(i bintest.Invocation) error {
if i.Args[0] == "clone" {
c := atomic.AddInt32(&cloneCounter, 1)
if c == 1 {
return errors.New("Sunspots have caused git clone to fail")
}
}
return nil
})
git.Expect().AtLeastOnce().WithAnyArguments()
tester.RunAndCheck(t)
}
func TestCheckoutDoesNotRetryOnHookFailure_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
var checkoutCounter int32
tester.ExpectGlobalHook("checkout").Once().AndCallFunc(func(c *bintest.Call) {
counter := atomic.AddInt32(&checkoutCounter, 1)
fmt.Fprintf(c.Stdout, "Checkout invocation %d\n", counter)
if counter == 1 {
fmt.Fprintf(c.Stdout, "Sunspots have caused checkout to fail\n")
c.Exit(1)
} else {
c.Exit(0)
}
})
if err := tester.Run(t); err == nil {
t.Fatalf("tester.Run(t) = %v, want non-nil error", err)
}
tester.CheckMocks(t)
}
func TestRepositorylessCheckout_WithGitMirrors(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("Not supported on windows")
}
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
script := []string{
"#!/usr/bin/env bash",
"export BUILDKITE_REPO=",
}
if err := os.WriteFile(filepath.Join(tester.HooksDir, "environment"), []byte(strings.Join(script, "\n")), 0o700); err != nil {
t.Fatalf("os.WriteFile(environment, script, 0o700) = %v", err)
}
tester.MustMock(t, "git").Expect().NotCalled()
tester.ExpectGlobalHook("pre-checkout").Once()
tester.ExpectGlobalHook("post-checkout").Once()
tester.ExpectGlobalHook("pre-command").Once()
tester.ExpectGlobalHook("post-command").Once()
tester.ExpectGlobalHook("pre-exit").Once()
tester.RunAndCheck(t)
}
func TestGitMirrorEnv(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
// assert the correct BUILDKITE_REPO_MIRROR _after_ the bootstrap has run
gitMirrorPath := ""
tester.ExpectGlobalHook("pre-command").Once().AndCallFunc(func(c *bintest.Call) {
gitMirrorPath = c.GetEnv("BUILDKITE_REPO_MIRROR")
c.Exit(0)
})
env := []string{
"BUILDKITE_GIT_CLONE_FLAGS=-v",
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS=--bare",
"BUILDKITE_GIT_CLEAN_FLAGS=-fdq",
"BUILDKITE_GIT_FETCH_FLAGS=-v",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// But assert which ones are called
git.ExpectAll([][]any{
{"clone", "--mirror", "--bare", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-fdq"},
{"fetch", "-v", "--", "origin", "main"},
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-fdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
if !strings.HasPrefix(gitMirrorPath, tester.GitMirrorsDir) {
t.Errorf("gitMirrorPath = %q, want prefix %q", gitMirrorPath, tester.GitMirrorsDir)
}
}
func TestCheckingOutWithCustomRefspec_WithGitMirrors(t *testing.T) {
t.Parallel()
tester, err := NewExecutorTester(mainCtx)
if err != nil {
t.Fatalf("NewExecutorTester() error = %v", err)
}
defer tester.Close()
if err := tester.EnableGitMirrors(); err != nil {
t.Fatalf("EnableGitMirrors() error = %v", err)
}
// Create a custom ref in the repository that's different from main
customRef := "refs/custom/test-ref"
out, err := tester.Repo.Execute("update-ref", customRef, "HEAD")
if err != nil {
t.Fatalf("tester.Repo.Execute(update-ref, %q, HEAD) error = %v\nout = %s", customRef, err, out)
}
// Create a new commit on a branch that's not in the custom ref
out, err = tester.Repo.Execute("checkout", "-b", "different-branch")
if err != nil {
t.Fatalf("tester.Repo.Execute(checkout, -b, different-branch) error = %v\nout = %s", err, out)
}
differentFile := "different.txt"
if err := os.WriteFile(filepath.Join(tester.Repo.Path, differentFile), []byte("different content"), 0o600); err != nil {
t.Fatalf("os.WriteFile(%s, different content, 0o600) = %v", differentFile, err)
}
out, err = tester.Repo.Execute("add", differentFile)
if err != nil {
t.Fatalf("tester.Repo.Execute(add, %s) error = %v\nout = %s", differentFile, err, out)
}
out, err = tester.Repo.Execute("commit", "-m", "Different commit")
if err != nil {
t.Fatalf("tester.Repo.Execute(commit, -m, Different commit) error = %v\nout = %s", err, out)
}
// Go back to main
out, err = tester.Repo.Execute("checkout", "main")
if err != nil {
t.Fatalf("tester.Repo.Execute(checkout, main) error = %v\nout = %s", err, out)
}
env := []string{
"BUILDKITE_REFSPEC=" + customRef,
"BUILDKITE_GIT_CLONE_MIRROR_FLAGS=--bare",
}
// Actually execute git commands, but with expectations
git := tester.
MustMock(t, "git").
PassthroughToLocalCommand()
// With the fix: the mirror should fetch the custom refspec instead of the branch
git.ExpectAll([][]any{
{"clone", "--mirror", "--bare", "--", tester.Repo.Path, matchSubDir(tester.GitMirrorsDir)},
{"clone", "-v", "--reference", matchSubDir(tester.GitMirrorsDir), "--", tester.Repo.Path, "."},
{"clean", "-ffxdq"},
{"fetch", "--", "origin", customRef}, // Mirror fetches custom refspec (correct!)
{"checkout", "-f", "FETCH_HEAD"},
{"clean", "-ffxdq"},
{"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg},
})
// Mock out the meta-data calls to the agent after checkout
agent := tester.MockAgent(t)
agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1)
agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern)
tester.RunAndCheck(t, env...)
}