This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not SIGKILL container if container stop is cancelled.
Signed-off-by: Lantao Liu <lantaol@google.com>
- Loading branch information
1 parent
3c81b30
commit b2568d2
Showing
4 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2019 The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" | ||
) | ||
|
||
func TestContainerStopCancellation(t *testing.T) { | ||
t.Log("Create a pod sandbox") | ||
sbConfig := PodSandboxConfig("sandbox", "cancel-container-stop") | ||
sb, err := runtimeService.RunPodSandbox(sbConfig) | ||
require.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, runtimeService.StopPodSandbox(sb)) | ||
assert.NoError(t, runtimeService.RemovePodSandbox(sb)) | ||
}() | ||
|
||
const ( | ||
testImage = "busybox" | ||
containerName = "test-container" | ||
) | ||
t.Logf("Pull test image %q", testImage) | ||
img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil) | ||
require.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img})) | ||
}() | ||
|
||
t.Log("Create a container which traps sigterm") | ||
cnConfig := ContainerConfig( | ||
containerName, | ||
testImage, | ||
WithCommand("sh", "-c", `trap "echo ignore sigterm" TERM; sleep 1000`), | ||
) | ||
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig) | ||
require.NoError(t, err) | ||
|
||
t.Log("Start the container") | ||
require.NoError(t, runtimeService.StartContainer(cn)) | ||
|
||
t.Log("Stop the container with 3s timeout, but 1s context timeout") | ||
// Note that with container pid namespace, the sleep process | ||
// is pid 1, and SIGTERM sent by `StopContainer` will be ignored. | ||
rawClient, err := RawRuntimeClient() | ||
require.NoError(t, err) | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
_, err = rawClient.StopContainer(ctx, &runtime.StopContainerRequest{ | ||
ContainerId: cn, | ||
Timeout: 3, | ||
}) | ||
assert.Error(t, err) | ||
|
||
t.Log("The container should still be running even after 5 seconds") | ||
assert.NoError(t, Consistently(func() (bool, error) { | ||
s, err := runtimeService.ContainerStatus(cn) | ||
if err != nil { | ||
return false, err | ||
} | ||
return s.GetState() == runtime.ContainerState_CONTAINER_RUNNING, nil | ||
}, 100*time.Millisecond, 5*time.Second)) | ||
|
||
t.Log("Stop the container with 1s timeout, without shorter context timeout") | ||
assert.NoError(t, runtimeService.StopContainer(cn, 1)) | ||
|
||
t.Log("The container state should be exited") | ||
s, err := runtimeService.ContainerStatus(cn) | ||
require.NoError(t, err) | ||
assert.Equal(t, s.GetState(), runtime.ContainerState_CONTAINER_EXITED) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters