Skip to content

Commit

Permalink
Add test for prestart hook
Browse files Browse the repository at this point in the history
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
  • Loading branch information
mrunalp committed Aug 24, 2015
1 parent 73b421a commit 590c7ad
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
44 changes: 44 additions & 0 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
16 changes: 16 additions & 0 deletions script/hook.py
Original file line number Diff line number Diff line change
@@ -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"))


0 comments on commit 590c7ad

Please sign in to comment.