blob: 7ebc28385ff33a3d0e69b165b7cd45ef66311334 [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,
33 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
42 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
45 its creation.
Joe Gordon8843f0f2015-03-17 15:07:34 -070046 :param volume_backed: Whether the instance is volume backed or not.
lei zhangdd552b22015-11-25 20:41:48 +080047 :returns: a tuple
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000048 """
49
50 # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE
51
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053052 if name is None:
53 name = data_utils.rand_name(__name__ + "-instance")
54 if flavor is None:
55 flavor = CONF.compute.flavor_ref
56 if image_id is None:
57 image_id = CONF.compute.image_ref
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000058
59 kwargs = fixed_network.set_networks_kwarg(
60 tenant_network, kwargs) or {}
61
Ghanshyam4de44ae2015-12-25 10:34:00 +090062 multiple_create_request = (max(kwargs.get('min_count', 0),
63 kwargs.get('max_count', 0)) > 1)
64
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000065 if CONF.validation.run_validation and validatable:
66 # As a first implementation, multiple pingable or sshable servers will
67 # not be supported
Ghanshyam4de44ae2015-12-25 10:34:00 +090068 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000069 msg = ("Multiple pingable or sshable servers not supported at "
70 "this stage.")
71 raise ValueError(msg)
72
73 if 'security_groups' in kwargs:
74 kwargs['security_groups'].append(
75 {'name': validation_resources['security_group']['name']})
76 else:
77 try:
78 kwargs['security_groups'] = [
79 {'name': validation_resources['security_group']['name']}]
80 except KeyError:
81 LOG.debug("No security group provided.")
82
83 if 'key_name' not in kwargs:
84 try:
85 kwargs['key_name'] = validation_resources['keypair']['name']
86 except KeyError:
87 LOG.debug("No key provided.")
88
89 if CONF.validation.connect_method == 'floating':
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000090 if wait_until is None:
91 wait_until = 'ACTIVE'
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000092
Joe Gordon8843f0f2015-03-17 15:07:34 -070093 if volume_backed:
94 volume_name = data_utils.rand_name('volume')
John Griffithbc678ad2015-09-29 09:38:39 -060095 volumes_client = clients.volumes_v2_client
96 if CONF.volume_feature_enabled.api_v1:
97 volumes_client = clients.volumes_client
98 volume = volumes_client.create_volume(
Joe Gordon8843f0f2015-03-17 15:07:34 -070099 display_name=volume_name,
100 imageRef=image_id)
Yaroslav Lobankoved3a35b2016-03-24 22:41:30 -0500101 waiters.wait_for_volume_status(volumes_client,
102 volume['volume']['id'], 'available')
Joe Gordon8843f0f2015-03-17 15:07:34 -0700103
104 bd_map_v2 = [{
105 'uuid': volume['volume']['id'],
106 'source_type': 'volume',
107 'destination_type': 'volume',
108 'boot_index': 0,
109 'delete_on_termination': True}]
110 kwargs['block_device_mapping_v2'] = bd_map_v2
111
112 # Since this is boot from volume an image does not need
113 # to be specified.
114 image_id = ''
115
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000116 body = clients.servers_client.create_server(name=name, imageRef=image_id,
117 flavorRef=flavor,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000118 **kwargs)
119
120 # handle the case of multiple servers
ghanshyam0f825252015-08-25 16:02:50 +0900121 servers = []
Ghanshyam4de44ae2015-12-25 10:34:00 +0900122 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000123 # Get servers created which name match with name param.
124 body_servers = clients.servers_client.list_servers()
125 servers = \
126 [s for s in body_servers['servers'] if s['name'].startswith(name)]
ghanshyam0f825252015-08-25 16:02:50 +0900127 else:
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -0800128 body = rest_client.ResponseBody(body.response, body['server'])
ghanshyam0f825252015-08-25 16:02:50 +0900129 servers = [body]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000130
131 # The name of the method to associate a floating IP to as server is too
132 # long for PEP8 compliance so:
John Warrene74890a2015-11-11 15:18:01 -0500133 assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000134
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000135 if wait_until:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000136 for server in servers:
137 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000138 waiters.wait_for_server_status(
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000139 clients.servers_client, server['id'], wait_until)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000140
141 # Multiple validatable servers are not supported for now. Their
142 # creation will fail with the condition above (l.58).
143 if CONF.validation.run_validation and validatable:
144 if CONF.validation.connect_method == 'floating':
145 assoc(floating_ip=validation_resources[
146 'floating_ip']['ip'],
147 server_id=servers[0]['id'])
148
149 except Exception:
150 with excutils.save_and_reraise_exception():
Jordan Pittier87ba2872016-03-08 11:43:11 +0100151 for server in servers:
152 try:
153 clients.servers_client.delete_server(
154 server['id'])
155 except Exception:
156 LOG.exception('Deleting server %s failed'
157 % server['id'])
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000158
159 return body, servers
ghanshyam017b5fe2016-04-15 18:49:26 +0900160
161
162def shelve_server(client, server_id, force_shelve_offload=False):
163 """Common wrapper utility to shelve server.
164
165 This method is a common wrapper to make server in 'SHELVED'
166 or 'SHELVED_OFFLOADED' state.
167
168 :param server_id: Server to make in shelve state
169 :param force_shelve_offload: Forcefully offload shelve server if it
170 is configured not to offload server
171 automatically after offload time.
172 """
173 client.shelve_server(server_id)
174
175 offload_time = CONF.compute.shelved_offload_time
176 if offload_time >= 0:
177 waiters.wait_for_server_status(client, server_id,
178 'SHELVED_OFFLOADED',
179 extra_timeout=offload_time)
180 else:
181 waiters.wait_for_server_status(client, server_id, 'SHELVED')
182 if force_shelve_offload:
183 client.shelve_offload_server(server_id)
184 waiters.wait_for_server_status(client, server_id,
185 'SHELVED_OFFLOADED')