2018-03-01 17:51:11 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-02-28 16:35:56 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-10 10:29:48 -04:00
|
|
|
"github.com/docker/docker/internal/test/daemon"
|
2018-02-08 12:57:38 -05:00
|
|
|
"golang.org/x/sys/unix"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/skip"
|
2018-02-08 12:57:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is a regression test for #36145
|
|
|
|
// It ensures that a container can be started when the daemon was improperly
|
|
|
|
// shutdown when the daemon is brought back up.
|
|
|
|
//
|
|
|
|
// The regression is due to improper error handling preventing a container from
|
|
|
|
// being restored and as such have the resources cleaned up.
|
|
|
|
//
|
|
|
|
// To test this, we need to kill dockerd, then kill both the containerd-shim and
|
|
|
|
// the container process, then start dockerd back up and attempt to start the
|
|
|
|
// container again.
|
|
|
|
func TestContainerStartOnDaemonRestart(t *testing.T) {
|
2018-04-25 05:03:43 -04:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
skip.If(t, testEnv.IsRemoteDaemon(), "cannot start daemon on remote test run")
|
2018-02-08 12:57:38 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-04-10 10:29:48 -04:00
|
|
|
d := daemon.New(t)
|
2018-02-08 12:57:38 -05:00
|
|
|
d.StartWithBusybox(t, "--iptables=false")
|
|
|
|
defer d.Stop(t)
|
|
|
|
|
|
|
|
client, err := d.NewClient()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "error creating client")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2018-02-28 16:35:56 -05:00
|
|
|
|
|
|
|
cID := container.Create(t, ctx, client)
|
|
|
|
defer client.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
|
|
|
|
|
|
|
|
err = client.ContainerStart(ctx, cID, types.ContainerStartOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "error starting test container")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
2018-02-28 16:35:56 -05:00
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "error getting inspect data")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
ppid := getContainerdShimPid(t, inspect)
|
|
|
|
|
|
|
|
err = d.Kill()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to kill test daemon")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
err = unix.Kill(inspect.State.Pid, unix.SIGKILL)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to kill container process")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
err = unix.Kill(ppid, unix.SIGKILL)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to kill containerd-shim")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
d.Start(t, "--iptables=false")
|
|
|
|
|
2018-02-28 16:35:56 -05:00
|
|
|
err = client.ContainerStart(ctx, cID, types.ContainerStartOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "failed to start test container")
|
2018-02-08 12:57:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerdShimPid(t *testing.T, c types.ContainerJSON) int {
|
|
|
|
statB, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", c.State.Pid))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "error looking up containerd-shim pid")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
|
|
|
// ppid is the 4th entry in `/proc/pid/stat`
|
|
|
|
ppid, err := strconv.Atoi(strings.Fields(string(statB))[3])
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err, "error converting ppid field to int")
|
2018-02-08 12:57:38 -05:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, ppid != 1, "got unexpected ppid")
|
2018-02-08 12:57:38 -05:00
|
|
|
return ppid
|
|
|
|
}
|