-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathexecutor_test.go
More file actions
872 lines (779 loc) · 25.3 KB
/
executor_test.go
File metadata and controls
872 lines (779 loc) · 25.3 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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
package executor
import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/sourcegraph/go-diff/diff"
"github.com/stretchr/testify/require"
"github.com/sourcegraph/sourcegraph/lib/errors"
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
"github.com/sourcegraph/sourcegraph/lib/batches/execution"
"github.com/sourcegraph/sourcegraph/lib/batches/git"
"github.com/sourcegraph/sourcegraph/lib/batches/template"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/batches/docker"
"github.com/sourcegraph/src-cli/internal/batches/mock"
"github.com/sourcegraph/src-cli/internal/batches/repozip"
"github.com/sourcegraph/src-cli/internal/batches/workspace"
)
func TestExecutor_Integration(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Test doesn't work on Windows because dummydocker is written in bash")
}
addToPath(t, "testdata/dummydocker")
defaultBatchChangeAttributes := &template.BatchChangeAttributes{
Name: "integration-test-batch-change",
Description: "this is an integration test",
}
const rootPath = ""
type filesByPath map[string][]string
type filesByRepository map[string]filesByPath
// create a temp directory with a simple shell file
tempDir := t.TempDir()
mountScript := filepath.Join(tempDir, "sample.sh")
err := os.WriteFile(mountScript, []byte(`echo -e "foobar\n" >> README.md`), 0777)
require.NoError(t, err)
tests := []struct {
name string
archives []mock.RepoArchive
additionalFiles []mock.MockRepoAdditionalFiles
// We define the steps only once per test case so there's less duplication
steps []batcheslib.Step
tasks []*Task
executorTimeout time.Duration
wantFilesChanged filesByRepository
wantTitle string
wantBody string
wantCommitMessage string
wantAuthorName string
wantAuthorEmail string
wantErrInclude string
wantFinished int
wantFinishedWithErr int
wantCacheCount int
failFast bool
workingDirectory string
}{
{
name: "success",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
"main.go": "package main\n\nfunc main() {\n\tfmt.Println( \"Hello World\")\n}\n",
}},
{RepoName: testRepo2.Name, Commit: testRepo2.Rev(), Files: map[string]string{
"README.md": "# Sourcegraph README\n",
}},
},
steps: []batcheslib.Step{
{Run: `echo -e "foobar\n" >> README.md`},
{Run: `[[ -f "main.go" ]] && go fmt main.go || exit 0`},
},
tasks: []*Task{
{Repository: testRepo1},
{Repository: testRepo2},
},
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{"README.md", "main.go"},
},
testRepo2.ID: {
rootPath: []string{"README.md"},
},
},
wantFinished: 2,
wantCacheCount: 4,
},
{
name: "empty",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
"main.go": "package main\n\nfunc main() {\n\tfmt.Println( \"Hello World\")\n}\n",
}},
},
steps: []batcheslib.Step{
{Run: "true"},
},
tasks: []*Task{
{Repository: testRepo1},
},
// No diff should be generated.
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{},
},
},
wantFinished: 1,
wantCacheCount: 1,
},
{
name: "timeout",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{"README.md": "line 1"}},
},
steps: []batcheslib.Step{
// This needs to be a loop, because when a process goes to sleep
// it's not interruptible, meaning that while it will receive SIGKILL
// it won't exit until it had its full night of sleep.
// So.
// Instead we take short powernaps.
{Run: `while true; do echo "zZzzZ" && sleep 0.05; done`},
},
tasks: []*Task{
{Repository: testRepo1},
},
executorTimeout: 100 * time.Millisecond,
wantErrInclude: "execution in github.com/sourcegraph/src-cli failed: Timeout reached. Execution took longer than 100ms.",
wantFinishedWithErr: 1,
},
{
name: "templated steps",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
"main.go": "package main\n\nfunc main() {\n\tfmt.Println( \"Hello World\")\n}\n",
}},
},
steps: []batcheslib.Step{
{Run: `go fmt main.go`},
{Run: `touch modified-${{ join previous_step.modified_files " " }}.md`},
{Run: `touch added-${{ join previous_step.added_files " " }}`},
{
Run: `echo "hello.txt"`,
Outputs: batcheslib.Outputs{
"myOutput": batcheslib.Output{
Value: "${{ step.stdout }}",
},
},
},
{Run: `touch output-${{ outputs.myOutput }}`},
},
tasks: []*Task{
{Repository: testRepo1},
},
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{
"main.go",
"modified-main.go.md",
"added-modified-main.go.md",
"output-hello.txt",
},
},
},
wantFinished: 1,
wantCacheCount: 5,
},
{
name: "workspaces",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Path: "", Files: map[string]string{
".gitignore": "node_modules",
"message.txt": "root-dir",
"a/message.txt": "a-dir",
"a/.gitignore": "node_modules-in-a",
"a/b/message.txt": "b-dir",
}},
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Path: "a", Files: map[string]string{
"a/message.txt": "a-dir",
"a/.gitignore": "node_modules-in-a",
"a/b/message.txt": "b-dir",
}},
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Path: "a/b", Files: map[string]string{
"a/b/message.txt": "b-dir",
}},
},
additionalFiles: []mock.MockRepoAdditionalFiles{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), AdditionalFiles: map[string]string{
".gitignore": "node_modules",
"a/.gitignore": "node_modules-in-a",
}},
},
steps: []batcheslib.Step{
{
Run: "cat message.txt && echo 'Hello' > hello.txt",
Outputs: batcheslib.Outputs{
"message": batcheslib.Output{
Value: "${{ step.stdout }}",
},
},
},
{Run: `if [[ -f ".gitignore" ]]; then echo "yes" >> gitignore-exists; fi`},
{Run: `if [[ $(basename $(pwd)) == "a" && -f "../.gitignore" ]]; then echo "yes" >> gitignore-exists; fi`},
// In `a/b` we want the `.gitignore` file in the root folder and in `a` to be fetched:
{Run: `if [[ $(basename $(pwd)) == "b" && -f "../../.gitignore" ]]; then echo "yes" >> gitignore-exists; fi`},
{Run: `if [[ $(basename $(pwd)) == "b" && -f "../.gitignore" ]]; then echo "yes" >> gitignore-exists-in-a; fi`},
},
tasks: []*Task{
{Repository: testRepo1, Path: ""},
{Repository: testRepo1, Path: "a"},
{Repository: testRepo1, Path: "a/b"},
},
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{"hello.txt", "gitignore-exists"},
"a": []string{"a/hello.txt", "a/gitignore-exists"},
"a/b": []string{"a/b/hello.txt", "a/b/gitignore-exists", "a/b/gitignore-exists-in-a"},
},
},
wantFinished: 3,
wantCacheCount: 15,
},
{
name: "step condition",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
}},
{RepoName: testRepo2.Name, Commit: testRepo2.Rev(), Files: map[string]string{
"README.md": "# Sourcegraph README\n",
}},
},
steps: []batcheslib.Step{
{Run: `echo -e "foobar\n" >> README.md`},
{
Run: `echo "foobar" >> hello.txt`,
If: `${{ matches repository.name "github.com/sourcegraph/sourcegra*" }}`,
},
{
Run: `echo "foobar" >> in-path.txt`,
If: `${{ matches steps.path "sub/directory/of/repo" }}`,
},
},
tasks: []*Task{
{Repository: testRepo1},
{Repository: testRepo2, Path: "sub/directory/of/repo"},
},
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{"README.md"},
},
testRepo2.ID: {
"sub/directory/of/repo": []string{"README.md", "hello.txt", "in-path.txt"},
},
},
wantFinished: 2,
wantCacheCount: 4,
},
{
name: "skips errors",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
}},
{RepoName: testRepo2.Name, Commit: testRepo2.Rev(), Files: map[string]string{
"README.md": "# Sourcegraph README\n",
}},
},
steps: []batcheslib.Step{
{Run: `echo -e "foobar\n" >> README.md`},
{
Run: `exit 1`,
If: fmt.Sprintf(`${{ eq repository.name %q }}`, testRepo2.Name),
},
},
tasks: []*Task{
{Repository: testRepo1},
{Repository: testRepo2},
},
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{"README.md"},
},
testRepo2.ID: {},
},
wantErrInclude: "execution in github.com/sourcegraph/sourcegraph failed: run: exit 1",
wantFinished: 1,
wantFinishedWithErr: 1,
wantCacheCount: 2,
},
{
name: "fail fast mode",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
}},
{RepoName: testRepo2.Name, Commit: testRepo2.Rev(), Files: map[string]string{
"README.md": "# Sourcegraph README\n",
}},
},
steps: []batcheslib.Step{
{
Run: `exit 1`,
// We must fail for the first repository, so that the other repo's work is cancelled in fail fast mode.
If: fmt.Sprintf(`${{ eq repository.name %q }}`, testRepo1.Name),
},
{
// We introduce an artificial way for the second repository, so that it can't complete before the failure of the first one.
Run: `sleep 0.1`,
If: fmt.Sprintf(`${{ eq repository.name %q }}`, testRepo2.Name),
},
{Run: `echo -e "foobar\n" >> README.md`},
},
tasks: []*Task{
{Repository: testRepo1},
{Repository: testRepo2},
},
wantErrInclude: "execution in github.com/sourcegraph/src-cli failed: run: exit 1",
// In fail fast mode, we expect that other steps are cancelled.
wantFinished: 0,
wantFinishedWithErr: 2,
failFast: true,
},
{
name: "mount path",
archives: []mock.RepoArchive{
{RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
}},
},
steps: []batcheslib.Step{
{
Run: mountScript,
Mount: []batcheslib.Mount{{Path: "sample.sh", Mountpoint: mountScript}},
},
},
tasks: []*Task{
{Repository: testRepo1},
},
wantFilesChanged: filesByRepository{
testRepo1.ID: filesByPath{
rootPath: []string{"README.md"},
},
},
wantFinished: 1,
wantCacheCount: 1,
workingDirectory: tempDir,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Make sure that the steps and tasks are setup properly
images := make(map[string]docker.Image)
for _, step := range tc.steps {
images[step.Container] = &mock.Image{RawDigest: step.Container}
}
for _, task := range tc.tasks {
task.BatchChangeAttributes = defaultBatchChangeAttributes
task.Steps = tc.steps
}
// Setup a mock test server so we also test the downloading of archives
mux := mock.NewZipArchivesMux(t, nil, tc.archives...)
middle := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
for _, additionalFiles := range tc.additionalFiles {
mock.HandleAdditionalFiles(mux, additionalFiles, middle)
}
ts := httptest.NewServer(mux)
defer ts.Close()
// Setup an api.Client that points to this test server
var clientBuffer bytes.Buffer
u, _ := url.ParseRequestURI(ts.URL)
client := api.NewClient(api.ClientOpts{EndpointURL: u, Out: &clientBuffer})
// Temp dir for log files and downloaded archives
testTempDir := t.TempDir()
ctx := context.Background()
cr, _ := workspace.NewCreator(ctx, "bind", testTempDir, testTempDir, images)
// Setup executor
parallelism := 0
if tc.failFast {
parallelism = 1
}
opts := NewExecutorOpts{
Creator: cr,
RepoArchiveRegistry: repozip.NewArchiveRegistry(client, testTempDir, false),
Logger: mock.LogNoOpManager{},
EnsureImage: imageMapEnsurer(images),
TempDir: testTempDir,
Parallelism: runtime.GOMAXPROCS(parallelism),
Timeout: tc.executorTimeout,
FailFast: tc.failFast,
WorkingDirectory: tc.workingDirectory,
}
if opts.Timeout == 0 {
opts.Timeout = 30 * time.Second
}
dummyUI := newDummyTaskExecutionUI()
executor := NewExecutor(opts)
// Run executor
executor.Start(ctx, tc.tasks, dummyUI)
results, err := executor.Wait()
if tc.wantErrInclude == "" {
if err != nil {
t.Fatalf("execution failed: %s", err)
}
} else {
if err == nil {
t.Fatalf("expected error to include %q, but got no error", tc.wantErrInclude)
} else if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.wantErrInclude)) {
t.Errorf("wrong error. have=%q want included=%q", err, tc.wantErrInclude)
}
}
wantResults := 0
resultsFound := map[string]map[string]bool{}
for repo, byPath := range tc.wantFilesChanged {
wantResults += len(byPath)
resultsFound[repo] = map[string]bool{}
for path := range byPath {
resultsFound[repo][path] = false
}
}
haveResults := 0
for _, res := range results {
if res.err == nil {
haveResults++
}
}
if have, want := haveResults, wantResults; have != want {
t.Fatalf("wrong number of results. want=%d, have=%d", want, have)
}
for _, taskResult := range results {
if taskResult.err != nil {
continue
}
repoID := taskResult.task.Repository.ID
path := taskResult.task.Path
wantFiles, ok := tc.wantFilesChanged[repoID]
if !ok {
t.Fatalf("unexpected file changes in repo %s", repoID)
}
resultsFound[repoID][path] = true
wantFilesInPath, ok := wantFiles[path]
if !ok {
t.Fatalf("spec for repo %q and path %q but no files expected in that branch", repoID, path)
}
lastStepResult := taskResult.stepResults[len(taskResult.stepResults)-1]
fileDiffs, err := diff.ParseMultiFileDiff(lastStepResult.Diff)
if err != nil {
t.Fatalf("failed to parse diff: %s", err)
}
if have, want := len(fileDiffs), len(wantFilesInPath); have != want {
t.Fatalf("repo %s: wrong number of fileDiffs. want=%d, have=%d", repoID, want, have)
}
diffsByName := map[string]*diff.FileDiff{}
for _, fd := range fileDiffs {
if fd.NewName == "/dev/null" {
diffsByName[fd.OrigName] = fd
} else {
diffsByName[fd.NewName] = fd
}
}
for _, file := range wantFilesInPath {
if _, ok := diffsByName[file]; !ok {
t.Errorf("%s was not changed (diffsByName=%#v)", file, diffsByName)
}
}
}
for repo, paths := range resultsFound {
for path, found := range paths {
for !found {
t.Fatalf("expected spec to be created in path %s of repo %s, but was not", path, repo)
}
}
}
// Make sure that all the Tasks have been updated correctly
if have, want := len(dummyUI.finished), tc.wantFinished; have != want {
t.Fatalf("wrong number of UI finished tasks. want=%d, have=%d", want, have)
}
if have, want := len(dummyUI.finishedWithErr), tc.wantFinishedWithErr; have != want {
t.Fatalf("wrong number of UI finished-with-err tasks. want=%d, have=%d", want, have)
}
})
}
}
func addToPath(t *testing.T, relPath string) {
t.Helper()
dummyDockerPath, err := filepath.Abs("testdata/dummydocker")
if err != nil {
t.Fatal(err)
}
os.Setenv("PATH", fmt.Sprintf("%s%c%s", dummyDockerPath, os.PathListSeparator, os.Getenv("PATH")))
}
func TestExecutor_CachedStepResults(t *testing.T) {
t.Run("single step cached", func(t *testing.T) {
archive := mock.RepoArchive{
RepoName: testRepo1.Name, Commit: testRepo1.Rev(), Files: map[string]string{
"README.md": "# Welcome to the README\n",
},
}
cachedDiff := []byte(`diff --git README.md README.md
index 02a19af..c9644dd 100644
--- README.md
+++ README.md
@@ -1 +1,2 @@
# Welcome to the README
+foobar
`)
task := &Task{
BatchChangeAttributes: &template.BatchChangeAttributes{},
Steps: []batcheslib.Step{
{Run: `echo -e "foobar\n" >> README.md`},
},
CachedStepResultFound: true,
CachedStepResult: execution.AfterStepResult{
Version: 2,
StepIndex: 0,
Diff: cachedDiff,
Outputs: map[string]any{},
},
Repository: testRepo1,
}
results, err := testExecuteTasks(t, []*Task{task}, archive)
if err != nil {
t.Fatalf("execution failed: %s", err)
}
if have, want := len(results), 1; have != want {
t.Fatalf("wrong number of results. want=%d, have=%d", want, have)
}
if have, want := len(results[0].stepResults), 1; have != want {
t.Fatalf("wrong length of step results. have=%d, want=%d", have, want)
}
// We want the diff to be the same as the cached one, since we only had to
// execute a single step
executionResult := results[0].stepResults[0]
if diff := cmp.Diff(executionResult.Diff, cachedDiff); diff != "" {
t.Fatalf("wrong diff: %s", diff)
}
stepResult := results[0].stepResults[0]
if diff := cmp.Diff(stepResult, task.CachedStepResult); diff != "" {
t.Fatalf("wrong stepResult: %s", diff)
}
})
t.Run("one of multiple steps cached", func(t *testing.T) {
archive := mock.RepoArchive{
RepoName: testRepo1.Name, Commit: testRepo1.Rev(),
Files: map[string]string{
"README.md": `# automation-testing
This repository is used to test opening and closing pull request with Automation
(c) Copyright Sourcegraph 2013-2020.
(c) Copyright Sourcegraph 2013-2020.
(c) Copyright Sourcegraph 2013-2020.`,
},
}
cachedDiff := []byte(`diff --git README.md README.md
index 1914491..cd2ccbf 100644
--- README.md
+++ README.md
@@ -3,4 +3,5 @@ This repository is used to test opening and closing pull request with Automation
(c) Copyright Sourcegraph 2013-2020.
(c) Copyright Sourcegraph 2013-2020.
-(c) Copyright Sourcegraph 2013-2020.
\ No newline at end of file
+(c) Copyright Sourcegraph 2013-2020.this is step 2
+this is step 3
diff --git README.txt README.txt
new file mode 100644
index 0000000..888e1ec
--- /dev/null
+++ README.txt
@@ -0,0 +1 @@
+this is step 1
`)
wantFinalDiff := []byte(`diff --git README.md README.md
index 1914491..d6782d3 100644
--- README.md
+++ README.md
@@ -3,4 +3,7 @@ This repository is used to test opening and closing pull request with Automation
(c) Copyright Sourcegraph 2013-2020.
(c) Copyright Sourcegraph 2013-2020.
-(c) Copyright Sourcegraph 2013-2020.
\ No newline at end of file
+(c) Copyright Sourcegraph 2013-2020.this is step 2
+this is step 3
+this is step 4
+previous_step.modified_files=[README.md]
diff --git README.txt README.txt
new file mode 100644
index 0000000..888e1ec
--- /dev/null
+++ README.txt
@@ -0,0 +1 @@
+this is step 1
diff --git my-output.txt my-output.txt
new file mode 100644
index 0000000..257ae8e
--- /dev/null
+++ my-output.txt
@@ -0,0 +1 @@
+this is step 5
`)
task := &Task{
Repository: testRepo1,
BatchChangeAttributes: &template.BatchChangeAttributes{},
Steps: []batcheslib.Step{
{Run: `echo "this is step 1" >> README.txt`},
{Run: `echo "this is step 2" >> README.md`},
{Run: `echo "this is step 3" >> README.md`, Outputs: batcheslib.Outputs{
"myOutput": batcheslib.Output{
Value: "my-output.txt",
},
}},
{Run: `echo "this is step 4" >> README.md
echo "previous_step.modified_files=${{ previous_step.modified_files }}" >> README.md
`},
{Run: `echo "this is step 5" >> ${{ outputs.myOutput }}`},
},
CachedStepResultFound: true,
CachedStepResult: execution.AfterStepResult{
Version: 2,
StepIndex: 2,
Diff: cachedDiff,
Outputs: map[string]any{
"myOutput": "my-output.txt",
},
ChangedFiles: git.Changes{
Modified: []string{"README.md"},
Added: []string{"README.txt"},
},
Stdout: "",
Stderr: "",
},
}
results, err := testExecuteTasks(t, []*Task{task}, archive)
if err != nil {
t.Fatalf("execution failed: %s", err)
}
if have, want := len(results), 1; have != want {
t.Fatalf("wrong number of results. want=%d, have=%d", want, have)
}
executionResult := results[0].stepResults[len(results[0].stepResults)-1]
if diff := cmp.Diff(executionResult.Diff, wantFinalDiff); diff != "" {
t.Fatalf("wrong diff: %s", diff)
}
if diff := cmp.Diff(executionResult.Outputs, task.CachedStepResult.Outputs); diff != "" {
t.Fatalf("wrong execution result outputs: %s", diff)
}
// Only two steps should've been executed
if have, want := len(results[0].stepResults), 2; have != want {
t.Fatalf("wrong length of step results. have=%d, want=%d", have, want)
}
lastStepResult := results[0].stepResults[1]
if have, want := lastStepResult.StepIndex, 4; have != want {
t.Fatalf("wrong stepIndex. have=%d, want=%d", have, want)
}
if diff := cmp.Diff(lastStepResult.Outputs, task.CachedStepResult.Outputs); diff != "" {
t.Fatalf("wrong step result outputs: %s", diff)
}
})
t.Run("step stdout cached", func(t *testing.T) {
archive := mock.RepoArchive{
RepoName: testRepo1.Name, Commit: testRepo1.Rev(),
Files: map[string]string{
"README.md": `# automation-testing
This repository is used to test opening and closing pull request with Automation
`,
},
}
wantFinalDiff := []byte(`diff --git README.md README.md
index 3040106..5f2f924 100644
--- README.md
+++ README.md
@@ -1,2 +1,3 @@
# automation-testing
This repository is used to test opening and closing pull request with Automation
+hello world
`)
task := &Task{
Repository: testRepo1,
BatchChangeAttributes: &template.BatchChangeAttributes{},
Steps: []batcheslib.Step{
{Run: "echo -n Hello world"},
{Run: `echo ${{ previous_step.stdout }} >> README.md`},
},
CachedStepResultFound: true,
CachedStepResult: execution.AfterStepResult{
Version: 2,
StepIndex: 0,
Diff: []byte(""),
Outputs: map[string]any{},
ChangedFiles: git.Changes{},
Stdout: "hello world",
Stderr: "",
},
}
results, err := testExecuteTasks(t, []*Task{task}, archive)
if err != nil {
t.Fatalf("execution failed: %s", err)
}
if have, want := len(results), 1; have != want {
t.Fatalf("wrong number of results. want=%d, have=%d", want, have)
}
executionResult := results[0].stepResults[len(results[0].stepResults)-1]
if diff := cmp.Diff(executionResult.Diff, wantFinalDiff); diff != "" {
t.Fatalf("wrong diff: %s", diff)
}
if diff := cmp.Diff(executionResult.Outputs, task.CachedStepResult.Outputs); diff != "" {
t.Fatalf("wrong execution result outputs: %s", diff)
}
// Only one step should've been executed
if have, want := len(results[0].stepResults), 1; have != want {
t.Fatalf("wrong length of step results. have=%d, want=%d", have, want)
}
lastStepResult := results[0].stepResults[0]
if have, want := lastStepResult.StepIndex, 1; have != want {
t.Fatalf("wrong stepIndex. have=%d, want=%d", have, want)
}
if diff := cmp.Diff(lastStepResult.Outputs, task.CachedStepResult.Outputs); diff != "" {
t.Fatalf("wrong step result outputs: %s", diff)
}
})
}
func testExecuteTasks(t *testing.T, tasks []*Task, archives ...mock.RepoArchive) ([]taskResult, error) {
if runtime.GOOS == "windows" {
t.Skip("Test doesn't work on Windows because dummydocker is written in bash")
}
testTempDir := t.TempDir()
// Setup dummydocker
addToPath(t, "testdata/dummydocker")
// Setup mock test server & client
mux := mock.NewZipArchivesMux(t, nil, archives...)
ts := httptest.NewServer(mux)
t.Cleanup(ts.Close)
var clientBuffer bytes.Buffer
u, _ := url.ParseRequestURI(ts.URL)
client := api.NewClient(api.ClientOpts{EndpointURL: u, Out: &clientBuffer})
// Prepare images
//
images := make(map[string]docker.Image)
for _, t := range tasks {
for _, step := range t.Steps {
images[step.Container] = &mock.Image{RawDigest: step.Container}
}
}
ctx := context.Background()
cr, _ := workspace.NewCreator(ctx, "bind", testTempDir, testTempDir, images)
// Setup executor
executor := NewExecutor(NewExecutorOpts{
Creator: cr,
RepoArchiveRegistry: repozip.NewArchiveRegistry(client, testTempDir, false),
Logger: mock.LogNoOpManager{},
EnsureImage: imageMapEnsurer(images),
TempDir: testTempDir,
Parallelism: runtime.GOMAXPROCS(0),
Timeout: 30 * time.Second,
})
executor.Start(ctx, tasks, newDummyTaskExecutionUI())
return executor.Wait()
}
func imageMapEnsurer(m map[string]docker.Image) imageEnsurer {
return func(_ context.Context, container string) (docker.Image, error) {
if i, ok := m[container]; ok {
return i, nil
}
return nil, errors.New(fmt.Sprintf("image for %s not found", container))
}
}