task: allow checkpoint on pause state

task.Checkpoint should check status of container before handle
checkpoint. If the container has been paused, task.Checkpoint should
handle checkpoint and leave it paused.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
Signed-off-by: Baijia <baijia.wr@antfin.com>
This commit is contained in:
Wei Fu 2021-03-24 13:59:54 +08:00
parent 31a0f92df1
commit 72b7f4bab1
2 changed files with 92 additions and 3 deletions

View File

@ -533,3 +533,83 @@ func TestCheckpointRestoreWithImagePath(t *testing.T) {
<-statusC
ntask.Delete(ctx)
}
func TestCheckpointOnPauseStatus(t *testing.T) {
if !supportsCriu {
t.Skip("system does not have criu installed")
}
client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
if client.Runtime() == plugin.RuntimeLinuxV1 {
t.Skip()
}
var (
ctx, cancel = testContext(t)
id = t.Name()
)
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), oci.WithProcessArgs("sleep", "10")))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)
task, err := container.NewTask(ctx, empty())
if err != nil {
t.Fatal(err)
}
defer func() {
task.Resume(ctx)
task.Delete(ctx)
}()
statusC, err := task.Wait(ctx)
if err != nil {
t.Fatal(err)
}
if err := task.Start(ctx); err != nil {
t.Fatal(err)
}
if err := task.Pause(ctx); err != nil {
t.Fatal(err)
}
_, err = container.Checkpoint(ctx, testCheckpointName+"on-pause", []CheckpointOpts{
WithCheckpointRuntime,
WithCheckpointRW,
WithCheckpointTask,
}...)
if err != nil {
t.Fatal(err)
}
status, err := task.Status(ctx)
if err != nil {
t.Fatal(err)
}
if status.Status != Paused {
t.Fatalf("expected paused state, but got %s", status.Status)
}
if err := task.Resume(ctx); err != nil {
t.Fatal(err)
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
t.Fatal(err)
}
<-statusC
}

15
task.go
View File

@ -451,11 +451,20 @@ func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Imag
}
request.Options = any
}
// make sure we pause it and resume after all other filesystem operations are completed
if err := t.Pause(ctx); err != nil {
status, err := t.Status(ctx)
if err != nil {
return nil, err
}
defer t.Resume(ctx)
if status.Status != Paused {
// make sure we pause it and resume after all other filesystem operations are completed
if err := t.Pause(ctx); err != nil {
return nil, err
}
defer t.Resume(ctx)
}
index := v1.Index{
Versioned: is.Versioned{
SchemaVersion: 2,