wait_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. containertypes "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/integration/internal/container"
  8. "github.com/docker/docker/testutil/request"
  9. "gotest.tools/v3/assert"
  10. is "gotest.tools/v3/assert/cmp"
  11. "gotest.tools/v3/poll"
  12. "gotest.tools/v3/skip"
  13. )
  14. func TestWaitNonBlocked(t *testing.T) {
  15. defer setupTest(t)()
  16. cli := request.NewAPIClient(t)
  17. testCases := []struct {
  18. doc string
  19. cmd string
  20. expectedCode int64
  21. }{
  22. {
  23. doc: "wait-nonblocking-exit-0",
  24. cmd: "exit 0",
  25. expectedCode: 0,
  26. },
  27. {
  28. doc: "wait-nonblocking-exit-random",
  29. cmd: "exit 99",
  30. expectedCode: 99,
  31. },
  32. }
  33. for _, tc := range testCases {
  34. tc := tc
  35. t.Run(tc.doc, func(t *testing.T) {
  36. t.Parallel()
  37. ctx := context.Background()
  38. containerID := container.Run(ctx, t, cli, container.WithCmd("sh", "-c", tc.cmd))
  39. poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "exited"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
  40. waitResC, errC := cli.ContainerWait(ctx, containerID, "")
  41. select {
  42. case err := <-errC:
  43. assert.NilError(t, err)
  44. case waitRes := <-waitResC:
  45. assert.Check(t, is.Equal(tc.expectedCode, waitRes.StatusCode))
  46. }
  47. })
  48. }
  49. }
  50. func TestWaitBlocked(t *testing.T) {
  51. // Windows busybox does not support trap in this way, not sleep with sub-second
  52. // granularity. It will always exit 0x40010004.
  53. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  54. defer setupTest(t)()
  55. cli := request.NewAPIClient(t)
  56. testCases := []struct {
  57. doc string
  58. cmd string
  59. expectedCode int64
  60. }{
  61. {
  62. doc: "test-wait-blocked-exit-zero",
  63. cmd: "trap 'exit 0' TERM; while true; do usleep 10; done",
  64. expectedCode: 0,
  65. },
  66. {
  67. doc: "test-wait-blocked-exit-random",
  68. cmd: "trap 'exit 99' TERM; while true; do usleep 10; done",
  69. expectedCode: 99,
  70. },
  71. }
  72. for _, tc := range testCases {
  73. tc := tc
  74. t.Run(tc.doc, func(t *testing.T) {
  75. t.Parallel()
  76. ctx := context.Background()
  77. containerID := container.Run(ctx, t, cli, container.WithCmd("sh", "-c", tc.cmd))
  78. poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "running"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
  79. waitResC, errC := cli.ContainerWait(ctx, containerID, "")
  80. err := cli.ContainerStop(ctx, containerID, containertypes.StopOptions{})
  81. assert.NilError(t, err)
  82. select {
  83. case err := <-errC:
  84. assert.NilError(t, err)
  85. case waitRes := <-waitResC:
  86. assert.Check(t, is.Equal(tc.expectedCode, waitRes.StatusCode))
  87. case <-time.After(2 * time.Second):
  88. t.Fatal("timeout waiting for `docker wait`")
  89. }
  90. })
  91. }
  92. }