def prepare_and_cleanup(request): """ This fixture returns the dictionary, which contains DUT ip, IPMI, spider, list of disks. This fixture also returns the executor of commands """ # There should be dut config file added to config package and # pytest should be executed with option --dut-config=conf_name'. # # 'ip' field should be filled with valid IP string to use remote ssh executor # or it should be commented out when user want to execute tests on local machine # # User can also have own test wrapper, which runs test prepare, cleanup, etc. # Then in the config/configuration.py file there should be added path to it: # test_wrapper_dir = 'wrapper_path' LOGGER.info(f"**********Test {request.node.name} started!**********") try: dut_config = importlib.import_module(f"config.{request.config.getoption('--dut-config')}") except: dut_config = None if os.path.exists(c.test_wrapper_dir): if hasattr(dut_config, 'ip'): try: IP(dut_config.ip) except ValueError: raise Exception("IP address from configuration file is in invalid format.") TestProperties.dut = Dut(test_wrapper.prepare(request, dut_config)) elif dut_config is not None: if hasattr(dut_config, 'ip'): try: IP(dut_config.ip) if hasattr(dut_config, 'user') and hasattr(dut_config, 'password'): executor = SshExecutor(dut_config.ip, dut_config.user, dut_config.password) TestProperties.executor = executor else: raise Exception("There is no credentials in config file.") if hasattr(dut_config, 'disks'): TestProperties.dut = Dut({'ip': dut_config.ip, 'disks': dut_config.disks}) else: TestProperties.dut = Dut( {'ip': dut_config.ip, 'disks': disk_finder.find_disks()}) except ValueError: raise Exception("IP address from configuration file is in invalid format.") elif hasattr(dut_config, 'disks'): TestProperties.executor = LocalExecutor() TestProperties.dut = Dut({'disks': dut_config.disks}) else: TestProperties.executor = LocalExecutor() TestProperties.dut = Dut({'disks': disk_finder.find_disks()}) else: raise Exception( "There is neither configuration file nor test wrapper attached to tests execution.") yield TestProperties.LOGGER.info("Test cleanup") casadm.stop_all_caches() if os.path.exists(c.test_wrapper_dir): test_wrapper.cleanup(TestProperties.dut)
def __setup(cls, dut_config): if not dut_config: TestRun.block("You need to specify DUT config!") if dut_config['type'] == 'ssh': try: IP(dut_config['ip']) except ValueError: TestRun.block("IP address from config is in invalid format.") port = dut_config.get('port', 22) if 'user' in dut_config and 'password' in dut_config: cls.executor = SshExecutor(dut_config['ip'], dut_config['user'], dut_config['password'], port) else: TestRun.block("There are no credentials in config.") elif dut_config['type'] == 'local': cls.executor = LocalExecutor() else: TestRun.block("Execution type (local/ssh) is missing in DUT config!") if list(cls.item.iter_markers(name="remote_only")): if not cls.executor.is_remote(): pytest.skip() if dut_config.get('allow_disk_autoselect', False): dut_config["disks"] = disk_finder.find_disks() try: cls.dut = Dut(dut_config) except Exception as ex: TestRun.LOGGER.exception(f"Failed to setup DUT instance:\n" f"{str(ex)}\n{traceback.format_exc()}") cls.__setup_disks()
def __setup(cls): if list(cls.item.iter_markers(name="remote_only")): if not cls.executor.is_remote(): pytest.skip() Disk.plug_all_disks() if cls.config.get('allow_disk_autoselect', False): cls.config["disks"] = disk_finder.find_disks() try: cls.dut = Dut(cls.config) except Exception as ex: raise Exception(f"Failed to setup DUT instance:\n" f"{str(ex)}\n{traceback.format_exc()}") cls.__setup_disks() TestRun.LOGGER.info( f"Re-seeding random number generator with seed: {cls.random_seed}") random.seed(cls.random_seed) cls.plugin_manager.hook_post_setup()