restart_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/internal/test/daemon"
  10. "gotest.tools/assert"
  11. "gotest.tools/skip"
  12. )
  13. func TestDaemonRestartKillContainers(t *testing.T) {
  14. skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
  15. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  16. type testCase struct {
  17. desc string
  18. config *container.Config
  19. hostConfig *container.HostConfig
  20. xRunning bool
  21. xRunningLiveRestore bool
  22. xStart bool
  23. }
  24. for _, tc := range []testCase{
  25. {
  26. desc: "container without restart policy",
  27. config: &container.Config{Image: "busybox", Cmd: []string{"top"}},
  28. xRunningLiveRestore: true,
  29. xStart: true,
  30. },
  31. {
  32. desc: "container with restart=always",
  33. config: &container.Config{Image: "busybox", Cmd: []string{"top"}},
  34. hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
  35. xRunning: true,
  36. xRunningLiveRestore: true,
  37. xStart: true,
  38. },
  39. {
  40. desc: "container created should not be restarted",
  41. config: &container.Config{Image: "busybox", Cmd: []string{"top"}},
  42. hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
  43. },
  44. } {
  45. for _, liveRestoreEnabled := range []bool{false, true} {
  46. for fnName, stopDaemon := range map[string]func(*testing.T, *daemon.Daemon){
  47. "kill-daemon": func(t *testing.T, d *daemon.Daemon) {
  48. err := d.Kill()
  49. assert.NilError(t, err)
  50. },
  51. "stop-daemon": func(t *testing.T, d *daemon.Daemon) {
  52. d.Stop(t)
  53. },
  54. } {
  55. t.Run(fmt.Sprintf("live-restore=%v/%s/%s", liveRestoreEnabled, tc.desc, fnName), func(t *testing.T) {
  56. c := tc
  57. liveRestoreEnabled := liveRestoreEnabled
  58. stopDaemon := stopDaemon
  59. t.Parallel()
  60. d := daemon.New(t)
  61. client := d.NewClientT(t)
  62. args := []string{"--iptables=false"}
  63. if liveRestoreEnabled {
  64. args = append(args, "--live-restore")
  65. }
  66. d.StartWithBusybox(t, args...)
  67. defer d.Stop(t)
  68. ctx := context.Background()
  69. resp, err := client.ContainerCreate(ctx, c.config, c.hostConfig, nil, "")
  70. assert.NilError(t, err)
  71. defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})
  72. if c.xStart {
  73. err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  74. assert.NilError(t, err)
  75. }
  76. stopDaemon(t, d)
  77. d.Start(t, args...)
  78. expected := c.xRunning
  79. if liveRestoreEnabled {
  80. expected = c.xRunningLiveRestore
  81. }
  82. var running bool
  83. for i := 0; i < 30; i++ {
  84. inspect, err := client.ContainerInspect(ctx, resp.ID)
  85. assert.NilError(t, err)
  86. running = inspect.State.Running
  87. if running == expected {
  88. break
  89. }
  90. time.Sleep(2 * time.Second)
  91. }
  92. assert.Equal(t, expected, running, "got unexpected running state, expected %v, got: %v", expected, running)
  93. // TODO(cpuguy83): test pause states... this seems to be rather undefined currently
  94. })
  95. }
  96. }
  97. }
  98. }