Best Python code snippet using tempest_python
tempest.py
Source:tempest.py  
1"""2Deploy and configure Tempest for Teuthology3"""4import configparser5import contextlib6import logging7from teuthology import misc as teuthology8from teuthology import contextutil9from teuthology.exceptions import ConfigError10from teuthology.orchestra import run11log = logging.getLogger(__name__)12def get_tempest_dir(ctx):13    return '{tdir}/tempest'.format(tdir=teuthology.get_testdir(ctx))14def run_in_tempest_dir(ctx, client, cmdargs, **kwargs):15    ctx.cluster.only(client).run(16        args=[ 'cd', get_tempest_dir(ctx), run.Raw('&&'), ] + cmdargs,17        **kwargs18    )19def run_in_tempest_rgw_dir(ctx, client, cmdargs, **kwargs):20    ctx.cluster.only(client).run(21        args=[ 'cd', get_tempest_dir(ctx) + '/rgw', run.Raw('&&'), ] + cmdargs,22        **kwargs23    )24def run_in_tempest_venv(ctx, client, cmdargs, **kwargs):25    run_in_tempest_dir(ctx, client,26                        [   'source',27                            '.tox/venv/bin/activate',28                            run.Raw('&&')29                        ] + cmdargs, **kwargs)30@contextlib.contextmanager31def download(ctx, config):32    """33    Download the Tempest from github.34    Remove downloaded file upon exit.35    The context passed in should be identical to the context36    passed in to the main task.37    """38    assert isinstance(config, dict)39    log.info('Downloading Tempest...')40    for (client, cconf) in config.items():41        ctx.cluster.only(client).run(42            args=[43                'git', 'clone',44                '-b', cconf.get('force-branch', 'master'),45                'https://github.com/openstack/tempest.git',46                get_tempest_dir(ctx)47            ],48        )49        sha1 = cconf.get('sha1')50        if sha1 is not None:51            run_in_tempest_dir(ctx, client, [ 'git', 'reset', '--hard', sha1 ])52    try:53        yield54    finally:55        log.info('Removing Tempest...')56        for client in config:57            ctx.cluster.only(client).run(58                args=[ 'rm', '-rf', get_tempest_dir(ctx) ],59            )60def get_toxvenv_dir(ctx):61    return ctx.tox.venv_path62@contextlib.contextmanager63def setup_venv(ctx, config):64    """65    Setup the virtualenv for Tempest using tox.66    """67    assert isinstance(config, dict)68    log.info('Setting up virtualenv for Tempest')69    for (client, _) in config.items():70        run_in_tempest_dir(ctx, client,71            [   '{tvdir}/bin/tox'.format(tvdir=get_toxvenv_dir(ctx)),72                '-e', 'venv', '--notest'73            ])74    yield75def setup_logging(ctx, cpar):76    cpar.set('DEFAULT', 'log_dir', teuthology.get_archive_dir(ctx))77    cpar.set('DEFAULT', 'log_file', 'tempest.log')78def to_config(config, params, section, cpar):79    for (k, v) in config[section].items():80        if isinstance(v, str):81            v = v.format(**params)82        elif isinstance(v, bool):83            v = 'true' if v else 'false'84        else:85            v = str(v)86        cpar.set(section, k, v)87@contextlib.contextmanager88def configure_instance(ctx, config):89    assert isinstance(config, dict)90    log.info('Configuring Tempest')91    for (client, cconfig) in config.items():92        run_in_tempest_venv(ctx, client,93            [94                'tempest',95                'init',96                '--workspace-path',97                get_tempest_dir(ctx) + '/workspace.yaml',98                'rgw'99            ])100        # prepare the config file101        tetcdir = '{tdir}/rgw/etc'.format(tdir=get_tempest_dir(ctx))102        (remote,) = ctx.cluster.only(client).remotes.keys()103        local_conf = remote.get_file(tetcdir + '/tempest.conf.sample')104        # fill the params dictionary which allows to use templatized configs105        keystone_role = cconfig.get('use-keystone-role', None)106        if keystone_role is None \107            or keystone_role not in ctx.keystone.public_endpoints:108            raise ConfigError('the use-keystone-role is misconfigured')109        public_host, public_port = ctx.keystone.public_endpoints[keystone_role]110        params = {111            'keystone_public_host': public_host,112            'keystone_public_port': str(public_port),113        }114        cpar = configparser.ConfigParser()115        cpar.read(local_conf)116        setup_logging(ctx, cpar)117        to_config(cconfig, params, 'auth', cpar)118        to_config(cconfig, params, 'identity', cpar)119        to_config(cconfig, params, 'object-storage', cpar)120        to_config(cconfig, params, 'object-storage-feature-enabled', cpar)121        cpar.write(open(local_conf, 'w+'))122        remote.put_file(local_conf, tetcdir + '/tempest.conf')123    yield124@contextlib.contextmanager125def run_tempest(ctx, config):126    assert isinstance(config, dict)127    log.info('Configuring Tempest')128    for (client, cconf) in config.items():129        blacklist = cconf.get('blacklist', [])130        assert isinstance(blacklist, list)131        run_in_tempest_venv(ctx, client,132            [133                'tempest',134                'run',135                '--workspace-path',136                get_tempest_dir(ctx) + '/workspace.yaml',137                '--workspace',138                'rgw',139                '--regex', '^tempest.api.object_storage',140                '--black-regex', '|'.join(blacklist)141            ])142    try:143        yield144    finally:145        pass146@contextlib.contextmanager147def task(ctx, config):148    """149    Deploy and run Tempest's object storage campaign150    Example of configuration:151      overrides:152        ceph:153          conf:154            client:155              rgw keystone api version: 3156              rgw keystone accepted roles: admin,Member157              rgw keystone implicit tenants: true158              rgw keystone accepted admin roles: admin159              rgw swift enforce content length: true160              rgw swift account in url: true161              rgw swift versioning enabled: true162              rgw keystone admin domain: Default163              rgw keystone admin user: admin164              rgw keystone admin password: ADMIN165              rgw keystone admin project: admin166      tasks:167      # typically, the task should be preceded with install, ceph, tox,168      # keystone and rgw. Tox and Keystone are specific requirements169      # of tempest.py.170      - rgw:171          # it's important to match the prefix with the endpoint's URL172          # in Keystone. Additionally, if we want to test /info and its173          # accompanying stuff, the whole Swift API must be put in root174          # of the whole URL  hierarchy (read: frontend_prefix == /swift).175          frontend_prefix: /swift176          client.0:177            use-keystone-role: client.0178      - tempest:179          client.0:180            force-branch: master181            use-keystone-role: client.0182            auth:183              admin_username: admin184              admin_project_name: admin185              admin_password: ADMIN186              admin_domain_name: Default187            identity:188              uri: http://{keystone_public_host}:{keystone_public_port}/v2.0/189              uri_v3: http://{keystone_public_host}:{keystone_public_port}/v3/190              admin_role: admin191            object-storage:192              reseller_admin_role: admin193            object-storage-feature-enabled:194              container_sync: false195              discoverability: false196            blacklist:197              # please strip half of these items after merging PRs #15369198              # and #12704199              - .*test_list_containers_reverse_order.*200              - .*test_list_container_contents_with_end_marker.*201              - .*test_delete_non_empty_container.*202              - .*test_container_synchronization.*203              - .*test_get_object_after_expiration_time.*204              - .*test_create_object_with_transfer_encoding.*205    """206    assert config is None or isinstance(config, list) \207        or isinstance(config, dict), \208        'task tempest only supports a list or dictionary for configuration'209    if not ctx.tox:210        raise ConfigError('tempest must run after the tox task')211    if not ctx.keystone:212        raise ConfigError('tempest must run after the keystone task')213    all_clients = ['client.{id}'.format(id=id_)214                   for id_ in teuthology.all_roles_of_type(ctx.cluster, 'client')]215    if config is None:216        config = all_clients217    if isinstance(config, list):218        config = dict.fromkeys(config)219    overrides = ctx.config.get('overrides', {})220    # merge each client section, not the top level.221    for client in config.keys():222        if not config[client]:223            config[client] = {}224        teuthology.deep_merge(config[client], overrides.get('keystone', {}))225    log.debug('Tempest config is %s', config)226    with contextutil.nested(227        lambda: download(ctx=ctx, config=config),228        lambda: setup_venv(ctx=ctx, config=config),229        lambda: configure_instance(ctx=ctx, config=config),230        lambda: run_tempest(ctx=ctx, config=config),231        ):...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!!
