-
Notifications
You must be signed in to change notification settings - Fork 288
/
buildcontroller_test.go
686 lines (554 loc) · 23.1 KB
/
buildcontroller_test.go
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
package engine
import (
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"github.com/windmilleng/tilt/internal/hud/server"
"github.com/windmilleng/tilt/internal/k8s/testyaml"
"github.com/windmilleng/tilt/internal/testutils/manifestbuilder"
"github.com/windmilleng/tilt/internal/testutils/podbuilder"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/internal/watch"
"github.com/windmilleng/tilt/pkg/model"
)
func TestBuildControllerOnePod(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := f.newManifest("fe")
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
pod := podbuilder.New(f.T(), manifest).Build()
f.podEvent(pod, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
assert.Equal(t, pod.Name, call.oneState().OneContainerInfo().PodID.String())
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestBuildControllerTooManyPodsForLiveUpdateErrorMessage(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := NewSanchoLiveUpdateManifest(f)
f.Start([]model.Manifest{manifest}, true)
// intial build
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
p1 := podbuilder.New(t, manifest).WithPodID("pod1").Build()
p2 := podbuilder.New(t, manifest).WithPodID("pod2").Build()
f.podEvent(p1, manifest.Name)
f.podEvent(p2, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
// Should not have sent container info b/c too many pods
assert.Equal(t, store.ContainerInfo{}, call.oneState().OneContainerInfo())
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
assert.Contains(t, f.log.String(), "can only get container info for a single pod",
"should print error message when trying to get Running Containers for manifest with more than one pod")
}
func TestBuildControllerTooManyPodsForDockerBuildNoErrorMessage(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := NewSanchoDockerBuildManifest(f)
f.Start([]model.Manifest{manifest}, true)
// intial build
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
p1 := podbuilder.New(t, manifest).WithPodID("pod1").Build()
p2 := podbuilder.New(t, manifest).WithPodID("pod2").Build()
f.podEvent(p1, manifest.Name)
f.podEvent(p2, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
// Should not have sent container info b/c too many pods
assert.Equal(t, store.ContainerInfo{}, call.oneState().OneContainerInfo())
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
// Should not have surfaced this log line b/c manifest doesn't have LiveUpdate instructions
assert.NotContains(t, f.log.String(), "can only get container info for a single pod",
"should print error message when trying to get Running Containers for manifest with more than one pod")
}
func TestBuildControllerIgnoresImageTags(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
ref := container.MustParseNamed("image-foo:tagged")
manifest := f.newManifestWithRef("fe", ref)
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
pod := podbuilder.New(t, manifest).
WithPodID("pod-id").
WithImage("image-foo:othertag").
Build()
f.podEvent(pod, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
assert.Equal(t, "pod-id", call.oneState().OneContainerInfo().PodID.String())
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestBuildControllerDockerCompose(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := NewSanchoFastBuildDCManifest(f)
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
imageTarget := manifest.ImageTargetAt(0)
assert.Equal(t, imageTarget, call.firstImgTarg())
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
imageState := call.state[imageTarget.ID()]
assert.Equal(t, "dc-sancho", imageState.OneContainerInfo().ContainerID.String())
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestBuildControllerWontContainerBuildWithTwoPods(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := f.newManifest("fe")
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
// Associate the pods with the manifest state
podA := podbuilder.New(f.T(), manifest).WithPodID("pod-a").Build()
podB := podbuilder.New(f.T(), manifest).WithPodID("pod-b").Build()
f.podEvent(podA, manifest.Name)
f.podEvent(podB, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
// We expect two pods associated with this manifest. We don't want to container-build
// if there are multiple pods, so make sure we're not sending deploy info (i.e. that
// we're doing an image build)
call = f.nextCall()
assert.Equal(t, "", call.oneState().OneContainerInfo().PodID.String())
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestBuildControllerTwoContainers(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := f.newManifest("fe")
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
// container already on this pod matches the image built by this manifest
pod := podbuilder.New(f.T(), manifest).Build()
imgName := pod.Status.ContainerStatuses[0].Image
pod.Status.ContainerStatuses = append(pod.Status.ContainerStatuses, v1.ContainerStatus{
Name: "same image",
Image: imgName, // matches image built by this manifest
Ready: true,
ContainerID: "docker://cID-same-image",
}, v1.ContainerStatus{
Name: "different image",
Image: "different-image", // does NOT match image built by this manifest
Ready: false,
ContainerID: "docker://cID-different-image",
})
f.podEvent(pod, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
runningContainers := call.oneState().RunningContainers
require.Len(t, runningContainers, 2, "expect info for two containers (those "+
"matching the image built by this manifest")
c0 := runningContainers[0]
c1 := runningContainers[1]
assert.Equal(t, pod.Name, c0.PodID.String(), "pod ID for cInfo at index 0")
assert.Equal(t, pod.Name, c1.PodID.String(), "pod ID for cInfo at index 1")
assert.Equal(t, podbuilder.FakeContainerID(), c0.ContainerID, "container ID for cInfo at index 0")
assert.Equal(t, "cID-same-image", c1.ContainerID.String(), "container ID for cInfo at index 1")
assert.Equal(t, "sancho", c0.ContainerName.String(), "container name for cInfo at index 0")
assert.Equal(t, "same image", c1.ContainerName.String(), "container name for cInfo at index 1")
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestBuildControllerWontContainerBuildWithSomeButNotAllReadyContainers(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := f.newManifest("fe")
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
// container already on this pod matches the image built by this manifest
pod := podbuilder.New(f.T(), manifest).Build()
imgName := pod.Status.ContainerStatuses[0].Image
pod.Status.ContainerStatuses = append(pod.Status.ContainerStatuses, v1.ContainerStatus{
Name: "same image",
Image: imgName, // matches image built by this manifest
Ready: false,
ContainerID: "docker://cID-same-image",
})
f.podEvent(pod, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
// If even one of the containers matching this image is !ready, we have to do a
// full rebuild, so don't return ANY RunningContainers.
call = f.nextCall()
runningContainers := call.oneState().RunningContainers
assert.Empty(t, runningContainers)
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestBuildControllerCrashRebuild(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := f.newManifest("fe")
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
assert.Equal(t, []string{}, call.oneState().FilesChanged())
f.waitForCompletedBuildCount(1)
f.b.nextLiveUpdateContainerIDs = []container.ID{podbuilder.FakeContainerID()}
pb := podbuilder.New(f.T(), manifest)
pod := pb.Build()
f.podEvent(pod, manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
assert.Equal(t, pod.Name, call.oneState().OneContainerInfo().PodID.String())
f.waitForCompletedBuildCount(2)
f.withManifestState("fe", func(ms store.ManifestState) {
assert.Equal(t, model.BuildReasonFlagChangedFiles, ms.LastBuild().Reason)
assert.Equal(t, podbuilder.FakeContainerIDSet(1), ms.LiveUpdatedContainerIDs)
})
// Restart the pod with a new container id, to simulate a container restart.
f.podEvent(pb.WithContainerID("funnyContainerID").Build(), manifest.Name)
call = f.nextCall()
assert.True(t, call.oneState().OneContainerInfo().Empty())
f.waitForCompletedBuildCount(3)
f.withManifestState("fe", func(ms store.ManifestState) {
assert.Equal(t, model.BuildReasonFlagCrash, ms.LastBuild().Reason)
})
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestCrashRebuildTwoContainersOneImage(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := manifestbuilder.New(f, "sancho").
WithK8sYAML(testyaml.SanchoTwoContainersOneImageYAML).
WithImageTarget(NewSanchoLiveUpdateImageTarget(f)).
Build()
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
f.waitForCompletedBuildCount(1)
f.b.nextLiveUpdateContainerIDs = []container.ID{"c1", "c2"}
f.podEvent(podbuilder.New(t, manifest).
WithContainerIDAtIndex("c1", 0).
WithContainerIDAtIndex("c2", 1).
Build(), manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
f.waitForCompletedBuildCount(2)
f.withManifestState("sancho", func(ms store.ManifestState) {
assert.Equal(t, 2, len(ms.LiveUpdatedContainerIDs))
})
// Simulate pod event where one of the containers has been restarted with a new ID.
f.podEvent(podbuilder.New(t, manifest).
WithContainerID("c1").
WithContainerIDAtIndex("c3", 1).
Build(), manifest.Name)
call = f.nextCall()
f.waitForCompletedBuildCount(3)
f.withManifestState("sancho", func(ms store.ManifestState) {
assert.Equal(t, model.BuildReasonFlagCrash, ms.LastBuild().Reason)
})
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestCrashRebuildTwoContainersTwoImages(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := manifestbuilder.New(f, "sancho").
WithK8sYAML(testyaml.SanchoTwoContainersOneImageYAML).
WithImageTarget(NewSanchoLiveUpdateImageTarget(f)).
WithImageTarget(NewSanchoSidecarLiveUpdateImageTarget(f)).
Build()
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
iTargs := call.imageTargets()
require.Len(t, iTargs, 2)
assert.Equal(t, manifest.ImageTargetAt(0), iTargs[0])
assert.Equal(t, manifest.ImageTargetAt(1), iTargs[1])
f.waitForCompletedBuildCount(1)
f.b.nextLiveUpdateContainerIDs = []container.ID{"c1", "c2"}
f.podEvent(podbuilder.New(t, manifest).
WithContainerIDAtIndex("c1", 0).
WithContainerIDAtIndex("c2", 1).
Build(), manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
f.waitForCompletedBuildCount(2)
f.withManifestState("sancho", func(ms store.ManifestState) {
assert.Equal(t, 2, len(ms.LiveUpdatedContainerIDs))
})
// Simulate pod event where one of the containers has been restarted with a new ID.
f.podEvent(podbuilder.New(t, manifest).
WithContainerID("c1").
WithContainerIDAtIndex("c3", 1).
Build(), manifest.Name)
call = f.nextCall()
f.waitForCompletedBuildCount(3)
f.withManifestState("sancho", func(ms store.ManifestState) {
assert.Equal(t, model.BuildReasonFlagCrash, ms.LastBuild().Reason)
})
err := f.Stop()
assert.NoError(t, err)
f.assertAllBuildsConsumed()
}
func TestRecordLiveUpdatedContainerIDsForFailedLiveUpdate(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifest := manifestbuilder.New(f, "sancho").
WithK8sYAML(testyaml.SanchoTwoContainersOneImageYAML).
WithImageTarget(NewSanchoLiveUpdateImageTarget(f)).
Build()
f.Start([]model.Manifest{manifest}, true)
call := f.nextCall()
assert.Equal(t, manifest.ImageTargetAt(0), call.firstImgTarg())
f.waitForCompletedBuildCount(1)
expectedErr := fmt.Errorf("i can't let you do that dave")
f.b.nextBuildFailure = expectedErr
f.b.nextLiveUpdateContainerIDs = []container.ID{"c1", "c2"}
f.podEvent(podbuilder.New(t, manifest).
WithContainerIDAtIndex("c1", 0).
WithContainerIDAtIndex("c2", 1).
Build(), manifest.Name)
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
call = f.nextCall()
f.waitForCompletedBuildCount(2)
f.withManifestState("sancho", func(ms store.ManifestState) {
// Manifest should have recorded last build as a failure, but
// ALSO have recorded the LiveUpdatedContainerIDs
require.Equal(t, expectedErr, ms.BuildHistory[0].Error)
assert.Equal(t, 2, len(ms.LiveUpdatedContainerIDs))
})
}
func TestBuildControllerManualTrigger(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
mName := model.ManifestName("foobar")
manifest := f.newManifest(mName.String()).WithTriggerMode(model.TriggerModeManual)
manifests := []model.Manifest{manifest}
f.Start(manifests, true)
f.nextCall()
f.waitForCompletedBuildCount(1)
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: mName})
f.assertNoCall("manifest has no pending changes, so shouldn't build even if we try to trigger it")
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
f.WaitUntil("pending change appears", func(st store.EngineState) bool {
return len(st.BuildStatus(manifest.ImageTargetAt(0).ID()).PendingFileChanges) > 0
})
f.assertNoCall("even tho there are pending changes, manual manifest shouldn't build w/o explicit trigger")
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: mName})
call := f.nextCall()
assert.Equal(t, []string{f.JoinPath("main.go")}, call.oneState().FilesChanged())
f.waitForCompletedBuildCount(2)
f.WaitUntil("manifest removed from queue", func(st store.EngineState) bool {
for _, mn := range st.TriggerQueue {
if mn == mName {
return false
}
}
return true
})
}
func TestBuildQueueOrdering(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
m1 := f.newManifestWithRef("manifest1", container.MustParseNamed("manifest1")).
WithTriggerMode(model.TriggerModeManual)
m2 := f.newManifestWithRef("manifest2", container.MustParseNamed("manifest2")).
WithTriggerMode(model.TriggerModeManual)
m3 := f.newManifestWithRef("manifest3", container.MustParseNamed("manifest3")).
WithTriggerMode(model.TriggerModeManual)
m4 := f.newManifestWithRef("manifest4", container.MustParseNamed("manifest4")).
WithTriggerMode(model.TriggerModeManual)
// attach to state in different order than we plan to trigger them
manifests := []model.Manifest{m4, m2, m3, m1}
f.Start(manifests, true)
// Wait for initial build
for _, _ = range manifests {
f.nextCall()
}
f.waitForCompletedBuildCount(len(manifests))
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("main.go"))
f.WaitUntil("pending change appears", func(st store.EngineState) bool {
return len(st.BuildStatus(m1.ImageTargetAt(0).ID()).PendingFileChanges) > 0 &&
len(st.BuildStatus(m2.ImageTargetAt(0).ID()).PendingFileChanges) > 0 &&
len(st.BuildStatus(m3.ImageTargetAt(0).ID()).PendingFileChanges) > 0 &&
len(st.BuildStatus(m4.ImageTargetAt(0).ID()).PendingFileChanges) > 0
})
f.assertNoCall("even tho there are pending changes, manual manifest shouldn't build w/o explicit trigger")
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest1"})
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest2"})
time.Sleep(10 * time.Millisecond)
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest3"})
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest4"})
for i, _ := range manifests {
expName := fmt.Sprintf("manifest%d", i+1)
call := f.nextCall()
imgID := call.firstImgTarg().ID().String()
if assert.True(t, strings.HasSuffix(imgID, expName),
"expected to get manifest '%s' but instead got: '%s' (checking suffix for manifest name)", expName, imgID) {
assert.Equal(t, []string{f.JoinPath("main.go")}, call.oneState().FilesChanged(),
"for manifest '%s", expName)
}
}
f.waitForCompletedBuildCount(2 * len(manifests))
}
// This test is tightly coupled with FastBuild, and needs to be
// rewritten to use DockerBuild
func TestBuildQueueAndAutobuildOrdering(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
// changes to this dir. will register with our manual manifests
syncDirManual := model.Sync{LocalPath: f.JoinPath("dirManual/"), ContainerPath: "/go"}
// changes to this dir. will register with our automatic manifests
syncDirAuto := model.Sync{LocalPath: f.JoinPath("dirAuto/"), ContainerPath: "/go"}
m1 := f.newFastBuildManifest("manifest1", []model.Sync{syncDirManual}).WithTriggerMode(model.TriggerModeManual)
m2 := f.newFastBuildManifest("manifest2", []model.Sync{syncDirManual}).WithTriggerMode(model.TriggerModeManual)
m3 := f.newFastBuildManifest("manifest3", []model.Sync{syncDirManual}).WithTriggerMode(model.TriggerModeManual)
m4 := f.newFastBuildManifest("manifest4", []model.Sync{syncDirManual}).WithTriggerMode(model.TriggerModeManual)
m5 := f.newFastBuildManifest("manifest5", []model.Sync{syncDirAuto}).WithTriggerMode(model.TriggerModeAuto)
// attach to state in different order than we plan to trigger them
manifests := []model.Manifest{m5, m4, m2, m3, m1}
f.Start(manifests, true)
// Wait for initial build
for _, _ = range manifests {
f.nextCall()
}
f.waitForCompletedBuildCount(len(manifests))
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("dirManual/main.go"))
f.WaitUntil("pending change appears", func(st store.EngineState) bool {
return len(st.BuildStatus(m1.ImageTargetAt(0).ID()).PendingFileChanges) > 0 &&
len(st.BuildStatus(m2.ImageTargetAt(0).ID()).PendingFileChanges) > 0 &&
len(st.BuildStatus(m3.ImageTargetAt(0).ID()).PendingFileChanges) > 0 &&
len(st.BuildStatus(m4.ImageTargetAt(0).ID()).PendingFileChanges) > 0
})
f.assertNoCall("even tho there are pending changes, manual manifest shouldn't build w/o explicit trigger")
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest1"})
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest2"})
// make our one auto-trigger manifest build - should be evaluated LAST, after
// all the manual manifests waiting in the queue
f.fsWatcher.events <- watch.NewFileEvent(f.JoinPath("dirAuto/main.go"))
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest3"})
f.store.Dispatch(server.AppendToTriggerQueueAction{Name: "manifest4"})
for i, _ := range manifests {
call := f.nextCall()
assert.True(t, strings.HasSuffix(call.firstImgTarg().ID().String(), fmt.Sprintf("manifest%d", i+1)))
if i < 4 {
assert.Equal(t, []string{f.JoinPath("dirManual/main.go")}, call.oneState().FilesChanged(), "for manifest %d", i+1)
} else {
// the automatic manifest
assert.Equal(t, []string{f.JoinPath("dirAuto/main.go")}, call.oneState().FilesChanged(), "for manifest %d", i+1)
}
}
f.waitForCompletedBuildCount(2 * len(manifests))
}
// any manifests without image targets should be deployed before any manifests WITH image targets
func TestBuildControllerNoBuildManifestsFirst(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifests := make([]model.Manifest, 10)
for i := 0; i < 10; i++ {
manifests[i] = f.newManifest(fmt.Sprintf("built%d", i+1))
}
for _, i := range []int{3, 7, 8} {
manifests[i] = manifestbuilder.New(f, model.ManifestName(fmt.Sprintf("unbuilt%d", i+1))).
WithK8sYAML("fake-yaml").
Build()
}
f.Start(manifests, true)
var observedBuildOrder []string
for i := 0; i < len(manifests); i++ {
call := f.nextCall()
observedBuildOrder = append(observedBuildOrder, call.k8s().Name.String())
}
// throwing a bunch of elements at it to increase confidence we maintain order between built and unbuilt
// this might miss bugs since we might just get these elements back in the right order via luck
expectedBuildOrder := []string{
"unbuilt4",
"unbuilt8",
"unbuilt9",
"built1",
"built2",
"built3",
"built5",
"built6",
"built7",
"built10",
}
assert.Equal(t, expectedBuildOrder, observedBuildOrder)
}
func TestBuildControllerUnresourcedYAMLFirst(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
manifests := []model.Manifest{
f.newManifest("built1"),
f.newManifest("built2"),
f.newManifest("built3"),
f.newManifest("built4"),
}
manifests = append(manifests, manifestbuilder.New(f, model.UnresourcedYAMLManifestName).WithK8sYAML("fake-yaml").Build())
f.Start(manifests, true)
var observedBuildOrder []string
for i := 0; i < len(manifests); i++ {
call := f.nextCall()
observedBuildOrder = append(observedBuildOrder, call.k8s().Name.String())
}
expectedBuildOrder := []string{
model.UnresourcedYAMLManifestName.String(),
"built1",
"built2",
"built3",
"built4",
}
assert.Equal(t, expectedBuildOrder, observedBuildOrder)
}
func TestBuildControllerRespectDockerComposeOrder(t *testing.T) {
f := newTestFixture(t)
defer f.TearDown()
sancho := NewSanchoFastBuildDCManifest(f)
redis := manifestbuilder.New(f, "redis").WithDockerCompose().Build()
donQuixote := manifestbuilder.New(f, "don-quixote").WithDockerCompose().Build()
manifests := []model.Manifest{redis, sancho, donQuixote}
f.Start(manifests, true)
var observedBuildOrder []string
for i := 0; i < len(manifests); i++ {
call := f.nextCall()
observedBuildOrder = append(observedBuildOrder, call.dc().Name.String())
}
// If these were Kubernetes resources, we would try to deploy don-quixote
// before sancho, because it doesn't have an image build.
//
// But this would be wrong, because Docker Compose has stricter ordering requirements, see:
// https://docs.docker.com/compose/startup-order/
expectedBuildOrder := []string{
"redis",
"sancho",
"don-quixote",
}
assert.Equal(t, expectedBuildOrder, observedBuildOrder)
}