diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 8e47e087..4ff2af38 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -932,3 +932,47 @@ func TestOomScoreAdj(t *testing.T) { t.Fatalf("Expected oom_score_adj %d; got %q", config.OomScoreAdj, outputOomScoreAdj) } } + +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 00000000..62c6c644 --- /dev/null +++ b/script/hook.py @@ -0,0 +1,15 @@ +#!/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")) +