Transfer error to ErrNotFound when kill a not exist container, also add

test case.

Signed-off-by: payall4u <404977848@qq.com>

Add integration test case

Signed-off-by: payall4u <404977848@qq.com>
This commit is contained in:
payall4u
2020-04-27 10:53:20 +08:00
parent 83084c9328
commit b437938d2f
3 changed files with 81 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"runtime"
"strings"
"syscall"
@@ -36,10 +37,12 @@ import (
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/platforms"
_ "github.com/containerd/containerd/runtime"
"github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/typeurl"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/go-runc"
gogotypes "github.com/gogo/protobuf/types"
)
@@ -629,6 +632,78 @@ func TestContainerKill(t *testing.T) {
}
}
func TestKillContainerDeletedByRunc(t *testing.T) {
t.Parallel()
// We skip this case when runtime is crun.
// More information in https://github.com/containerd/containerd/pull/4214#discussion_r422769497
if os.Getenv("RUNC_FLAVOR") == "crun" {
t.Skip("skip it when using crun")
}
client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
var (
image Image
ctx, cancel = testContext(t)
id = t.Name()
runcRoot = "/tmp/runc-test"
)
defer cancel()
image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}
container, err := client.NewContainer(ctx, id,
WithNewSnapshot(id, image),
WithNewSpec(oci.WithImageConfig(image), withProcessArgs("sleep", "10")),
WithRuntime(client.runtime, &options.Options{Root: runcRoot}))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx)
task, err := container.NewTask(ctx, empty())
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)
statusC, err := task.Wait(ctx)
if err != nil {
t.Fatal(err)
}
if err := task.Start(ctx); err != nil {
t.Fatal(err)
}
rcmd := &runc.Runc{
Root: path.Join(runcRoot, testNamespace),
}
if err := rcmd.Delete(ctx, id, &runc.DeleteOpts{Force: true}); err != nil {
t.Fatal(err)
}
err = task.Kill(ctx, syscall.SIGKILL)
if err == nil {
t.Fatal("kill should return NotFound error")
} else if !errdefs.IsNotFound(err) {
t.Errorf("expected error %q but received %q", errdefs.ErrNotFound, err)
}
select {
case <-statusC:
case <-time.After(2 * time.Second):
t.Errorf("unexpected timeout when try to get exited container's status")
}
}
func TestContainerNoBinaryExists(t *testing.T) {
t.Parallel()