diff --git a/libcontainer/configs/config_test.go b/libcontainer/configs/config_test.go index 8ca3dcb34c2..49951c3038e 100644 --- a/libcontainer/configs/config_test.go +++ b/libcontainer/configs/config_test.go @@ -39,6 +39,7 @@ func TestUnmarshalHooks(t *testing.T) { configs.Prestart: hook.Prestart, configs.CreateRuntime: hook.CreateRuntime, configs.CreateContainer: hook.CreateContainer, + configs.StartContainer: hook.StartContainer, configs.Poststart: hook.Poststart, configs.Poststop: hook.Poststop, } @@ -82,7 +83,7 @@ func TestMarshalHooks(t *testing.T) { // Note Marshal seems to output fields in alphabetical order hookCmdJson := `[{"path":"/var/vcap/hooks/hook","args":["--pid=123"],"env":["FOO=BAR"],"dir":"/var/vcap","timeout":1000000000}]` - h := fmt.Sprintf(`{"createContainer":null,"createRuntime":%[1]s,"poststart":%[1]s,"poststop":%[1]s,"prestart":%[1]s}`, hookCmdJson) + h := fmt.Sprintf(`{"createContainer":null,"createRuntime":%[1]s,"poststart":%[1]s,"poststop":%[1]s,"prestart":%[1]s,"startContainer":null}`, hookCmdJson) if string(hooks) != h { t.Errorf("Expected hooks %s to equal %s", string(hooks), h) } @@ -132,7 +133,7 @@ func TestMarshalHooksWithUnexpectedType(t *testing.T) { t.Fatal(err) } - h := `{"createContainer":null,"createRuntime":null,"poststart":null,"poststop":null,"prestart":null}` + h := `{"createContainer":null,"createRuntime":null,"poststart":null,"poststop":null,"prestart":null,"startContainer":null}` if string(hooks) != h { t.Errorf("Expected hooks %s to equal %s", string(hooks), h) } diff --git a/libcontainer/integration/.exec_test.go.swp b/libcontainer/integration/.exec_test.go.swp new file mode 100644 index 00000000000..aef254d81c5 Binary files /dev/null and b/libcontainer/integration/.exec_test.go.swp differ diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 792f7c1cc3c..452a2fb442b 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -1135,6 +1135,7 @@ func TestHook(t *testing.T) { configs.Prestart: "prestart", configs.CreateRuntime: "createRuntime", configs.CreateContainer: "createContainer", + configs.StartContainer: "startContainer", configs.Poststart: "poststart", } @@ -1168,6 +1169,19 @@ func TestHook(t *testing.T) { return f.Close() }), }, + startContainer: []configs.Hook{ + configs.NewFunctionHook(func(s *specs.State) error { + if s.Bundle != expectedBundle { + t.Fatalf("Expected startContainer hook bundlePath '%s'; got '%s'", expectedBundle, s.Bundle) + } + // Here we expect to be in the container namespace and cwd to be the root of the container + f, err := os.Create(filepath.Join("./", hookFiles[configs.StartContainer])) + if err != nil { + return err + } + return f.Close() + }), + }, Poststart: []configs.Hook{ configs.NewFunctionHook(func(s *specs.State) error { if s.Bundle != expectedBundle { diff --git a/libcontainer/specconv/spec_linux_test.go b/libcontainer/specconv/spec_linux_test.go index 43386b2f417..d9733a4bd55 100644 --- a/libcontainer/specconv/spec_linux_test.go +++ b/libcontainer/specconv/spec_linux_test.go @@ -60,6 +60,15 @@ func TestCreateHooks(t *testing.T) { Args: []string{"--some", "thing"}, }, }, + StartContainer: []specs.Hook{ + { + Path: "/some/hook/path", + }, + { + Path: "/some/hook2/path", + Args: []string{"--some", "thing"}, + }, + }, Poststart: []specs.Hook{ { Path: "/some/hook/path", @@ -113,6 +122,12 @@ func TestCreateHooks(t *testing.T) { t.Error("Expected 2 createContainer hooks") } + startContainer := conf.Hooks.StartContainer + + if len(startContainer) != 2 { + t.Error("Expected 2 startContainer hooks") + } + poststart := conf.Hooks.Poststart if len(poststart) != 3 {