def parse_system_context(system_config: Configuration) -> SystemContext: if not system_config: return SystemContext() credentials = TargetFactory.parse_credentials(system_config.pop('credentials')) system = SystemContext(prompt_regex=system_config.pop('prompt_regex'), credentials=credentials) system_config.ensure_consumed() return system
def parse_system_context(system_config: Optional[Configuration]) -> SystemContext: if not system_config: return SystemContext() credentials = TargetFactory.parse_credentials( system_config.pop_optional(Configuration, 'credentials')) prompt_regex = system_config.pop_optional(str, 'prompt_regex', context='system') system = SystemContext(prompt_regex=prompt_regex, credentials=credentials) system_config.ensure_consumed() return system
def __init__(self, name: str, power: PowerBase = None, hub=None, storage: StorageBase = None, console: Union[ConsoleBase, Dict[str, ConsoleBase]] = None, bootstr: str = None, boot_max_s: int = None, login_user_match: str = None, login_pass_match: str = None, system: SystemContext = None): self.name = name self.power = power self.storage = storage self.hub = hub self._current_console_name: Optional[str] = None self._consoles: Dict[str, ConsoleBase] = {} self.consoles = console self.system = system or SystemContext() self.login_user_match = login_user_match or 'login:'******'Password:' self.bootstr = bootstr or self.login_user_match self.boot_max_s = boot_max_s or 60 self.last_boot_len: Optional[float] = None self.booted_to_prompt = False self.log_recurse = True
def test_TargetFactory_create_ssh_should_use_password(ssh_config): password = '******' ssh_config['password'] = password console = TargetFactory.create_ssh(Configuration(ssh_config), SystemContext()) assert console.system.credentials.password == password
def test_TargetFactory_create_serial(serial_config): port = serial_config['port'] baudrate = serial_config['baud'] config = Configuration(copy.deepcopy(serial_config)) console = TargetFactory.create_serial(config, SystemContext()) assert console.port == port assert console.baud == baudrate
def test_ConsoleBase_wait_for_prompt_should_succeed_if_matched( basic_console_class): prompt = 'user@host' console = basic_console_class(system=SystemContext(prompt_regex=prompt)) console.engine.wait_for_match = MagicMock( return_value=MatchResult(prompt, prompt, prompt)) console.wait_for_prompt(timeout=0.1)
def test_ConsoleBase_wait_for_prompt_should_error_if_not_detected( basic_console_class): prompt = 'user@host' console = basic_console_class(system=SystemContext(prompt_regex=prompt)) console.engine.wait_for_match = MagicMock( return_value=MatchResult(None, None, '')) with pytest.raises(ConsoleError): console.wait_for_prompt(timeout=0.1)
def test_TargetFactory_create_ssh(ssh_config): target = ssh_config['target'] login = ssh_config['login'] config = Configuration(copy.deepcopy(ssh_config)) console = TargetFactory.create_ssh(config, SystemContext()) assert console.target == target assert console.system.credentials.login == login assert console.system.credentials.password is None
def __init__(self, encoding: str = None, linesep: str = None, raw_logfile: str = None, system: SystemContext = None, engine: ConsoleEngine = None): self.engine = engine or PexpectEngine( linesep=linesep, encoding=encoding, raw_logfile=raw_logfile) self.system = system or SystemContext() self._requires_login = True
def test_TargetFactory_create_ssh_should_default_to_credentials(ssh_config): ssh_config.pop('login') credslogin = '******' credspassword = '******' system = SystemContext(credentials=Credentials(credslogin, credspassword)) console = TargetFactory.create_ssh(Configuration(ssh_config), system) assert console.system.credentials.login == credslogin assert console.system.credentials.password == credspassword
def test_ConsoleBase_wait_for_prompt_should_call_wait_for_match( basic_console_class): prompt = 'user@host' console = basic_console_class(system=SystemContext(prompt_regex=prompt)) timeout = 0.13 console.engine.wait_for_match = MagicMock( return_value=MatchResult(prompt, prompt, prompt)) console.wait_for_prompt(timeout=timeout) console.engine.wait_for_match.assert_called_with(match=prompt, timeout=timeout)
def test_TargetFactory_create_ssh_should_prefer_ssh_credentials(ssh_config): sshlogin = ssh_config['login'] sshpassword = '******' ssh_config['password'] = sshpassword credslogin = '******' credspassword = '******' system = SystemContext(credentials=Credentials(credslogin, credspassword)) console = TargetFactory.create_ssh(Configuration(ssh_config), system) assert console.system.credentials.login == sshlogin assert console.system.credentials.password == sshpassword
def __init__(self, encoding: str = None, linesep: str = None, raw_logfile: str = None, system: SystemContext = None): if not hasattr(self, '_pex'): raise AttributeError( "Variable '_pex' must be created by inheriting class") timestamp = datetime_to_timestamp(datetime.now()) default_raw_logfile = os.path.join( '/tmp', 'pluma', f'{self.__class__.__name__}_raw_{timestamp}.log') self.encoding = encoding or 'ascii' self.linesep = linesep or '\n' self.raw_logfile = raw_logfile or default_raw_logfile self.system = system or SystemContext() self._buffer = '' self._last_received = '' self._raw_logfile_fd = None self._pex = None self._requires_login = True
def test_TargetFactory_create_serial_should_error_if_uncomsuned(): config = Configuration({'baud': 123}) with pytest.raises(TargetConfigError): TargetFactory.create_serial(config, SystemContext())
def test_TargetFactory_create_serial_should_error_with_no_port(serial_config): serial_config.pop('port') with pytest.raises(ConfigurationError): TargetFactory.create_serial(Configuration(serial_config), SystemContext())
def test_TargetFactory_create_serial_should_error_if_unconsumed(serial_config): serial_config['unused'] = 'abc' with pytest.raises(ConfigurationError): TargetFactory.create_serial(Configuration(serial_config), SystemContext())
def minimal_ssh_console(): return SSHConsole(target="localhost", system=SystemContext(Credentials("root")))