blob: f489908f5b3bb606623d511aaa3d2478734af55f [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
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000021from tempest.common import waiters
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000022from tempest import config
23
24CONF = config.CONF
25
26LOG = logging.getLogger(__name__)
27
28
Andrea Frittoli (andreaf)476e9192015-08-14 23:59:58 +010029def create_test_server(clients, validatable=False, validation_resources=None,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000030 tenant_network=None, **kwargs):
31 """Common wrapper utility returning a test server.
32
33 This method is a common wrapper returning a test server that can be
34 pingable or sshable.
35
36 :param clients: Client manager which provides Openstack Tempest clients.
37 :param validatable: Whether the server will be pingable or sshable.
38 :param validation_resources: Resources created for the connection to the
39 server. Include a keypair, a security group and an IP.
Ken'ichi Ohmichid5bc31a2015-09-02 01:45:28 +000040 :param tenant_network: Tenant network to be used for creating a server.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000041 :returns a tuple
42 """
43
44 # TODO(jlanoux) add support of wait_until PINGABLE/SSHABLE
45
46 if 'name' in kwargs:
47 name = kwargs.pop('name')
48 else:
49 name = data_utils.rand_name(__name__ + "-instance")
50
51 flavor = kwargs.get('flavor', CONF.compute.flavor_ref)
52 image_id = kwargs.get('image_id', CONF.compute.image_ref)
53
54 kwargs = fixed_network.set_networks_kwarg(
55 tenant_network, kwargs) or {}
56
57 if CONF.validation.run_validation and validatable:
58 # As a first implementation, multiple pingable or sshable servers will
59 # not be supported
60 if 'min_count' in kwargs or 'max_count' in kwargs:
61 msg = ("Multiple pingable or sshable servers not supported at "
62 "this stage.")
63 raise ValueError(msg)
64
65 if 'security_groups' in kwargs:
66 kwargs['security_groups'].append(
67 {'name': validation_resources['security_group']['name']})
68 else:
69 try:
70 kwargs['security_groups'] = [
71 {'name': validation_resources['security_group']['name']}]
72 except KeyError:
73 LOG.debug("No security group provided.")
74
75 if 'key_name' not in kwargs:
76 try:
77 kwargs['key_name'] = validation_resources['keypair']['name']
78 except KeyError:
79 LOG.debug("No key provided.")
80
81 if CONF.validation.connect_method == 'floating':
82 if 'wait_until' not in kwargs:
83 kwargs['wait_until'] = 'ACTIVE'
84
85 body = clients.servers_client.create_server(name, image_id, flavor,
86 **kwargs)
87
88 # handle the case of multiple servers
89 servers = [body]
90 if 'min_count' in kwargs or 'max_count' in kwargs:
91 # Get servers created which name match with name param.
92 body_servers = clients.servers_client.list_servers()
93 servers = \
94 [s for s in body_servers['servers'] if s['name'].startswith(name)]
95
96 # The name of the method to associate a floating IP to as server is too
97 # long for PEP8 compliance so:
98 assoc = clients.floating_ips_client.associate_floating_ip_to_server
99
100 if 'wait_until' in kwargs:
101 for server in servers:
102 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000103 waiters.wait_for_server_status(
104 clients.servers_client, server['id'], kwargs['wait_until'])
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000105
106 # Multiple validatable servers are not supported for now. Their
107 # creation will fail with the condition above (l.58).
108 if CONF.validation.run_validation and validatable:
109 if CONF.validation.connect_method == 'floating':
110 assoc(floating_ip=validation_resources[
111 'floating_ip']['ip'],
112 server_id=servers[0]['id'])
113
114 except Exception:
115 with excutils.save_and_reraise_exception():
116 if ('preserve_server_on_error' not in kwargs
117 or kwargs['preserve_server_on_error'] is False):
118 for server in servers:
119 try:
120 clients.servers_client.delete_server(
121 server['id'])
122 except Exception:
123 LOG.exception('Deleting server %s failed'
124 % server['id'])
125
126 return body, servers