states.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package swarm
  2. import (
  3. "context"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/api/types/filters"
  6. "github.com/docker/docker/client"
  7. "gotest.tools/poll"
  8. )
  9. // NoTasksForService verifies that there are no more tasks for the given service
  10. func NoTasksForService(ctx context.Context, client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
  11. return func(log poll.LogT) poll.Result {
  12. tasks, err := client.TaskList(ctx, types.TaskListOptions{
  13. Filters: filters.NewArgs(
  14. filters.Arg("service", serviceID),
  15. ),
  16. })
  17. if err == nil {
  18. if len(tasks) == 0 {
  19. return poll.Success()
  20. }
  21. if len(tasks) > 0 {
  22. return poll.Continue("task count for service %s at %d waiting for 0", serviceID, len(tasks))
  23. }
  24. return poll.Continue("waiting for tasks for service %s to be deleted", serviceID)
  25. }
  26. // TODO we should not use an error as indication that the tasks are gone. There may be other reasons for an error to occur.
  27. return poll.Success()
  28. }
  29. }
  30. // NoTasks verifies that all tasks are gone
  31. func NoTasks(ctx context.Context, client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
  32. return func(log poll.LogT) poll.Result {
  33. tasks, err := client.TaskList(ctx, types.TaskListOptions{})
  34. switch {
  35. case err != nil:
  36. return poll.Error(err)
  37. case len(tasks) == 0:
  38. return poll.Success()
  39. default:
  40. return poll.Continue("waiting for all tasks to be removed: task count at %d", len(tasks))
  41. }
  42. }
  43. }