diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index eb7019d606e..d4d5330e848 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -905,3 +905,47 @@ func TestMountCgroupRW(t *testing.T) { } } } + +func TestPrestartHook(t *testing.T) { + if testing.Short() { + return + } + root, err := newTestRoot() + ok(t, err) + defer os.RemoveAll(root) + + rootfs, err := newRootfs() + ok(t, err) + defer remove(rootfs) + + config := newTemplateConfig(rootfs) + pwd, _ := os.Getwd() + + hookPath := filepath.Join(pwd, "../../script", "hook.py") + prestartCmd := configs.Command{Path: hookPath} + config.Prestart = append(config.Prestart, prestartCmd) + + container, err := factory.Create("test", config) + ok(t, err) + defer container.Destroy() + + var stdout bytes.Buffer + pconfig := libcontainer.Process{ + Args: []string{"sh", "-c", "ls /tmp.txt"}, + Env: standardEnvironment, + Stdin: nil, + Stdout: &stdout, + } + err = container.Start(&pconfig) + ok(t, err) + + // Wait for process + waitProcess(&pconfig, t) + + outputLs := string(stdout.Bytes()) + + // Check that the ls output has the expected file touched by the prestart hook + if !strings.Contains(outputLs, "/tmp.txt") { + t.Fatal("ls output doesn't have the expected file: ", outputLs) + } +} diff --git a/script/hook.py b/script/hook.py new file mode 100755 index 00000000000..6ec322d7295 --- /dev/null +++ b/script/hook.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +import sys +import json +import os + +def touch(filepath): + if os.path.exists(filepath): + os.utime(filepath, None) + else: + open(filepath, 'a').close() + +if __name__ == "__main__": + rootfs = json.load(sys.stdin)["config"]["rootfs"] + touch(os.path.join(rootfs, "tmp.txt")) + +