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