daemon_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package container
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/integration/internal/container"
  7. "github.com/docker/docker/testutil/daemon"
  8. "gotest.tools/v3/assert"
  9. is "gotest.tools/v3/assert/cmp"
  10. "gotest.tools/v3/skip"
  11. )
  12. // Make sure a container that does not exit when it upon receiving it's stop signal is actually shutdown on daemon
  13. // startup.
  14. func TestContainerKillOnDaemonStart(t *testing.T) {
  15. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  16. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  17. skip.If(t, testEnv.IsRootless, "scenario doesn't work with rootless mode")
  18. t.Parallel()
  19. d := daemon.New(t)
  20. defer d.Cleanup(t)
  21. d.StartWithBusybox(t, "--iptables=false")
  22. defer d.Stop(t)
  23. client := d.NewClientT(t)
  24. ctx := context.Background()
  25. // The intention of this container is to ignore stop signals.
  26. // Sadly this means the test will take longer, but at least this test can be parallelized.
  27. id := container.Run(ctx, t, client, container.WithCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done"))
  28. defer func() {
  29. err := client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{Force: true})
  30. assert.NilError(t, err)
  31. }()
  32. inspect, err := client.ContainerInspect(ctx, id)
  33. assert.NilError(t, err)
  34. assert.Assert(t, inspect.State.Running)
  35. assert.NilError(t, d.Kill())
  36. d.Start(t)
  37. inspect, err = client.ContainerInspect(ctx, id)
  38. assert.Check(t, is.Nil(err))
  39. assert.Assert(t, !inspect.State.Running)
  40. }