blob: 55bc93e846e97d225e5ffb70e29c0dae920ac560 [file] [log] [blame]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +00001# 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
16from oslo_log import log as logging
17from oslo_utils import excutils
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000018
19from tempest.common import fixed_network
Joshua White02446972017-01-27 13:02:27 -080020from tempest.common.utils import data_utils
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000021from tempest.common import waiters
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000022from tempest import config
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080023from tempest.lib.common import rest_client
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000024
25CONF = config.CONF
26
27LOG = logging.getLogger(__name__)
28
29
Andrea Frittoli (andreaf)476e9192015-08-14 23:59:58 +010030def create_test_server(clients, validatable=False, validation_resources=None,
Joe Gordon8843f0f2015-03-17 15:07:34 -070031 tenant_network=None, wait_until=None,
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053032 volume_backed=False, name=None, flavor=None,
ghanshyam61db96e2016-12-16 12:49:25 +090033 image_id=None, **kwargs):
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000034 """Common wrapper utility returning a test server.
35
36 This method is a common wrapper returning a test server that can be
37 pingable or sshable.
38
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +090039 :param clients: Client manager which provides OpenStack Tempest clients.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000040 :param validatable: Whether the server will be pingable or sshable.
41 :param validation_resources: Resources created for the connection to the
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010042 server. Include a keypair, a security group and an IP.
Ken'ichi Ohmichid5bc31a2015-09-02 01:45:28 +000043 :param tenant_network: Tenant network to be used for creating a server.
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000044 :param wait_until: Server status to wait for the server to reach after
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010045 its creation.
ghanshyam61db96e2016-12-16 12:49:25 +090046 :param volume_backed: Whether the server is volume backed or not.
47 If this is true, a volume will be created and
48 create server will be requested with
49 'block_device_mapping_v2' populated with below
50 values:
51 --------------------------------------------
52 bd_map_v2 = [{
53 'uuid': volume['volume']['id'],
54 'source_type': 'volume',
55 'destination_type': 'volume',
56 'boot_index': 0,
57 'delete_on_termination': True}]
58 kwargs['block_device_mapping_v2'] = bd_map_v2
59 ---------------------------------------------
60 If server needs to be booted from volume with other
61 combination of bdm inputs than mentioned above, then
62 pass the bdm inputs explicitly as kwargs and image_id
63 as empty string ('').
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010064 :param name: Name of the server to be provisioned. If not defined a random
65 string ending with '-instance' will be generated.
66 :param flavor: Flavor of the server to be provisioned. If not defined,
67 CONF.compute.flavor_ref will be used instead.
68 :param image_id: ID of the image to be used to provision the server. If not
69 defined, CONF.compute.image_ref will be used instead.
lei zhangdd552b22015-11-25 20:41:48 +080070 :returns: a tuple
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000071 """
72
73 # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE
74
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053075 if name is None:
76 name = data_utils.rand_name(__name__ + "-instance")
77 if flavor is None:
78 flavor = CONF.compute.flavor_ref
79 if image_id is None:
80 image_id = CONF.compute.image_ref
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000081
82 kwargs = fixed_network.set_networks_kwarg(
83 tenant_network, kwargs) or {}
84
Ghanshyam4de44ae2015-12-25 10:34:00 +090085 multiple_create_request = (max(kwargs.get('min_count', 0),
86 kwargs.get('max_count', 0)) > 1)
87
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000088 if CONF.validation.run_validation and validatable:
89 # As a first implementation, multiple pingable or sshable servers will
90 # not be supported
Ghanshyam4de44ae2015-12-25 10:34:00 +090091 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000092 msg = ("Multiple pingable or sshable servers not supported at "
93 "this stage.")
94 raise ValueError(msg)
95
96 if 'security_groups' in kwargs:
97 kwargs['security_groups'].append(
98 {'name': validation_resources['security_group']['name']})
99 else:
100 try:
101 kwargs['security_groups'] = [
102 {'name': validation_resources['security_group']['name']}]
103 except KeyError:
104 LOG.debug("No security group provided.")
105
106 if 'key_name' not in kwargs:
107 try:
108 kwargs['key_name'] = validation_resources['keypair']['name']
109 except KeyError:
110 LOG.debug("No key provided.")
111
112 if CONF.validation.connect_method == 'floating':
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000113 if wait_until is None:
114 wait_until = 'ACTIVE'
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000115
Joe Gordon8843f0f2015-03-17 15:07:34 -0700116 if volume_backed:
zhuflc6ce5392016-08-17 14:34:37 +0800117 volume_name = data_utils.rand_name(__name__ + '-volume')
John Griffithbc678ad2015-09-29 09:38:39 -0600118 volumes_client = clients.volumes_v2_client
ghanshyam61db96e2016-12-16 12:49:25 +0900119 name_field = 'name'
120 if not CONF.volume_feature_enabled.api_v2:
John Griffithbc678ad2015-09-29 09:38:39 -0600121 volumes_client = clients.volumes_client
ghanshyam61db96e2016-12-16 12:49:25 +0900122 name_field = 'display_name'
123 params = {name_field: volume_name,
124 'imageRef': image_id,
125 'size': CONF.volume.volume_size}
126 volume = volumes_client.create_volume(**params)
Yaroslav Lobankoved3a35b2016-03-24 22:41:30 -0500127 waiters.wait_for_volume_status(volumes_client,
128 volume['volume']['id'], 'available')
Joe Gordon8843f0f2015-03-17 15:07:34 -0700129
130 bd_map_v2 = [{
131 'uuid': volume['volume']['id'],
132 'source_type': 'volume',
133 'destination_type': 'volume',
134 'boot_index': 0,
ghanshyam61db96e2016-12-16 12:49:25 +0900135 'delete_on_termination': True}]
Joe Gordon8843f0f2015-03-17 15:07:34 -0700136 kwargs['block_device_mapping_v2'] = bd_map_v2
137
138 # Since this is boot from volume an image does not need
139 # to be specified.
140 image_id = ''
141
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000142 body = clients.servers_client.create_server(name=name, imageRef=image_id,
143 flavorRef=flavor,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000144 **kwargs)
145
146 # handle the case of multiple servers
Ghanshyam4de44ae2015-12-25 10:34:00 +0900147 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000148 # Get servers created which name match with name param.
149 body_servers = clients.servers_client.list_servers()
150 servers = \
151 [s for s in body_servers['servers'] if s['name'].startswith(name)]
ghanshyam0f825252015-08-25 16:02:50 +0900152 else:
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -0800153 body = rest_client.ResponseBody(body.response, body['server'])
ghanshyam0f825252015-08-25 16:02:50 +0900154 servers = [body]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000155
156 # The name of the method to associate a floating IP to as server is too
157 # long for PEP8 compliance so:
John Warrene74890a2015-11-11 15:18:01 -0500158 assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000159
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000160 if wait_until:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000161 for server in servers:
162 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000163 waiters.wait_for_server_status(
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000164 clients.servers_client, server['id'], wait_until)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000165
166 # Multiple validatable servers are not supported for now. Their
167 # creation will fail with the condition above (l.58).
168 if CONF.validation.run_validation and validatable:
169 if CONF.validation.connect_method == 'floating':
170 assoc(floating_ip=validation_resources[
171 'floating_ip']['ip'],
172 server_id=servers[0]['id'])
173
174 except Exception:
175 with excutils.save_and_reraise_exception():
Jordan Pittier87ba2872016-03-08 11:43:11 +0100176 for server in servers:
177 try:
178 clients.servers_client.delete_server(
179 server['id'])
180 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100181 LOG.exception('Deleting server %s failed',
182 server['id'])
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000183
184 return body, servers
ghanshyam017b5fe2016-04-15 18:49:26 +0900185
186
ghanshyam4c1391c2016-12-01 13:13:06 +0900187def shelve_server(servers_client, server_id, force_shelve_offload=False):
ghanshyam017b5fe2016-04-15 18:49:26 +0900188 """Common wrapper utility to shelve server.
189
190 This method is a common wrapper to make server in 'SHELVED'
191 or 'SHELVED_OFFLOADED' state.
192
ghanshyam4c1391c2016-12-01 13:13:06 +0900193 :param servers_clients: Compute servers client instance.
ghanshyam017b5fe2016-04-15 18:49:26 +0900194 :param server_id: Server to make in shelve state
195 :param force_shelve_offload: Forcefully offload shelve server if it
196 is configured not to offload server
197 automatically after offload time.
198 """
ghanshyam4c1391c2016-12-01 13:13:06 +0900199 servers_client.shelve_server(server_id)
ghanshyam017b5fe2016-04-15 18:49:26 +0900200
201 offload_time = CONF.compute.shelved_offload_time
202 if offload_time >= 0:
ghanshyam4c1391c2016-12-01 13:13:06 +0900203 waiters.wait_for_server_status(servers_client, server_id,
ghanshyam017b5fe2016-04-15 18:49:26 +0900204 'SHELVED_OFFLOADED',
205 extra_timeout=offload_time)
206 else:
ghanshyam4c1391c2016-12-01 13:13:06 +0900207 waiters.wait_for_server_status(servers_client, server_id, 'SHELVED')
ghanshyam017b5fe2016-04-15 18:49:26 +0900208 if force_shelve_offload:
ghanshyam4c1391c2016-12-01 13:13:06 +0900209 servers_client.shelve_offload_server(server_id)
210 waiters.wait_for_server_status(servers_client, server_id,
ghanshyam017b5fe2016-04-15 18:49:26 +0900211 'SHELVED_OFFLOADED')