Beispiel #1
0
// start + stop of gonit daemonized process
func (s *ProcessSuite) TestSimple(c *C) {
	process := helper.NewTestProcess("simple", nil, false)
	defer helper.Cleanup(process)

	pid, err := process.StartProcess()
	if err != nil {
		log.Panic(err)
	}

	pause()

	c.Check(true, Equals, process.IsRunning())

	info := processInfo(process)

	c.Check(pid, Equals, info.Pid)

	assertProcessInfo(c, process, info)

	err = process.StopProcess()
	c.Check(err, IsNil)

	pause()

	c.Check(false, Equals, process.IsRunning())
}
Beispiel #2
0
// start + stop of gonit daemonized process w/ setuid
func (s *ProcessSuite) TestSimpleSetuid(c *C) {
	if skipUnlessRoot(c) {
		return
	}

	process := helper.NewTestProcess("simple_setuid", nil, false)
	defer helper.Cleanup(process)

	helper.TouchFile(processJsonFile(process), 0666)

	process.Uid = "nobody"
	process.Gid = "nogroup"

	pid, err := process.StartProcess()
	if err != nil {
		c.Fatal(err)
	}

	pause()

	c.Check(true, Equals, process.IsRunning())

	info := processInfo(process)
	assertProcessInfo(c, process, info)
	c.Check(pid, Equals, info.Pid)

	err = process.StopProcess()
	c.Check(err, IsNil)

	pause()

	c.Check(false, Equals, process.IsRunning())
}
Beispiel #3
0
func (s *WatcherIntSuite) TearDownTest(c *C) {
	if err := helper.StopGonit(s.gonitCmd, s.dir); err != nil {
		c.Errorf(err.Error())
	}

	for _, process := range s.procs {
		helper.Cleanup(process)
	}
}
Beispiel #4
0
func (s *ApiIntSuite) TearDownSuite(c *C) {
	s.client.Close()

	if err := helper.StopGonit(s.gonitCmd, s.dir); err != nil {
		c.Errorf(err.Error())
	}

	for _, process := range s.procs {
		helper.Cleanup(process)
	}
}
Beispiel #5
0
// test invalid executable
func (s *ProcessSuite) TestFailExe(c *C) {
	process := helper.NewTestProcess("fail_exe", nil, false)
	defer helper.Cleanup(process)

	err := os.Chmod(helper.TestProcess, 0444)
	if err != nil {
		log.Panic(err)
	}

	_, err = process.StartProcess()
	c.Check(err, NotNil)

	pause()

	c.Check(false, Equals, process.IsRunning())
}
Beispiel #6
0
// test invalid uid
func (s *ProcessSuite) TestFailSetuid(c *C) {
	if skipUnlessRoot(c) {
		return
	}

	process := helper.NewTestProcess("fail_setuid", nil, false)
	defer helper.Cleanup(process)

	process.Uid = "aint_nobody"

	_, err := process.StartProcess()
	if err == nil {
		c.Fatalf("user.LookupId(%q) should have failed", process.Uid)
	}

	pause()

	c.Check(false, Equals, process.IsRunning())
}
Beispiel #7
0
func (s *ControlSuite) TestActions(c *C) {
	fem := &FakeEventMonitor{}
	configManager := &ConfigManager{
		Settings: &Settings{PersistFile: gonitPersistFile},
	}
	ctl := &Control{ConfigManager: configManager, EventMonitor: fem}

	name := "simple"
	process := helper.NewTestProcess(name, nil, false)
	defer helper.Cleanup(process)

	err := ctl.Config().AddProcess(groupName, process)
	c.Check(err, IsNil)
	c.Check(MONITOR_NOT, Equals, ctl.State(process).Monitor)
	c.Check(0, Equals, ctl.State(process).Starts)

	rv := ctl.DoAction(name, NewControlAction(ACTION_START))
	c.Check(1, Equals, fem.numStartMonitoringCalled)
	c.Check(rv, IsNil)

	c.Check(MONITOR_INIT, Equals, ctl.State(process).Monitor)
	c.Check(1, Equals, ctl.State(process).Starts)

	c.Check(true, Equals, process.IsRunning())

	rv = ctl.DoAction(name, NewControlAction(ACTION_RESTART))
	c.Check(2, Equals, fem.numStartMonitoringCalled)
	c.Check(rv, IsNil)

	c.Check(2, Equals, ctl.State(process).Starts)

	rv = ctl.DoAction(name, NewControlAction(ACTION_STOP))
	c.Check(rv, IsNil)

	c.Check(MONITOR_NOT, Equals, ctl.State(process).Monitor)

	rv = ctl.DoAction(name, NewControlAction(ACTION_MONITOR))
	c.Check(3, Equals, fem.numStartMonitoringCalled)
	c.Check(rv, IsNil)

	c.Check(MONITOR_INIT, Equals, ctl.State(process).Monitor)
}
Beispiel #8
0
func (s *ControlSuite) TestDepends(c *C) {
	configManager := &ConfigManager{
		Settings: &Settings{PersistFile: gonitPersistFile},
	}
	ctl := &Control{ConfigManager: configManager, EventMonitor: &FakeEventMonitor{}}

	name := "depsimple"
	process := helper.NewTestProcess(name, nil, false)
	defer helper.Cleanup(process)

	nprocesses := 4
	var oprocesses []string

	for i := 0; i < nprocesses; i++ {
		dname := fmt.Sprintf("%s_dep_%d", name, i)
		dprocess := helper.NewTestProcess(dname, nil, false)
		defer helper.Cleanup(dprocess)

		err := ctl.Config().AddProcess(groupName, dprocess)
		c.Check(err, IsNil)
		if i%2 == 0 {
			process.DependsOn = append(process.DependsOn, dname)
		} else {
			oprocesses = append(oprocesses, dname)
		}
	}

	err := ctl.Config().AddProcess(groupName, process)
	c.Check(err, IsNil)

	// start main process
	rv := ctl.DoAction(name, NewControlAction(ACTION_START))
	c.Check(rv, IsNil)

	c.Check(true, Equals, process.IsRunning())

	// stop main process
	rv = ctl.DoAction(name, NewControlAction(ACTION_STOP))
	c.Check(rv, IsNil)
	c.Check(false, Equals, process.IsRunning())

	// save pids to verify deps are not restarted
	var dpids = make([]int, len(process.DependsOn))

	// dependencies should still be running
	for i, dname := range process.DependsOn {
		dprocess, _ := ctl.Config().FindProcess(dname)
		c.Check(true, Equals, dprocess.IsRunning())
		dpids[i], err = dprocess.Pid()
		c.Check(err, IsNil)
	}

	// check start count for main process and deps

	c.Check(1, Equals, ctl.State(process).Starts)

	for _, dname := range process.DependsOn {
		dprocess, _ := ctl.Config().FindProcess(dname)
		c.Check(dprocess, NotNil)
		c.Check(1, Equals, ctl.State(dprocess).Starts)
	}

	// other processes should not have been started
	for _, oname := range oprocesses {
		oprocess, _ := ctl.Config().FindProcess(oname)
		c.Check(oprocess, NotNil)
		c.Check(0, Equals, ctl.State(oprocess).Starts)
	}

	// test start/stop of dependant

	// start main sevice
	rv = ctl.DoAction(name, NewControlAction(ACTION_START))
	c.Check(rv, IsNil)
	c.Check(true, Equals, process.IsRunning())
	c.Check(2, Equals, ctl.State(process).Starts)

	// dependencies should still be running w/ same pids
	for i, dname := range process.DependsOn {
		dprocess, _ := ctl.Config().FindProcess(dname)
		c.Check(true, Equals, dprocess.IsRunning())
		pid, err := dprocess.Pid()
		c.Check(err, IsNil)
		c.Check(dpids[i], Equals, pid)
	}

	// stop a dependency
	rv = ctl.DoAction(process.DependsOn[0], NewControlAction(ACTION_STOP))
	c.Check(rv, IsNil)

	// dependent will also stop
	c.Check(false, Equals, process.IsRunning())

	// start a dependency
	rv = ctl.DoAction(process.DependsOn[0], NewControlAction(ACTION_START))
	c.Check(rv, IsNil)

	// main process will come back up
	c.Check(true, Equals, process.IsRunning())

	ctl.DoAction(process.Name, NewControlAction(ACTION_STOP))

	c.Check(3, Equals, ctl.State(process).Starts)

	c.Check(MONITOR_NOT, Equals, ctl.State(process).Monitor)

	// stop all dependencies
	for _, dname := range process.DependsOn {
		ctl.DoAction(dname, NewControlAction(ACTION_STOP))
	}

	// verify every process has been stopped
	ctl.Config().VisitProcesses(func(p *Process) bool {
		c.Check(false, Equals, p.IsRunning())
		return true
	})
}
Beispiel #9
0
// start / restart / stop self-daemonized process
func (s *ProcessSuite) TestDetached(c *C) {
	// start process
	process := helper.NewTestProcess("detached", nil, true)
	defer helper.Cleanup(process)

	pid, err := process.StartProcess()
	if err != nil {
		log.Panic(err)
	}

	pause()

	c.Check(true, Equals, process.IsRunning())

	info := processInfo(process)

	c.Check(true, Equals, grandArgs(info.Args))
	assertProcessInfo(c, process, info)
	c.Check(pid, Not(Equals), info.Pid)
	pid, err = process.Pid()
	if err != nil {
		log.Panic(err)
	}
	c.Check(pid, Equals, info.Pid)

	// restart via SIGHUP
	prevPid := info.Pid

	c.Check(0, Equals, info.Restarts)

	for i := 1; i < 3; i++ {
		err = process.RestartProcess()

		pause()
		c.Check(true, Equals, process.IsRunning())

		pid, err = process.Pid()
		if err != nil {
			log.Panic(err)
		}

		c.Check(prevPid, Equals, pid)
		info = processInfo(process)
		c.Check(true, Equals, grandArgs(info.Args))
		assertProcessInfo(c, process, info)

		// SIGHUP increments restarts counter
		c.Check(i, Equals, info.Restarts)
	}

	// restart via full stop+start
	prevPid = info.Pid

	process.Restart = ""

	err = process.RestartProcess()

	pause()
	c.Check(true, Equals, process.IsRunning())

	pid, err = process.Pid()
	if err != nil {
		log.Panic(err)
	}

	c.Check(prevPid, Not(Equals), pid)
	info = processInfo(process)
	c.Check(true, Equals, grandArgs(info.Args))
	assertProcessInfo(c, process, info)

	err = process.StopProcess()
	c.Check(err, IsNil)

	pause()

	c.Check(false, Equals, process.IsRunning())
}