func createHooks(rspec *specs.LinuxRuntimeSpec, config *configs.Config) { config.Hooks = &configs.Hooks{} for _, h := range rspec.Hooks.Prestart { cmd := configs.Command{ Path: h.Path, Args: h.Args, Env: h.Env, } config.Hooks.Prestart = append(config.Hooks.Prestart, configs.NewCommandHook(cmd)) } for _, h := range rspec.Hooks.Poststart { cmd := configs.Command{ Path: h.Path, Args: h.Args, Env: h.Env, } config.Hooks.Poststart = append(config.Hooks.Poststart, configs.NewCommandHook(cmd)) } for _, h := range rspec.Hooks.Poststop { cmd := configs.Command{ Path: h.Path, Args: h.Args, Env: h.Env, } config.Hooks.Poststop = append(config.Hooks.Poststop, configs.NewCommandHook(cmd)) } }
func createHooks(rspec *specs.Spec, config *configs.Config) { config.Hooks = &configs.Hooks{} for _, h := range rspec.Hooks.Prestart { cmd := createCommandHook(h) config.Hooks.Prestart = append(config.Hooks.Prestart, configs.NewCommandHook(cmd)) } for _, h := range rspec.Hooks.Poststart { cmd := createCommandHook(h) config.Hooks.Poststart = append(config.Hooks.Poststart, configs.NewCommandHook(cmd)) } for _, h := range rspec.Hooks.Poststop { cmd := createCommandHook(h) config.Hooks.Poststop = append(config.Hooks.Poststop, configs.NewCommandHook(cmd)) } }
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 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 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 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 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) } }