|
| 1 | +/* |
| 2 | +Copyright 2019 The containerd Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" |
| 27 | +) |
| 28 | + |
| 29 | +func TestContainerStopCancellation(t *testing.T) { |
| 30 | + t.Log("Create a pod sandbox") |
| 31 | + sbConfig := PodSandboxConfig("sandbox", "cancel-container-stop") |
| 32 | + sb, err := runtimeService.RunPodSandbox(sbConfig) |
| 33 | + require.NoError(t, err) |
| 34 | + defer func() { |
| 35 | + assert.NoError(t, runtimeService.StopPodSandbox(sb)) |
| 36 | + assert.NoError(t, runtimeService.RemovePodSandbox(sb)) |
| 37 | + }() |
| 38 | + |
| 39 | + const ( |
| 40 | + testImage = "busybox" |
| 41 | + containerName = "test-container" |
| 42 | + ) |
| 43 | + t.Logf("Pull test image %q", testImage) |
| 44 | + img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil) |
| 45 | + require.NoError(t, err) |
| 46 | + defer func() { |
| 47 | + assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img})) |
| 48 | + }() |
| 49 | + |
| 50 | + t.Log("Create a container which traps sigterm") |
| 51 | + cnConfig := ContainerConfig( |
| 52 | + containerName, |
| 53 | + testImage, |
| 54 | + WithCommand("sh", "-c", `trap "echo ignore sigterm" TERM; sleep 1000`), |
| 55 | + ) |
| 56 | + cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + t.Log("Start the container") |
| 60 | + require.NoError(t, runtimeService.StartContainer(cn)) |
| 61 | + |
| 62 | + t.Log("Stop the container with 3s timeout, but 1s context timeout") |
| 63 | + // Note that with container pid namespace, the sleep process |
| 64 | + // is pid 1, and SIGTERM sent by `StopContainer` will be ignored. |
| 65 | + rawClient, err := RawRuntimeClient() |
| 66 | + require.NoError(t, err) |
| 67 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 68 | + defer cancel() |
| 69 | + _, err = rawClient.StopContainer(ctx, &runtime.StopContainerRequest{ |
| 70 | + ContainerId: cn, |
| 71 | + Timeout: 3, |
| 72 | + }) |
| 73 | + assert.Error(t, err) |
| 74 | + |
| 75 | + t.Log("The container should still be running even after 5 seconds") |
| 76 | + assert.NoError(t, Consistently(func() (bool, error) { |
| 77 | + s, err := runtimeService.ContainerStatus(cn) |
| 78 | + if err != nil { |
| 79 | + return false, err |
| 80 | + } |
| 81 | + return s.GetState() == runtime.ContainerState_CONTAINER_RUNNING, nil |
| 82 | + }, 100*time.Millisecond, 5*time.Second)) |
| 83 | + |
| 84 | + t.Log("Stop the container with 1s timeout, without shorter context timeout") |
| 85 | + assert.NoError(t, runtimeService.StopContainer(cn, 1)) |
| 86 | + |
| 87 | + t.Log("The container state should be exited") |
| 88 | + s, err := runtimeService.ContainerStatus(cn) |
| 89 | + require.NoError(t, err) |
| 90 | + assert.Equal(t, s.GetState(), runtime.ContainerState_CONTAINER_EXITED) |
| 91 | +} |
0 commit comments