github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/orchestrator/testutils/testutils.go (about) 1 package testutils 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/docker/go-events" 9 "github.com/docker/swarmkit/api" 10 "github.com/docker/swarmkit/manager/state" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // EnsureRuns takes a closure and runs it in a goroutine, blocking until the 15 // goroutine has had an opportunity to run. It returns a channel which will be 16 // closed when the provided closure exits. 17 func EnsureRuns(closure func()) <-chan struct{} { 18 started := make(chan struct{}) 19 stopped := make(chan struct{}) 20 go func() { 21 close(started) 22 closure() 23 close(stopped) 24 }() 25 26 <-started 27 return stopped 28 } 29 30 // WatchTaskCreate waits for a task to be created. 31 func WatchTaskCreate(t *testing.T, watch chan events.Event) *api.Task { 32 for { 33 select { 34 case event := <-watch: 35 if task, ok := event.(api.EventCreateTask); ok { 36 return task.Task 37 } 38 if _, ok := event.(api.EventUpdateTask); ok { 39 assert.FailNow(t, "got EventUpdateTask when expecting EventCreateTask", fmt.Sprint(event)) 40 } 41 case <-time.After(3 * time.Second): 42 assert.FailNow(t, "no task creation") 43 } 44 } 45 } 46 47 // WatchTaskUpdate waits for a task to be updated. 48 func WatchTaskUpdate(t *testing.T, watch chan events.Event) *api.Task { 49 for { 50 select { 51 case event := <-watch: 52 if task, ok := event.(api.EventUpdateTask); ok { 53 return task.Task 54 } 55 if _, ok := event.(api.EventCreateTask); ok { 56 assert.FailNow(t, "got EventCreateTask when expecting EventUpdateTask", fmt.Sprint(event)) 57 } 58 case <-time.After(2 * time.Second): 59 assert.FailNow(t, "no task update") 60 } 61 } 62 } 63 64 // WatchTaskDelete waits for a task to be deleted. 65 func WatchTaskDelete(t *testing.T, watch chan events.Event) *api.Task { 66 for { 67 select { 68 case event := <-watch: 69 if task, ok := event.(api.EventDeleteTask); ok { 70 return task.Task 71 } 72 case <-time.After(time.Second): 73 assert.FailNow(t, "no task deletion") 74 } 75 } 76 } 77 78 // WatchShutdownTask fails the test if the next event is not a task having its 79 // desired state changed to Shutdown. 80 func WatchShutdownTask(t *testing.T, watch chan events.Event) *api.Task { 81 for { 82 select { 83 case event := <-watch: 84 if task, ok := event.(api.EventUpdateTask); ok && task.Task.DesiredState == api.TaskStateShutdown { 85 return task.Task 86 } 87 if _, ok := event.(api.EventCreateTask); ok { 88 assert.FailNow(t, "got EventCreateTask when expecting EventUpdateTask", fmt.Sprint(event)) 89 } 90 case <-time.After(time.Second): 91 assert.FailNow(t, "no task shutdown") 92 } 93 } 94 } 95 96 // Expect fails the test if the next event is not one of the specified events. 97 func Expect(t *testing.T, watch chan events.Event, specifiers ...api.Event) { 98 matcher := state.Matcher(specifiers...) 99 for { 100 select { 101 case event := <-watch: 102 if !matcher.Match(event) { 103 assert.FailNow(t, fmt.Sprintf("unexpected event: %T", event)) 104 } 105 return 106 case <-time.After(time.Second): 107 assert.FailNow(t, "no matching event") 108 } 109 } 110 }