daemon_linux_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/integration/internal/container"
  11. "github.com/docker/docker/internal/test/daemon"
  12. "golang.org/x/sys/unix"
  13. "gotest.tools/assert"
  14. is "gotest.tools/assert/cmp"
  15. "gotest.tools/skip"
  16. )
  17. // This is a regression test for #36145
  18. // It ensures that a container can be started when the daemon was improperly
  19. // shutdown when the daemon is brought back up.
  20. //
  21. // The regression is due to improper error handling preventing a container from
  22. // being restored and as such have the resources cleaned up.
  23. //
  24. // To test this, we need to kill dockerd, then kill both the containerd-shim and
  25. // the container process, then start dockerd back up and attempt to start the
  26. // container again.
  27. func TestContainerStartOnDaemonRestart(t *testing.T) {
  28. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  29. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  30. t.Parallel()
  31. d := daemon.New(t)
  32. d.StartWithBusybox(t, "--iptables=false")
  33. defer d.Stop(t)
  34. c := d.NewClientT(t)
  35. ctx := context.Background()
  36. cID := container.Create(t, ctx, c)
  37. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  38. err := c.ContainerStart(ctx, cID, types.ContainerStartOptions{})
  39. assert.Check(t, err, "error starting test container")
  40. inspect, err := c.ContainerInspect(ctx, cID)
  41. assert.Check(t, err, "error getting inspect data")
  42. ppid := getContainerdShimPid(t, inspect)
  43. err = d.Kill()
  44. assert.Check(t, err, "failed to kill test daemon")
  45. err = unix.Kill(inspect.State.Pid, unix.SIGKILL)
  46. assert.Check(t, err, "failed to kill container process")
  47. err = unix.Kill(ppid, unix.SIGKILL)
  48. assert.Check(t, err, "failed to kill containerd-shim")
  49. d.Start(t, "--iptables=false")
  50. err = c.ContainerStart(ctx, cID, types.ContainerStartOptions{})
  51. assert.Check(t, err, "failed to start test container")
  52. }
  53. func getContainerdShimPid(t *testing.T, c types.ContainerJSON) int {
  54. statB, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", c.State.Pid))
  55. assert.Check(t, err, "error looking up containerd-shim pid")
  56. // ppid is the 4th entry in `/proc/pid/stat`
  57. ppid, err := strconv.Atoi(strings.Fields(string(statB))[3])
  58. assert.Check(t, err, "error converting ppid field to int")
  59. assert.Check(t, ppid != 1, "got unexpected ppid")
  60. return ppid
  61. }
  62. // TestDaemonRestartIpcMode makes sure a container keeps its ipc mode
  63. // (derived from daemon default) even after the daemon is restarted
  64. // with a different default ipc mode.
  65. func TestDaemonRestartIpcMode(t *testing.T) {
  66. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  67. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  68. t.Parallel()
  69. d := daemon.New(t)
  70. d.StartWithBusybox(t, "--iptables=false", "--default-ipc-mode=private")
  71. defer d.Stop(t)
  72. c := d.NewClientT(t)
  73. ctx := context.Background()
  74. // check the container is created with private ipc mode as per daemon default
  75. cID := container.Run(t, ctx, c,
  76. container.WithCmd("top"),
  77. container.WithRestartPolicy("always"),
  78. )
  79. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  80. inspect, err := c.ContainerInspect(ctx, cID)
  81. assert.NilError(t, err)
  82. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  83. // restart the daemon with shareable default ipc mode
  84. d.Restart(t, "--iptables=false", "--default-ipc-mode=shareable")
  85. // check the container is still having private ipc mode
  86. inspect, err = c.ContainerInspect(ctx, cID)
  87. assert.NilError(t, err)
  88. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  89. // check a new container is created with shareable ipc mode as per new daemon default
  90. cID = container.Run(t, ctx, c)
  91. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  92. inspect, err = c.ContainerInspect(ctx, cID)
  93. assert.NilError(t, err)
  94. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "shareable"))
  95. }