wait_test.go 2.7 KB

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