stop_windows_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/integration/internal/container"
  8. "gotest.tools/v3/assert"
  9. "gotest.tools/v3/poll"
  10. "gotest.tools/v3/skip"
  11. )
  12. // TestStopContainerWithTimeout checks that ContainerStop with
  13. // a timeout works as documented, i.e. in case of negative timeout
  14. // waiting is not limited (issue #35311).
  15. func TestStopContainerWithTimeout(t *testing.T) {
  16. skip.If(t, testEnv.OSType == "windows")
  17. defer setupTest(t)()
  18. client := testEnv.APIClient()
  19. ctx := context.Background()
  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. id := container.Run(ctx, t, client, testCmd)
  50. timeout := time.Duration(d.timeout) * time.Second
  51. err := client.ContainerStop(ctx, id, &timeout)
  52. assert.NilError(t, err)
  53. poll.WaitOn(t, container.IsStopped(ctx, client, id),
  54. poll.WithDelay(100*time.Millisecond))
  55. inspect, err := client.ContainerInspect(ctx, id)
  56. assert.NilError(t, err)
  57. assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
  58. })
  59. }
  60. }