blob: 9f467fe55513cf6c4ccedf2dd29db5c133c3e582 [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
Clark Boylan844180e2017-03-15 15:24:58 -070016import base64
Markus Zoellerae36ce82017-03-20 16:27:26 +010017import socket
xxj8eb90982017-04-10 21:18:39 +080018import ssl
Markus Zoellerae36ce82017-03-20 16:27:26 +010019import struct
Clark Boylan844180e2017-03-15 15:24:58 -070020import textwrap
21
Markus Zoellerae36ce82017-03-20 16:27:26 +010022import six
23from six.moves.urllib import parse as urlparse
24
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000025from oslo_log import log as logging
26from oslo_utils import excutils
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000027
28from tempest.common import fixed_network
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000029from tempest.common import waiters
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000030from tempest import config
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080031from tempest.lib.common import rest_client
Ken'ichi Ohmichi757833a2017-03-10 10:30:30 -080032from tempest.lib.common.utils import data_utils
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000033
Markus Zoellerae36ce82017-03-20 16:27:26 +010034if six.PY2:
35 ord_func = ord
36else:
37 ord_func = int
38
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000039CONF = config.CONF
40
41LOG = logging.getLogger(__name__)
42
43
Andrea Frittoli (andreaf)476e9192015-08-14 23:59:58 +010044def create_test_server(clients, validatable=False, validation_resources=None,
Joe Gordon8843f0f2015-03-17 15:07:34 -070045 tenant_network=None, wait_until=None,
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053046 volume_backed=False, name=None, flavor=None,
ghanshyam61db96e2016-12-16 12:49:25 +090047 image_id=None, **kwargs):
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000048 """Common wrapper utility returning a test server.
49
50 This method is a common wrapper returning a test server that can be
51 pingable or sshable.
52
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +090053 :param clients: Client manager which provides OpenStack Tempest clients.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000054 :param validatable: Whether the server will be pingable or sshable.
55 :param validation_resources: Resources created for the connection to the
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010056 server. Include a keypair, a security group and an IP.
Ken'ichi Ohmichid5bc31a2015-09-02 01:45:28 +000057 :param tenant_network: Tenant network to be used for creating a server.
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000058 :param wait_until: Server status to wait for the server to reach after
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010059 its creation.
ghanshyam61db96e2016-12-16 12:49:25 +090060 :param volume_backed: Whether the server is volume backed or not.
61 If this is true, a volume will be created and
62 create server will be requested with
63 'block_device_mapping_v2' populated with below
64 values:
65 --------------------------------------------
66 bd_map_v2 = [{
67 'uuid': volume['volume']['id'],
68 'source_type': 'volume',
69 'destination_type': 'volume',
70 'boot_index': 0,
71 'delete_on_termination': True}]
72 kwargs['block_device_mapping_v2'] = bd_map_v2
73 ---------------------------------------------
74 If server needs to be booted from volume with other
75 combination of bdm inputs than mentioned above, then
76 pass the bdm inputs explicitly as kwargs and image_id
77 as empty string ('').
Andrea Frittoli (andreaf)9df3a522016-07-06 14:09:48 +010078 :param name: Name of the server to be provisioned. If not defined a random
79 string ending with '-instance' will be generated.
80 :param flavor: Flavor of the server to be provisioned. If not defined,
81 CONF.compute.flavor_ref will be used instead.
82 :param image_id: ID of the image to be used to provision the server. If not
83 defined, CONF.compute.image_ref will be used instead.
lei zhangdd552b22015-11-25 20:41:48 +080084 :returns: a tuple
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000085 """
86
87 # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE
88
Anusha Ramineni9aaef8b2016-01-19 10:56:40 +053089 if name is None:
90 name = data_utils.rand_name(__name__ + "-instance")
91 if flavor is None:
92 flavor = CONF.compute.flavor_ref
93 if image_id is None:
94 image_id = CONF.compute.image_ref
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000095
96 kwargs = fixed_network.set_networks_kwarg(
97 tenant_network, kwargs) or {}
98
Ghanshyam4de44ae2015-12-25 10:34:00 +090099 multiple_create_request = (max(kwargs.get('min_count', 0),
100 kwargs.get('max_count', 0)) > 1)
101
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000102 if CONF.validation.run_validation and validatable:
103 # As a first implementation, multiple pingable or sshable servers will
104 # not be supported
Ghanshyam4de44ae2015-12-25 10:34:00 +0900105 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000106 msg = ("Multiple pingable or sshable servers not supported at "
107 "this stage.")
108 raise ValueError(msg)
109
110 if 'security_groups' in kwargs:
111 kwargs['security_groups'].append(
112 {'name': validation_resources['security_group']['name']})
113 else:
114 try:
115 kwargs['security_groups'] = [
116 {'name': validation_resources['security_group']['name']}]
117 except KeyError:
118 LOG.debug("No security group provided.")
119
120 if 'key_name' not in kwargs:
121 try:
122 kwargs['key_name'] = validation_resources['keypair']['name']
123 except KeyError:
124 LOG.debug("No key provided.")
125
126 if CONF.validation.connect_method == 'floating':
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000127 if wait_until is None:
128 wait_until = 'ACTIVE'
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000129
Clark Boylan844180e2017-03-15 15:24:58 -0700130 if 'user_data' not in kwargs:
131 # If nothing overrides the default user data script then run
132 # a simple script on the host to print networking info. This is
133 # to aid in debugging ssh failures.
134 script = '''
135 #!/bin/sh
136 echo "Printing {user} user authorized keys"
137 cat ~{user}/.ssh/authorized_keys || true
138 '''.format(user=CONF.validation.image_ssh_user)
139 script_clean = textwrap.dedent(script).lstrip().encode('utf8')
140 script_b64 = base64.b64encode(script_clean)
141 kwargs['user_data'] = script_b64
142
Joe Gordon8843f0f2015-03-17 15:07:34 -0700143 if volume_backed:
zhuflc6ce5392016-08-17 14:34:37 +0800144 volume_name = data_utils.rand_name(__name__ + '-volume')
John Griffithbc678ad2015-09-29 09:38:39 -0600145 volumes_client = clients.volumes_v2_client
ghanshyam3bd0d2b2017-03-23 01:57:28 +0000146 params = {'name': volume_name,
ghanshyam61db96e2016-12-16 12:49:25 +0900147 'imageRef': image_id,
148 'size': CONF.volume.volume_size}
149 volume = volumes_client.create_volume(**params)
lkuchlan52d7b0d2016-11-07 20:53:19 +0200150 waiters.wait_for_volume_resource_status(volumes_client,
151 volume['volume']['id'],
152 'available')
Joe Gordon8843f0f2015-03-17 15:07:34 -0700153
154 bd_map_v2 = [{
155 'uuid': volume['volume']['id'],
156 'source_type': 'volume',
157 'destination_type': 'volume',
158 'boot_index': 0,
ghanshyam61db96e2016-12-16 12:49:25 +0900159 'delete_on_termination': True}]
Joe Gordon8843f0f2015-03-17 15:07:34 -0700160 kwargs['block_device_mapping_v2'] = bd_map_v2
161
162 # Since this is boot from volume an image does not need
163 # to be specified.
164 image_id = ''
165
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000166 body = clients.servers_client.create_server(name=name, imageRef=image_id,
167 flavorRef=flavor,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000168 **kwargs)
169
170 # handle the case of multiple servers
Ghanshyam4de44ae2015-12-25 10:34:00 +0900171 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000172 # Get servers created which name match with name param.
173 body_servers = clients.servers_client.list_servers()
174 servers = \
175 [s for s in body_servers['servers'] if s['name'].startswith(name)]
ghanshyam0f825252015-08-25 16:02:50 +0900176 else:
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -0800177 body = rest_client.ResponseBody(body.response, body['server'])
ghanshyam0f825252015-08-25 16:02:50 +0900178 servers = [body]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000179
180 # The name of the method to associate a floating IP to as server is too
181 # long for PEP8 compliance so:
John Warrene74890a2015-11-11 15:18:01 -0500182 assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000183
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000184 if wait_until:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000185 for server in servers:
186 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000187 waiters.wait_for_server_status(
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000188 clients.servers_client, server['id'], wait_until)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000189
190 # Multiple validatable servers are not supported for now. Their
191 # creation will fail with the condition above (l.58).
192 if CONF.validation.run_validation and validatable:
193 if CONF.validation.connect_method == 'floating':
194 assoc(floating_ip=validation_resources[
195 'floating_ip']['ip'],
196 server_id=servers[0]['id'])
197
198 except Exception:
199 with excutils.save_and_reraise_exception():
Jordan Pittier87ba2872016-03-08 11:43:11 +0100200 for server in servers:
201 try:
202 clients.servers_client.delete_server(
203 server['id'])
204 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100205 LOG.exception('Deleting server %s failed',
206 server['id'])
Artom Lifshitz9b3f42b2017-06-19 05:46:32 +0000207 for server in servers:
208 # NOTE(artom) If the servers were booted with volumes
209 # and with delete_on_termination=False we need to wait
210 # for the servers to go away before proceeding with
211 # cleanup, otherwise we'll attempt to delete the
212 # volumes while they're still attached to servers that
213 # are in the process of being deleted.
214 try:
215 waiters.wait_for_server_termination(
216 clients.servers_client, server['id'])
217 except Exception:
218 LOG.exception('Server %s failed to delete in time',
219 server['id'])
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000220
221 return body, servers
ghanshyam017b5fe2016-04-15 18:49:26 +0900222
223
ghanshyam4c1391c2016-12-01 13:13:06 +0900224def shelve_server(servers_client, server_id, force_shelve_offload=False):
ghanshyam017b5fe2016-04-15 18:49:26 +0900225 """Common wrapper utility to shelve server.
226
227 This method is a common wrapper to make server in 'SHELVED'
228 or 'SHELVED_OFFLOADED' state.
229
ghanshyam4c1391c2016-12-01 13:13:06 +0900230 :param servers_clients: Compute servers client instance.
ghanshyam017b5fe2016-04-15 18:49:26 +0900231 :param server_id: Server to make in shelve state
232 :param force_shelve_offload: Forcefully offload shelve server if it
233 is configured not to offload server
234 automatically after offload time.
235 """
ghanshyam4c1391c2016-12-01 13:13:06 +0900236 servers_client.shelve_server(server_id)
ghanshyam017b5fe2016-04-15 18:49:26 +0900237
238 offload_time = CONF.compute.shelved_offload_time
239 if offload_time >= 0:
ghanshyam4c1391c2016-12-01 13:13:06 +0900240 waiters.wait_for_server_status(servers_client, server_id,
ghanshyam017b5fe2016-04-15 18:49:26 +0900241 'SHELVED_OFFLOADED',
242 extra_timeout=offload_time)
243 else:
ghanshyam4c1391c2016-12-01 13:13:06 +0900244 waiters.wait_for_server_status(servers_client, server_id, 'SHELVED')
ghanshyam017b5fe2016-04-15 18:49:26 +0900245 if force_shelve_offload:
ghanshyam4c1391c2016-12-01 13:13:06 +0900246 servers_client.shelve_offload_server(server_id)
247 waiters.wait_for_server_status(servers_client, server_id,
ghanshyam017b5fe2016-04-15 18:49:26 +0900248 'SHELVED_OFFLOADED')
Markus Zoellerae36ce82017-03-20 16:27:26 +0100249
250
251def create_websocket(url):
252 url = urlparse.urlparse(url)
xxj8eb90982017-04-10 21:18:39 +0800253 if url.scheme == 'https':
254 client_socket = ssl.wrap_socket(socket.socket(socket.AF_INET,
255 socket.SOCK_STREAM))
256 else:
257 client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Markus Zoellerae36ce82017-03-20 16:27:26 +0100258 client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
259 client_socket.connect((url.hostname, url.port))
260 # Turn the Socket into a WebSocket to do the communication
261 return _WebSocket(client_socket, url)
262
263
264class _WebSocket(object):
265 def __init__(self, client_socket, url):
266 """Contructor for the WebSocket wrapper to the socket."""
267 self._socket = client_socket
jianghua wangd22514a2017-05-08 08:05:04 +0100268 # cached stream for early frames.
269 self.cached_stream = b''
Markus Zoellerae36ce82017-03-20 16:27:26 +0100270 # Upgrade the HTTP connection to a WebSocket
271 self._upgrade(url)
272
jianghua wangd22514a2017-05-08 08:05:04 +0100273 def _recv(self, recv_size):
274 """Wrapper to receive data from the cached stream or socket."""
275 if recv_size <= 0:
276 return None
277
278 data_from_cached = b''
279 data_from_socket = b''
280 if len(self.cached_stream) > 0:
281 read_from_cached = min(len(self.cached_stream), recv_size)
282 data_from_cached += self.cached_stream[:read_from_cached]
283 self.cached_stream = self.cached_stream[read_from_cached:]
284 recv_size -= read_from_cached
285 if recv_size > 0:
286 data_from_socket = self._socket.recv(recv_size)
287 return data_from_cached + data_from_socket
288
Markus Zoellerae36ce82017-03-20 16:27:26 +0100289 def receive_frame(self):
290 """Wrapper for receiving data to parse the WebSocket frame format"""
291 # We need to loop until we either get some bytes back in the frame
292 # or no data was received (meaning the socket was closed). This is
293 # done to handle the case where we get back some empty frames
294 while True:
jianghua wangd22514a2017-05-08 08:05:04 +0100295 header = self._recv(2)
Markus Zoellerae36ce82017-03-20 16:27:26 +0100296 # If we didn't receive any data, just return None
Masayuki Igawa0c0f0142017-04-10 17:22:02 +0900297 if not header:
Markus Zoellerae36ce82017-03-20 16:27:26 +0100298 return None
299 # We will make the assumption that we are only dealing with
300 # frames less than 125 bytes here (for the negotiation) and
301 # that only the 2nd byte contains the length, and since the
302 # server doesn't do masking, we can just read the data length
303 if ord_func(header[1]) & 127 > 0:
jianghua wangd22514a2017-05-08 08:05:04 +0100304 return self._recv(ord_func(header[1]) & 127)
Markus Zoellerae36ce82017-03-20 16:27:26 +0100305
306 def send_frame(self, data):
307 """Wrapper for sending data to add in the WebSocket frame format."""
308 frame_bytes = list()
309 # For the first byte, want to say we are sending binary data (130)
310 frame_bytes.append(130)
311 # Only sending negotiation data so don't need to worry about > 125
312 # We do need to add the bit that says we are masking the data
313 frame_bytes.append(len(data) | 128)
314 # We don't really care about providing a random mask for security
315 # So we will just hard-code a value since a test program
316 mask = [7, 2, 1, 9]
317 for i in range(len(mask)):
318 frame_bytes.append(mask[i])
319 # Mask each of the actual data bytes that we are going to send
320 for i in range(len(data)):
321 frame_bytes.append(ord_func(data[i]) ^ mask[i % 4])
322 # Convert our integer list to a binary array of bytes
323 frame_bytes = struct.pack('!%iB' % len(frame_bytes), * frame_bytes)
324 self._socket.sendall(frame_bytes)
325
326 def close(self):
327 """Helper method to close the connection."""
328 # Close down the real socket connection and exit the test program
329 if self._socket is not None:
330 self._socket.shutdown(1)
331 self._socket.close()
332 self._socket = None
333
334 def _upgrade(self, url):
335 """Upgrade the HTTP connection to a WebSocket and verify."""
336 # The real request goes to the /websockify URI always
337 reqdata = 'GET /websockify HTTP/1.1\r\n'
338 reqdata += 'Host: %s:%s\r\n' % (url.hostname, url.port)
339 # Tell the HTTP Server to Upgrade the connection to a WebSocket
340 reqdata += 'Upgrade: websocket\r\nConnection: Upgrade\r\n'
341 # The token=xxx is sent as a Cookie not in the URI
342 reqdata += 'Cookie: %s\r\n' % url.query
343 # Use a hard-coded WebSocket key since a test program
344 reqdata += 'Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n'
345 reqdata += 'Sec-WebSocket-Version: 13\r\n'
346 # We are choosing to use binary even though browser may do Base64
347 reqdata += 'Sec-WebSocket-Protocol: binary\r\n\r\n'
348 # Send the HTTP GET request and get the response back
349 self._socket.sendall(reqdata.encode('utf8'))
350 self.response = data = self._socket.recv(4096)
351 # Loop through & concatenate all of the data in the response body
jianghua wangd22514a2017-05-08 08:05:04 +0100352 end_loc = self.response.find(b'\r\n\r\n')
353 while data and end_loc < 0:
Markus Zoellerae36ce82017-03-20 16:27:26 +0100354 data = self._socket.recv(4096)
355 self.response += data
jianghua wangd22514a2017-05-08 08:05:04 +0100356 end_loc = self.response.find(b'\r\n\r\n')
357
358 if len(self.response) > end_loc + 4:
359 # In case some frames (e.g. the first RFP negotiation) have
360 # arrived, cache it for next reading.
361 self.cached_stream = self.response[end_loc + 4:]
362 # ensure response ends with '\r\n\r\n'.
363 self.response = self.response[:end_loc + 4]