|
1 | | -// +build !windows |
| 1 | +// +build linux |
2 | 2 |
|
3 | 3 | /* |
4 | 4 | Copyright The containerd Authors. |
|
19 | 19 | package containerd |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "bytes" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "strings" |
| 26 | + "sync" |
22 | 27 | "syscall" |
23 | 28 | "testing" |
24 | 29 |
|
25 | 30 | "github.com/containerd/containerd/oci" |
26 | 31 | ) |
27 | 32 |
|
| 33 | +func TestCheckpointRestorePTY(t *testing.T) { |
| 34 | + if !supportsCriu { |
| 35 | + t.Skip("system does not have criu installed") |
| 36 | + } |
| 37 | + client, err := newClient(t, address) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + defer client.Close() |
| 42 | + |
| 43 | + var ( |
| 44 | + ctx, cancel = testContext() |
| 45 | + id = t.Name() |
| 46 | + ) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + image, err := client.GetImage(ctx, testImage) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + container, err := client.NewContainer(ctx, id, |
| 54 | + WithNewSpec(oci.WithImageConfig(image), |
| 55 | + oci.WithProcessArgs("sh", "-c", "read A; echo z${A}z"), |
| 56 | + oci.WithTTY), |
| 57 | + WithNewSnapshot(id, image)) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + defer container.Delete(ctx, WithSnapshotCleanup) |
| 62 | + |
| 63 | + direct, err := newDirectIOWithTerminal(ctx) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + defer direct.Delete() |
| 68 | + |
| 69 | + task, err := container.NewTask(ctx, direct.IOCreate) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + defer task.Delete(ctx) |
| 74 | + |
| 75 | + statusC, err := task.Wait(ctx) |
| 76 | + if err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + |
| 80 | + if err := task.Start(ctx); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + |
| 84 | + checkpoint, err := task.Checkpoint(ctx, WithExit) |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + |
| 89 | + <-statusC |
| 90 | + |
| 91 | + if _, err := task.Delete(ctx); err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + direct.Delete() |
| 95 | + direct, err = newDirectIOWithTerminal(ctx) |
| 96 | + if err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + |
| 100 | + var ( |
| 101 | + wg sync.WaitGroup |
| 102 | + buf = bytes.NewBuffer(nil) |
| 103 | + ) |
| 104 | + wg.Add(1) |
| 105 | + go func() { |
| 106 | + defer wg.Done() |
| 107 | + io.Copy(buf, direct.Stdout) |
| 108 | + }() |
| 109 | + |
| 110 | + if task, err = container.NewTask(ctx, direct.IOCreate, |
| 111 | + WithTaskCheckpoint(checkpoint)); err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + |
| 115 | + statusC, err = task.Wait(ctx) |
| 116 | + if err != nil { |
| 117 | + t.Fatal(err) |
| 118 | + } |
| 119 | + |
| 120 | + if err := task.Start(ctx); err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + |
| 124 | + direct.Stdin.Write([]byte("hello\n")) |
| 125 | + <-statusC |
| 126 | + wg.Wait() |
| 127 | + |
| 128 | + if err := direct.Close(); err != nil { |
| 129 | + t.Error(err) |
| 130 | + } |
| 131 | + |
| 132 | + out := buf.String() |
| 133 | + if !strings.Contains(fmt.Sprintf("%#q", out), `zhelloz`) { |
| 134 | + t.Fatalf(`expected \x00 in output: %s`, out) |
| 135 | + } |
| 136 | +} |
| 137 | + |
28 | 138 | func TestCheckpointRestore(t *testing.T) { |
29 | 139 | if !supportsCriu { |
30 | 140 | t.Skip("system does not have criu installed") |
|
0 commit comments