Best Python code snippet using tempest_python
init.py
Source:init.py  
...27test_id_option=--load-list $IDFILE28test_list_option=--list29group_regex=([^\.]*\.)*30"""31def get_tempest_default_config_dir():32    """Returns the correct default config dir to support both cases of33    tempest being or not installed in a virtualenv.34    Cases considered:35    - no virtual env, python2: real_prefix and base_prefix not set36    - no virtual env, python3: real_prefix not set, base_prefix set and37      identical to prefix38    - virtualenv, python2: real_prefix and prefix are set and different39    - virtualenv, python3: real_prefix not set, base_prefix and prefix are40      set and identical41    - pyvenv, any python version: real_prefix not set, base_prefix and prefix42      are set and different43    :return: default config dir44    """45    real_prefix = getattr(sys, 'real_prefix', None)46    base_prefix = getattr(sys, 'base_prefix', None)47    prefix = sys.prefix48    if (real_prefix is None and49            (base_prefix is None or base_prefix == prefix)):50        # Probably not running in a virtual environment.51        # NOTE(andreaf) we cannot distinguish this case from the case of52        # a virtual environment created with virtualenv, and running python3.53        # Also if it appears we are not in virtual env and fail to find54        # global config: '/etc/tempest', fall back to55        # '[sys.prefix]/etc/tempest'56        global_conf_dir = '/etc/tempest'57        if os.path.isdir(global_conf_dir):58            return global_conf_dir59        else:60            return os.path.join(prefix, 'etc/tempest')61    else:62        return os.path.join(prefix, 'etc/tempest')63class TempestInit(command.Command):64    """Setup a local working environment for running tempest"""65    def get_parser(self, prog_name):66        parser = super(TempestInit, self).get_parser(prog_name)67        parser.add_argument('dir', nargs='?', default=os.getcwd())68        parser.add_argument('--config-dir', '-c', default=None)69        return parser70    def generate_testr_conf(self, local_path):71        testr_conf_path = os.path.join(local_path, '.testr.conf')72        top_level_path = os.path.dirname(os.path.dirname(__file__))73        discover_path = os.path.join(top_level_path, 'test_discover')74        testr_conf = TESTR_CONF % (top_level_path, discover_path)75        with open(testr_conf_path, 'w+') as testr_conf_file:76            testr_conf_file.write(testr_conf)77    def update_local_conf(self, conf_path, lock_dir, log_dir):78        config_parse = moves.configparser.SafeConfigParser()79        config_parse.optionxform = str80        with open(conf_path, 'w+') as conf_file:81            config_parse.readfp(conf_file)82            # Set local lock_dir in tempest conf83            if not config_parse.has_section('oslo_concurrency'):84                config_parse.add_section('oslo_concurrency')85            config_parse.set('oslo_concurrency', 'lock_path', lock_dir)86            # Set local log_dir in tempest conf87            config_parse.set('DEFAULT', 'log_dir', log_dir)88            # Set default log filename to tempest.log89            config_parse.set('DEFAULT', 'log_file', 'tempest.log')90    def copy_config(self, etc_dir, config_dir):91        shutil.copytree(config_dir, etc_dir)92    def generate_sample_config(self, local_dir, config_dir):93        conf_generator = os.path.join(config_dir,94                                      'config-generator.tempest.conf')95        subprocess.call(['oslo-config-generator', '--config-file',96                         conf_generator],97                        cwd=local_dir)98    def create_working_dir(self, local_dir, config_dir):99        # Create local dir if missing100        if not os.path.isdir(local_dir):101            LOG.debug('Creating local working dir: %s' % local_dir)102            os.mkdir(local_dir)103        else:104            raise OSError("Directory you are trying to initialize already "105                          "exists: %s" % local_dir)106        lock_dir = os.path.join(local_dir, 'tempest_lock')107        etc_dir = os.path.join(local_dir, 'etc')108        config_path = os.path.join(etc_dir, 'tempest.conf')109        log_dir = os.path.join(local_dir, 'logs')110        testr_dir = os.path.join(local_dir, '.testrepository')111        # Create lock dir112        if not os.path.isdir(lock_dir):113            LOG.debug('Creating lock dir: %s' % lock_dir)114            os.mkdir(lock_dir)115        # Create log dir116        if not os.path.isdir(log_dir):117            LOG.debug('Creating log dir: %s' % log_dir)118            os.mkdir(log_dir)119        # Create and copy local etc dir120        self.copy_config(etc_dir, config_dir)121        # Generate the sample config file122        self.generate_sample_config(local_dir, config_dir)123        # Update local confs to reflect local paths124        self.update_local_conf(config_path, lock_dir, log_dir)125        # Generate a testr conf file126        self.generate_testr_conf(local_dir)127        # setup local testr working dir128        if not os.path.isdir(testr_dir):129            subprocess.call(['testr', 'init'], cwd=local_dir)130    def take_action(self, parsed_args):131        config_dir = parsed_args.config_dir or get_tempest_default_config_dir()...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
