def test_no_sudo(self): """ RestartController requires root to run, no need to append executed helpers with sudo """ daemons = [ MockDaemon("sudo_restart", ["sudo service restart daemon"]), MockDaemon("sudoless_restart", ["service restart daemon"]), MockDaemon("daemon_containing_sudo", ["sudo service restart sudodaemon"]) ] # Assert that the call_helper never has to call sudo def mock_call_helper(helper): self.assertFalse(helper.startswith("sudo ")) RestartController.restart_daemons(daemons, call_helper=mock_call_helper)
def test_restart(self): """ Test the RestartController.restart_daemons method """ daemons = [ MockDaemon("should_succeed_one", ["sudo should_succeed"]), MockDaemon("should_succeed_two", ["sudo should_succeed", "sudo second_unused_helper"]), MockDaemon("should_fail", ["sudo fail"]), MockDaemon("unrestartable", []) ] # Emulate service restarting def mock_call_helper(helper): return helper == "should_succeed" # Restart each of the daemons and get the result dict restarted_daemons = RestartController.restart_daemons(daemons, call_helper=mock_call_helper) # Test that each daemon has a result self.assertEquals(len(daemons), len(restarted_daemons)) # Test that each daemon was correctly reported as restarted, failed or un-restartabe for daemon, result in restarted_daemons.items(): if "succeed" in daemon.name(): self.assertTrue(result) elif "fail" in daemon.name(): self.assertFalse(result) else: self.assertTrue(result is None)
def dispatch(self): if self.args.helper: from tracer.controllers.helper import HelperController controller = HelperController(self.args, self.packages) controller.render() elif self.args.version: print(__version__) elif self.args.resource: from tracer.controllers.resource import ResourceController controller = ResourceController(self.args) controller.render() elif self.args.restart_daemons: from tracer.controllers.restart import RestartController controller = RestartController(self.args) controller.render() else: from tracer.controllers.default import DefaultController controller = DefaultController(self.args, self.packages) if self.args.helpers: controller.render_helpers() elif self.args.interactive: controller.render_interactive() else: controller.render()
def test_restart(self): """ Test the RestartController.restart_daemons method """ daemons = [ MockDaemon("should_succeed_one", ["sudo should_succeed"]), MockDaemon("should_succeed_two", ["sudo should_succeed", "sudo second_unused_helper"]), MockDaemon("should_fail", ["sudo fail"]), MockDaemon("unrestartable", []) ] # Emulate service restarting def mock_call_helper(helper): return helper == "should_succeed" # Restart each of the daemons and get the result dict restarted_daemons = RestartController.restart_daemons( daemons, call_helper=mock_call_helper) # Test that each daemon has a result self.assertEquals(len(daemons), len(restarted_daemons)) # Test that each daemon was correctly reported as restarted, failed or un-restartabe for daemon, result in restarted_daemons.items(): if "succeed" in daemon.name(): self.assertTrue(result) elif "fail" in daemon.name(): self.assertFalse(result) else: self.assertTrue(result is None)