restart_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/client"
  10. testContainer "github.com/docker/docker/integration/internal/container"
  11. "github.com/docker/docker/testutil/daemon"
  12. "gotest.tools/v3/assert"
  13. "gotest.tools/v3/poll"
  14. "gotest.tools/v3/skip"
  15. )
  16. func TestDaemonRestartKillContainers(t *testing.T) {
  17. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  18. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  19. skip.If(t, testEnv.IsRootless, "rootless mode doesn't support live-restore")
  20. type testCase struct {
  21. desc string
  22. config *container.Config
  23. hostConfig *container.HostConfig
  24. xRunning bool
  25. xRunningLiveRestore bool
  26. xStart bool
  27. xHealthCheck bool
  28. }
  29. for _, tc := range []testCase{
  30. {
  31. desc: "container without restart policy",
  32. config: &container.Config{Image: "busybox", Cmd: []string{"top"}},
  33. xRunningLiveRestore: true,
  34. xStart: true,
  35. },
  36. {
  37. desc: "container with restart=always",
  38. config: &container.Config{Image: "busybox", Cmd: []string{"top"}},
  39. hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
  40. xRunning: true,
  41. xRunningLiveRestore: true,
  42. xStart: true,
  43. },
  44. {
  45. desc: "container with restart=always and with healthcheck",
  46. config: &container.Config{Image: "busybox", Cmd: []string{"top"},
  47. Healthcheck: &container.HealthConfig{
  48. Test: []string{"CMD-SHELL", "sleep 1"},
  49. Interval: time.Second,
  50. },
  51. },
  52. hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
  53. xRunning: true,
  54. xRunningLiveRestore: true,
  55. xStart: true,
  56. xHealthCheck: true,
  57. },
  58. {
  59. desc: "container created should not be restarted",
  60. config: &container.Config{Image: "busybox", Cmd: []string{"top"}},
  61. hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
  62. },
  63. } {
  64. for _, liveRestoreEnabled := range []bool{false, true} {
  65. for fnName, stopDaemon := range map[string]func(*testing.T, *daemon.Daemon){
  66. "kill-daemon": func(t *testing.T, d *daemon.Daemon) {
  67. err := d.Kill()
  68. assert.NilError(t, err)
  69. },
  70. "stop-daemon": func(t *testing.T, d *daemon.Daemon) {
  71. d.Stop(t)
  72. },
  73. } {
  74. t.Run(fmt.Sprintf("live-restore=%v/%s/%s", liveRestoreEnabled, tc.desc, fnName), func(t *testing.T) {
  75. c := tc
  76. liveRestoreEnabled := liveRestoreEnabled
  77. stopDaemon := stopDaemon
  78. t.Parallel()
  79. d := daemon.New(t)
  80. client := d.NewClientT(t)
  81. args := []string{"--iptables=false"}
  82. if liveRestoreEnabled {
  83. args = append(args, "--live-restore")
  84. }
  85. d.StartWithBusybox(t, args...)
  86. defer d.Stop(t)
  87. ctx := context.Background()
  88. resp, err := client.ContainerCreate(ctx, c.config, c.hostConfig, nil, nil, "")
  89. assert.NilError(t, err)
  90. defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})
  91. if c.xStart {
  92. err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  93. assert.NilError(t, err)
  94. }
  95. stopDaemon(t, d)
  96. d.Start(t, args...)
  97. expected := c.xRunning
  98. if liveRestoreEnabled {
  99. expected = c.xRunningLiveRestore
  100. }
  101. var running bool
  102. for i := 0; i < 30; i++ {
  103. inspect, err := client.ContainerInspect(ctx, resp.ID)
  104. assert.NilError(t, err)
  105. running = inspect.State.Running
  106. if running == expected {
  107. break
  108. }
  109. time.Sleep(2 * time.Second)
  110. }
  111. assert.Equal(t, expected, running, "got unexpected running state, expected %v, got: %v", expected, running)
  112. if c.xHealthCheck {
  113. startTime := time.Now()
  114. ctxPoll, cancel := context.WithTimeout(ctx, 30*time.Second)
  115. defer cancel()
  116. poll.WaitOn(t, pollForNewHealthCheck(ctxPoll, client, startTime, resp.ID), poll.WithDelay(100*time.Millisecond))
  117. }
  118. // TODO(cpuguy83): test pause states... this seems to be rather undefined currently
  119. })
  120. }
  121. }
  122. }
  123. }
  124. func pollForNewHealthCheck(ctx context.Context, client *client.Client, startTime time.Time, containerID string) func(log poll.LogT) poll.Result {
  125. return func(log poll.LogT) poll.Result {
  126. inspect, err := client.ContainerInspect(ctx, containerID)
  127. if err != nil {
  128. return poll.Error(err)
  129. }
  130. healthChecksTotal := len(inspect.State.Health.Log)
  131. if healthChecksTotal > 0 {
  132. if inspect.State.Health.Log[healthChecksTotal-1].Start.After(startTime) {
  133. return poll.Success()
  134. }
  135. }
  136. return poll.Continue("waiting for a new container healthcheck")
  137. }
  138. }
  139. // Container started with --rm should be able to be restarted.
  140. // It should be removed only if killed or stopped
  141. func TestContainerWithAutoRemoveCanBeRestarted(t *testing.T) {
  142. defer setupTest(t)()
  143. cli := testEnv.APIClient()
  144. ctx := context.Background()
  145. noWaitTimeout := 0
  146. for _, tc := range []struct {
  147. desc string
  148. doSth func(ctx context.Context, containerID string) error
  149. }{
  150. {
  151. desc: "kill",
  152. doSth: func(ctx context.Context, containerID string) error {
  153. return cli.ContainerKill(ctx, containerID, "SIGKILL")
  154. },
  155. },
  156. {
  157. desc: "stop",
  158. doSth: func(ctx context.Context, containerID string) error {
  159. return cli.ContainerStop(ctx, containerID, container.StopOptions{Timeout: &noWaitTimeout})
  160. },
  161. },
  162. } {
  163. tc := tc
  164. t.Run(tc.desc, func(t *testing.T) {
  165. cID := testContainer.Run(ctx, t, cli,
  166. testContainer.WithName("autoremove-restart-and-"+tc.desc),
  167. testContainer.WithAutoRemove,
  168. )
  169. defer func() {
  170. err := cli.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  171. if t.Failed() && err != nil {
  172. t.Logf("Cleaning up test container failed with error: %v", err)
  173. }
  174. }()
  175. err := cli.ContainerRestart(ctx, cID, container.StopOptions{Timeout: &noWaitTimeout})
  176. assert.NilError(t, err)
  177. inspect, err := cli.ContainerInspect(ctx, cID)
  178. assert.NilError(t, err)
  179. assert.Assert(t, inspect.State.Status != "removing", "Container should not be removing yet")
  180. poll.WaitOn(t, testContainer.IsInState(ctx, cli, cID, "running"))
  181. err = tc.doSth(ctx, cID)
  182. assert.NilError(t, err)
  183. poll.WaitOn(t, testContainer.IsRemoved(ctx, cli, cID))
  184. })
  185. }
  186. }