gopkg.in/docker/docker.v20@v20.10.27/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/testutil/daemon"
    12  	"gotest.tools/v3/assert"
    13  	"gotest.tools/v3/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  	skip.If(t, testEnv.IsRootless, "rootless mode doesn't support live-restore")
    20  	type testCase struct {
    21  		desc       string
    22  		config     *container.Config
    23  		hostConfig *container.HostConfig
    24  
    25  		xRunning            bool
    26  		xRunningLiveRestore bool
    27  		xStart              bool
    28  	}
    29  
    30  	for _, tc := range []testCase{
    31  		{
    32  			desc:                "container without restart policy",
    33  			config:              &container.Config{Image: "busybox", Cmd: []string{"top"}},
    34  			xRunningLiveRestore: true,
    35  			xStart:              true,
    36  		},
    37  		{
    38  			desc:                "container with restart=always",
    39  			config:              &container.Config{Image: "busybox", Cmd: []string{"top"}},
    40  			hostConfig:          &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
    41  			xRunning:            true,
    42  			xRunningLiveRestore: true,
    43  			xStart:              true,
    44  		},
    45  		{
    46  			desc:       "container created should not be restarted",
    47  			config:     &container.Config{Image: "busybox", Cmd: []string{"top"}},
    48  			hostConfig: &container.HostConfig{RestartPolicy: container.RestartPolicy{Name: "always"}},
    49  		},
    50  	} {
    51  		for _, liveRestoreEnabled := range []bool{false, true} {
    52  			for fnName, stopDaemon := range map[string]func(*testing.T, *daemon.Daemon){
    53  				"kill-daemon": func(t *testing.T, d *daemon.Daemon) {
    54  					err := d.Kill()
    55  					assert.NilError(t, err)
    56  				},
    57  				"stop-daemon": func(t *testing.T, d *daemon.Daemon) {
    58  					d.Stop(t)
    59  				},
    60  			} {
    61  				tc := tc
    62  				liveRestoreEnabled := liveRestoreEnabled
    63  				stopDaemon := stopDaemon
    64  				t.Run(fmt.Sprintf("live-restore=%v/%s/%s", liveRestoreEnabled, tc.desc, fnName), func(t *testing.T) {
    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, tc.config, tc.hostConfig, nil, nil, "")
    80  					assert.NilError(t, err)
    81  					defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})
    82  
    83  					if tc.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 := tc.xRunning
    92  					if liveRestoreEnabled {
    93  						expected = tc.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  }