forked from flynn/flynn
/
test_scheduler.go
720 lines (631 loc) · 21.2 KB
/
test_scheduler.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
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
package main
import (
"encoding/json"
"net"
"strings"
"time"
ct "github.com/flynn/flynn/controller/types"
"github.com/flynn/flynn/discoverd/client"
"github.com/flynn/flynn/host/types"
logaggc "github.com/flynn/flynn/logaggregator/client"
"github.com/flynn/flynn/pkg/cluster"
"github.com/flynn/flynn/pkg/typeconv"
c "github.com/flynn/go-check"
)
type SchedulerSuite struct {
Helper
}
const scaleTimeout = 60 * time.Second
var _ = c.Suite(&SchedulerSuite{})
func (s *SchedulerSuite) checkJobState(t *c.C, appID, jobID string, state ct.JobState) {
job, err := s.controllerClient(t).GetJob(appID, jobID)
t.Assert(err, c.IsNil)
t.Assert(job.State, c.Equals, state)
}
func jobEventsEqual(expected, actual ct.JobEvents) bool {
for typ, events := range expected {
diff, ok := actual[typ]
if !ok {
return false
}
for state, count := range events {
if diff[state] != count {
return false
}
}
}
return true
}
func (s *SchedulerSuite) TestScale(t *c.C) {
app, release := s.createApp(t)
formation := &ct.Formation{
AppID: app.ID,
ReleaseID: release.ID,
Processes: make(map[string]int),
}
current := make(map[string]int)
updates := []map[string]int{
{"printer": 2},
{"printer": 3, "crasher": 1},
{"printer": 1},
}
watcher, err := s.controllerClient(t).WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
for _, procs := range updates {
debugf(t, "scaling formation to %v", procs)
formation.Processes = procs
expected := s.controllerClient(t).ExpectedScalingEvents(current, procs, release.Processes, 1)
t.Assert(s.controllerClient(t).PutFormation(formation), c.IsNil)
err = watcher.WaitFor(expected, scaleTimeout, nil)
t.Assert(err, c.IsNil)
current = procs
}
}
func (s *SchedulerSuite) TestScaleTags(t *c.C) {
// ensure we have more than 1 host to test with
hosts, err := s.clusterClient(t).Hosts()
t.Assert(err, c.IsNil)
if len(hosts) <= 1 {
t.Skip("not enough hosts to test tagged based scheduling")
}
// stream the scheduler leader log so we can synchronize tag changes
leader, err := s.discoverdClient(t).Service("controller-scheduler").Leader()
t.Assert(err, c.IsNil)
client := s.controllerClient(t)
res, err := client.GetAppLog("controller", &ct.LogOpts{
Follow: true,
JobID: leader.Meta["FLYNN_JOB_ID"],
ProcessType: typeconv.StringPtr("scheduler"),
Lines: typeconv.IntPtr(0),
})
t.Assert(err, c.IsNil)
defer res.Close()
tagChange := make(chan struct{})
go func() {
dec := json.NewDecoder(res)
for {
var msg logaggc.Message
if err := dec.Decode(&msg); err != nil {
return
}
if strings.Contains(msg.Msg, "host tags changed") {
tagChange <- struct{}{}
}
}
}()
waitSchedulerTagChange := func() {
select {
case <-tagChange:
return
case <-time.After(10 * time.Second):
t.Fatalf("timed out waiting for scheduler leader to see tag change")
}
}
// watch service events so we can wait for tag changes
events := make(chan *discoverd.Event)
stream, err := s.discoverdClient(t).Service("flynn-host").Watch(events)
t.Assert(err, c.IsNil)
defer stream.Close()
waitServiceEvent := func(kind discoverd.EventKind) *discoverd.Event {
for {
select {
case event, ok := <-events:
if !ok {
t.Fatalf("service event stream closed unexpectedly: %s", stream.Err())
}
if event.Kind == kind {
return event
}
case <-time.After(10 * time.Second):
t.Fatalf("timed out waiting for service %s event", kind)
}
}
}
// wait for the watch to be current before changing tags
waitServiceEvent(discoverd.EventKindCurrent)
updateTags := func(host *cluster.Host, tags map[string]string) {
debugf(t, "setting host tags: %s => %v", host.ID(), tags)
t.Assert(host.UpdateTags(tags), c.IsNil)
event := waitServiceEvent(discoverd.EventKindUpdate)
t.Assert(event.Instance.Meta["id"], c.Equals, host.ID())
for key, val := range tags {
t.Assert(event.Instance.Meta["tag:"+key], c.Equals, val)
}
waitSchedulerTagChange()
}
// create an app with a tagged process and watch job events
app, release := s.createApp(t)
formation := &ct.Formation{
AppID: app.ID,
ReleaseID: release.ID,
Tags: map[string]map[string]string{"printer": {"active": "true"}},
}
watcher, err := client.WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
// add tag to host 1
host1 := hosts[0]
updateTags(host1, map[string]string{"active": "true"})
// start jobs
debug(t, "scaling printer=2")
formation.Processes = map[string]int{"printer": 2}
t.Assert(client.PutFormation(formation), c.IsNil)
t.Assert(watcher.WaitFor(ct.JobEvents{"printer": ct.JobUpEvents(2)}, scaleTimeout, nil), c.IsNil)
assertHostJobCounts := func(expected map[string]int) {
jobs, err := client.JobList(app.ID)
t.Assert(err, c.IsNil)
actual := make(map[string]int)
for _, job := range jobs {
if job.State == ct.JobStateUp {
actual[job.HostID]++
}
}
t.Assert(actual, c.DeepEquals, expected)
}
// check all jobs on host 1
assertHostJobCounts(map[string]int{host1.ID(): 2})
// add tag to host 2
host2 := hosts[1]
updateTags(host2, map[string]string{"active": "true"})
// scale up
debug(t, "scaling printer=4")
formation.Processes["printer"] = 4
t.Assert(client.PutFormation(formation), c.IsNil)
t.Assert(watcher.WaitFor(ct.JobEvents{"printer": ct.JobUpEvents(2)}, scaleTimeout, nil), c.IsNil)
// check jobs distributed across hosts 1 and 2
assertHostJobCounts(map[string]int{host1.ID(): 2, host2.ID(): 2})
// remove tag from host 2
updateTags(host2, map[string]string{"active": ""})
// check jobs are moved to host1
jobEvents := ct.JobEvents{"printer": map[ct.JobState]int{
ct.JobStateDown: 2,
ct.JobStateUp: 2,
}}
t.Assert(watcher.WaitFor(jobEvents, scaleTimeout, nil), c.IsNil)
assertHostJobCounts(map[string]int{host1.ID(): 4})
// remove tag from host 1
updateTags(host1, map[string]string{"active": ""})
assertStateCounts := func(expected map[ct.JobState]int) {
jobs, err := client.JobList(app.ID)
t.Assert(err, c.IsNil)
actual := make(map[ct.JobState]int)
for _, job := range jobs {
actual[job.State]++
}
t.Assert(actual, c.DeepEquals, expected)
}
// check 4 pending jobs, rest are stopped
t.Assert(watcher.WaitFor(ct.JobEvents{"printer": ct.JobDownEvents(4)}, scaleTimeout, nil), c.IsNil)
assertStateCounts(map[ct.JobState]int{ct.JobStatePending: 4, ct.JobStateDown: 6})
// re-add tag to host 1
updateTags(host1, map[string]string{"active": "true"})
// check pending jobs are started on host 1
t.Assert(watcher.WaitFor(ct.JobEvents{"printer": ct.JobUpEvents(4)}, scaleTimeout, nil), c.IsNil)
assertHostJobCounts(map[string]int{host1.ID(): 4})
assertStateCounts(map[ct.JobState]int{ct.JobStateUp: 4, ct.JobStateDown: 6})
// add different tag to host 2
updateTags(host2, map[string]string{"disk": "ssd"})
// update formation tags, check jobs are moved to host 2
debug(t, "updating formation tags to disk=ssd")
formation.Tags["printer"] = map[string]string{"disk": "ssd"}
t.Assert(client.PutFormation(formation), c.IsNil)
jobEvents = ct.JobEvents{"printer": map[ct.JobState]int{
ct.JobStateDown: 4,
ct.JobStateUp: 4,
}}
t.Assert(watcher.WaitFor(jobEvents, scaleTimeout, nil), c.IsNil)
assertHostJobCounts(map[string]int{host2.ID(): 4})
assertStateCounts(map[ct.JobState]int{ct.JobStateUp: 4, ct.JobStateDown: 10})
// scale down stops the jobs
debug(t, "scaling printer=0")
formation.Processes = nil
t.Assert(client.PutFormation(formation), c.IsNil)
t.Assert(watcher.WaitFor(ct.JobEvents{"printer": ct.JobDownEvents(4)}, scaleTimeout, nil), c.IsNil)
assertStateCounts(map[ct.JobState]int{ct.JobStateDown: 14})
}
func (s *SchedulerSuite) TestControllerRestart(t *c.C) {
// get the current controller details
app, err := s.controllerClient(t).GetApp("controller")
t.Assert(err, c.IsNil)
release, err := s.controllerClient(t).GetAppRelease("controller")
t.Assert(err, c.IsNil)
formation, err := s.controllerClient(t).GetFormation(app.ID, release.ID)
t.Assert(err, c.IsNil)
list, err := s.controllerClient(t).JobList("controller")
t.Assert(err, c.IsNil)
var jobs []*ct.Job
for _, job := range list {
if job.Type == "web" && job.State == ct.JobStateUp {
jobs = append(jobs, job)
}
}
t.Assert(jobs, c.HasLen, formation.Processes["web"])
jobID := jobs[0].ID
hostID, _ := cluster.ExtractHostID(jobID)
t.Assert(hostID, c.Not(c.Equals), "")
debugf(t, "current controller app[%s] host[%s] job[%s]", app.ID, hostID, jobID)
// subscribe to service events, wait for current event
events := make(chan *discoverd.Event)
stream, err := s.discoverdClient(t).Service("controller").Watch(events)
t.Assert(err, c.IsNil)
defer stream.Close()
type serviceEvents map[discoverd.EventKind]int
wait := func(expected serviceEvents) {
actual := make(serviceEvents)
outer:
for {
select {
case event := <-events:
actual[event.Kind]++
for kind, count := range expected {
if actual[kind] != count {
continue outer
}
}
return
case <-time.After(scaleTimeout):
t.Fatal("timed out waiting for controller service event")
}
}
}
wait(serviceEvents{discoverd.EventKindCurrent: 1})
// start another controller and wait for it to come up
debug(t, "scaling the controller up")
formation.Processes["web"]++
t.Assert(s.controllerClient(t).PutFormation(formation), c.IsNil)
wait(serviceEvents{discoverd.EventKindUp: 1})
// kill the first controller and check the scheduler brings it back online
cc := cluster.NewClientWithServices(s.discoverdClient(t).Service)
hc, err := cc.Host(hostID)
t.Assert(err, c.IsNil)
debug(t, "stopping job ", jobID)
t.Assert(hc.StopJob(jobID), c.IsNil)
wait(serviceEvents{discoverd.EventKindUp: 1, discoverd.EventKindDown: 1})
// scale back down
debug(t, "scaling the controller down")
formation.Processes["web"]--
t.Assert(s.controllerClient(t).PutFormation(formation), c.IsNil)
wait(serviceEvents{discoverd.EventKindDown: 1})
// unset the suite's client so other tests use a new client
s.controller = nil
}
func (s *SchedulerSuite) TestJobMeta(t *c.C) {
app, release := s.createApp(t)
watcher, err := s.controllerClient(t).WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
// start 1 one-off job
_, err = s.controllerClient(t).RunJobDetached(app.ID, &ct.NewJob{
ReleaseID: release.ID,
Args: []string{"sh", "-c", "while true; do echo one-off-job; sleep 1; done"},
Meta: map[string]string{
"foo": "baz",
},
})
t.Assert(err, c.IsNil)
err = watcher.WaitFor(ct.JobEvents{"": {ct.JobStateUp: 1}}, scaleTimeout, nil)
t.Assert(err, c.IsNil)
list, err := s.controllerClient(t).JobList(app.ID)
t.Assert(err, c.IsNil)
t.Assert(list, c.HasLen, 1)
t.Assert(list[0].Meta, c.DeepEquals, map[string]string{
"foo": "baz",
})
}
func (s *SchedulerSuite) TestJobStatus(t *c.C) {
app, release := s.createApp(t)
watcher, err := s.controllerClient(t).WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
// start 2 formation processes and 1 one-off job
t.Assert(s.controllerClient(t).PutFormation(&ct.Formation{
AppID: app.ID,
ReleaseID: release.ID,
Processes: map[string]int{"printer": 1, "crasher": 1},
}), c.IsNil)
_, err = s.controllerClient(t).RunJobDetached(app.ID, &ct.NewJob{
ReleaseID: release.ID,
Args: []string{"sh", "-c", "while true; do echo one-off-job; sleep 1; done"},
})
t.Assert(err, c.IsNil)
err = watcher.WaitFor(ct.JobEvents{"printer": {ct.JobStateUp: 1}, "crasher": {ct.JobStateUp: 1}, "": {ct.JobStateUp: 1}}, scaleTimeout, nil)
t.Assert(err, c.IsNil)
list, err := s.controllerClient(t).JobList(app.ID)
t.Assert(err, c.IsNil)
t.Assert(list, c.HasLen, 3)
jobs := make(map[string]*ct.Job, len(list))
for _, job := range list {
debugf(t, "%s job started with ID %s", job.Type, job.ID)
jobs[job.Type] = job
}
// Check jobs are marked as up once started
t.Assert(jobs["printer"].State, c.Equals, ct.JobStateUp)
t.Assert(jobs["crasher"].State, c.Equals, ct.JobStateUp)
t.Assert(jobs[""].State, c.Equals, ct.JobStateUp)
// Check that when a formation's job is removed, it is marked as down and a new one is scheduled
job := jobs["printer"]
s.stopJob(t, job.ID)
err = watcher.WaitFor(ct.JobEvents{"printer": {ct.JobStateDown: 1, ct.JobStateUp: 1}}, scaleTimeout, nil)
t.Assert(err, c.IsNil)
s.checkJobState(t, app.ID, job.ID, ct.JobStateDown)
list, err = s.controllerClient(t).JobList(app.ID)
t.Assert(err, c.IsNil)
t.Assert(list, c.HasLen, 4)
// Check that when a one-off job is removed, it is marked as down but a new one is not scheduled
job = jobs[""]
s.stopJob(t, job.ID)
err = watcher.WaitFor(ct.JobEvents{"": {ct.JobStateDown: 1}}, scaleTimeout, nil)
t.Assert(err, c.IsNil)
s.checkJobState(t, app.ID, job.ID, ct.JobStateDown)
list, err = s.controllerClient(t).JobList(app.ID)
t.Assert(err, c.IsNil)
t.Assert(list, c.HasLen, 4)
// Check that when a job errors, it is marked as down and a new one is started
job = jobs["crasher"]
s.stopJob(t, job.ID)
err = watcher.WaitFor(ct.JobEvents{"crasher": {ct.JobStateDown: 1, ct.JobStateUp: 1}}, scaleTimeout, nil)
t.Assert(err, c.IsNil)
s.checkJobState(t, app.ID, job.ID, ct.JobStateDown)
list, err = s.controllerClient(t).JobList(app.ID)
t.Assert(err, c.IsNil)
t.Assert(list, c.HasLen, 5)
}
func (s *SchedulerSuite) TestOmniJobs(t *c.C) {
if testCluster == nil {
t.Skip("cannot boot new hosts")
}
app, release := s.createApp(t)
watcher, err := s.controllerClient(t).WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
formation := &ct.Formation{
AppID: app.ID,
ReleaseID: release.ID,
Processes: make(map[string]int),
}
current := make(map[string]int)
updates := []map[string]int{
{"printer": 2},
{"printer": 3, "omni": 2},
{"printer": 1, "omni": 1},
}
for _, procs := range updates {
debugf(t, "scaling formation to %v", procs)
formation.Processes = procs
t.Assert(s.controllerClient(t).PutFormation(formation), c.IsNil)
expected := s.controllerClient(t).ExpectedScalingEvents(current, procs, release.Processes, testCluster.Size())
err = watcher.WaitFor(expected, scaleTimeout, nil)
t.Assert(err, c.IsNil)
current = procs
}
// Check that new hosts get omni jobs
newHosts := s.addHosts(t, 2, false, "router-api")
defer s.removeHosts(t, newHosts, "router-api")
err = watcher.WaitFor(ct.JobEvents{"omni": {ct.JobStateUp: 2}}, scaleTimeout, nil)
t.Assert(err, c.IsNil)
}
func (s *SchedulerSuite) TestJobRestartBackoffPolicy(t *c.C) {
startTimeout := 20 * time.Second
app, release := s.createApp(t)
watcher, err := s.controllerClient(t).WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
t.Assert(s.controllerClient(t).PutFormation(&ct.Formation{
AppID: app.ID,
ReleaseID: release.ID,
Processes: map[string]int{"printer": 1},
}), c.IsNil)
var id string
var assignId = func(j *ct.Job) error {
debugf(t, "got job event: %s %s", j.ID, j.State)
id = j.ID
return nil
}
err = watcher.WaitFor(ct.JobEvents{"printer": {ct.JobStateUp: 1}}, scaleTimeout, assignId)
t.Assert(err, c.IsNil)
waitForRestart := func(duration time.Duration) {
start := time.Now()
s.stopJob(t, id)
debugf(t, "expecting new job to start in %s", duration)
err = watcher.WaitFor(ct.JobEvents{"printer": {ct.JobStateUp: 1}}, duration+startTimeout, assignId)
t.Assert(err, c.IsNil)
actual := time.Now().Sub(start)
if actual < duration {
t.Fatalf("expected new job to start after %s but started after %s", duration, actual)
}
}
waitForRestart(0)
waitForRestart(0)
waitForRestart(0)
waitForRestart(0)
}
func (s *SchedulerSuite) TestTCPApp(t *c.C) {
app, _ := s.createApp(t)
t.Assert(flynn(t, "/", "-a", app.Name, "scale", "echoer=1"), Succeeds)
newRoute := flynn(t, "/", "-a", app.Name, "route", "add", "tcp", "-s", "echo-service")
t.Assert(newRoute, Succeeds)
t.Assert(newRoute.Output, Matches, `.+ on port \d+`)
str := strings.Split(strings.TrimSpace(string(newRoute.Output)), " ")
port := str[len(str)-1]
// use Attempts to give the processes time to start
if err := Attempts.Run(func() error {
servAddr := routerIP + ":" + port
conn, err := net.Dial("tcp", servAddr)
if err != nil {
return err
}
defer conn.Close()
msg := []byte("hello there!\n")
_, err = conn.Write(msg)
if err != nil {
return err
}
reply := make([]byte, len(msg))
_, err = conn.Read(reply)
if err != nil {
return err
}
t.Assert(reply, c.DeepEquals, msg)
return nil
}); err != nil {
t.Fatal(err)
}
}
func (s *SchedulerSuite) TestRollbackController(t *c.C) {
// get the current controller release
client := s.controllerClient(t)
app, err := client.GetApp("controller")
t.Assert(err, c.IsNil)
release, err := client.GetAppRelease(app.ID)
t.Assert(err, c.IsNil)
watcher, err := s.controllerClient(t).WatchJobEvents(app.ID, release.ID)
t.Assert(err, c.IsNil)
defer watcher.Close()
// get the current controller formation
formation, err := client.GetFormation(app.ID, release.ID)
t.Assert(err, c.IsNil)
currentReleaseID := release.ID
// create a controller deployment that will fail
release.ID = ""
worker := release.Processes["worker"]
worker.Args = []string{"/i/dont/exist"}
release.Processes["worker"] = worker
t.Assert(client.CreateRelease(release), c.IsNil)
deployment, err := client.CreateDeployment(app.ID, release.ID)
t.Assert(err, c.IsNil)
events := make(chan *ct.DeploymentEvent)
eventStream, err := client.StreamDeployment(deployment, events)
t.Assert(err, c.IsNil)
defer eventStream.Close()
// wait for the deploy to fail
loop:
for {
select {
case e, ok := <-events:
if !ok {
t.Fatal("unexpected close of deployment event stream")
}
debugf(t, "got deployment event: %s %s", e.JobType, e.JobState)
switch e.Status {
case "complete":
t.Fatal("the deployment succeeded when it should have failed")
case "failed":
break loop
}
case <-time.After(2 * time.Minute):
t.Fatal("timed out waiting for the deploy to fail")
}
}
// wait for jobs to come back up
hosts, err := s.clusterClient(t).Hosts()
expected := map[string]map[ct.JobState]int{
"web": {ct.JobStateUp: formation.Processes["web"]},
"scheduler": {ct.JobStateUp: len(hosts)},
}
t.Assert(watcher.WaitFor(expected, scaleTimeout, nil), c.IsNil)
// check the correct controller jobs are running
t.Assert(err, c.IsNil)
t.Assert(hosts, c.Not(c.HasLen), 0)
actual := make(map[string]map[string]int)
for _, h := range hosts {
jobs, err := h.ListJobs()
t.Assert(err, c.IsNil)
for _, job := range jobs {
if job.Status != host.StatusRunning {
continue
}
appID := job.Job.Metadata["flynn-controller.app"]
if appID != app.ID {
continue
}
releaseID := job.Job.Metadata["flynn-controller.release"]
if releaseID != currentReleaseID {
continue
}
if _, ok := actual[releaseID]; !ok {
actual[releaseID] = make(map[string]int)
}
typ := job.Job.Metadata["flynn-controller.type"]
actual[releaseID][typ]++
}
}
t.Assert(actual, c.DeepEquals, map[string]map[string]int{
currentReleaseID: {
"web": formation.Processes["web"],
"scheduler": formation.Processes["scheduler"] * len(hosts),
"worker": formation.Processes["worker"],
},
})
}
func (s *SchedulerSuite) TestDeployController(t *c.C) {
// get the current controller release
client := s.controllerClient(t)
app, err := client.GetApp("controller")
t.Assert(err, c.IsNil)
release, err := client.GetAppRelease(app.ID)
t.Assert(err, c.IsNil)
// get the current controller formation
formation, err := client.GetFormation(app.ID, release.ID)
t.Assert(err, c.IsNil)
// create a controller deployment
release.ID = ""
t.Assert(client.CreateRelease(release), c.IsNil)
deployment, err := client.CreateDeployment(app.ID, release.ID)
t.Assert(err, c.IsNil)
events := make(chan *ct.DeploymentEvent)
eventStream, err := client.StreamDeployment(deployment, events)
t.Assert(err, c.IsNil)
defer eventStream.Close()
// wait for the deploy to complete (this doesn't wait for specific events
// due to the fact that when the deployer deploys itself, some events will
// not get sent)
loop:
for {
select {
case e, ok := <-events:
if !ok {
t.Fatal("unexpected close of deployment event stream")
}
debugf(t, "got deployment event: %s %s", e.JobType, e.JobState)
switch e.Status {
case "complete":
break loop
case "failed":
t.Fatal("the deployment failed")
}
case <-time.After(time.Duration(app.DeployTimeout) * time.Second):
t.Fatal("timed out waiting for the deploy to complete")
}
}
// check the correct controller jobs are running
hosts, err := s.clusterClient(t).Hosts()
t.Assert(err, c.IsNil)
t.Assert(hosts, c.Not(c.HasLen), 0)
actual := make(map[string]map[string]int)
for _, h := range hosts {
jobs, err := h.ListJobs()
t.Assert(err, c.IsNil)
for _, job := range jobs {
if job.Status != host.StatusRunning {
continue
}
appID := job.Job.Metadata["flynn-controller.app"]
if appID != app.ID {
continue
}
releaseID := job.Job.Metadata["flynn-controller.release"]
if _, ok := actual[releaseID]; !ok {
actual[releaseID] = make(map[string]int)
}
typ := job.Job.Metadata["flynn-controller.type"]
actual[releaseID][typ]++
}
}
expected := map[string]map[string]int{release.ID: {
"web": formation.Processes["web"],
"worker": formation.Processes["worker"],
"scheduler": len(hosts),
}}
t.Assert(actual, c.DeepEquals, expected)
}