From dc480bc3adfce27c6c356439bf1cb8c2e71e16f0 Mon Sep 17 00:00:00 2001 From: Dan Walsh Date: Thu, 16 Apr 2015 16:55:12 -0400 Subject: [PATCH] add integration test for premount/postmount hooks Docker-DCO-1.1-Signed-off-by: Daniel, Dao Quang Minh (github: rhatdan) Docker-DCO-1.1-Signed-off-by: Dan Walsh (github: rhatdan) --- integration/exec_test.go | 75 ++++++++++++++++++++++++++++++++++++++++ rootfs_linux.go | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/integration/exec_test.go b/integration/exec_test.go index 8f6719d0..9570701d 100644 --- a/integration/exec_test.go +++ b/integration/exec_test.go @@ -4,8 +4,10 @@ import ( "bytes" "io/ioutil" "os" + "path/filepath" "strconv" "strings" + "syscall" "testing" "github.com/docker/libcontainer" @@ -613,3 +615,76 @@ func TestPassExtraFiles(t *testing.T) { t.Fatalf("expected second pipe to receive '2', got '%s'", out2) } } + +func TestMountCmds(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) + + tmpDir, err := ioutil.TempDir("", "tmpdir") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + config := newTemplateConfig(rootfs) + config.Mounts = append(config.Mounts, &configs.Mount{ + Source: tmpDir, + Destination: filepath.Join(rootfs, "tmp"), + Device: "bind", + Flags: syscall.MS_BIND | syscall.MS_REC, + PremountCmds: []configs.Command{ + {Path: "touch", Args: []string{filepath.Join(tmpDir, "hello")}}, + {Path: "touch", Args: []string{filepath.Join(tmpDir, "world")}}, + }, + PostmountCmds: []configs.Command{ + {Path: "cp", Args: []string{filepath.Join(rootfs, "tmp", "hello"), filepath.Join(rootfs, "tmp", "hello-backup")}}, + {Path: "cp", Args: []string{filepath.Join(rootfs, "tmp", "world"), filepath.Join(rootfs, "tmp", "world-backup")}}, + }, + }) + + 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() + + pconfig := libcontainer.Process{ + Args: []string{"sh", "-c", "env"}, + Env: standardEnvironment, + } + err = container.Start(&pconfig) + if err != nil { + t.Fatal(err) + } + + // Wait for process + waitProcess(&pconfig, t) + + entries, err := ioutil.ReadDir(tmpDir) + if err != nil { + t.Fatal(err) + } + expected := []string{"hello", "hello-backup", "world", "world-backup"} + for i, e := range entries { + if e.Name() != expected[i] { + t.Errorf("Got(%s), expect %s", e.Name(), expected[i]) + } + } +} diff --git a/rootfs_linux.go b/rootfs_linux.go index 1e2ec104..e198bbd2 100644 --- a/rootfs_linux.go +++ b/rootfs_linux.go @@ -80,7 +80,7 @@ func mountCmd(cmd configs.Command) error { command.Env = cmd.Env command.Dir = cmd.Dir if out, err := command.CombinedOutput(); err != nil { - return fmt.Errorf("%s failed: %s: %v", cmd, string(out), err) + return fmt.Errorf("%#v failed: %s: %v", cmd, string(out), err) } return nil