github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/restart_test.go (about)

     1  package container // import "github.com/demonoid81/moby/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/demonoid81/moby/api/types"
    10  	"github.com/demonoid81/moby/api/types/container"
    11  	"github.com/demonoid81/moby/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  				t.Run(fmt.Sprintf("live-restore=%v/%s/%s", liveRestoreEnabled, tc.desc, fnName), func(t *testing.T) {
    62  					c := tc
    63  					liveRestoreEnabled := liveRestoreEnabled
    64  					stopDaemon := stopDaemon
    65  
    66  					t.Parallel()
    67  
    68  					d := daemon.New(t)
    69  					client := d.NewClientT(t)
    70  
    71  					args := []string{"--iptables=false"}
    72  					if liveRestoreEnabled {
    73  						args = append(args, "--live-restore")
    74  					}
    75  
    76  					d.StartWithBusybox(t, args...)
    77  					defer d.Stop(t)
    78  					ctx := context.Background()
    79  
    80  					resp, err := client.ContainerCreate(ctx, c.config, c.hostConfig, nil, "")
    81  					assert.NilError(t, err)
    82  					defer client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})
    83  
    84  					if c.xStart {
    85  						err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
    86  						assert.NilError(t, err)
    87  					}
    88  
    89  					stopDaemon(t, d)
    90  					d.Start(t, args...)
    91  
    92  					expected := c.xRunning
    93  					if liveRestoreEnabled {
    94  						expected = c.xRunningLiveRestore
    95  					}
    96  
    97  					var running bool
    98  					for i := 0; i < 30; i++ {
    99  						inspect, err := client.ContainerInspect(ctx, resp.ID)
   100  						assert.NilError(t, err)
   101  
   102  						running = inspect.State.Running
   103  						if running == expected {
   104  							break
   105  						}
   106  						time.Sleep(2 * time.Second)
   107  
   108  					}
   109  					assert.Equal(t, expected, running, "got unexpected running state, expected %v, got: %v", expected, running)
   110  					// TODO(cpuguy83): test pause states... this seems to be rather undefined currently
   111  				})
   112  			}
   113  		}
   114  	}
   115  }