github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration/internal/swarm/states.go (about) 1 package swarm 2 3 import ( 4 "context" 5 6 "github.com/docker/docker/api/types" 7 "github.com/docker/docker/api/types/filters" 8 swarmtypes "github.com/docker/docker/api/types/swarm" 9 "github.com/docker/docker/client" 10 "gotest.tools/poll" 11 ) 12 13 // NoTasksForService verifies that there are no more tasks for the given service 14 func NoTasksForService(ctx context.Context, client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result { 15 return func(log poll.LogT) poll.Result { 16 tasks, err := client.TaskList(ctx, types.TaskListOptions{ 17 Filters: filters.NewArgs( 18 filters.Arg("service", serviceID), 19 ), 20 }) 21 if err == nil { 22 if len(tasks) == 0 { 23 return poll.Success() 24 } 25 if len(tasks) > 0 { 26 return poll.Continue("task count for service %s at %d waiting for 0", serviceID, len(tasks)) 27 } 28 return poll.Continue("waiting for tasks for service %s to be deleted", serviceID) 29 } 30 // TODO we should not use an error as indication that the tasks are gone. There may be other reasons for an error to occur. 31 return poll.Success() 32 } 33 } 34 35 // NoTasks verifies that all tasks are gone 36 func NoTasks(ctx context.Context, client client.ServiceAPIClient) func(log poll.LogT) poll.Result { 37 return func(log poll.LogT) poll.Result { 38 tasks, err := client.TaskList(ctx, types.TaskListOptions{}) 39 switch { 40 case err != nil: 41 return poll.Error(err) 42 case len(tasks) == 0: 43 return poll.Success() 44 default: 45 return poll.Continue("waiting for all tasks to be removed: task count at %d", len(tasks)) 46 } 47 } 48 } 49 50 // RunningTasksCount verifies there are `instances` tasks running for `serviceID` 51 func RunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result { 52 return func(log poll.LogT) poll.Result { 53 filter := filters.NewArgs() 54 filter.Add("service", serviceID) 55 tasks, err := client.TaskList(context.Background(), types.TaskListOptions{ 56 Filters: filter, 57 }) 58 switch { 59 case err != nil: 60 return poll.Error(err) 61 case len(tasks) == int(instances): 62 for _, task := range tasks { 63 if task.Status.State != swarmtypes.TaskStateRunning { 64 return poll.Continue("waiting for tasks to enter run state") 65 } 66 } 67 return poll.Success() 68 default: 69 return poll.Continue("task count at %d waiting for %d", len(tasks), instances) 70 } 71 } 72 }