stop_windows_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "strconv"
  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"
  9. "gotest.tools/v3/assert"
  10. "gotest.tools/v3/poll"
  11. "gotest.tools/v3/skip"
  12. )
  13. // TestStopContainerWithTimeout checks that ContainerStop with
  14. // a timeout works as documented, i.e. in case of negative timeout
  15. // waiting is not limited (issue #35311).
  16. func TestStopContainerWithTimeout(t *testing.T) {
  17. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  18. ctx := setupTest(t)
  19. apiClient := testEnv.APIClient()
  20. testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
  21. testData := []struct {
  22. doc string
  23. timeout int
  24. expectedExitCode int
  25. }{
  26. // In case container is forcefully killed, 137 is returned,
  27. // otherwise the exit code from the above script
  28. {
  29. "zero timeout: expect forceful container kill",
  30. 1, 0x40010004,
  31. },
  32. {
  33. "too small timeout: expect forceful container kill",
  34. 2, 0x40010004,
  35. },
  36. {
  37. "big enough timeout: expect graceful container stop",
  38. 120, 42,
  39. },
  40. {
  41. "unlimited timeout: expect graceful container stop",
  42. -1, 42,
  43. },
  44. }
  45. for _, d := range testData {
  46. d := d
  47. t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
  48. t.Parallel()
  49. ctx := testutil.StartSpan(ctx, t)
  50. id := container.Run(ctx, t, apiClient, testCmd)
  51. err := apiClient.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
  52. assert.NilError(t, err)
  53. poll.WaitOn(t, container.IsStopped(ctx, apiClient, id),
  54. poll.WithDelay(100*time.Millisecond))
  55. inspect, err := apiClient.ContainerInspect(ctx, id)
  56. assert.NilError(t, err)
  57. assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
  58. })
  59. }
  60. }