From 69be363508949ebafafd20493b7804c6194d06a1 Mon Sep 17 00:00:00 2001 From: Alberto Leal Date: Sat, 2 Apr 2016 11:05:47 +0000 Subject: [PATCH] Add unit tests for configs.Hooks Signed-off-by: Alberto Leal --- libcontainer/configs/config_test.go | 191 ++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 libcontainer/configs/config_test.go diff --git a/libcontainer/configs/config_test.go b/libcontainer/configs/config_test.go new file mode 100644 index 00000000..006a772a --- /dev/null +++ b/libcontainer/configs/config_test.go @@ -0,0 +1,191 @@ +package configs_test + +import ( + "encoding/json" + "fmt" + "os" + "reflect" + "testing" + "time" + + "github.com/opencontainers/runc/libcontainer/configs" +) + +func TestUnmarshalHooks(t *testing.T) { + timeout := time.Second + + prestartCmd := configs.NewCommandHook(configs.Command{ + Path: "/var/vcap/hooks/prestart", + Args: []string{"--pid=123"}, + Env: []string{"FOO=BAR"}, + Dir: "/var/vcap", + Timeout: &timeout, + }) + prestart, err := json.Marshal(prestartCmd.Command) + if err != nil { + t.Fatal(err) + } + + hook := configs.Hooks{} + err = hook.UnmarshalJSON([]byte(fmt.Sprintf(`{"Prestart" :[%s]}`, prestart))) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(hook.Prestart[0], prestartCmd) { + t.Errorf("Expected prestart to equal %+v but it was %+v", + prestartCmd, hook.Prestart[0]) + } +} + +func TestUnmarshalHooksWithInvalidData(t *testing.T) { + hook := configs.Hooks{} + err := hook.UnmarshalJSON([]byte(`{invalid-json}`)) + if err == nil { + t.Error("Expected error to occur but it was nil") + } +} + +func TestMarshalHooks(t *testing.T) { + timeout := time.Second + + prestartCmd := configs.NewCommandHook(configs.Command{ + Path: "/var/vcap/hooks/prestart", + Args: []string{"--pid=123"}, + Env: []string{"FOO=BAR"}, + Dir: "/var/vcap", + Timeout: &timeout, + }) + + hook := configs.Hooks{ + Prestart: []configs.Hook{prestartCmd}, + } + hooks, err := hook.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + h := `{"poststart":null,"poststop":null,"prestart":[{"path":"/var/vcap/hooks/prestart","args":["--pid=123"],"env":["FOO=BAR"],"dir":"/var/vcap","timeout":1000000000}]}` + if string(hooks) != h { + t.Errorf("Expected hooks %s to equal %s", string(hooks), h) + } +} + +func TestMarshalUnmarshalHooks(t *testing.T) { + timeout := time.Second + + prestart := configs.NewCommandHook(configs.Command{ + Path: "/var/vcap/hooks/prestart", + Args: []string{"--pid=123"}, + Env: []string{"FOO=BAR"}, + Dir: "/var/vcap", + Timeout: &timeout, + }) + + hook := configs.Hooks{ + Prestart: []configs.Hook{prestart}, + } + hooks, err := hook.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + umMhook := configs.Hooks{} + err = umMhook.UnmarshalJSON(hooks) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(umMhook.Prestart[0], prestart) { + t.Errorf("Expected hooks to be equal after mashaling -> unmarshaling them: %+v, %+v", umMhook.Prestart[0], prestart) + } +} + +func TestMarshalHooksWithUnexpectedType(t *testing.T) { + fHook := configs.NewFunctionHook(func(configs.HookState) error { + return nil + }) + hook := configs.Hooks{ + Prestart: []configs.Hook{fHook}, + } + hooks, err := hook.MarshalJSON() + if err != nil { + t.Fatal(err) + } + + h := `{"poststart":null,"poststop":null,"prestart":null}` + if string(hooks) != h { + t.Errorf("Expected hooks %s to equal %s", string(hooks), h) + } +} + +func TestFuncHookRun(t *testing.T) { + state := configs.HookState{ + Version: "1", + ID: "1", + Pid: 1, + Root: "root", + } + + fHook := configs.NewFunctionHook(func(s configs.HookState) error { + if !reflect.DeepEqual(state, s) { + t.Errorf("Expected state %+v to equal %+v", state, s) + } + return nil + }) + + fHook.Run(state) +} + +func TestCommandHookRun(t *testing.T) { + state := configs.HookState{ + Version: "1", + ID: "1", + Pid: 1, + Root: "root", + } + timeout := time.Second + + cmdHook := configs.NewCommandHook(configs.Command{ + Path: os.Args[0], + Args: []string{os.Args[0], "-test.run=TestHelperProcess"}, + Env: []string{"FOO=BAR"}, + Dir: "/", + Timeout: &timeout, + }) + + err := cmdHook.Run(state) + if err != nil { + t.Errorf(fmt.Sprintf("Expected error to not occur but it was %+v", err)) + } +} + +func TestCommandHookRunTimeout(t *testing.T) { + state := configs.HookState{ + Version: "1", + ID: "1", + Pid: 1, + Root: "root", + } + timeout := (10 * time.Millisecond) + + cmdHook := configs.NewCommandHook(configs.Command{ + Path: os.Args[0], + Args: []string{os.Args[0], "-test.run=TestHelperProcessWithTimeout"}, + Env: []string{"FOO=BAR"}, + Dir: "/", + Timeout: &timeout, + }) + + err := cmdHook.Run(state) + if err == nil { + t.Error("Expected error to occur but it was nil") + } +} + +func TestHelperProcess(*testing.T) { + fmt.Println("Helper Process") + os.Exit(0) +} +func TestHelperProcessWithTimeout(*testing.T) { + time.Sleep(time.Second) +}