daemon_linux_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/testutil/daemon"
  12. "golang.org/x/sys/unix"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. "gotest.tools/v3/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. skip.If(t, testEnv.IsRootless)
  31. t.Parallel()
  32. d := daemon.New(t)
  33. d.StartWithBusybox(t, "--iptables=false")
  34. defer d.Stop(t)
  35. c := d.NewClientT(t)
  36. ctx := context.Background()
  37. cID := container.Create(ctx, t, c)
  38. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  39. err := c.ContainerStart(ctx, cID, types.ContainerStartOptions{})
  40. assert.Check(t, err, "error starting test container")
  41. inspect, err := c.ContainerInspect(ctx, cID)
  42. assert.Check(t, err, "error getting inspect data")
  43. ppid := getContainerdShimPid(t, inspect)
  44. err = d.Kill()
  45. assert.Check(t, err, "failed to kill test daemon")
  46. err = unix.Kill(inspect.State.Pid, unix.SIGKILL)
  47. assert.Check(t, err, "failed to kill container process")
  48. err = unix.Kill(ppid, unix.SIGKILL)
  49. assert.Check(t, err, "failed to kill containerd-shim")
  50. d.Start(t, "--iptables=false")
  51. err = c.ContainerStart(ctx, cID, types.ContainerStartOptions{})
  52. assert.Check(t, err, "failed to start test container")
  53. }
  54. func getContainerdShimPid(t *testing.T, c types.ContainerJSON) int {
  55. statB, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", c.State.Pid))
  56. assert.Check(t, err, "error looking up containerd-shim pid")
  57. // ppid is the 4th entry in `/proc/pid/stat`
  58. ppid, err := strconv.Atoi(strings.Fields(string(statB))[3])
  59. assert.Check(t, err, "error converting ppid field to int")
  60. assert.Check(t, ppid != 1, "got unexpected ppid")
  61. return ppid
  62. }
  63. // TestDaemonRestartIpcMode makes sure a container keeps its ipc mode
  64. // (derived from daemon default) even after the daemon is restarted
  65. // with a different default ipc mode.
  66. func TestDaemonRestartIpcMode(t *testing.T) {
  67. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  68. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  69. t.Parallel()
  70. d := daemon.New(t)
  71. d.StartWithBusybox(t, "--iptables=false", "--default-ipc-mode=private")
  72. defer d.Stop(t)
  73. c := d.NewClientT(t)
  74. ctx := context.Background()
  75. // check the container is created with private ipc mode as per daemon default
  76. cID := container.Run(ctx, t, c,
  77. container.WithCmd("top"),
  78. container.WithRestartPolicy("always"),
  79. )
  80. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  81. inspect, err := c.ContainerInspect(ctx, cID)
  82. assert.NilError(t, err)
  83. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  84. // restart the daemon with shareable default ipc mode
  85. d.Restart(t, "--iptables=false", "--default-ipc-mode=shareable")
  86. // check the container is still having private ipc mode
  87. inspect, err = c.ContainerInspect(ctx, cID)
  88. assert.NilError(t, err)
  89. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  90. // check a new container is created with shareable ipc mode as per new daemon default
  91. cID = container.Run(ctx, t, c)
  92. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  93. inspect, err = c.ContainerInspect(ctx, cID)
  94. assert.NilError(t, err)
  95. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "shareable"))
  96. }
  97. // TestDaemonHostGatewayIP verifies that when a magic string "host-gateway" is passed
  98. // to ExtraHosts (--add-host) instead of an IP address, its value is set to
  99. // 1. Daemon config flag value specified by host-gateway-ip or
  100. // 2. IP of the default bridge network
  101. // and is added to the /etc/hosts file
  102. func TestDaemonHostGatewayIP(t *testing.T) {
  103. skip.If(t, testEnv.IsRemoteDaemon)
  104. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  105. skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
  106. t.Parallel()
  107. // Verify the IP in /etc/hosts is same as host-gateway-ip
  108. d := daemon.New(t)
  109. // Verify the IP in /etc/hosts is same as the default bridge's IP
  110. d.StartWithBusybox(t)
  111. c := d.NewClientT(t)
  112. ctx := context.Background()
  113. cID := container.Run(ctx, t, c,
  114. container.WithExtraHost("host.docker.internal:host-gateway"),
  115. )
  116. res, err := container.Exec(ctx, c, cID, []string{"cat", "/etc/hosts"})
  117. assert.NilError(t, err)
  118. assert.Assert(t, is.Len(res.Stderr(), 0))
  119. assert.Equal(t, 0, res.ExitCode)
  120. inspect, err := c.NetworkInspect(ctx, "bridge", types.NetworkInspectOptions{})
  121. assert.NilError(t, err)
  122. assert.Check(t, is.Contains(res.Stdout(), inspect.IPAM.Config[0].Gateway))
  123. c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  124. d.Stop(t)
  125. // Verify the IP in /etc/hosts is same as host-gateway-ip
  126. d.StartWithBusybox(t, "--host-gateway-ip=6.7.8.9")
  127. cID = container.Run(ctx, t, c,
  128. container.WithExtraHost("host.docker.internal:host-gateway"),
  129. )
  130. res, err = container.Exec(ctx, c, cID, []string{"cat", "/etc/hosts"})
  131. assert.NilError(t, err)
  132. assert.Assert(t, is.Len(res.Stderr(), 0))
  133. assert.Equal(t, 0, res.ExitCode)
  134. assert.Check(t, is.Contains(res.Stdout(), "6.7.8.9"))
  135. c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  136. d.Stop(t)
  137. }