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"
|
2015-07-30 14:43:40 +08:00
|
|
|
"path/filepath"
|
2015-05-19 05:52:26 +08:00
|
|
|
"strings"
|
2015-07-30 13:24:27 +08:00
|
|
|
"syscall"
|
2015-05-19 05:52:26 +08:00
|
|
|
"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"
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-19 05:52:26 +08:00
|
|
|
func TestCheckpoint(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2015-07-30 13:24:27 +08:00
|
|
|
config.Mounts = append(config.Mounts, &configs.Mount{
|
|
|
|
Destination: "/sys/fs/cgroup",
|
|
|
|
Device: "cgroup",
|
|
|
|
Flags: defaultMountFlags | syscall.MS_RDONLY,
|
|
|
|
})
|
|
|
|
|
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{
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Start(&pconfig)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if state != libcontainer.Checkpointed {
|
|
|
|
t.Fatal("Unexpected state: ", state)
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
Stdin: restoreStdinR,
|
|
|
|
Stdout: &stdout,
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Fatal("Unexpected state: ", state)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
s, err := process.Wait()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|