Best Python code snippet using tempest_python
compute.py
Source:compute.py  
...44    if flavor is None:45        flavor = CONF.compute.flavor_ref46    if image_id is None:47        image_id = CONF.compute.image_ref48    kwargs = fixed_network.set_networks_kwarg(49        tenant_network, kwargs) or {}50    multiple_create_request = (max(kwargs.get('min_count', 0),51                                   kwargs.get('max_count', 0)) > 1)52    if CONF.validation.run_validation and validatable:53        # As a first implementation, multiple pingable or sshable servers will54        # not be supported55        if multiple_create_request:56            msg = ("Multiple pingable or sshable servers not supported at "57                   "this stage.")58            raise ValueError(msg)59        if 'security_groups' in kwargs:60            kwargs['security_groups'].append(61                {'name': validation_resources['security_group']['name']})62        else:63            try:64                kwargs['security_groups'] = [65                    {'name': validation_resources['security_group']['name']}]66            except KeyError:67                LOG.debug("No security group provided.")68        if 'key_name' not in kwargs:69            try:70                kwargs['key_name'] = validation_resources['keypair']['name']71            except KeyError:72                LOG.debug("No key provided.")73        if CONF.validation.connect_method == 'floating':74            if wait_until is None:75                wait_until = 'ACTIVE'76    if volume_backed:77        volume_name = data_utils.rand_name('volume')78        volumes_client = clients.volumes_v2_client79        if CONF.volume_feature_enabled.api_v1:80            volumes_client = clients.volumes_client81        volume = volumes_client.create_volume(82            display_name=volume_name,83            imageRef=image_id)84        waiters.wait_for_volume_status(volumes_client,85                                       volume['volume']['id'], 'available')86        bd_map_v2 = [{87            'uuid': volume['volume']['id'],88            'source_type': 'volume',89            'destination_type': 'volume',90            'boot_index': 0,91            'delete_on_termination': True}]92        kwargs['block_device_mapping_v2'] = bd_map_v293        # Since this is boot from volume an image does not need94        # to be specified.95        image_id = ''96    body = clients.servers_client.create_server(name=name, imageRef=image_id,97                                                flavorRef=flavor,98                                                **kwargs)99    # handle the case of multiple servers100    servers = []101    if multiple_create_request:102        # Get servers created which name match with name param.103        body_servers = clients.servers_client.list_servers()104        servers = \105            [s for s in body_servers['servers'] if s['name'].startswith(name)]106    else:107        body = rest_client.ResponseBody(body.response, body['server'])108        servers = [body]109    # The name of the method to associate a floating IP to as server is too110    # long for PEP8 compliance so:111    assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server112    if wait_until:113        for server in servers:114            try:115                waiters.wait_for_server_status(116                    clients.servers_client, server['id'], wait_until)117                # Multiple validatable servers are not supported for now. Their118                # creation will fail with the condition above (l.58).119                if CONF.validation.run_validation and validatable:120                    if CONF.validation.connect_method == 'floating':121                        assoc(floating_ip=validation_resources[122                              'floating_ip']['ip'],123                              server_id=servers[0]['id'])124            except Exception:125                with excutils.save_and_reraise_exception():126                    for server in servers:127                        try:128                            clients.servers_client.delete_server(129                                server['id'])130                        except Exception:131                            LOG.exception('Deleting server %s failed'132                                          % server['id'])133    return body, servers134def create_test_server_for_bdm(clients, validatable=False, validation_resources=None,135                       tenant_network=None, wait_until=None,136                       volume_backed=False, name=None, flavor=None,137                       image_id=None, **kwargs):138    """Common wrapper utility returning a test server.139    This method is a common wrapper returning a test server that can be140    pingable or sshable.141    :param clients: Client manager which provides OpenStack Tempest clients.142    :param validatable: Whether the server will be pingable or sshable.143    :param validation_resources: Resources created for the connection to the144    server. Include a keypair, a security group and an IP.145    :param tenant_network: Tenant network to be used for creating a server.146    :param wait_until: Server status to wait for the server to reach after147    its creation.148    :param volume_backed: Whether the instance is volume backed or not.149    :returns: a tuple150    """151    # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE152    if name is None:153        name = data_utils.rand_name(__name__ + "-instance")154    if flavor is None:155        flavor = CONF.compute.flavor_ref156    kwargs = fixed_network.set_networks_kwarg(157        tenant_network, kwargs) or {}158    multiple_create_request = (max(kwargs.get('min_count', 0),159                                   kwargs.get('max_count', 0)) > 1)160    if CONF.validation.run_validation and validatable:161        # As a first implementation, multiple pingable or sshable servers will162        # not be supported163        if multiple_create_request:164            msg = ("Multiple pingable or sshable servers not supported at "165                   "this stage.")166            raise ValueError(msg)167        if 'security_groups' in kwargs:168            kwargs['security_groups'].append(169                {'name': validation_resources['security_group']['name']})170        else:...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!!
