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