github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration/container/daemon_linux_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/integration/internal/container"
    13  	"github.com/docker/docker/internal/test/daemon"
    14  	"github.com/gotestyourself/gotestyourself/assert"
    15  	"github.com/gotestyourself/gotestyourself/skip"
    16  	"golang.org/x/sys/unix"
    17  )
    18  
    19  // This is a regression test for #36145
    20  // It ensures that a container can be started when the daemon was improperly
    21  // shutdown when the daemon is brought back up.
    22  //
    23  // The regression is due to improper error handling preventing a container from
    24  // being restored and as such have the resources cleaned up.
    25  //
    26  // To test this, we need to kill dockerd, then kill both the containerd-shim and
    27  // the container process, then start dockerd back up and attempt to start the
    28  // container again.
    29  func TestContainerStartOnDaemonRestart(t *testing.T) {
    30  	skip.If(t, testEnv.IsRemoteDaemon(), "cannot start daemon on remote test run")
    31  	t.Parallel()
    32  
    33  	d := daemon.New(t)
    34  	d.StartWithBusybox(t, "--iptables=false")
    35  	defer d.Stop(t)
    36  
    37  	client, err := d.NewClient()
    38  	assert.Check(t, err, "error creating client")
    39  
    40  	ctx := context.Background()
    41  
    42  	cID := container.Create(t, ctx, client)
    43  	defer client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
    44  
    45  	err = client.ContainerStart(ctx, cID, types.ContainerStartOptions{})
    46  	assert.Check(t, err, "error starting test container")
    47  
    48  	inspect, err := client.ContainerInspect(ctx, cID)
    49  	assert.Check(t, err, "error getting inspect data")
    50  
    51  	ppid := getContainerdShimPid(t, inspect)
    52  
    53  	err = d.Kill()
    54  	assert.Check(t, err, "failed to kill test daemon")
    55  
    56  	err = unix.Kill(inspect.State.Pid, unix.SIGKILL)
    57  	assert.Check(t, err, "failed to kill container process")
    58  
    59  	err = unix.Kill(ppid, unix.SIGKILL)
    60  	assert.Check(t, err, "failed to kill containerd-shim")
    61  
    62  	d.Start(t, "--iptables=false")
    63  
    64  	err = client.ContainerStart(ctx, cID, types.ContainerStartOptions{})
    65  	assert.Check(t, err, "failed to start test container")
    66  }
    67  
    68  func getContainerdShimPid(t *testing.T, c types.ContainerJSON) int {
    69  	statB, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", c.State.Pid))
    70  	assert.Check(t, err, "error looking up containerd-shim pid")
    71  
    72  	// ppid is the 4th entry in `/proc/pid/stat`
    73  	ppid, err := strconv.Atoi(strings.Fields(string(statB))[3])
    74  	assert.Check(t, err, "error converting ppid field to int")
    75  
    76  	assert.Check(t, ppid != 1, "got unexpected ppid")
    77  	return ppid
    78  }