def run(self, action_name): """Run an action over a service""" # A service using run become the calling point self.origin = True # Prepare the service and start the master task self.prepare(action_name) action_manager_self().run()
def test_remove_task(self): """Test the behaviour of the remove_task method""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 260 task2 = Action('stop') task2.fanout = 85 task3 = Action('status') task3.fanout = 85 task4 = Action('check') task4.fanout = 148 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) task_manager.add_task(task4) task_manager.remove_task(task2) self.assertEqual(task_manager.fanout, 85) task_manager.remove_task(task3) self.assertEqual(task_manager.fanout, 148) task_manager.remove_task(task4) self.assertEqual(task_manager.fanout, 260) task_manager.remove_task(task1) self.assertFalse(task_manager.fanout) self.assertEqual(task_manager.tasks_count, 0) self.assertEqual(task_manager.tasks_done_count, 4)
def test_remove_task(self): """Test the behaviour of the remove_task method""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 260 task2 = Action('stop') task2.fanout = 85 task3 = Action('status') task3.fanout = 85 task4 = Action('check') task4.fanout = 148 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) task_manager.add_task(task4) self.assertEqual(task_manager.fanout, 85) task_manager.remove_task(task2) self.assertEqual(task_manager.fanout, 85) task_manager.remove_task(task3) self.assertEqual(task_manager.fanout, 148) task_manager.remove_task(task4) self.assertEqual(task_manager.fanout, 260) task_manager.remove_task(task1) self.assertFalse(task_manager.fanout) self.assertEqual(task_manager.tasks_count, 0) self.assertEqual(task_manager.tasks_done_count, 4)
def test_perform_action_bad_service(self): '''test perform action with a simulate service hooked to the action''' action = Action(name='start', command=':') ser = Service('TEST') ser.simulate = True ser.add_action(action) ser.run('start') task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 0)
def test_perform_action_bad_service(self): """test perform action with a simulate service hooked to the action""" action = Action(name="start", command=":") ser = Service("TEST") ser.simulate = True ser.add_action(action) ser.run("start") task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 0)
def test_perform_action(self): """test perform an action without any delay""" action = Action("start", command="/bin/true") ser = Service("TEST") ser.add_action(action) ser.run("start") task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 1) self.assertTrue(action.duration < 0.5, "Too long: %.2f > 0.5" % action.duration)
def test_perform_delayed_action(self): """test perform an action with a delay""" action = Action('start', command='sleep 0.3') ser = Service('TEST') ser.add_action(action) ser.run('start') task_manager = action_manager_self() ActionManager._instance = None self.assertEqual(task_manager.tasks_done_count, 1) self.assert_near(0.3, 0.1, action.duration)
def test_perform_action(self): """test perform an action without any delay""" action = Action('start', command='/bin/true') ser = Service('TEST') ser.add_action(action) ser.run('start') task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 1) self.assertTrue(action.duration < 0.5, "Too long: %.2f > 0.5" % action.duration)
def test_add_task_weird_values(self): """Test the method add task with task without fanout""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 60 task2 = Action('stop') task3 = Action('status') task3.fanout = 50 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) self.assertEqual(task_manager.fanout, 50) self.assertEqual(task_manager.tasks_count, 3)
def test_set_of_running_task(self): """ Test return a sets of running tasks from the property running_tasks """ task_manager = action_manager_self() task1 = Action('start') task1.fanout = 260 task2 = Action('stop') task2.fanout = 85 task3 = Action('status') task3.fanout = 148 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) self.assertTrue(task_manager.running_tasks) self.assertEqual(len(task_manager.running_tasks), 3)
def test__is_running_task(self): """Test the behaviour of the method _is_running_task""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 260 task2 = Action('stop') task2.fanout = 85 task3 = Action('status') task3.fanout = 148 task4 = Action('check') task_manager.add_task(task1) task_manager.add_task(task3) task_manager.add_task(task4) self.assertTrue(task_manager._is_running_task(task3)) self.assertTrue(task_manager._is_running_task(task4)) self.assertRaises(AssertionError, task_manager._is_running_task, None) self.assertFalse(task_manager._is_running_task(task2))
def print_running_tasks(self): '''Rewrite the current line and print the current running tasks''' rtasks = [t.parent.name for t in action_manager_self().running_tasks] if rtasks and self._show_running: tasks_disp = '[%s]' % NodeSet.fromlist(rtasks) width = min(self._pl_width, self._term_width) # truncate display to avoid buggy display when the length on # the displayed tasks is bigger than the screen width if len(tasks_disp) >= self._term_width: tasks_disp = "%s...]" % tasks_disp[:self._term_width - 4] eol = ' ' * (width - len(tasks_disp)) if not self.cleanup: eol = '' sys.stderr.write('%s%s\r' % (tasks_disp, eol)) sys.stderr.flush() self._pl_width = len(tasks_disp)
def run(self): '''Poll stdin and print current running status if needed''' self._register(sys.stdin) self._register(self._run_state) runnable = True while (runnable): for (desc, event) in self._got_events(): if event: if desc == self._run_state: # _run_state hangs up, stop the loop runnable = False elif event & (select.POLLIN | select.POLLPRI): self._flush_events() self._console.print_manager_status( action_manager_self()) # Unexpected or not wanted event else: print("Unexpected event on interactive thread") runnable = False
def run(self): '''Poll stdin and print current running status if needed''' self._register(sys.stdin) self._register(self._run_state) runnable = True while(runnable): for (desc, event) in self._got_events(): if event: if desc == self._run_state: # _run_state hangs up, stop the loop runnable = False elif event & (select.POLLIN | select.POLLPRI): self._flush_events() self._console.print_manager_status( action_manager_self()) # Unexpected or not wanted event else: print "Unexpected event on interactive thread" runnable = False
def test_add_task(self): """Test the behaviour of the add_task method""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 60 task2 = Action('stop') task2.fanout = 12 task3 = Action('status') task3.fanout = 50 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) task_manager.add_task(task3) self.assertEqual(task_manager.fanout, 12) task4 = Action('check') task4.fanout = 3 task_manager.add_task(task4) self.assertEqual(task_manager.fanout, 3) self.assertEqual(task_manager.tasks_count, 4) self.assertEqual(task_manager.tasks_done_count, 4)
def execute(self, command_line): ''' Ask for the manager to execute orders given by the command line. ''' self._mop = McOptionParser() self._mop.configure_mop() retcode = RC_OK try: (self._options, self._args) = self._mop.parse_args(command_line) self._conf = ConfigParser(self._options) # Configure ActionManager action_manager_self().default_fanout = self._conf['fanout'] action_manager_self().dryrun = self._conf['dryrun'] self.manager = self.manager or ServiceManager() # Case 0: build the graph if self._conf.get('graph', False): self.manager.load_config(self._conf['config_dir']) # Deps graph generation self._console.output( self.manager.output_graph( self._args, self._conf.get('excluded_svc', []))) # Case 1 : call services referenced in the manager with # the required action elif self._args: # Compute all services with the required action services = self._args[:-1] action = self._args[-1] # Ask for confirmation if the configuration requests it. if action in self._conf['confirm_actions'] and \ not self._conf['assumeyes'] and \ not Terminal.confirm("Are you sure to run %s action?" % action): raise UserError('Execution aborted by user') # Create a thread in interactive mode to manage # current running status if self.interactive: self.inter_thread.start() # Run tasks self.manager.call_services(services, action, conf=self._conf) retcode = self.retcode() if self._conf.get('report', 'no').lower() != 'no': r_type = self._conf.get('report', 'default') self._console.print_summary(self.actions, report=r_type) # Case 2 : Check configuration elif self._conf.get('config_dir', False): self._console.output("No actions specified, " "checking configuration...") self.manager.load_config(self._conf['config_dir']) self._console.output("%s seems good" % self._conf['config_dir']) # Case 3: Nothing to do so just print MilkCheck help else: self._mop.print_help() except (ServiceNotFoundError, ActionNotFoundError, InvalidVariableError, UndefinedVariableError, VariableAlreadyExistError, DependencyAlreadyReferenced, UnknownDependencyError, IllegalDependencyTypeError, ConfigError, ScannerError, UserError) as exc: self._logger.error(str(exc)) retcode = RC_EXCEPTION except InvalidOptionError as exc: self._logger.critical('Invalid options: %s\n' % exc) self._mop.print_help() retcode = RC_EXCEPTION except KeyboardInterrupt as exc: self._logger.error('Keyboard Interrupt') retcode = (128 + SIGINT) except ScannerError as exc: self._logger.error('Bad syntax in config file :\n%s' % exc) retcode = RC_EXCEPTION except ImportError as exc: self._logger.error('Missing python dependency: %s' % exc) for line in traceback.format_exc().splitlines()[-3:-1]: self._logger.error(line) retcode = RC_EXCEPTION except Exception as exc: # In high verbosity mode, propagate the error if (not self._conf or self._conf.get('verbosity') >= 5): traceback.print_exc(file=sys.stdout) else: self._logger.error('Unexpected Exception : %s' % exc) retcode = RC_UNKNOWN_EXCEPTION # Quit the interactive thread self.inter_thread.quit() self.inter_thread.join() return retcode
def test_instanciation(self): """Test singleton handling of action_manager_self""" task_manager = action_manager_self() other_task_manager = action_manager_self() self.assertTrue(task_manager is other_task_manager)
def execute(self, command_line): ''' Ask for the manager to execute orders given by the command line. ''' self._mop = McOptionParser() self._mop.configure_mop() retcode = RC_OK try: (self._options, self._args) = self._mop.parse_args(command_line) self._conf = ConfigParser(self._options) # Configure ActionManager action_manager_self().default_fanout = self._conf['fanout'] action_manager_self().dryrun = self._conf['dryrun'] manager = service_manager_self() # Case 0: build the graph if self._conf.get('graph', False): manager.load_config(self._conf['config_dir']) # Deps graph generation self._console.output(manager.output_graph(self._args, self._conf.get('excluded_svc', []))) # Case 1 : call services referenced in the manager with # the required action elif self._args: # Compute all services with the required action services = self._args[:-1] action = self._args[-1] # Create a thread in interactive mode to manage # current running status if self.interactive: self.inter_thread.start() # Run tasks manager.call_services(services, action, conf=self._conf) retcode = self.retcode() if self._conf.get('summary', False): self._console.print_summary(self.actions) # Case 2 : Check configuration elif self._conf.get('config_dir', False): self._console.output("No actions specified, " "checking configuration...") manager.load_config(self._conf['config_dir']) self._console.output("%s seems good" % self._conf['config_dir']) # Case 3: Nothing to do so just print MilkCheck help else: self._mop.print_help() except (ServiceNotFoundError, ActionNotFoundError, InvalidVariableError, UndefinedVariableError, VariableAlreadyExistError, DependencyAlreadyReferenced, UnknownDependencyError, IllegalDependencyTypeError, ConfigParserError, ConfigurationError, ScannerError), exc: self._logger.error(str(exc)) retcode = RC_EXCEPTION