daemon_linux_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/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(ctx, t, 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(ctx, t, 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(ctx, t, 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. }
  96. // TestDaemonHostGatewayIP verifies that when a magic string "host-gateway" is passed
  97. // to ExtraHosts (--add-host) instead of an IP address, its value is set to
  98. // 1. Daemon config flag value specified by host-gateway-ip or
  99. // 2. IP of the default bridge network
  100. // and is added to the /etc/hosts file
  101. func TestDaemonHostGatewayIP(t *testing.T) {
  102. skip.If(t, testEnv.IsRemoteDaemon)
  103. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  104. t.Parallel()
  105. // Verify the IP in /etc/hosts is same as host-gateway-ip
  106. d := daemon.New(t)
  107. // Verify the IP in /etc/hosts is same as the default bridge's IP
  108. d.StartWithBusybox(t)
  109. c := d.NewClientT(t)
  110. ctx := context.Background()
  111. cID := container.Run(ctx, t, c,
  112. container.WithExtraHost("host.docker.internal:host-gateway"),
  113. )
  114. res, err := container.Exec(ctx, c, cID, []string{"cat", "/etc/hosts"})
  115. assert.NilError(t, err)
  116. assert.Assert(t, is.Len(res.Stderr(), 0))
  117. assert.Equal(t, 0, res.ExitCode)
  118. inspect, err := c.NetworkInspect(ctx, "bridge", types.NetworkInspectOptions{})
  119. assert.NilError(t, err)
  120. assert.Check(t, is.Contains(res.Stdout(), inspect.IPAM.Config[0].Gateway))
  121. c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  122. d.Stop(t)
  123. // Verify the IP in /etc/hosts is same as host-gateway-ip
  124. d.StartWithBusybox(t, "--host-gateway-ip=6.7.8.9")
  125. cID = container.Run(ctx, t, c,
  126. container.WithExtraHost("host.docker.internal:host-gateway"),
  127. )
  128. res, err = container.Exec(ctx, c, cID, []string{"cat", "/etc/hosts"})
  129. assert.NilError(t, err)
  130. assert.Assert(t, is.Len(res.Stderr(), 0))
  131. assert.Equal(t, 0, res.ExitCode)
  132. assert.Check(t, is.Contains(res.Stdout(), "6.7.8.9"))
  133. c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  134. d.Stop(t)
  135. }