def test_exception_logged_once(self): mock_logger = Mock(spec=['exception']) err = Exception('another error') with self.assertRaises(LoggedException) as ctx: with logged_exception(mock_logger): with logged_exception(mock_logger): raise err self.assertIs(ctx.exception.exception, err) mock_logger.exception.assert_called_once_with(err)
def handle_bootstrap_exceptions(self): """If an exception is raised during bootstrap, handle it. Log the exception, re-raise as a LoggedException. Copy logs for the bootstrap host Tear down. (self.keep_env is ignored.) """ try: # If an exception is raised that indicates an error, log it # before tearing down so that the error is closely tied to # the failed operation. with logged_exception(logging): yield except: # If run from a windows machine may not have ssh to get # logs with self.client.ignore_soft_deadline(): with self.tear_down_client.ignore_soft_deadline(): if self.bootstrap_host is not None and _can_run_ssh(): remote = remote_from_address(self.bootstrap_host, series=self.series) copy_remote_logs(remote, self.log_dir) archive_logs(self.log_dir) self.controller_strategy.prepare() raise
def test_keyboard_interrupt_wrapped(self): mock_logger = Mock(spec=['exception']) err = KeyboardInterrupt() with self.assertRaises(LoggedException) as ctx: with logged_exception(mock_logger): raise err self.assertIs(ctx.exception.exception, err) mock_logger.exception.assert_called_once_with(err)
def test_output_logged(self): mock_logger = Mock(spec=['exception', 'info']) err = Exception('some error') err.output = 'some output' with self.assertRaises(LoggedException) as ctx: with logged_exception(mock_logger): raise err self.assertIs(ctx.exception.exception, err) mock_logger.exception.assert_called_once_with(err) mock_logger.info.assert_called_once_with( 'Output from exception:\nstdout:\n%s\nstderr:\n%s', 'some output', None)
def runtime_context(self, addable_machines): """Context for running non-bootstrap operations. If any manual machines need to be added, they will be added before control is yielded. """ try: with logged_exception(logging): if len(self.known_hosts) == 0: self.known_hosts.update( self.controller_strategy.get_hosts()) if addable_machines is not None: self.client.add_ssh_machines(addable_machines) yield except: if self.has_controller: safe_print_status(self.client) else: logging.info("Client lost controller, not calling status.") raise else: if self.has_controller: with self.client.ignore_soft_deadline(): self.client.list_controllers() self.client.list_models() for m_client in self.client.iter_model_clients(): m_client.show_status() finally: with self.client.ignore_soft_deadline(): with self.tear_down_client.ignore_soft_deadline(): try: if self.has_controller: self.dump_all_logs() except KeyboardInterrupt: pass if not self.keep_env: if self.has_controller: self.collect_resource_details() self.tear_down(self.jes_enabled) unclean_resources = self.ensure_cleanup() error_if_unclean(unclean_resources)
def test_generator_exit_not_wrapped(self): mock_logger = Mock(spec_set=[]) with self.assertRaises(GeneratorExit): with logged_exception(mock_logger): raise GeneratorExit
def test_no_error_no_log(self): mock_logger = Mock(spec_set=[]) with logged_exception(mock_logger): pass