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