blob: b14012efa3ef3aa9ad6810e392f0e2c28a2fe8de [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
ghanshyam0f825252015-08-25 16:02:50 +090020from tempest.common import service_client
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000021from tempest.common import waiters
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000022from tempest import config
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 name = name
53 flavor = flavor
54 image_id = image_id
55
56 if name is None:
57 name = data_utils.rand_name(__name__ + "-instance")
58 if flavor is None:
59 flavor = CONF.compute.flavor_ref
60 if image_id is None:
61 image_id = CONF.compute.image_ref
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000062
63 kwargs = fixed_network.set_networks_kwarg(
64 tenant_network, kwargs) or {}
65
Ghanshyam4de44ae2015-12-25 10:34:00 +090066 multiple_create_request = (max(kwargs.get('min_count', 0),
67 kwargs.get('max_count', 0)) > 1)
68
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000069 if CONF.validation.run_validation and validatable:
70 # As a first implementation, multiple pingable or sshable servers will
71 # not be supported
Ghanshyam4de44ae2015-12-25 10:34:00 +090072 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000073 msg = ("Multiple pingable or sshable servers not supported at "
74 "this stage.")
75 raise ValueError(msg)
76
77 if 'security_groups' in kwargs:
78 kwargs['security_groups'].append(
79 {'name': validation_resources['security_group']['name']})
80 else:
81 try:
82 kwargs['security_groups'] = [
83 {'name': validation_resources['security_group']['name']}]
84 except KeyError:
85 LOG.debug("No security group provided.")
86
87 if 'key_name' not in kwargs:
88 try:
89 kwargs['key_name'] = validation_resources['keypair']['name']
90 except KeyError:
91 LOG.debug("No key provided.")
92
93 if CONF.validation.connect_method == 'floating':
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000094 if wait_until is None:
95 wait_until = 'ACTIVE'
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000096
Joe Gordon8843f0f2015-03-17 15:07:34 -070097 if volume_backed:
98 volume_name = data_utils.rand_name('volume')
John Griffithbc678ad2015-09-29 09:38:39 -060099 volumes_client = clients.volumes_v2_client
100 if CONF.volume_feature_enabled.api_v1:
101 volumes_client = clients.volumes_client
102 volume = volumes_client.create_volume(
Joe Gordon8843f0f2015-03-17 15:07:34 -0700103 display_name=volume_name,
104 imageRef=image_id)
John Griffithbc678ad2015-09-29 09:38:39 -0600105 volumes_client.wait_for_volume_status(volume['volume']['id'],
106 'available')
Joe Gordon8843f0f2015-03-17 15:07:34 -0700107
108 bd_map_v2 = [{
109 'uuid': volume['volume']['id'],
110 'source_type': 'volume',
111 'destination_type': 'volume',
112 'boot_index': 0,
113 'delete_on_termination': True}]
114 kwargs['block_device_mapping_v2'] = bd_map_v2
115
116 # Since this is boot from volume an image does not need
117 # to be specified.
118 image_id = ''
119
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000120 body = clients.servers_client.create_server(name=name, imageRef=image_id,
121 flavorRef=flavor,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000122 **kwargs)
123
124 # handle the case of multiple servers
ghanshyam0f825252015-08-25 16:02:50 +0900125 servers = []
Ghanshyam4de44ae2015-12-25 10:34:00 +0900126 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000127 # Get servers created which name match with name param.
128 body_servers = clients.servers_client.list_servers()
129 servers = \
130 [s for s in body_servers['servers'] if s['name'].startswith(name)]
ghanshyam0f825252015-08-25 16:02:50 +0900131 else:
132 body = service_client.ResponseBody(body.response, body['server'])
133 servers = [body]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000134
135 # The name of the method to associate a floating IP to as server is too
136 # long for PEP8 compliance so:
John Warrene74890a2015-11-11 15:18:01 -0500137 assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000138
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000139 if wait_until:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000140 for server in servers:
141 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000142 waiters.wait_for_server_status(
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000143 clients.servers_client, server['id'], wait_until)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000144
145 # Multiple validatable servers are not supported for now. Their
146 # creation will fail with the condition above (l.58).
147 if CONF.validation.run_validation and validatable:
148 if CONF.validation.connect_method == 'floating':
149 assoc(floating_ip=validation_resources[
150 'floating_ip']['ip'],
151 server_id=servers[0]['id'])
152
153 except Exception:
154 with excutils.save_and_reraise_exception():
155 if ('preserve_server_on_error' not in kwargs
156 or kwargs['preserve_server_on_error'] is False):
157 for server in servers:
158 try:
159 clients.servers_client.delete_server(
160 server['id'])
161 except Exception:
162 LOG.exception('Deleting server %s failed'
163 % server['id'])
164
165 return body, servers