states.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package container
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/docker/docker/client"
  6. "github.com/docker/docker/errdefs"
  7. "github.com/pkg/errors"
  8. "gotest.tools/v3/poll"
  9. )
  10. // RunningStateFlagIs polls for the container's Running state flag to be equal to running.
  11. func RunningStateFlagIs(ctx context.Context, apiClient client.APIClient, containerID string, running bool) func(log poll.LogT) poll.Result {
  12. return func(log poll.LogT) poll.Result {
  13. inspect, err := apiClient.ContainerInspect(ctx, containerID)
  14. switch {
  15. case err != nil:
  16. return poll.Error(err)
  17. case inspect.State.Running == running:
  18. return poll.Success()
  19. default:
  20. return poll.Continue("waiting for container to be %s", map[bool]string{true: "running", false: "stopped"}[running])
  21. }
  22. }
  23. }
  24. // IsStopped verifies the container is in stopped state.
  25. func IsStopped(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
  26. return RunningStateFlagIs(ctx, apiClient, containerID, false)
  27. }
  28. // IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc.
  29. func IsInState(ctx context.Context, apiClient client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
  30. return func(log poll.LogT) poll.Result {
  31. inspect, err := apiClient.ContainerInspect(ctx, containerID)
  32. if err != nil {
  33. return poll.Error(err)
  34. }
  35. for _, v := range state {
  36. if inspect.State.Status == v {
  37. return poll.Success()
  38. }
  39. }
  40. return poll.Continue("waiting for container to be one of (%s), currently %s", strings.Join(state, ", "), inspect.State.Status)
  41. }
  42. }
  43. // IsSuccessful verifies state.Status == "exited" && state.ExitCode == 0
  44. func IsSuccessful(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
  45. return func(log poll.LogT) poll.Result {
  46. inspect, err := apiClient.ContainerInspect(ctx, containerID)
  47. if err != nil {
  48. return poll.Error(err)
  49. }
  50. if inspect.State.Status == "exited" {
  51. if inspect.State.ExitCode == 0 {
  52. return poll.Success()
  53. }
  54. return poll.Error(errors.Errorf("expected exit code 0, got %d", inspect.State.ExitCode))
  55. }
  56. return poll.Continue("waiting for container to be \"exited\", currently %s", inspect.State.Status)
  57. }
  58. }
  59. // IsRemoved verifies the container has been removed
  60. func IsRemoved(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
  61. return func(log poll.LogT) poll.Result {
  62. inspect, err := apiClient.ContainerInspect(ctx, containerID)
  63. if err != nil {
  64. if errdefs.IsNotFound(err) {
  65. return poll.Success()
  66. }
  67. return poll.Error(err)
  68. }
  69. return poll.Continue("waiting for container to be removed, currently %s", inspect.State.Status)
  70. }
  71. }