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
|
package agent
import (
"context"
"testing"
"time"
"github.com/docker/swarmkit/agent/exec"
"github.com/docker/swarmkit/api"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
}
func TestTaskManager(t *testing.T) {
ctx := context.Background()
task := &api.Task{
Status: api.TaskStatus{},
DesiredState: api.TaskStateAccepted,
}
accepted := make(chan struct{})
ready := make(chan struct{})
shutdown := make(chan struct{})
ctlr := &controllerStub{t: t, calls: map[string]int{}}
tm := newTaskManager(ctx, task, ctlr, statusReporterFunc(func(ctx context.Context, taskID string, status *api.TaskStatus) error {
switch status.State {
case api.TaskStateAccepted:
select {
case <-accepted:
default:
close(accepted)
}
case api.TaskStatePreparing:
case api.TaskStateReady:
select {
case <-ready:
default:
close(ready)
}
case api.TaskStateStarting:
case api.TaskStateRunning:
select {
case <-ready:
default:
t.Fatal("should be running before ready")
}
case api.TaskStateCompleted:
select {
case <-shutdown:
default:
close(shutdown)
}
default:
t.Fatalf("unexpected state encountered: %v", status.State)
}
return nil
}))
acceptedWait := accepted
readyWait := ready
shutdownWait := shutdown
for {
select {
case <-acceptedWait:
task.DesiredState = api.TaskStateReady // proceed to ready
assert.NoError(t, tm.Update(ctx, task))
acceptedWait = nil
case <-readyWait:
time.Sleep(time.Second)
task.DesiredState = api.TaskStateRunning // proceed to running.
assert.NoError(t, tm.Update(ctx, task))
readyWait = nil
case <-shutdownWait:
assert.NoError(t, tm.Close())
select {
case <-tm.closed:
default:
t.Fatal("not actually closed")
}
assert.NoError(t, tm.Close()) // hit a second time to make sure it behaves
assert.Equal(t, tm.Update(ctx, task), ErrClosed)
assert.Equal(t, map[string]int{
"start": 1,
"wait": 1,
"prepare": 1,
"update": 2}, ctlr.calls)
return
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
}
type controllerStub struct {
t *testing.T
exec.Controller
calls map[string]int
}
func (cs *controllerStub) Prepare(ctx context.Context) error {
cs.calls["prepare"]++
cs.t.Log("(*controllerStub).Prepare")
return nil
}
func (cs *controllerStub) Start(ctx context.Context) error {
cs.calls["start"]++
cs.t.Log("(*controllerStub).Start")
return nil
}
func (cs *controllerStub) Wait(ctx context.Context) error {
cs.calls["wait"]++
cs.t.Log("(*controllerStub).Wait")
return nil
}
func (cs *controllerStub) Update(ctx context.Context, task *api.Task) error {
cs.calls["update"]++
cs.t.Log("(*controllerStub).Update")
return nil
}
func (cs *controllerStub) Remove(ctx context.Context) error {
cs.calls["remove"]++
cs.t.Log("(*controllerStub).Remove")
return nil
}
func (cs *controllerStub) Close() error {
cs.calls["close"]++
cs.t.Log("(*controllerStub).Close")
return nil
}
|