restart_test.go 2.8 KB

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