blob: 4f2fe67c9a5796a7e77116d29c7ee93e8f9bcddf [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
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000020from tempest.common import waiters
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000021from tempest import config
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080022from tempest.lib.common import rest_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050023from tempest.lib.common.utils import data_utils
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,
Andrea Rosac4b0e002016-07-01 14:22:38 +010033 image_id=None, delete_vol_on_termination=True,
34 **kwargs):
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000035 """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 NATSUME6d5a2b42015-09-08 11:27:49 +090040 :param clients: Client manager which provides OpenStack Tempest clients.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000041 :param validatable: Whether the server will be pingable or sshable.
42 :param validation_resources: Resources created for the connection to the
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010043 server. Include a keypair, a security group and an IP.
Ken'ichi Ohmichid5bc31a2015-09-02 01:45:28 +000044 :param tenant_network: Tenant network to be used for creating a server.
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000045 :param wait_until: Server status to wait for the server to reach after
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010046 its creation.
Joe Gordon8843f0f2015-03-17 15:07:34 -070047 :param volume_backed: Whether the instance is volume backed or not.
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010048 :param name: Name of the server to be provisioned. If not defined a random
49 string ending with '-instance' will be generated.
50 :param flavor: Flavor of the server to be provisioned. If not defined,
51 CONF.compute.flavor_ref will be used instead.
52 :param image_id: ID of the image to be used to provision the server. If not
53 defined, CONF.compute.image_ref will be used instead.
54 :param delete_vol_on_termination: Controls whether the backing volume
55 should be deleted when the server is deleted. Only applies to volume
56 backed servers.
lei zhangdd552b22015-11-25 20:41:48 +080057 :returns: a tuple
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000058 """
59
60 # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE
61
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053062 if name is None:
63 name = data_utils.rand_name(__name__ + "-instance")
64 if flavor is None:
65 flavor = CONF.compute.flavor_ref
66 if image_id is None:
67 image_id = CONF.compute.image_ref
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000068
69 kwargs = fixed_network.set_networks_kwarg(
70 tenant_network, kwargs) or {}
71
Ghanshyam4de44ae2015-12-25 10:34:00 +090072 multiple_create_request = (max(kwargs.get('min_count', 0),
73 kwargs.get('max_count', 0)) > 1)
74
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000075 if CONF.validation.run_validation and validatable:
76 # As a first implementation, multiple pingable or sshable servers will
77 # not be supported
Ghanshyam4de44ae2015-12-25 10:34:00 +090078 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000079 msg = ("Multiple pingable or sshable servers not supported at "
80 "this stage.")
81 raise ValueError(msg)
82
83 if 'security_groups' in kwargs:
84 kwargs['security_groups'].append(
85 {'name': validation_resources['security_group']['name']})
86 else:
87 try:
88 kwargs['security_groups'] = [
89 {'name': validation_resources['security_group']['name']}]
90 except KeyError:
91 LOG.debug("No security group provided.")
92
93 if 'key_name' not in kwargs:
94 try:
95 kwargs['key_name'] = validation_resources['keypair']['name']
96 except KeyError:
97 LOG.debug("No key provided.")
98
99 if CONF.validation.connect_method == 'floating':
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000100 if wait_until is None:
101 wait_until = 'ACTIVE'
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000102
Joe Gordon8843f0f2015-03-17 15:07:34 -0700103 if volume_backed:
zhuflc6ce5392016-08-17 14:34:37 +0800104 volume_name = data_utils.rand_name(__name__ + '-volume')
John Griffithbc678ad2015-09-29 09:38:39 -0600105 volumes_client = clients.volumes_v2_client
106 if CONF.volume_feature_enabled.api_v1:
107 volumes_client = clients.volumes_client
108 volume = volumes_client.create_volume(
Joe Gordon8843f0f2015-03-17 15:07:34 -0700109 display_name=volume_name,
Ken'ichi Ohmichiadb905e2016-08-26 15:16:23 -0700110 imageRef=image_id,
111 size=CONF.volume.volume_size)
Yaroslav Lobankoved3a35b2016-03-24 22:41:30 -0500112 waiters.wait_for_volume_status(volumes_client,
113 volume['volume']['id'], 'available')
Joe Gordon8843f0f2015-03-17 15:07:34 -0700114
115 bd_map_v2 = [{
116 'uuid': volume['volume']['id'],
117 'source_type': 'volume',
118 'destination_type': 'volume',
119 'boot_index': 0,
Andrea Rosac4b0e002016-07-01 14:22:38 +0100120 'delete_on_termination': delete_vol_on_termination}]
Joe Gordon8843f0f2015-03-17 15:07:34 -0700121 kwargs['block_device_mapping_v2'] = bd_map_v2
122
123 # Since this is boot from volume an image does not need
124 # to be specified.
125 image_id = ''
126
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000127 body = clients.servers_client.create_server(name=name, imageRef=image_id,
128 flavorRef=flavor,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000129 **kwargs)
130
131 # handle the case of multiple servers
Ghanshyam4de44ae2015-12-25 10:34:00 +0900132 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000133 # Get servers created which name match with name param.
134 body_servers = clients.servers_client.list_servers()
135 servers = \
136 [s for s in body_servers['servers'] if s['name'].startswith(name)]
ghanshyam0f825252015-08-25 16:02:50 +0900137 else:
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -0800138 body = rest_client.ResponseBody(body.response, body['server'])
ghanshyam0f825252015-08-25 16:02:50 +0900139 servers = [body]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000140
141 # The name of the method to associate a floating IP to as server is too
142 # long for PEP8 compliance so:
John Warrene74890a2015-11-11 15:18:01 -0500143 assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000144
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000145 if wait_until:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000146 for server in servers:
147 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000148 waiters.wait_for_server_status(
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000149 clients.servers_client, server['id'], wait_until)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000150
151 # Multiple validatable servers are not supported for now. Their
152 # creation will fail with the condition above (l.58).
153 if CONF.validation.run_validation and validatable:
154 if CONF.validation.connect_method == 'floating':
155 assoc(floating_ip=validation_resources[
156 'floating_ip']['ip'],
157 server_id=servers[0]['id'])
158
159 except Exception:
160 with excutils.save_and_reraise_exception():
Jordan Pittier87ba2872016-03-08 11:43:11 +0100161 for server in servers:
162 try:
163 clients.servers_client.delete_server(
164 server['id'])
165 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100166 LOG.exception('Deleting server %s failed',
167 server['id'])
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000168
169 return body, servers
ghanshyam017b5fe2016-04-15 18:49:26 +0900170
171
ghanshyam4c1391c2016-12-01 13:13:06 +0900172def shelve_server(servers_client, server_id, force_shelve_offload=False):
ghanshyam017b5fe2016-04-15 18:49:26 +0900173 """Common wrapper utility to shelve server.
174
175 This method is a common wrapper to make server in 'SHELVED'
176 or 'SHELVED_OFFLOADED' state.
177
ghanshyam4c1391c2016-12-01 13:13:06 +0900178 :param servers_clients: Compute servers client instance.
ghanshyam017b5fe2016-04-15 18:49:26 +0900179 :param server_id: Server to make in shelve state
180 :param force_shelve_offload: Forcefully offload shelve server if it
181 is configured not to offload server
182 automatically after offload time.
183 """
ghanshyam4c1391c2016-12-01 13:13:06 +0900184 servers_client.shelve_server(server_id)
ghanshyam017b5fe2016-04-15 18:49:26 +0900185
186 offload_time = CONF.compute.shelved_offload_time
187 if offload_time >= 0:
ghanshyam4c1391c2016-12-01 13:13:06 +0900188 waiters.wait_for_server_status(servers_client, server_id,
ghanshyam017b5fe2016-04-15 18:49:26 +0900189 'SHELVED_OFFLOADED',
190 extra_timeout=offload_time)
191 else:
ghanshyam4c1391c2016-12-01 13:13:06 +0900192 waiters.wait_for_server_status(servers_client, server_id, 'SHELVED')
ghanshyam017b5fe2016-04-15 18:49:26 +0900193 if force_shelve_offload:
ghanshyam4c1391c2016-12-01 13:13:06 +0900194 servers_client.shelve_offload_server(server_id)
195 waiters.wait_for_server_status(servers_client, server_id,
ghanshyam017b5fe2016-04-15 18:49:26 +0900196 'SHELVED_OFFLOADED')