def test_determine_current_mode_call_defined_modes( self, defined_modes_by_prompt): prompt = "test" defined_modes_by_prompt.return_value = {prompt: self._command_mode} self._session.probe_for_prompt.return_value = prompt CommandModeHelper.determine_current_mode(self._session, self._command_mode, self._logger) defined_modes_by_prompt.assert_called_once_with(self._command_mode)
def test_determine_current_mode_raise_exception(self, defined_modes_by_prompt): prompt = "test" defined_modes_by_prompt.return_value = {prompt: self._command_mode} self._session.probe_for_prompt = Mock(side_effect=Exception()) exception = CommandModeException with self.assertRaises(exception): CommandModeHelper.determine_current_mode(self._session, self._command_mode, self._logger)
def test_determine_current_mode_call_probe_for_prompt( self, defined_modes_by_prompt): prompt = "test" defined_modes = {prompt: self._command_mode} defined_modes_by_prompt.return_value = defined_modes self._session.probe_for_prompt.return_value = prompt CommandModeHelper.determine_current_mode(self._session, self._command_mode, self._logger) prompts_re = r"|".join(defined_modes.keys()) self._session.probe_for_prompt.assert_called_once_with( expected_string=prompts_re, logger=self._logger)
def test_determine_current_mode_return_mode(self, defined_modes_by_prompt): prompt = "test" defined_modes_by_prompt.return_value = {prompt: self._command_mode} self._session.probe_for_prompt.return_value = prompt mode = CommandModeHelper.determine_current_mode( self._session, self._command_mode, self._logger) self.assertTrue(mode == self._command_mode)
def reconnect(self, timeout=None): """Reconnect session, keep current command mode. :param timeout: Timeout for operation """ prompts_re = r"|".join( CommandModeHelper.defined_modes_by_prompt(self.command_mode).keys() ) self.session.reconnect(prompts_re, self._logger, timeout) self._initialize(self.command_mode)
def _initialize(self, requested_command_mode): """Initialize. :type requested_command_mode: cloudshell.cli.command_mode.CommandMode """ self.command_mode = CommandModeHelper.determine_current_mode( self.session, requested_command_mode, self._logger) self.command_mode.enter_actions(self) self.command_mode.prompt_actions(self, self._logger) self._change_mode(requested_command_mode)
def __init__(self, cli, resource_config, logger): """Checkpoint cli configurator. :param cloudshell.cli.service.cli.CLI cli: :param cloudshell.shell.standards.firewall.resource_config. FirewallResourceConfig resource_config: :param logging.logger logger: """ super(CheckpointCliConfigurator, self).__init__(resource_config, logger, cli) self.modes = CommandModeHelper.create_command_mode(resource_config)
def _change_mode(self, requested_command_mode): """Change command mode. :param requested_command_mode: :type requested_command_mode: CommandMode """ if requested_command_mode: steps = CommandModeHelper.calculate_route_steps( self.command_mode, requested_command_mode ) list(map(lambda x: x(self, self._logger), steps))
def _change_mode(self, requested_command_mode): """Change command mode. :param requested_command_mode: :type requested_command_mode: CommandMode """ if requested_command_mode: steps = CommandModeHelper.calculate_route_steps( self.command_mode, requested_command_mode) for s in steps: s(self, self._logger)
def __enter__(self): """Enter. :rtype: CliService """ prompts_re = r"|".join( CommandModeHelper.defined_modes_by_prompt( self._command_mode).keys()) self._active_session = self._session_pool.get_session( self._defined_sessions, prompts_re, self._logger) try: return self._initialize_cli_service(self._active_session, prompts_re) except Exception: self._session_pool.remove_session(self._active_session, self._logger) raise
def __init__( self, resource_config: NetworkingResourceConfig, api: CloudShellAPISession, logger: Logger, cli: CLI, ): super().__init__(resource_config=resource_config, logger=logger, cli=cli) self._resource_config = resource_config self.modes = CommandModeHelper.create_command_mode( resource_config, api) self._registered_sessions = ( *self._registered_sessions, # type: ignore ConsoleSSHSession, ConsoleTelnetSession, ConsoleTelnetSessionNewLine, )
def __init__(self, cli, resource_config, logger): super(CiscoCliHandler, self).__init__(resource_config, logger, cli) self.modes = CommandModeHelper.create_command_mode(resource_config)