From 2b2b381c8b10131216ac5920e31540c59f567949 Mon Sep 17 00:00:00 2001 From: Zachary Gershman Date: Tue, 16 Feb 2016 17:03:03 -0800 Subject: [PATCH] poststop hooks receive correct pid Signed-off-by: Gabe Rosenhouse --- libcontainer/container_linux_test.go | 52 ++++++++++++++++++++++++++++ libcontainer/state_linux.go | 3 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/libcontainer/container_linux_test.go b/libcontainer/container_linux_test.go index 3af30bce970..d43c827eecf 100644 --- a/libcontainer/container_linux_test.go +++ b/libcontainer/container_linux_test.go @@ -216,3 +216,55 @@ func TestGetContainerState(t *testing.T) { } } } + +type MockHook struct { + state configs.HookState +} + +func (m *MockHook) Run(h configs.HookState) error { + m.state = h + return nil +} + +func TestPostStopHooks(t *testing.T) { + var ( + pid = os.Getpid() + expectedMemoryPath = "/sys/fs/cgroup/memory/myid" + ) + mockHook := &MockHook{} + container := &linuxContainer{ + id: "myid", + config: &configs.Config{ + Hooks: &configs.Hooks{ + Poststop: []configs.Hook{ + mockHook, + }, + }, + }, + initProcess: &mockProcess{ + _pid: pid, + started: "010", + }, + cgroupManager: &mockCgroupManager{ + pids: []int{1, 2, 3}, + stats: &cgroups.Stats{ + MemoryStats: cgroups.MemoryStats{ + Usage: cgroups.MemoryData{ + Usage: 1024, + }, + }, + }, + paths: map[string]string{ + "memory": expectedMemoryPath, + }, + }, + } + container.state = &stoppedState{c: container} + err := container.Destroy() + if err != nil { + t.Fatal(err) + } + if mockHook.state.Pid != pid { + t.Fatalf("expected pid %d but received %d", pid, mockHook.state.Pid) + } +} diff --git a/libcontainer/state_linux.go b/libcontainer/state_linux.go index 9ffe15a4367..ca9c5e36599 100644 --- a/libcontainer/state_linux.go +++ b/libcontainer/state_linux.go @@ -45,10 +45,10 @@ func destroy(c *linuxContainer) error { if rerr := os.RemoveAll(c.root); err == nil { err = rerr } - c.initProcess = nil if herr := runPoststopHooks(c); err == nil { err = herr } + c.initProcess = nil c.state = &stoppedState{c: c} return err } @@ -59,6 +59,7 @@ func runPoststopHooks(c *linuxContainer) error { Version: c.config.Version, ID: c.id, Root: c.config.Rootfs, + Pid: c.initProcess.pid(), } for _, hook := range c.config.Hooks.Poststop { if err := hook.Run(s); err != nil {