Add test for prestart hook

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>

Conflicts:
	libcontainer/integration/exec_test.go
This commit is contained in:
Mrunal Patel 2015-08-05 18:26:29 -04:00 committed by Michael Crosby
parent 05567f2c94
commit 1dca365393
2 changed files with 59 additions and 0 deletions

View File

@ -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)
}
}

15
script/hook.py Executable file
View File

@ -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"))