github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/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 var running int 59 var taskError string 60 for _, task := range tasks { 61 switch task.Status.State { 62 case swarmtypes.TaskStateRunning: 63 running++ 64 case swarmtypes.TaskStateFailed: 65 if task.Status.Err != "" { 66 taskError = task.Status.Err 67 } 68 } 69 } 70 71 switch { 72 case err != nil: 73 return poll.Error(err) 74 case running > int(instances): 75 return poll.Continue("waiting for tasks to terminate") 76 case running < int(instances) && taskError != "": 77 return poll.Continue("waiting for tasks to enter run state. task failed with error: %s", taskError) 78 case running == int(instances): 79 return poll.Success() 80 default: 81 return poll.Continue("running task count at %d waiting for %d (total tasks: %d)", running, instances, len(tasks)) 82 } 83 } 84 }