stop_linux_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. )
  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. defer setupTest(t)()
  17. client := testEnv.APIClient()
  18. ctx := context.Background()
  19. testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
  20. testData := []struct {
  21. doc string
  22. timeout int
  23. expectedExitCode int
  24. }{
  25. // In case container is forcefully killed, 137 is returned,
  26. // otherwise the exit code from the above script
  27. {
  28. "zero timeout: expect forceful container kill",
  29. 0, 137,
  30. },
  31. {
  32. "too small timeout: expect forceful container kill",
  33. 1, 137,
  34. },
  35. {
  36. "big enough timeout: expect graceful container stop",
  37. 3, 42,
  38. },
  39. {
  40. "unlimited timeout: expect graceful container stop",
  41. -1, 42,
  42. },
  43. }
  44. for _, d := range testData {
  45. d := d
  46. t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
  47. t.Parallel()
  48. id := container.Run(ctx, t, client, testCmd)
  49. err := client.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
  50. assert.NilError(t, err)
  51. poll.WaitOn(t, container.IsStopped(ctx, client, id),
  52. poll.WithDelay(100*time.Millisecond))
  53. inspect, err := client.ContainerInspect(ctx, id)
  54. assert.NilError(t, err)
  55. assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
  56. })
  57. }
  58. }