-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
checkpoint_test.go
171 lines (133 loc) · 3.52 KB
/
checkpoint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package integration
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/opencontainers/runc/libcontainer"
"golang.org/x/sys/unix"
)
func criuFeature(feature string) bool {
return exec.Command("criu", "check", "--feature", feature).Run() == nil
}
func TestUsernsCheckpoint(t *testing.T) {
testCheckpoint(t, true)
}
func TestCheckpoint(t *testing.T) {
testCheckpoint(t, false)
}
func testCheckpoint(t *testing.T, userns bool) {
if testing.Short() {
return
}
if _, err := exec.LookPath("criu"); err != nil {
t.Skipf("criu binary not found: %v", err)
}
// Workaround for https://github.com/opencontainers/runc/issues/3532.
out, err := exec.Command("rpm", "-q", "criu").CombinedOutput()
if err == nil && regexp.MustCompile(`^criu-3\.17-[123]\.el9`).Match(out) {
t.Skip("Test requires criu >= 3.17-4 on CentOS Stream 9.")
}
if userns && !criuFeature("userns") {
t.Skip("Test requires userns")
}
config := newTemplateConfig(t, &tParam{userns: userns})
stateDir := t.TempDir()
container, err := libcontainer.Create(stateDir, "test", config)
ok(t, err)
defer destroyContainer(container)
stdinR, stdinW, err := os.Pipe()
ok(t, err)
var stdout bytes.Buffer
pconfig := libcontainer.Process{
Cwd: "/",
Args: []string{"cat"},
Env: standardEnvironment,
Stdin: stdinR,
Stdout: &stdout,
Init: true,
}
err = container.Run(&pconfig)
_ = stdinR.Close()
defer stdinW.Close() //nolint: errcheck
ok(t, err)
pid, err := pconfig.Pid()
ok(t, err)
process, err := os.FindProcess(pid)
ok(t, err)
tmp := t.TempDir()
var parentImage string
// Test pre-dump if mem_dirty_track is available.
if criuFeature("mem_dirty_track") {
parentImage = "../criu-parent"
parentDir := filepath.Join(tmp, "criu-parent")
preDumpOpts := &libcontainer.CriuOpts{
ImagesDirectory: parentDir,
WorkDirectory: parentDir,
PreDump: true,
}
if err := container.Checkpoint(preDumpOpts); err != nil {
t.Fatal(err)
}
state, err := container.Status()
ok(t, err)
if state != libcontainer.Running {
t.Fatal("Unexpected preDump state: ", state)
}
}
imagesDir := filepath.Join(tmp, "criu")
checkpointOpts := &libcontainer.CriuOpts{
ImagesDirectory: imagesDir,
WorkDirectory: imagesDir,
ParentImage: parentImage,
}
if err := container.Checkpoint(checkpointOpts); err != nil {
t.Fatal(err)
}
state, err := container.Status()
ok(t, err)
if state != libcontainer.Stopped {
t.Fatal("Unexpected state checkpoint: ", state)
}
_ = stdinW.Close()
_, err = process.Wait()
ok(t, err)
// reload the container
container, err = libcontainer.Load(stateDir, "test")
ok(t, err)
restoreStdinR, restoreStdinW, err := os.Pipe()
ok(t, err)
var restoreStdout bytes.Buffer
restoreProcessConfig := &libcontainer.Process{
Cwd: "/",
Stdin: restoreStdinR,
Stdout: &restoreStdout,
Init: true,
}
err = container.Restore(restoreProcessConfig, checkpointOpts)
_ = restoreStdinR.Close()
defer restoreStdinW.Close() //nolint: errcheck
if err != nil {
t.Fatal(err)
}
state, err = container.Status()
ok(t, err)
if state != libcontainer.Running {
t.Fatal("Unexpected restore state: ", state)
}
pid, err = restoreProcessConfig.Pid()
ok(t, err)
err = unix.Kill(pid, 0)
ok(t, err)
_, err = restoreStdinW.WriteString("Hello!")
ok(t, err)
_ = restoreStdinW.Close()
waitProcess(restoreProcessConfig, t)
output := restoreStdout.String()
if !strings.Contains(output, "Hello!") {
t.Fatal("Did not restore the pipe correctly:", output)
}
}