blob: d57bae308e2a78deb962222a0a9ac9a1d5efeac9 [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
18from tempest_lib.common.utils import data_utils
19
20from tempest.common import fixed_network
ghanshyam0f825252015-08-25 16:02:50 +090021from tempest.common import service_client
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000022from tempest.common import waiters
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000023from tempest import config
24
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,
32 volume_backed=False, **kwargs):
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000033 """Common wrapper utility returning a test server.
34
35 This method is a common wrapper returning a test server that can be
36 pingable or sshable.
37
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +090038 :param clients: Client manager which provides OpenStack Tempest clients.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000039 :param validatable: Whether the server will be pingable or sshable.
40 :param validation_resources: Resources created for the connection to the
41 server. Include a keypair, a security group and an IP.
Ken'ichi Ohmichid5bc31a2015-09-02 01:45:28 +000042 :param tenant_network: Tenant network to be used for creating a server.
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000043 :param wait_until: Server status to wait for the server to reach after
44 its creation.
Joe Gordon8843f0f2015-03-17 15:07:34 -070045 :param volume_backed: Whether the instance is volume backed or not.
lei zhangdd552b22015-11-25 20:41:48 +080046 :returns: a tuple
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000047 """
48
49 # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE
50
Yaroslav Lobankov32204f32015-12-25 15:24:53 +030051 name = kwargs.pop('name', data_utils.rand_name(__name__ + "-instance"))
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +000052 flavor = kwargs.pop('flavor', CONF.compute.flavor_ref)
53 image_id = kwargs.pop('image_id', CONF.compute.image_ref)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000054
55 kwargs = fixed_network.set_networks_kwarg(
56 tenant_network, kwargs) or {}
57
Ghanshyam4de44ae2015-12-25 10:34:00 +090058 multiple_create_request = (max(kwargs.get('min_count', 0),
59 kwargs.get('max_count', 0)) > 1)
60
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000061 if CONF.validation.run_validation and validatable:
62 # As a first implementation, multiple pingable or sshable servers will
63 # not be supported
Ghanshyam4de44ae2015-12-25 10:34:00 +090064 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000065 msg = ("Multiple pingable or sshable servers not supported at "
66 "this stage.")
67 raise ValueError(msg)
68
69 if 'security_groups' in kwargs:
70 kwargs['security_groups'].append(
71 {'name': validation_resources['security_group']['name']})
72 else:
73 try:
74 kwargs['security_groups'] = [
75 {'name': validation_resources['security_group']['name']}]
76 except KeyError:
77 LOG.debug("No security group provided.")
78
79 if 'key_name' not in kwargs:
80 try:
81 kwargs['key_name'] = validation_resources['keypair']['name']
82 except KeyError:
83 LOG.debug("No key provided.")
84
85 if CONF.validation.connect_method == 'floating':
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +000086 if wait_until is None:
87 wait_until = 'ACTIVE'
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000088
Joe Gordon8843f0f2015-03-17 15:07:34 -070089 if volume_backed:
90 volume_name = data_utils.rand_name('volume')
John Griffithbc678ad2015-09-29 09:38:39 -060091 volumes_client = clients.volumes_v2_client
92 if CONF.volume_feature_enabled.api_v1:
93 volumes_client = clients.volumes_client
94 volume = volumes_client.create_volume(
Joe Gordon8843f0f2015-03-17 15:07:34 -070095 display_name=volume_name,
96 imageRef=image_id)
John Griffithbc678ad2015-09-29 09:38:39 -060097 volumes_client.wait_for_volume_status(volume['volume']['id'],
98 'available')
Joe Gordon8843f0f2015-03-17 15:07:34 -070099
100 bd_map_v2 = [{
101 'uuid': volume['volume']['id'],
102 'source_type': 'volume',
103 'destination_type': 'volume',
104 'boot_index': 0,
105 'delete_on_termination': True}]
106 kwargs['block_device_mapping_v2'] = bd_map_v2
107
108 # Since this is boot from volume an image does not need
109 # to be specified.
110 image_id = ''
111
Ken'ichi Ohmichif2d436e2015-09-03 01:13:16 +0000112 body = clients.servers_client.create_server(name=name, imageRef=image_id,
113 flavorRef=flavor,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000114 **kwargs)
115
116 # handle the case of multiple servers
ghanshyam0f825252015-08-25 16:02:50 +0900117 servers = []
Ghanshyam4de44ae2015-12-25 10:34:00 +0900118 if multiple_create_request:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000119 # Get servers created which name match with name param.
120 body_servers = clients.servers_client.list_servers()
121 servers = \
122 [s for s in body_servers['servers'] if s['name'].startswith(name)]
ghanshyam0f825252015-08-25 16:02:50 +0900123 else:
124 body = service_client.ResponseBody(body.response, body['server'])
125 servers = [body]
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000126
127 # The name of the method to associate a floating IP to as server is too
128 # long for PEP8 compliance so:
John Warrene74890a2015-11-11 15:18:01 -0500129 assoc = clients.compute_floating_ips_client.associate_floating_ip_to_server
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000130
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000131 if wait_until:
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000132 for server in servers:
133 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000134 waiters.wait_for_server_status(
Ken'ichi Ohmichifc25e692015-09-02 01:48:06 +0000135 clients.servers_client, server['id'], wait_until)
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000136
137 # Multiple validatable servers are not supported for now. Their
138 # creation will fail with the condition above (l.58).
139 if CONF.validation.run_validation and validatable:
140 if CONF.validation.connect_method == 'floating':
141 assoc(floating_ip=validation_resources[
142 'floating_ip']['ip'],
143 server_id=servers[0]['id'])
144
145 except Exception:
146 with excutils.save_and_reraise_exception():
147 if ('preserve_server_on_error' not in kwargs
148 or kwargs['preserve_server_on_error'] is False):
149 for server in servers:
150 try:
151 clients.servers_client.delete_server(
152 server['id'])
153 except Exception:
154 LOG.exception('Deleting server %s failed'
155 % server['id'])
156
157 return body, servers