@@ -12,17 +12,6 @@ function setup() {
1212
1313function teardown() {
1414 teardown_busybox
15- local pid fd
16-
17- for pid in " ${PIDS_TO_KILL[@]} " ; do
18- kill -9 " $pid " || true
19- done
20- PIDS_TO_KILL=()
21-
22- for fd in " ${FDS_TO_CLOSE[@]} " ; do
23- exec {fd}>& -
24- done
25- FDS_TO_CLOSE=()
2615}
2716
2817function setup_pipes() {
@@ -31,19 +20,22 @@ function setup_pipes() {
3120 update_config ' (.. | select(.terminal? != null)) .terminal |= false
3221 | (.. | select(.[]? == "sh")) += ["-c", "for i in `seq 10`; do read xxx || continue; echo ponG $xxx; done"]'
3322
34- # Create two sets of pipes
35- # for stdout/stderr
23+ # Create three sets of pipes for __runc run.
24+ # for stderr
25+ exec {pipe}<> <( :)
26+ exec {err_r}< /proc/self/fd/$pipe
27+ exec {err_w}> /proc/self/fd/$pipe
28+ exec {pipe}>& -
29+ # for stdout
3630 exec {pipe}<> <( :)
3731 exec {out_r}< /proc/self/fd/$pipe
3832 exec {out_w}> /proc/self/fd/$pipe
3933 exec {pipe}>& -
40- # ... and stdin
34+ # for stdin
4135 exec {pipe}<> <( :)
4236 exec {in_r}< /proc/self/fd/$pipe
4337 exec {in_w}> /proc/self/fd/$pipe
4438 exec {pipe}>& -
45- # shellcheck disable=SC2206
46- FDS_TO_CLOSE=($in_r $in_w $out_r $out_w )
4739}
4840
4941function check_pipes() {
@@ -55,6 +47,50 @@ function check_pipes() {
5547 [[ " ${output} " == * " ponG Ping" * ]]
5648}
5749
50+ # Usage: runc_run_with_pipes container-name
51+ function runc_run_with_pipes() {
52+ # Start a container to be checkpointed, with stdin/stdout redirected
53+ # so that check_pipes can be used to check it's working fine.
54+ # We have to redirect stderr as well because otherwise it is
55+ # redirected to a bats log file, which is not accessible to CRIU
56+ # (i.e. outside of container) so checkpointing will fail.
57+ ret=0
58+ __runc run -d " $1 " < & ${in_r} >& ${out_w} 2>& ${err_w} || ret=$?
59+ if [ " $ret " -ne 0 ]; then
60+ echo " runc run -d $1 (status: $ret ):"
61+ exec {err_w}>& -
62+ cat < & ${err_r}
63+ fail " runc run failed"
64+ fi
65+
66+ testcontainer " $1 " running
67+ }
68+
69+ # Usage: runc_restore_with_pipes work-dir container-name [optional-arguments ...]
70+ function runc_restore_with_pipes() {
71+ workdir=" $1 "
72+ shift
73+ name=" $1 "
74+ shift
75+
76+ ret=0
77+ __runc --criu " $CRIU " restore -d --work-path " $workdir " --image-path ./image-dir " $@ " " $name " < & ${in_r} >& ${out_w} 2>& ${err_w} || ret=$?
78+ if [ " $ret " -ne 0 ]; then
79+ echo " __runc restore $name failed (status: $ret )"
80+ exec {err_w}>& -
81+ cat < & ${err_r}
82+ echo " CRIU restore log errors (if any):"
83+ grep -B 5 Error " $workdir " /restore.log || true
84+ fail " runc restore failed"
85+ fi
86+
87+ testcontainer " $name " running
88+
89+ runc exec --cwd /bin " $name " echo ok
90+ [ " $status " -eq 0 ]
91+ [[ ${output} == " ok" ]]
92+ }
93+
5894function simple_cr() {
5995 runc run -d --console-socket " $CONSOLE_SOCKET " test_busybox
6096 [ " $status " -eq 0 ]
@@ -96,11 +132,7 @@ function simple_cr() {
96132
97133@test " checkpoint --pre-dump and restore" {
98134 setup_pipes
99-
100- # run busybox
101- __runc run -d test_busybox < & ${in_r} >& ${out_w} 2>& ${out_w}
102-
103- testcontainer test_busybox running
135+ runc_run_with_pipes test_busybox
104136
105137 # test checkpoint pre-dump
106138 mkdir parent-dir
@@ -120,19 +152,7 @@ function simple_cr() {
120152 # after checkpoint busybox is no longer running
121153 testcontainer test_busybox checkpointed
122154
123- # restore from checkpoint
124- ret=0
125- __runc --criu " $CRIU " restore -d --work-path ./work-dir --image-path ./image-dir test_busybox < & ${in_r} >& ${out_w} 2>& ${out_w} || ret=$?
126- grep -B 5 Error ./work-dir/restore.log || true
127- [ $ret -eq 0 ]
128-
129- # busybox should be back up and running
130- testcontainer test_busybox running
131-
132- runc exec --cwd /bin test_busybox echo ok
133- [ " $status " -eq 0 ]
134- [[ ${output} == " ok" ]]
135-
155+ runc_restore_with_pipes ./work-dir test_busybox
136156 check_pipes
137157}
138158
@@ -144,14 +164,7 @@ function simple_cr() {
144164 fi
145165
146166 setup_pipes
147-
148- # TCP port for lazy migration
149- port=27277
150-
151- # run busybox
152- __runc run -d test_busybox < & ${in_r} >& ${out_w} 2>& ${out_w}
153-
154- testcontainer test_busybox running
167+ runc_run_with_pipes test_busybox
155168
156169 # checkpoint the running container
157170 mkdir image-dir
@@ -163,13 +176,12 @@ function simple_cr() {
163176 # shellcheck disable=SC2094
164177 exec {lazy_r}< /proc/self/fd/$pipe {lazy_w}> /proc/self/fd/$pipe
165178 exec {pipe}>& -
166- # shellcheck disable=SC2206
167- FDS_TO_CLOSE+=($lazy_r $lazy_w )
179+
180+ # TCP port for lazy migration
181+ port=27277
168182
169183 __runc --criu " $CRIU " checkpoint --lazy-pages --page-server 0.0.0.0:${port} --status-fd ${lazy_w} --work-path ./work-dir --image-path ./image-dir test_busybox &
170184 cpt_pid=$!
171- # shellcheck disable=SC2206
172- PIDS_TO_KILL=($cpt_pid )
173185
174186 # wait for lazy page server to be ready
175187 out=$( timeout 2 dd if=/proc/self/fd/${lazy_r} bs=1 count=1 2> /dev/null | od)
@@ -187,31 +199,18 @@ function simple_cr() {
187199 # Start CRIU in lazy-daemon mode
188200 ${CRIU} lazy-pages --page-server --address 127.0.0.1 --port ${port} -D image-dir &
189201 lp_pid=$!
190- # shellcheck disable=SC2206
191- PIDS_TO_KILL+=($lp_pid )
192202
193203 # Restore lazily from checkpoint.
194204 # The restored container needs a different name as the checkpointed
195205 # container is not yet destroyed. It is only destroyed at that point
196206 # in time when the last page is lazily transferred to the destination.
197207 # Killing the CRIU on the checkpoint side will let the container
198208 # continue to run if the migration failed at some point.
199- ret=0
200- __runc --criu " $CRIU " restore -d --work-path ./image-dir --image-path ./image-dir --lazy-pages test_busybox_restore < & ${in_r} >& ${out_w} 2>& ${out_w} || ret=$?
201- grep -B 5 Error ./work-dir/restore.log || true
202- [ $ret -eq 0 ]
203-
204- # busybox should be back up and running
205- testcontainer test_busybox_restore running
206-
207- runc exec --cwd /bin test_busybox_restore echo ok
208- [ " $status " -eq 0 ]
209- [[ ${output} == " ok" ]]
209+ runc_restore_with_pipes ./image-dir test_busybox_restore --lazy-pages
210210
211211 wait $cpt_pid
212212
213213 wait $lp_pid
214- PIDS_TO_KILL=()
215214
216215 check_pipes
217216}
0 commit comments