def test_copy_list(): from moler.helpers import copy_list src = [1] dst = copy_list(src, deep_copy=True) assert src == dst dst[0] = 2 assert src != dst
def __init__(self, connection, paths=None, prompt=None, newline_chars=None, options=None, operators=None, runner=None): """ :param connection: moler connection to device, terminal when command is executed :param paths: list of paths to find :param prompt: prompt on start system (where command find starts). :param newline_chars: characters to split lines. :param options: unix find command options :param operators: operators of unix command find :param runner: Runner to run command """ super(Find, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) self.options = options self.operators = operators self.paths = copy_list(paths) self.current_ret['RESULT'] = list()
def __init__(self, connection, path, content, border="END_OF_FILE", prompt=None, newline_chars=None, runner=None): """ Unix tee command :param connection: Moler connection to device, terminal when command is executed. :param path: path to file to save. :param content: content of file. A string or list of strings. :param border: border string, to finish command output :param prompt: Prompt of the starting shell :param newline_chars: Characters to split lines - list. :param runner: Runner to run command. """ super(Tee, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) self.ret_required = False self.path = path if isinstance(content, six.string_types): self._content = [content] else: self._content = copy_list( src=content) # copy of list of content to modify self.border = border self.end_sent = False self._first_line_sent = False
def __init__(self, detect_patterns, connection=None, till_occurs_times=-1, match='any', runner=None): super(LineEvent, self).__init__(connection=connection, runner=runner, till_occurs_times=till_occurs_times) self.detect_patterns = copy_list(detect_patterns) self.process_full_lines_only = False self.match = match self.convert_string_to_number = True self._prepare_parameters()
def get_unraised_exceptions(remove=True): with ConnectionObserver._exceptions_lock: if remove: list_of_exceptions = ConnectionObserver._not_raised_exceptions ConnectionObserver._not_raised_exceptions = list() return list_of_exceptions else: list_of_exceptions = copy_list(ConnectionObserver._not_raised_exceptions) return list_of_exceptions
def remove_all_devices(cls): """ Remove all created devices. :return: None """ devices = copy_list(cls._devices.keys(), deep_copy=False) for device_name in devices: cls.remove_device(name=device_name) devices_config.clear()
def iterate_over_device_states(device): states = device.states states.remove("NOT_CONNECTED") source_states = copy_list(states) target_states = copy_list(states) random.shuffle(source_states) random.shuffle(target_states) for source_state in source_states: for target_state in target_states: try: device.goto_state(source_state) device.goto_state(target_state) except Exception as exc: raise MolerException( "Cannot trigger change state: '{}' -> '{}'\n{}".format( source_state, target_state, exc))
def _runner_loop(self): """ Loop to check list of ConnectionObservers if anything to remove. :return: """ while not self._stop_loop_runner.is_set(): with self._connection_observer_lock: if self._copy_of_connections_observers != self._connections_observers: self._copy_of_connections_observers = copy_list(self._connections_observers, deep_copy=False) # ConnectionObserver is feed by registering data_received in moler connection self._check_last_feed_connection_observers() self._check_timeout_connection_observers() self._remove_unnecessary_connection_observers() time.sleep(self._tick)
def _copy_list(self, src): if src is None: ret = list() elif isinstance(src, six.string_types): ret = [src] elif isinstance(src, list) or isinstance(src, tuple): ret = copy_list(src, deep_copy=False) else: ret = [src] ret_val = [ re.compile(val) if isinstance(val, six.string_types) else val for val in ret ] return ret_val
def get_neighbour_devices(self, device_type): """ Returns list of neighbour devices of passed type. :param device_type: type of device. If None then all neighbour devices will be returned. :return: list of devices. """ neighbour_devices = list() if self._neighbour_devices is not None: if device_type is None: neighbour_devices = copy_list(src=self._neighbour_devices, deep_copy=False) else: for device in self._neighbour_devices: if isinstance(device, device_type): neighbour_devices.append(device) return neighbour_devices
def iterate_over_device_states(device): source_states = _get_all_states_from_device(device=device) target_states = copy_list(source_states) random.shuffle(source_states) random.shuffle(target_states) for source_state in source_states: for target_state in target_states: try: device.goto_state(source_state) device.goto_state(target_state) except Exception as exc: raise MolerException( "Cannot trigger change state: '{}' -> '{}'\n{}".format( source_state, target_state, exc))
def get_devices_by_type(cls, device_type): """ Returns list of devices filtered by device_type. :param device_type: type of device. If None then return all devices. :return: List of devices. Can be an empty list. """ with cls._lock_device: if device_type is None: devices = copy_list(src=cls._devices.values(), deep_copy=False) else: devices = list() for device in cls._devices.values(): if isinstance(device, device_type): devices.append(device) return devices
def remove_all_devices(cls, clear_device_history=False): """ Remove all created devices. :param clear_device_history: set True to clear the history of devices. Caution: you may overwrite your logs! :return: None """ devices = copy_list(cls._devices.keys(), deep_copy=False) for device_name in devices: cls.remove_device(name=device_name) devices_config.clear() if clear_device_history: MolerTest.warning( "All history of devices will be forgotten. The same names can be used again with" " different meaning!") cls._clear()
def _get_all_states_from_device(device): states = copy_list(device.states) states.remove("NOT_CONNECTED") for attr_name in dir(device): attr = getattr(device, attr_name) if type(attr) is str and not attr_name.startswith( '_') and attr_name not in dir(TextualDevice): if attr not in states: states.append(attr) if "PROXY_PC" in states and hasattr( device, "_use_proxy_pc") and not getattr(device, "_use_proxy_pc"): states.remove("PROXY_PC") return states
def __init__(self, connection, host, login=None, password=None, port=0, prompt=None, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", prefix=None, newline_chars=None, cmds_before_establish_connection=None, cmds_after_establish_connection=None, telnet_prompt=r"^\s*telnet>\s*", encrypt_password=True, runner=None, target_newline="\n", allowed_newline_after_prompt=False, repeat_password=True, failure_exceptions_indication=None, prompt_after_login=None, send_enter_after_connection=True, username=None): """ Moler class of Unix command telnet. :param connection: moler connection to device, terminal when command is executed. :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param prefix: prefix telnet command. :param newline_chars: characters to split lines. :param cmds_before_establish_connection: list of commands to execute by telnet command before open. :param cmds_after_establish_connection: list of commands to execute by telnet commands after establishing. :param telnet_prompt: prompt for telnet commands. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text. :param runner: Runner to run command. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param repeat_password: If True then repeat last password if no more provided. If False then exception is set. :param failure_exceptions_indication: String with regex or regex object to omit failure even if failed string was found. :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1 then leave it None. :param send_enter_after_connection: set True to send new line char(s) after connection is established, False otherwise. :param username: login for ssh. Set this or login but not both. """ super(Telnet, self).__init__( connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner, port=port, host=host, login=login, password=password, expected_prompt=expected_prompt, set_timeout=set_timeout, set_prompt=set_prompt, term_mono=term_mono, encrypt_password=encrypt_password, target_newline=target_newline, allowed_newline_after_prompt=allowed_newline_after_prompt, repeat_password=repeat_password, failure_exceptions_indication=failure_exceptions_indication, prompt_after_login=prompt_after_login, send_enter_after_connection=send_enter_after_connection, username=username) self.prefix = prefix # Parameters defined by calling the command self._re_telnet_prompt = Telnet._calculate_prompt( telnet_prompt) # Prompt for telnet commands self.cmds_before_establish_connection = copy_list( cmds_before_establish_connection) self.cmds_after_establish_connection = copy_list( cmds_after_establish_connection) # Internal variables self._telnet_command_mode = False
def _prepare_new_cycle_parameters(self): self.copy_compiled_patterns = copy_list(self.compiled_patterns) self._current_ret = []
def __init__(self, connection, host, login=None, password=None, port=0, prompt=None, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", prefix=None, newline_chars=None, cmds_before_establish_connection=None, cmds_after_establish_connection=None, telnet_prompt=r"^\s*telnet>\s*", encrypt_password=True, runner=None, target_newline="\n", allowed_newline_after_prompt=False): """ Moler class of Unix command telnet. :param connection: moler connection to device, terminal when command is executed :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param prefix: prefix telnet command. :param newline_chars: characters to split lines. :param cmds_before_establish_connection: list of commands to execute by telnet command before open. :param cmds_after_establish_connection: list of commands to execute by telnet commands after establishing. :param telnet_prompt: prompt for telnet commands. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text :param runner: Runner to run command :param target_newline: newline chars on remote system where ssh connects :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt """ super(Telnet, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self._re_telnet_prompt = CommandTextualGeneric._calculate_prompt( telnet_prompt) # Prompt for telnet commands self.login = login self.password = password self.host = host self.port = port self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.prefix = prefix self.encrypt_password = encrypt_password self.cmds_before_establish_connection = copy_list( cmds_before_establish_connection) self.cmds_after_establish_connection = copy_list( cmds_after_establish_connection) self.target_newline = target_newline self.allowed_newline_after_prompt = allowed_newline_after_prompt # Internal variables self._sent_timeout = False self._sent_prompt = False self._sent_login = False self._sent_password = False self._telnet_command_mode = False
def __init__(self, connection, host, login=None, password=None, newline_chars=None, prompt=None, runner=None, port=0, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", encrypt_password=True, target_newline="\n", allowed_newline_after_prompt=False, repeat_password=True, failure_exceptions_indication=None, prompt_after_login=None, send_enter_after_connection=True, username=None): """ Base Moler class of Unix commands telnet and ssh. :param connection: moler connection to device, terminal when command is executed. :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param newline_chars: characters to split lines. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text. :param runner: Runner to run command. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param repeat_password: If True then repeat last password if no more provided. If False then exception is set. :param failure_exceptions_indication: String with regex or regex object to omit failure even if failed string was found. :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1 then leave it None. :param send_enter_after_connection: set True to send new line char(s) after connection is established, False otherwise. """ super(GenericTelnetSsh, self).__init__( connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner, expected_prompt=expected_prompt, set_timeout=set_timeout, set_prompt=set_prompt, target_newline=target_newline, allowed_newline_after_prompt=allowed_newline_after_prompt, prompt_after_login=prompt_after_login) self.timeout = 90 # Parameters defined by calling the command self._re_failure_exceptions_indication = None if failure_exceptions_indication: self._re_failure_exceptions_indication = CommandTextualGeneric._calculate_prompt( failure_exceptions_indication) self.login = login if isinstance(password, six.string_types): self._passwords = [password] elif password is None: self._passwords = [] else: self._passwords = copy_list( password, deep_copy=False) # copy of list of passwords to modify self.host = host self.port = port self.term_mono = term_mono self.encrypt_password = encrypt_password self.repeat_password = repeat_password self.send_enter_after_connection = send_enter_after_connection # Internal variables self._sent_login = False self._last_password = "" if login and username: self.command_string = self.__class__.__name__ raise CommandFailure( self, "Please set login ('{}') or username ('{}') but not both.". format(login, username)) elif username: self.login = username self.current_ret['LINES'] = list() self.current_ret['LAST_LOGIN'] = dict() self.current_ret['FAILED_LOGIN_ATTEMPTS'] = None self._converter_helper = ConverterHelper.get_converter_helper()
def iterate_over_device_states(device, max_time=None, tests_per_device=300, max_no_of_threads=11): """ Check all states in device under test. :param device: device :param max_time: maximum time of check. None for infinity. If execution time is greater then max_time then test is interrupted. :param tests_per_device: how many tests should be performed in one thread. :param max_no_of_threads: maximum number of threads that can be started. :return: None """ source_states = _get_all_states_from_device(device=device) target_states = copy_list(source_states) nr_of_tests = len(source_states) * len(target_states) device.last_wrong_wait4_occurrence = None device.set_all_prompts_on_line(True) device._goto_state_in_production_mode = False random.shuffle(source_states) random.shuffle(target_states) tested = set() states_to_test = queue.Queue(maxsize=nr_of_tests) for source in source_states: for target in target_states: states_to_test.put([source, target]) assert states_to_test.qsize() == nr_of_tests nr_of_threads = math.ceil(nr_of_tests / float(tests_per_device)) if nr_of_threads > max_no_of_threads: nr_of_threads = max_no_of_threads thread_nr = 1 test_threads = list() exceptions = list() while thread_nr < nr_of_threads: new_connection = get_memory_device_connection() new_connection.open() new_device_name = "{}_C{}".format(device.name, thread_nr) th = _perform_device_tests_start_thread( source_device=device, tested=tested, states_to_test=states_to_test, max_time=max_time, new_device_name=new_device_name, connection=new_connection, exceptions=exceptions) test_threads.append((th, new_connection)) thread_nr += 1 _perform_device_tests(device=device, tested=tested, states_to_test=states_to_test, max_time=max_time) for th, dev_connection in test_threads: th.join() dev_connection.close() if max_time is None: assert 0 == states_to_test.qsize() for ex in exceptions: print("ex: '{}' -> '{}'.".format(ex, repr(ex))) assert 0 == len(exceptions)
def __init__(self, connection, host, login=None, password=None, newline_chars=None, prompt=None, runner=None, port=0, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", encrypt_password=True, target_newline="\n", allowed_newline_after_prompt=False, repeat_password=True, failure_exceptions_indication=None): """ Base Moler class of Unix commands telnet and ssh. :param connection: moler connection to device, terminal when command is executed. :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param newline_chars: characters to split lines. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text. :param runner: Runner to run command. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param repeat_password: If True then repeat last password if no more provided. If False then exception is set. :param failure_exceptions_indication: String with regex or regex object to omit failure even if failed string was found. """ super(GenericTelnetSsh, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self._re_failure_exceptions_indication = None if failure_exceptions_indication: self._re_failure_exceptions_indication = CommandTextualGeneric._calculate_prompt( failure_exceptions_indication) self.login = login if isinstance(password, six.string_types): self._passwords = [password] elif password is None: self._passwords = [] else: self._passwords = copy_list( password, deep_copy=False) # copy of list of passwords to modify self.host = host self.port = port self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.encrypt_password = encrypt_password self.target_newline = target_newline self.allowed_newline_after_prompt = allowed_newline_after_prompt self.repeat_password = repeat_password # Internal variables self._sent_timeout = False self._sent_prompt = False self._sent_login = False self._sent = False self._last_password = ""