Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 1 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | from oslo_log import log as logging |
| 17 | from oslo_utils import excutils |
| 18 | from tempest_lib.common.utils import data_utils |
| 19 | |
| 20 | from tempest.common import fixed_network |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 21 | from tempest.common import waiters |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 22 | from tempest import config |
| 23 | |
| 24 | CONF = config.CONF |
| 25 | |
| 26 | LOG = logging.getLogger(__name__) |
| 27 | |
| 28 | |
Andrea Frittoli (andreaf) | 476e919 | 2015-08-14 23:59:58 +0100 | [diff] [blame] | 29 | def create_test_server(clients, validatable=False, validation_resources=None, |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 30 | tenant_network=None, **kwargs): |
| 31 | """Common wrapper utility returning a test server. |
| 32 | |
| 33 | This method is a common wrapper returning a test server that can be |
| 34 | pingable or sshable. |
| 35 | |
| 36 | :param clients: Client manager which provides Openstack Tempest clients. |
| 37 | :param validatable: Whether the server will be pingable or sshable. |
| 38 | :param validation_resources: Resources created for the connection to the |
| 39 | server. Include a keypair, a security group and an IP. |
| 40 | :returns a tuple |
| 41 | """ |
| 42 | |
| 43 | # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE |
| 44 | |
| 45 | if 'name' in kwargs: |
| 46 | name = kwargs.pop('name') |
| 47 | else: |
| 48 | name = data_utils.rand_name(__name__ + "-instance") |
| 49 | |
| 50 | flavor = kwargs.get('flavor', CONF.compute.flavor_ref) |
| 51 | image_id = kwargs.get('image_id', CONF.compute.image_ref) |
| 52 | |
| 53 | kwargs = fixed_network.set_networks_kwarg( |
| 54 | tenant_network, kwargs) or {} |
| 55 | |
| 56 | if CONF.validation.run_validation and validatable: |
| 57 | # As a first implementation, multiple pingable or sshable servers will |
| 58 | # not be supported |
| 59 | if 'min_count' in kwargs or 'max_count' in kwargs: |
| 60 | msg = ("Multiple pingable or sshable servers not supported at " |
| 61 | "this stage.") |
| 62 | raise ValueError(msg) |
| 63 | |
| 64 | if 'security_groups' in kwargs: |
| 65 | kwargs['security_groups'].append( |
| 66 | {'name': validation_resources['security_group']['name']}) |
| 67 | else: |
| 68 | try: |
| 69 | kwargs['security_groups'] = [ |
| 70 | {'name': validation_resources['security_group']['name']}] |
| 71 | except KeyError: |
| 72 | LOG.debug("No security group provided.") |
| 73 | |
| 74 | if 'key_name' not in kwargs: |
| 75 | try: |
| 76 | kwargs['key_name'] = validation_resources['keypair']['name'] |
| 77 | except KeyError: |
| 78 | LOG.debug("No key provided.") |
| 79 | |
| 80 | if CONF.validation.connect_method == 'floating': |
| 81 | if 'wait_until' not in kwargs: |
| 82 | kwargs['wait_until'] = 'ACTIVE' |
| 83 | |
| 84 | body = clients.servers_client.create_server(name, image_id, flavor, |
| 85 | **kwargs) |
| 86 | |
| 87 | # handle the case of multiple servers |
| 88 | servers = [body] |
| 89 | if 'min_count' in kwargs or 'max_count' in kwargs: |
| 90 | # Get servers created which name match with name param. |
| 91 | body_servers = clients.servers_client.list_servers() |
| 92 | servers = \ |
| 93 | [s for s in body_servers['servers'] if s['name'].startswith(name)] |
| 94 | |
| 95 | # The name of the method to associate a floating IP to as server is too |
| 96 | # long for PEP8 compliance so: |
| 97 | assoc = clients.floating_ips_client.associate_floating_ip_to_server |
| 98 | |
| 99 | if 'wait_until' in kwargs: |
| 100 | for server in servers: |
| 101 | try: |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 102 | waiters.wait_for_server_status( |
| 103 | clients.servers_client, server['id'], kwargs['wait_until']) |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 104 | |
| 105 | # Multiple validatable servers are not supported for now. Their |
| 106 | # creation will fail with the condition above (l.58). |
| 107 | if CONF.validation.run_validation and validatable: |
| 108 | if CONF.validation.connect_method == 'floating': |
| 109 | assoc(floating_ip=validation_resources[ |
| 110 | 'floating_ip']['ip'], |
| 111 | server_id=servers[0]['id']) |
| 112 | |
| 113 | except Exception: |
| 114 | with excutils.save_and_reraise_exception(): |
| 115 | if ('preserve_server_on_error' not in kwargs |
| 116 | or kwargs['preserve_server_on_error'] is False): |
| 117 | for server in servers: |
| 118 | try: |
| 119 | clients.servers_client.delete_server( |
| 120 | server['id']) |
| 121 | except Exception: |
| 122 | LOG.exception('Deleting server %s failed' |
| 123 | % server['id']) |
| 124 | |
| 125 | return body, servers |