2018-08-02 20:24:51 +00:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-02-16 10:36:37 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-08-02 20:24:51 +00:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-08-02 20:24:51 +00: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) {
|
2023-06-14 09:46:00 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
apiClient := testEnv.APIClient()
|
2018-08-02 20:24:51 +00:00
|
|
|
|
|
|
|
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()
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.StartSpan(ctx, t)
|
2023-08-11 11:45:45 +00:00
|
|
|
id := container.Run(ctx, t, apiClient, testCmd)
|
2018-08-02 20:24:51 +00:00
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
err := apiClient.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
|
2018-08-02 20:24:51 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
poll.WaitOn(t, container.IsStopped(ctx, apiClient, id),
|
2018-08-02 20:24:51 +00:00
|
|
|
poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
inspect, err := apiClient.ContainerInspect(ctx, id)
|
2018-08-02 20:24:51 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|