def test_Configuration_pop_int_fails_if_not_int(): attribute = 'att' value = 'abc' config = Configuration({attribute: value}) with pytest.raises(ConfigurationError): config.pop(int, attribute)
def create_consoles(config: Configuration, system: SystemContext) -> List[ConsoleBase]: if not config: return None, None serial = TargetFactory.create_serial(config.pop('serial'), system) ssh = TargetFactory.create_ssh(config.pop('ssh'), system) config.ensure_consumed() return serial, ssh
def parse_credentials(credentials_config: Configuration) -> Credentials: if not credentials_config: return Credentials() credentials = Credentials( credentials_config.pop('login'), credentials_config.pop('password')) credentials_config.ensure_consumed() return credentials
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 create_serial(serial_config: Configuration, system: SystemContext) -> ConsoleBase: if not serial_config: return None log.debug(f'Serial config = {serial_config}') port = serial_config.pop('port') if not port: raise TargetConfigError( 'Missing "port" attributes for serial console in the configuration file') serial = SerialConsole(port=port, baud=int(serial_config.pop('baud') or 115200), system=system) serial_config.ensure_consumed() return serial
def create_consoles(config: Optional[Configuration], system: SystemContext) -> Dict[str, ConsoleBase]: if not config: return {} consoles = {} serial = TargetFactory.create_serial( config.pop_optional(Configuration, 'serial'), system) if serial: consoles['serial'] = serial ssh = TargetFactory.create_ssh( config.pop_optional(Configuration, 'ssh'), system) if ssh: consoles['ssh'] = ssh while (config): console_name, console_dict = config.popitem() console_config = Configuration(console_dict) console_type = console_config.pop(str, 'type') if console_type == 'ssh': console = TargetFactory.create_ssh(console_config, system) elif console_type == 'serial': console = TargetFactory.create_serial(console_config, system) else: raise TargetConfigError( f'Unknown console type {console_type}. ' 'Console type must be "ssh" or "serial"') consoles[console_name] = console config.ensure_consumed() return consoles
def __populate_tests(self, tests_config: Configuration): self.tests = [] sequence = tests_config.pop('sequence') if sequence: self.tests = self.tests_from_sequence(sequence, self.test_providers)
def test_Configuration_pop_configuration(): attribute = 'att' value = {'a': 2} config = Configuration({attribute: value}) expected_value = Configuration(value) assert len(expected_value) == 1 assert config.pop(Configuration, attribute) == Configuration(value)
def create_power_control(power_config: Configuration, console: ConsoleBase) -> PowerBase: if not power_config: power_config = Configuration() POWER_SOFT = 'soft' POWER_IPPOWER9258 = 'ippower9258' POWER_UHUBCTL = 'uhubctl' POWER_LIST = [POWER_SOFT, POWER_IPPOWER9258, POWER_UHUBCTL] if len(power_config) > 1: raise TargetConfigError( 'Only one power control should be provided in the target configuration,' f' but two or more provided:{os.linesep}{power_config}') if power_config: control_type = power_config.first() elif console: control_type = POWER_SOFT else: return None if control_type not in POWER_LIST: raise TargetConfigError( f'Unsupported power control type "{control_type}". Supported types: {POWER_LIST}') power_config = power_config.pop(control_type) or Configuration() power = None if control_type == POWER_SOFT: if not console: raise TargetConfigError( 'No console available for soft power control') power = SoftPower(console) elif control_type == POWER_IPPOWER9258: host = power_config.pop('host') outlet = power_config.pop('outlet') if not host or not outlet: raise TargetConfigError( 'IP Power PDU "host" and/or "outlet" attributes are missing') port = power_config.pop('port') login = power_config.pop('login') password = power_config.pop('password') power = IPPowerPDU(outlet, host, netport=port, username=login, password=password) elif control_type == POWER_UHUBCTL: power = Uhubctl(location=power_config.pop('location'), port=power_config.pop('port')) else: raise Exception('Unreachable, unknown power controller') power_config.ensure_consumed() return power
def create_ssh(ssh_config: Configuration, system: SystemContext) -> ConsoleBase: if not ssh_config: return None log.debug('SSH config = ' + json.dumps(ssh_config.content())) target = ssh_config.pop('target') login = ssh_config.pop('login', system.credentials.login) if not target or not login: raise TargetConfigError( 'Missing "target" or "login" attributes for SSH console in the configuration file') password = ssh_config.pop('password', system.credentials.password) ssh_config.ensure_consumed() # Create a new system config to override default credentials ssh_system = deepcopy(system) ssh_system.credentials.login = login ssh_system.credentials.password = password return SSHConsole(target, system=ssh_system)
def _create_context(config: Configuration) -> PlumaContext: variables = TargetFactory.parse_variables(config.pop('variables')) system = TargetFactory.parse_system_context(config.pop('system')) serial, ssh = TargetFactory.create_consoles(config.pop('console'), system) if not serial and not ssh: log.warning("No console defined in the device configuration file") power = TargetFactory.create_power_control( config.pop('power'), ssh) config.ensure_consumed() consoles = {} if serial: consoles['serial'] = serial if ssh: consoles['ssh'] = ssh board = Board('Test board', console=consoles, power=power, system=system) return PlumaContext(board, variables=variables)
def _create_test_controller(self, board: Board, settings: Configuration) -> TestController: testrunner = TestRunner( board=board, tests=TestsConfig.create_tests(self.selected_tests(), board), sequential=True, email_on_fail=settings.pop('email_on_fail', default=False), continue_on_fail=settings.pop('continue_on_fail', default=True), skip_tasks=settings.pop('skip_tasks', default=[]), use_testcore=settings.pop('board_test_sequence', default=False)) controller = TestController( testrunner, log_func=partial(log.log, level=LogLevel.INFO), verbose_log_func=partial(log.log, level=LogLevel.NOTICE), debug_log_func=partial(log.log, level=LogLevel.DEBUG)) iterations = settings.pop('iterations') if iterations: controller.run_condition = sc_run_n_iterations( ntimes=int(iterations)) return controller
def __init__(self, config: Configuration, test_providers: List[TestsProvider]): if not config or not isinstance(config, Configuration): raise ValueError( f'Null or invalid \'config\', which must be of type \'{Configuration}\'' ) if not test_providers: raise ValueError('Null test providers passed') if not isinstance(test_providers, list): test_providers = [test_providers] self.settings_config = config.pop(SETTINGS_SECTION, Configuration()) self.test_providers = test_providers self.tests = None self.__populate_tests(config) config.ensure_consumed()
def test_Configuration_pop_str(): attribute = 'att' value = 'abc' config = Configuration({attribute: value}) assert config.pop(str, attribute) == value
def test_Configuration_pop_int_str(): attribute = 'att' value = '123' config = Configuration({attribute: value}) assert config.pop(int, attribute) == int(value)
def test_Configuration_pop_int(): attribute = 'att' value = 123 config = Configuration({attribute: value}) assert config.pop(int, attribute) == value
def test_Configuration_pop_config_fails_if_not_config(): attribute = 'att' value = 'a' config = Configuration({attribute: value}) with pytest.raises(ConfigurationError): config.pop(Configuration, attribute)
def test_Configuration_pop_list(): attribute = 'att' value = ['a', 'b'] config = Configuration({attribute: value}) assert config.pop(list, attribute) == value
def test_Configuration_pop_bool(): attribute = 'att' value = True config = Configuration({attribute: value}) assert config.pop(bool, attribute) == value
def test_Configuration_pop_str_fails_if_not_str(): attribute = 'att' value = {'a': 2} config = Configuration({attribute: value}) with pytest.raises(ConfigurationError): config.pop(str, attribute)