Skip to content

Commit

Permalink
Add tests for the StartContainer Hook
Browse files Browse the repository at this point in the history
Signed-off-by: Renaud Gaubert <rgaubert@nvidia.com>
  • Loading branch information
Renaud Gaubert committed May 10, 2020
1 parent 672d28b commit 8de63fb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions libcontainer/configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
Binary file added libcontainer/integration/.exec_test.go.swp
Binary file not shown.
14 changes: 14 additions & 0 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ func TestHook(t *testing.T) {
configs.Prestart: "prestart",
configs.CreateRuntime: "createRuntime",
configs.CreateContainer: "createContainer",
configs.StartContainer: "startContainer",
configs.Poststart: "poststart",
}

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions libcontainer/specconv/spec_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8de63fb

Please sign in to comment.