stop_windows_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "strconv"
  5. "testing"
  6. "time"
  7. containertypes "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/integration/internal/container"
  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. t.Cleanup(setupTest(t))
  19. apiClient := testEnv.APIClient()
  20. ctx := context.Background()
  21. testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
  22. testData := []struct {
  23. doc string
  24. timeout int
  25. expectedExitCode int
  26. }{
  27. // In case container is forcefully killed, 137 is returned,
  28. // otherwise the exit code from the above script
  29. {
  30. "zero timeout: expect forceful container kill",
  31. 1, 0x40010004,
  32. },
  33. {
  34. "too small timeout: expect forceful container kill",
  35. 2, 0x40010004,
  36. },
  37. {
  38. "big enough timeout: expect graceful container stop",
  39. 120, 42,
  40. },
  41. {
  42. "unlimited timeout: expect graceful container stop",
  43. -1, 42,
  44. },
  45. }
  46. for _, d := range testData {
  47. d := d
  48. t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
  49. t.Parallel()
  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. }