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