2015-05-19 05:52:26 +08:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2015-07-30 14:43:40 +08:00
|
|
|
"bufio"
|
2015-05-19 05:52:26 +08:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-10-18 07:18:49 +08:00
|
|
|
"os/exec"
|
2015-07-30 14:43:40 +08:00
|
|
|
"path/filepath"
|
2015-05-19 05:52:26 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2015-07-30 13:24:27 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2015-05-19 05:52:26 +08:00
|
|
|
)
|
|
|
|
|
2015-07-30 14:43:40 +08:00
|
|
|
func showFile(t *testing.T, fname string) error {
|
|
|
|
t.Logf("=== %s ===\n", fname)
|
|
|
|
|
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
t.Log(scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("=== END ===\n")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:18:49 +08:00
|
|
|
func TestUsernsCheckpoint(t *testing.T) {
|
|
|
|
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) {
|
|
|
|
t.Skip("userns is unsupported")
|
|
|
|
}
|
|
|
|
cmd := exec.Command("criu", "check", "--feature", "userns")
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Skip("Unable to c/r a container with userns")
|
|
|
|
}
|
|
|
|
testCheckpoint(t, true)
|
|
|
|
}
|
|
|
|
|
2015-05-19 05:52:26 +08:00
|
|
|
func TestCheckpoint(t *testing.T) {
|
2016-10-18 07:18:49 +08:00
|
|
|
testCheckpoint(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckpoint(t *testing.T, userns bool) {
|
2015-05-19 05:52:26 +08:00
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
2019-10-31 13:38:27 +08:00
|
|
|
|
2015-05-19 05:52:26 +08:00
|
|
|
root, err := newTestRoot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
rootfs, err := newRootfs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
|
2016-10-18 07:18:49 +08:00
|
|
|
if userns {
|
|
|
|
config.UidMappings = []configs.IDMap{{HostID: 0, ContainerID: 0, Size: 1000}}
|
|
|
|
config.GidMappings = []configs.IDMap{{HostID: 0, ContainerID: 0, Size: 1000}}
|
|
|
|
config.Namespaces = append(config.Namespaces, configs.Namespace{Type: configs.NEWUSER})
|
2020-02-05 21:58:25 +08:00
|
|
|
} else {
|
|
|
|
config.Mounts = append(config.Mounts, &configs.Mount{
|
|
|
|
Destination: "/sys/fs/cgroup",
|
2020-04-24 06:18:01 +08:00
|
|
|
Device: "cgroup",
|
2020-02-05 21:58:25 +08:00
|
|
|
Flags: defaultMountFlags | unix.MS_RDONLY,
|
|
|
|
})
|
2016-10-18 07:18:49 +08:00
|
|
|
}
|
|
|
|
|
2015-05-19 05:52:26 +08:00
|
|
|
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := factory.Create("test", config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer container.Destroy()
|
|
|
|
|
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
|
|
|
|
pconfig := libcontainer.Process{
|
2016-01-15 07:21:36 +08:00
|
|
|
Cwd: "/",
|
2015-06-16 17:26:27 +08:00
|
|
|
Args: []string{"cat"},
|
|
|
|
Env: standardEnvironment,
|
|
|
|
Stdin: stdinR,
|
2015-05-19 05:52:26 +08:00
|
|
|
Stdout: &stdout,
|
2018-06-02 03:56:13 +08:00
|
|
|
Init: true,
|
2015-05-19 05:52:26 +08:00
|
|
|
}
|
|
|
|
|
2016-05-28 04:13:11 +08:00
|
|
|
err = container.Run(&pconfig)
|
2015-05-19 05:52:26 +08:00
|
|
|
stdinR.Close()
|
|
|
|
defer stdinW.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := pconfig.Pid()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
process, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-24 17:48:56 +08:00
|
|
|
parentDir, err := ioutil.TempDir("", "criu-parent")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(parentDir)
|
|
|
|
|
|
|
|
preDumpOpts := &libcontainer.CriuOpts{
|
|
|
|
ImagesDirectory: parentDir,
|
|
|
|
WorkDirectory: parentDir,
|
|
|
|
PreDump: true,
|
|
|
|
}
|
|
|
|
preDumpLog := filepath.Join(preDumpOpts.WorkDirectory, "dump.log")
|
|
|
|
|
|
|
|
if err := container.Checkpoint(preDumpOpts); err != nil {
|
|
|
|
showFile(t, preDumpLog)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if state != libcontainer.Running {
|
|
|
|
t.Fatal("Unexpected preDump state: ", state)
|
|
|
|
}
|
|
|
|
|
2015-05-19 05:52:26 +08:00
|
|
|
imagesDir, err := ioutil.TempDir("", "criu")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(imagesDir)
|
|
|
|
|
|
|
|
checkpointOpts := &libcontainer.CriuOpts{
|
|
|
|
ImagesDirectory: imagesDir,
|
2015-06-16 17:26:27 +08:00
|
|
|
WorkDirectory: imagesDir,
|
2016-08-24 17:48:56 +08:00
|
|
|
ParentImage: "../criu-parent",
|
2015-05-19 05:52:26 +08:00
|
|
|
}
|
2015-07-30 14:43:40 +08:00
|
|
|
dumpLog := filepath.Join(checkpointOpts.WorkDirectory, "dump.log")
|
|
|
|
restoreLog := filepath.Join(checkpointOpts.WorkDirectory, "restore.log")
|
2015-05-19 05:52:26 +08:00
|
|
|
|
|
|
|
if err := container.Checkpoint(checkpointOpts); err != nil {
|
2015-07-30 14:43:40 +08:00
|
|
|
showFile(t, dumpLog)
|
2015-05-19 05:52:26 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-24 17:48:56 +08:00
|
|
|
state, err = container.Status()
|
2015-05-19 05:52:26 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:41:16 +08:00
|
|
|
if state != libcontainer.Stopped {
|
2015-10-03 02:16:50 +08:00
|
|
|
t.Fatal("Unexpected state checkpoint: ", state)
|
2015-05-19 05:52:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
stdinW.Close()
|
|
|
|
_, err = process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reload the container
|
|
|
|
container, err = factory.Load("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreStdinR, restoreStdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreProcessConfig := &libcontainer.Process{
|
2016-01-15 07:21:36 +08:00
|
|
|
Cwd: "/",
|
2015-05-19 05:52:26 +08:00
|
|
|
Stdin: restoreStdinR,
|
|
|
|
Stdout: &stdout,
|
2018-06-02 03:56:13 +08:00
|
|
|
Init: true,
|
2015-05-19 05:52:26 +08:00
|
|
|
}
|
|
|
|
|
2015-07-30 14:43:40 +08:00
|
|
|
err = container.Restore(restoreProcessConfig, checkpointOpts)
|
2015-05-19 05:52:26 +08:00
|
|
|
restoreStdinR.Close()
|
|
|
|
defer restoreStdinW.Close()
|
2015-07-30 14:43:40 +08:00
|
|
|
if err != nil {
|
|
|
|
showFile(t, restoreLog)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-19 05:52:26 +08:00
|
|
|
|
|
|
|
state, err = container.Status()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if state != libcontainer.Running {
|
2015-10-03 02:16:50 +08:00
|
|
|
t.Fatal("Unexpected restore state: ", state)
|
2015-05-19 05:52:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, err = restoreProcessConfig.Pid()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
process, err = os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = restoreStdinW.WriteString("Hello!")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreStdinW.Close()
|
2020-02-10 14:32:56 +08:00
|
|
|
s, err := restoreProcessConfig.Wait()
|
2015-05-19 05:52:26 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.Success() {
|
|
|
|
t.Fatal(s.String(), pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := string(stdout.Bytes())
|
|
|
|
if !strings.Contains(output, "Hello!") {
|
|
|
|
t.Fatal("Did not restore the pipe correctly:", output)
|
|
|
|
}
|
|
|
|
}
|