def cli_execute(self, cmd): try: args = parse_quotes(cmd) azlogging.configure_logging(args) azure_folder = get_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) config = Configuration() self.app.initialize(config) result = self.app.execute(args) self.last_exit = 0 if result and result.result is not None: from azure.cli.core._output import OutputProducer if self.output: self.output.out(result) else: formatter = OutputProducer.get_formatter( self.app.configuration.output_format) OutputProducer(formatter=formatter, file=sys.stdout).out(result) self.last = result except Exception as ex: # pylint: disable=broad-except self.last_exit = handle_exception(ex) except SystemExit as ex: self.last_exit = int(ex.code)
def main(args, file=sys.stdout): # pylint: disable=redefined-builtin azlogging.configure_logging(args) logger.debug('Command arguments %s', args) if len(args) > 0 and args[0] == '--version': show_version_info_exit(file) azure_folder = get_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) APPLICATION.initialize(Configuration()) try: cmd_result = APPLICATION.execute(args) # Commands can return a dictionary/list of results # If they do, we print the results. if cmd_result and cmd_result.result is not None: from azure.cli.core._output import OutputProducer formatter = OutputProducer.get_formatter(APPLICATION.configuration.output_format) OutputProducer(formatter=formatter, file=file).out(cmd_result) except Exception as ex: # pylint: disable=broad-except # TODO: include additional details of the exception in telemetry telemetry.set_exception(ex, 'outer-exception', 'Unexpected exception caught during application execution.') telemetry.set_failure() error_code = handle_exception(ex) return error_code
def cli_execute(self, cmd): """ sends the command to the CLI to be executed """ try: args = parse_quotes(cmd) if args and args[0] == 'feedback': self.config.set_feedback('yes') self.user_feedback = False azure_folder = get_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) invocation = self.cli_ctx.invocation_cls( cli_ctx=self.cli_ctx, parser_cls=self.cli_ctx.parser_cls, commands_loader_cls=self.cli_ctx.commands_loader_cls, help_cls=self.cli_ctx.help_cls) if '--progress' in args: args.remove('--progress') execute_args = [args] thread = Thread(target=invocation.execute, args=execute_args) thread.daemon = True thread.start() self.threads.append(thread) self.curr_thread = thread progress_args = [self] thread = Thread(target=progress_view, args=progress_args) thread.daemon = True thread.start() self.threads.append(thread) result = None else: result = invocation.execute(args) self.last_exit = 0 if result and result.result is not None: if self.output: self.output.write(result) self.output.flush() else: formatter = self.cli_ctx.output.get_formatter( self.cli_ctx.invocation.data['output']) self.cli_ctx.output.out(result, formatter=formatter, out_file=sys.stdout) self.last = result except Exception as ex: # pylint: disable=broad-except self.last_exit = handle_exception(ex) except SystemExit as ex: self.last_exit = int(ex.code)
def test_handle_exception_keyboardinterrupt(self, mock_logger_error): # create test KeyboardInterrupt Exception keyboard_interrupt_ex = KeyboardInterrupt("KeyboardInterrupt") # call handle_exception ex_result = handle_exception(keyboard_interrupt_ex) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(ex_result, 1)
def test_handle_exception_keyboardinterrupt(self, mock_logger_error): # create test KeyboardInterrupt Exception keyboard_interrupt_ex = KeyboardInterrupt("KeyboardInterrupt") # call handle_exception ex_result = handle_exception(keyboard_interrupt_ex) # test behavior self.assertFalse(mock_logger_error.called) self.assertEqual(ex_result, 1)
def cli_execute(self, cmd): """ sends the command to the CLI to be executed """ try: args = parse_quotes(cmd) if args and args[0] == 'feedback': self.config.set_feedback('yes') self.user_feedback = False azure_folder = get_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) invocation = self.cli_ctx.invocation_cls(cli_ctx=self.cli_ctx, parser_cls=self.cli_ctx.parser_cls, commands_loader_cls=self.cli_ctx.commands_loader_cls, help_cls=self.cli_ctx.help_cls) if '--progress' in args: args.remove('--progress') execute_args = [args] thread = Thread(target=invocation.execute, args=execute_args) thread.daemon = True thread.start() self.threads.append(thread) self.curr_thread = thread progress_args = [self] thread = Thread(target=progress_view, args=progress_args) thread.daemon = True thread.start() self.threads.append(thread) result = None else: result = invocation.execute(args) self.last_exit = 0 if result and result.result is not None: from azure.cli.core._output import OutputProducer if self.output: self.output.write(result) self.output.flush() else: formatter = OutputProducer.get_formatter(self.cli_ctx.invocation.data['output']) OutputProducer(formatter=formatter).out(result) self.last = result except Exception as ex: # pylint: disable=broad-except self.last_exit = handle_exception(ex) except SystemExit as ex: self.last_exit = int(ex.code)
def cli_execute(self, cmd): """ sends the command to the CLI to be executed """ try: args = parse_quotes(cmd) azlogging.configure_logging(args) if len(args) > 0 and args[0] == 'feedback': SHELL_CONFIGURATION.set_feedback('yes') self.user_feedback = False azure_folder = get_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) self.app.initialize(Configuration()) if '--progress' in args: args.remove('--progress') thread = ExecuteThread(self.app.execute, args) thread.daemon = True thread.start() self.threads.append(thread) self.curr_thread = thread thread = ProgressViewThread(progress_view, self) thread.daemon = True thread.start() self.threads.append(thread) result = None else: result = self.app.execute(args) self.last_exit = 0 if result and result.result is not None: from azure.cli.core._output import OutputProducer if self.output: self.output.write(result) self.output.flush() else: formatter = OutputProducer.get_formatter( self.app.configuration.output_format) OutputProducer(formatter=formatter, file=self.output).out(result) self.last = result except Exception as ex: # pylint: disable=broad-except self.last_exit = handle_exception(ex) except SystemExit as ex: self.last_exit = int(ex.code)
def test_handle_exception_clierror(self, mock_logger_error): from knack.util import CLIError # create test CLIError Exception err_msg = "Error Message" cli_error = CLIError(err_msg) # call handle_exception ex_result = handle_exception(cli_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertIn(err_msg, mock_logger_error.call_args.args[0]) self.assertEqual(ex_result, 1)
def test_handle_exception_clierror(self, mock_logger_error): from knack.util import CLIError # create test CLIError Exception err_msg = "Error Message" cli_error = CLIError(err_msg) # call handle_exception ex_result = handle_exception(cli_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(mock.call(err_msg), mock_logger_error.call_args) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_no_response_text(self, mock_logger_error): # test no response text # create test HttpOperationError Exception response_text = "" mock_http_error = self._get_mock_HttpOperationError(response_text) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertIn(str(mock.call(mock_http_error).args[0]), mock_logger_error.call_args.args[0]) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_no_response_text(self, mock_logger_error): # test no response text # create test HttpOperationError Exception response_text = "" mock_http_error = self._get_mock_HttpOperationError(response_text) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(mock.call(mock_http_error), mock_logger_error.call_args) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_no_error_key(self, mock_logger_error): # test error not in response # create test HttpOperationError Exception err_msg = "BadRequest" err = dict(foo=err_msg) response_text = json.dumps(err) mock_http_error = self._get_mock_HttpOperationError(response_text) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertIn(str(mock.call(mock_http_error).args[0]), mock_logger_error.call_args.args[0]) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_typical_response_error(self, mock_logger_error): # create test HttpOperationError Exception err_msg = "Bad Request because of some incorrect param" err_code = "BadRequest" err = dict(error=dict(code=err_code, message=err_msg)) response_text = json.dumps(err) mock_http_error = self._get_mock_HttpOperationError(response_text) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertIn(err_msg, mock_logger_error.call_args.args[0]) self.assertIn(err_code, mock_logger_error.call_args.args[0]) self.assertEqual(ex_result, 1)
def test_handle_exception_clouderror(self, mock_logger_error): from msrestazure.azure_exceptions import CloudError # create test CloudError Exception err_detail = "There was a Cloud Error." err_msg = "CloudError" mock_cloud_error = mock.MagicMock(spec=CloudError) mock_cloud_error.args = (err_detail, err_msg) # call handle_exception ex_result = handle_exception(mock_cloud_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertIn(mock_cloud_error.args[0], mock_logger_error.call_args.args[0]) self.assertEqual(ex_result, 1)
def test_handle_exception_clouderror(self, mock_logger_error): from msrestazure.azure_exceptions import CloudError # create test CloudError Exception err_detail = "There was a Cloud Error." err_msg = "CloudError" mock_cloud_error = mock.MagicMock(spec=CloudError) mock_cloud_error.args = (err_detail, err_msg) # call handle_exception ex_result = handle_exception(mock_cloud_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(mock.call(mock_cloud_error.args[0]), mock_logger_error.call_args) self.assertEqual(ex_result, mock_cloud_error.args[1])
def test_handle_exception_httpoperationerror_no_error_key(self, mock_logger_error): # test error not in response # create test HttpOperationError Exception err_msg = "BadRequest" err = dict(foo=err_msg) response_text = json.dumps(err) mock_http_error = self._get_mock_HttpOperationError(response_text) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(mock.call(mock_http_error), mock_logger_error.call_args) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_typical_response_error(self, mock_logger_error): # create test HttpOperationError Exception err_msg = "Bad Request because of some incorrect param" err_code = "BadRequest" err = dict(error=dict(code=err_code, message=err_msg)) response_text = json.dumps(err) mock_http_error = self._get_mock_HttpOperationError(response_text) expected_call = mock.call("%s%s", "{} - ".format(err_code), err_msg) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(expected_call, mock_logger_error.call_args) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_error_key_has_string_value(self, mock_logger_error): # test error in response, but has str value. # create test HttpOperationError Exception err_msg = "BadRequest" err = dict(error=err_msg) response_text = json.dumps(err) mock_http_error = self._get_mock_HttpOperationError(response_text) expected_message = "{}".format(err_msg) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertIn(expected_message, mock_logger_error.call_args.args[0]) self.assertEqual(ex_result, 1)
def test_handle_exception_httpoperationerror_error_key_has_string_value(self, mock_logger_error): # test error in response, but has str value. # create test HttpOperationError Exception err_msg = "BadRequest" err = dict(error=err_msg) response_text = json.dumps(err) mock_http_error = self._get_mock_HttpOperationError(response_text) expected_message = "{}".format(err_msg) # call handle_exception ex_result = handle_exception(mock_http_error) # test behavior self.assertTrue(mock_logger_error.called) self.assertEqual(mock.call(expected_message), mock_logger_error.call_args) self.assertEqual(ex_result, 1)
def main(args, output=sys.stdout, logging_stream=None): configure_logging(args, logging_stream) logger = get_az_logger(__name__) logger.debug('Command arguments %s', args) if args and (args[0] == '--version' or args[0] == '-v'): show_version_info_exit(output) azure_folder = get_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) APPLICATION.initialize(Configuration()) try: cmd_result = APPLICATION.execute(args) # Commands can return a dictionary/list of results # If they do, we print the results. if cmd_result and cmd_result.result is not None: from azure.cli.core._output import OutputProducer formatter = OutputProducer.get_formatter( APPLICATION.configuration.output_format) OutputProducer(formatter=formatter, file=output).out(cmd_result) except Exception as ex: # pylint: disable=broad-except # TODO: include additional details of the exception in telemetry telemetry.set_exception( ex, 'outer-exception', 'Unexpected exception caught during application execution.') telemetry.set_failure() error_code = handle_exception(ex) return error_code
def exception_handler(self, ex): # pylint: disable=no-self-use from azure.cli.core.util import handle_exception return handle_exception(ex)