github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration/container/restart_test.go (about)

     1  package container // import "github.com/docker/docker/integration/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/internal/test/daemon"
    12  	"gotest.tools/assert"
    13  	"gotest.tools/skip"
    14  )
    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  	type testCase struct {
    20  		desc       string
    21  		config     *container.Config
    22  		hostConfig *container.HostConfig
    23  
    24  		xRunning            bool
    25  		xRunningLiveRestore bool
    26  		xStart              bool
    27  	}
    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 created should not be restarted",
    46  			config:     &container.Config{Image: "busybox", Cmd: []string{"top"}},
    47  			hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
    48  		},
    49  	} {
    50  		for _, liveRestoreEnabled := range []bool{false, true} {
    51  			for fnName, stopDaemon := range map[string]func(*testing.T, *daemon.Daemon){
    52  				"kill-daemon": func(t *testing.T, d *daemon.Daemon) {
    53  					err := d.Kill()
    54  					assert.NilError(t, err)
    55  				},
    56  				"stop-daemon": func(t *testing.T, d *daemon.Daemon) {
    57  					d.Stop(t)
    58  				},
    59  			} {
    60  				t.Run(fmt.Sprintf("live-restore=%v/%s/%s", liveRestoreEnabled, tc.desc, fnName), func(t *testing.T) {
    61  					c := tc
    62  					liveRestoreEnabled := liveRestoreEnabled
    63  					stopDaemon := stopDaemon
    64  
    65  					t.Parallel()
    66  
    67  					d := daemon.New(t)
    68  					client := d.NewClientT(t)
    69  
    70  					args := []string{"--iptables=false"}
    71  					if liveRestoreEnabled {
    72  						args = append(args, "--live-restore")
    73  					}
    74  
    75  					d.StartWithBusybox(t, args...)
    76  					defer d.Stop(t)
    77  					ctx := context.Background()
    78  
    79  					resp, err := client.ContainerCreate(ctx, c.config, c.hostConfig, nil, "")
    80  					assert.NilError(t, err)
    81  					defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})
    82  
    83  					if c.xStart {
    84  						err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
    85  						assert.NilError(t, err)
    86  					}
    87  
    88  					stopDaemon(t, d)
    89  					d.Start(t, args...)
    90  
    91  					expected := c.xRunning
    92  					if liveRestoreEnabled {
    93  						expected = c.xRunningLiveRestore
    94  					}
    95  
    96  					var running bool
    97  					for i := 0; i < 30; i++ {
    98  						inspect, err := client.ContainerInspect(ctx, resp.ID)
    99  						assert.NilError(t, err)
   100  
   101  						running = inspect.State.Running
   102  						if running == expected {
   103  							break
   104  						}
   105  						time.Sleep(2 * time.Second)
   106  
   107  					}
   108  					assert.Equal(t, expected, running, "got unexpected running state, expected %v, got: %v", expected, running)
   109  					// TODO(cpuguy83): test pause states... this seems to be rather undefined currently
   110  				})
   111  			}
   112  		}
   113  	}
   114  }