2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-01-02 23:55:33 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-04-16 19:58:42 -04:00
|
|
|
"strconv"
|
2018-01-02 23:55:33 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-02-10 18:01:37 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/icmd"
|
|
|
|
"gotest.tools/poll"
|
|
|
|
"gotest.tools/skip"
|
2018-01-02 23:55:33 -05:00
|
|
|
)
|
|
|
|
|
2018-01-29 18:36:45 -05:00
|
|
|
func TestStopContainerWithRestartPolicyAlways(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-03-21 17:47:49 -04:00
|
|
|
names := []string{"verifyRestart1-" + t.Name(), "verifyRestart2-" + t.Name()}
|
2018-01-29 18:36:45 -05:00
|
|
|
for _, name := range names {
|
2018-02-10 18:01:37 -05:00
|
|
|
container.Run(t, ctx, client, container.WithName(name), container.WithCmd("false"), func(c *container.TestContainerConfig) {
|
|
|
|
c.HostConfig.RestartPolicy.Name = "always"
|
|
|
|
})
|
2018-01-29 18:36:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, name, "running", "restarting"), poll.WithDelay(100*time.Millisecond))
|
2018-01-29 18:36:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
err := client.ContainerStop(ctx, name, nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-29 18:36:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsStopped(ctx, client, name), poll.WithDelay(100*time.Millisecond))
|
2018-01-29 18:36:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 19:58:42 -04:00
|
|
|
// TestStopContainerWithTimeout checks that ContainerStop with
|
|
|
|
// a timeout works as documented, i.e. in case of negative timeout
|
|
|
|
// waiting is not limited (issue #35311).
|
|
|
|
func TestStopContainerWithTimeout(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
|
|
|
|
testData := []struct {
|
|
|
|
doc string
|
|
|
|
timeout int
|
|
|
|
expectedExitCode int
|
|
|
|
}{
|
|
|
|
// In case container is forcefully killed, 137 is returned,
|
|
|
|
// otherwise the exit code from the above script
|
|
|
|
{
|
|
|
|
"zero timeout: expect forceful container kill",
|
|
|
|
0, 137,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"too small timeout: expect forceful container kill",
|
|
|
|
1, 137,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"big enough timeout: expect graceful container stop",
|
|
|
|
3, 42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unlimited timeout: expect graceful container stop",
|
|
|
|
-1, 42,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
|
|
|
d := d
|
|
|
|
t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
id := container.Run(t, ctx, client, testCmd)
|
|
|
|
|
|
|
|
timeout := time.Duration(d.timeout) * time.Second
|
|
|
|
err := client.ContainerStop(ctx, id, &timeout)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
poll.WaitOn(t, container.IsStopped(ctx, client, id),
|
|
|
|
poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, id)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 23:55:33 -05:00
|
|
|
func TestDeleteDevicemapper(t *testing.T) {
|
2018-05-04 17:15:00 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.Driver != "devicemapper")
|
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
|
2018-01-02 23:55:33 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-03-21 17:47:49 -04:00
|
|
|
id := container.Run(t, ctx, client, container.WithName("foo-"+t.Name()), container.WithCmd("echo"))
|
2018-01-02 23:55:33 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsStopped(ctx, client, id), poll.WithDelay(100*time.Millisecond))
|
2018-01-02 23:55:33 -05:00
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
inspect, err := client.ContainerInspect(ctx, id)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-02 23:55:33 -05:00
|
|
|
|
|
|
|
deviceID := inspect.GraphDriver.Data["DeviceId"]
|
|
|
|
|
|
|
|
// Find pool name from device name
|
|
|
|
deviceName := inspect.GraphDriver.Data["DeviceName"]
|
|
|
|
devicePrefix := deviceName[:strings.LastIndex(deviceName, "-")]
|
|
|
|
devicePool := fmt.Sprintf("/dev/mapper/%s-pool", devicePrefix)
|
|
|
|
|
|
|
|
result := icmd.RunCommand("dmsetup", "message", devicePool, "0", fmt.Sprintf("delete %s", deviceID))
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-02 23:55:33 -05:00
|
|
|
}
|