Add test for function based hooks
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
1dca365393
commit
dd969cbacd
|
@ -194,12 +194,16 @@ type Hook interface {
|
|||
// NewFunctionHooks will call the provided function when the hook is run.
|
||||
func NewFunctionHook(f func(*HookState) error) *FuncHook {
|
||||
return &FuncHook{
|
||||
Run: f,
|
||||
run: f,
|
||||
}
|
||||
}
|
||||
|
||||
type FuncHook struct {
|
||||
Run func(*HookState) error
|
||||
run func(*HookState) error
|
||||
}
|
||||
|
||||
func (f *FuncHook) Run(s *HookState) error {
|
||||
return f.run(s)
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
|
|
|
@ -185,6 +185,7 @@ func (c *linuxContainer) newInitProcess(p *Process, cmd *exec.Cmd, parentPipe, c
|
|||
parentPipe: parentPipe,
|
||||
manager: c.cgroupManager,
|
||||
config: c.newInitConfig(p),
|
||||
container: c,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -946,19 +946,24 @@ func TestPrestartHook(t *testing.T) {
|
|||
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)
|
||||
|
||||
config.Hooks = &configs.Hooks{
|
||||
Prestart: []configs.Hook{
|
||||
configs.NewFunctionHook(func(s *configs.HookState) error {
|
||||
f, err := os.Create(filepath.Join(rootfs, "test"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}),
|
||||
},
|
||||
}
|
||||
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"},
|
||||
Args: []string{"sh", "-c", "ls /test"},
|
||||
Env: standardEnvironment,
|
||||
Stdin: nil,
|
||||
Stdout: &stdout,
|
||||
|
@ -972,7 +977,7 @@ func TestPrestartHook(t *testing.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") {
|
||||
if !strings.Contains(outputLs, "/test") {
|
||||
t.Fatal("ls output doesn't have the expected file: ", outputLs)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
#!/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"))
|
||||
|
Loading…
Reference in New Issue