2018-08-02 16:24:51 -04:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-02-16 05:36:37 -05:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-08-02 16:24:51 -04:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-08-02 16:24:51 -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) {
|
|
|
|
skip.If(t, testEnv.OSType == "windows")
|
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-08-02 16:24:51 -04:00
|
|
|
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",
|
|
|
|
1, 0x40010004,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"too small timeout: expect forceful container kill",
|
|
|
|
2, 0x40010004,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"big enough timeout: expect graceful container stop",
|
|
|
|
120, 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()
|
2019-06-06 07:15:31 -04:00
|
|
|
id := container.Run(ctx, t, client, testCmd)
|
2018-08-02 16:24:51 -04:00
|
|
|
|
2022-02-16 05:36:37 -05:00
|
|
|
err := client.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
|
2018-08-02 16:24:51 -04:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|