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 |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 21 | from tempest.common import service_client |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 22 | from tempest.common import waiters |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 23 | from tempest import config |
| 24 | |
| 25 | CONF = config.CONF |
| 26 | |
| 27 | LOG = logging.getLogger(__name__) |
| 28 | |
| 29 | |
Andrea Frittoli (andreaf) | 476e919 | 2015-08-14 23:59:58 +0100 | [diff] [blame] | 30 | def create_test_server(clients, validatable=False, validation_resources=None, |
Joe Gordon | 8843f0f | 2015-03-17 15:07:34 -0700 | [diff] [blame] | 31 | tenant_network=None, wait_until=None, |
| 32 | volume_backed=False, **kwargs): |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 33 | """Common wrapper utility returning a test server. |
| 34 | |
| 35 | This method is a common wrapper returning a test server that can be |
| 36 | pingable or sshable. |
| 37 | |
Takashi NATSUME | 6d5a2b4 | 2015-09-08 11:27:49 +0900 | [diff] [blame] | 38 | :param clients: Client manager which provides OpenStack Tempest clients. |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 39 | :param validatable: Whether the server will be pingable or sshable. |
| 40 | :param validation_resources: Resources created for the connection to the |
| 41 | server. Include a keypair, a security group and an IP. |
Ken'ichi Ohmichi | d5bc31a | 2015-09-02 01:45:28 +0000 | [diff] [blame] | 42 | :param tenant_network: Tenant network to be used for creating a server. |
Ken'ichi Ohmichi | fc25e69 | 2015-09-02 01:48:06 +0000 | [diff] [blame] | 43 | :param wait_until: Server status to wait for the server to reach after |
| 44 | its creation. |
Joe Gordon | 8843f0f | 2015-03-17 15:07:34 -0700 | [diff] [blame] | 45 | :param volume_backed: Whether the instance is volume backed or not. |
lei zhang | dd552b2 | 2015-11-25 20:41:48 +0800 | [diff] [blame] | 46 | :returns: a tuple |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 47 | """ |
| 48 | |
| 49 | # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE |
| 50 | |
Yaroslav Lobankov | 32204f3 | 2015-12-25 15:24:53 +0300 | [diff] [blame] | 51 | name = kwargs.pop('name', data_utils.rand_name(__name__ + "-instance")) |
Ken'ichi Ohmichi | f2d436e | 2015-09-03 01:13:16 +0000 | [diff] [blame] | 52 | flavor = kwargs.pop('flavor', CONF.compute.flavor_ref) |
| 53 | image_id = kwargs.pop('image_id', CONF.compute.image_ref) |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 54 | |
| 55 | kwargs = fixed_network.set_networks_kwarg( |
| 56 | tenant_network, kwargs) or {} |
| 57 | |
| 58 | if CONF.validation.run_validation and validatable: |
| 59 | # As a first implementation, multiple pingable or sshable servers will |
| 60 | # not be supported |
| 61 | if 'min_count' in kwargs or 'max_count' in kwargs: |
| 62 | msg = ("Multiple pingable or sshable servers not supported at " |
| 63 | "this stage.") |
| 64 | raise ValueError(msg) |
| 65 | |
| 66 | if 'security_groups' in kwargs: |
| 67 | kwargs['security_groups'].append( |
| 68 | {'name': validation_resources['security_group']['name']}) |
| 69 | else: |
| 70 | try: |
| 71 | kwargs['security_groups'] = [ |
| 72 | {'name': validation_resources['security_group']['name']}] |
| 73 | except KeyError: |
| 74 | LOG.debug("No security group provided.") |
| 75 | |
| 76 | if 'key_name' not in kwargs: |
| 77 | try: |
| 78 | kwargs['key_name'] = validation_resources['keypair']['name'] |
| 79 | except KeyError: |
| 80 | LOG.debug("No key provided.") |
| 81 | |
| 82 | if CONF.validation.connect_method == 'floating': |
Ken'ichi Ohmichi | fc25e69 | 2015-09-02 01:48:06 +0000 | [diff] [blame] | 83 | if wait_until is None: |
| 84 | wait_until = 'ACTIVE' |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 85 | |
Joe Gordon | 8843f0f | 2015-03-17 15:07:34 -0700 | [diff] [blame] | 86 | if volume_backed: |
| 87 | volume_name = data_utils.rand_name('volume') |
John Griffith | bc678ad | 2015-09-29 09:38:39 -0600 | [diff] [blame] | 88 | volumes_client = clients.volumes_v2_client |
| 89 | if CONF.volume_feature_enabled.api_v1: |
| 90 | volumes_client = clients.volumes_client |
| 91 | volume = volumes_client.create_volume( |
Joe Gordon | 8843f0f | 2015-03-17 15:07:34 -0700 | [diff] [blame] | 92 | display_name=volume_name, |
| 93 | imageRef=image_id) |
John Griffith | bc678ad | 2015-09-29 09:38:39 -0600 | [diff] [blame] | 94 | volumes_client.wait_for_volume_status(volume['volume']['id'], |
| 95 | 'available') |
Joe Gordon | 8843f0f | 2015-03-17 15:07:34 -0700 | [diff] [blame] | 96 | |
| 97 | bd_map_v2 = [{ |
| 98 | 'uuid': volume['volume']['id'], |
| 99 | 'source_type': 'volume', |
| 100 | 'destination_type': 'volume', |
| 101 | 'boot_index': 0, |
| 102 | 'delete_on_termination': True}] |
| 103 | kwargs['block_device_mapping_v2'] = bd_map_v2 |
| 104 | |
| 105 | # Since this is boot from volume an image does not need |
| 106 | # to be specified. |
| 107 | image_id = '' |
| 108 | |
Ken'ichi Ohmichi | f2d436e | 2015-09-03 01:13:16 +0000 | [diff] [blame] | 109 | body = clients.servers_client.create_server(name=name, imageRef=image_id, |
| 110 | flavorRef=flavor, |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 111 | **kwargs) |
| 112 | |
| 113 | # handle the case of multiple servers |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 114 | servers = [] |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 115 | if 'min_count' in kwargs or 'max_count' in kwargs: |
| 116 | # Get servers created which name match with name param. |
| 117 | body_servers = clients.servers_client.list_servers() |
| 118 | servers = \ |
| 119 | [s for s in body_servers['servers'] if s['name'].startswith(name)] |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 120 | else: |
| 121 | body = service_client.ResponseBody(body.response, body['server']) |
| 122 | servers = [body] |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 123 | |
| 124 | # The name of the method to associate a floating IP to as server is too |
| 125 | # long for PEP8 compliance so: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 126 | assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 127 | |
Ken'ichi Ohmichi | fc25e69 | 2015-09-02 01:48:06 +0000 | [diff] [blame] | 128 | if wait_until: |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 129 | for server in servers: |
| 130 | try: |
Ken'ichi Ohmichi | 0eb153c | 2015-07-13 02:18:25 +0000 | [diff] [blame] | 131 | waiters.wait_for_server_status( |
Ken'ichi Ohmichi | fc25e69 | 2015-09-02 01:48:06 +0000 | [diff] [blame] | 132 | clients.servers_client, server['id'], wait_until) |
Joseph Lanoux | b3e1f87 | 2015-01-30 11:13:07 +0000 | [diff] [blame] | 133 | |
| 134 | # Multiple validatable servers are not supported for now. Their |
| 135 | # creation will fail with the condition above (l.58). |
| 136 | if CONF.validation.run_validation and validatable: |
| 137 | if CONF.validation.connect_method == 'floating': |
| 138 | assoc(floating_ip=validation_resources[ |
| 139 | 'floating_ip']['ip'], |
| 140 | server_id=servers[0]['id']) |
| 141 | |
| 142 | except Exception: |
| 143 | with excutils.save_and_reraise_exception(): |
| 144 | if ('preserve_server_on_error' not in kwargs |
| 145 | or kwargs['preserve_server_on_error'] is False): |
| 146 | for server in servers: |
| 147 | try: |
| 148 | clients.servers_client.delete_server( |
| 149 | server['id']) |
| 150 | except Exception: |
| 151 | LOG.exception('Deleting server %s failed' |
| 152 | % server['id']) |
| 153 | |
| 154 | return body, servers |