ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 2 | # Copyright 2013 IBM Corp. |
| 3 | # All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
Attila Fazekas | fb7552a | 2013-08-27 13:02:26 +0200 | [diff] [blame] | 17 | import logging |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 18 | import os |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 19 | import re |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 20 | import subprocess |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 21 | import time |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 22 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 23 | from cinderclient import exceptions as cinder_exceptions |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 24 | import glanceclient |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 25 | from heatclient import exc as heat_exceptions |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 26 | import netaddr |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 27 | from neutronclient.common import exceptions as exc |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 28 | from novaclient import exceptions as nova_exceptions |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 29 | import six |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 30 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 31 | from tempest.api.network import common as net_common |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 32 | from tempest import auth |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 33 | from tempest import clients |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 34 | from tempest.common import debug |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 35 | from tempest.common import isolated_creds |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 36 | from tempest.common.utils import data_utils |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 37 | from tempest.common.utils.linux import remote_client |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 38 | from tempest import config |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 39 | from tempest import exceptions |
Attila Fazekas | fb7552a | 2013-08-27 13:02:26 +0200 | [diff] [blame] | 40 | from tempest.openstack.common import log |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 41 | from tempest.openstack.common import timeutils |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 42 | from tempest.services.network import resources as net_resources |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 43 | import tempest.test |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 44 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 45 | CONF = config.CONF |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 46 | |
Attila Fazekas | fb7552a | 2013-08-27 13:02:26 +0200 | [diff] [blame] | 47 | LOG = log.getLogger(__name__) |
| 48 | |
| 49 | # NOTE(afazekas): Workaround for the stdout logging |
| 50 | LOG_nova_client = logging.getLogger('novaclient.client') |
| 51 | LOG_nova_client.addHandler(log.NullHandler()) |
| 52 | |
| 53 | LOG_cinder_client = logging.getLogger('cinderclient.client') |
| 54 | LOG_cinder_client.addHandler(log.NullHandler()) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 55 | |
| 56 | |
Andrea Frittoli | 2e733b5 | 2014-07-16 14:12:11 +0100 | [diff] [blame] | 57 | class ScenarioTest(tempest.test.BaseTestCase): |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 58 | """Replaces the OfficialClientTest base class. |
| 59 | |
| 60 | Uses tempest own clients as opposed to OfficialClients. |
| 61 | |
| 62 | Common differences: |
| 63 | - replace resource.attribute with resource['attribute'] |
| 64 | - replace resouce.delete with delete_callable(resource['id']) |
| 65 | - replace local waiters with common / rest_client waiters |
| 66 | """ |
Andrea Frittoli | 2e733b5 | 2014-07-16 14:12:11 +0100 | [diff] [blame] | 67 | |
| 68 | @classmethod |
| 69 | def setUpClass(cls): |
| 70 | super(ScenarioTest, cls).setUpClass() |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 71 | # Using tempest client for isolated credentials as well |
Andrea Frittoli | 2e733b5 | 2014-07-16 14:12:11 +0100 | [diff] [blame] | 72 | cls.isolated_creds = isolated_creds.IsolatedCreds( |
| 73 | cls.__name__, tempest_client=True, |
| 74 | network_resources=cls.network_resources) |
| 75 | cls.manager = clients.Manager( |
| 76 | credentials=cls.credentials() |
| 77 | ) |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 78 | cls.admin_manager = clients.Manager(cls.admin_credentials()) |
| 79 | # Clients (in alphabetical order) |
| 80 | cls.floating_ips_client = cls.manager.floating_ips_client |
| 81 | # Glance image client v1 |
| 82 | cls.image_client = cls.manager.image_client |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 83 | # Compute image client |
| 84 | cls.images_client = cls.manager.images_client |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 85 | cls.keypairs_client = cls.manager.keypairs_client |
| 86 | cls.networks_client = cls.admin_manager.networks_client |
| 87 | # Nova security groups client |
| 88 | cls.security_groups_client = cls.manager.security_groups_client |
| 89 | cls.servers_client = cls.manager.servers_client |
| 90 | cls.volumes_client = cls.manager.volumes_client |
Joseph Lanoux | eef192f | 2014-08-01 14:32:53 +0000 | [diff] [blame] | 91 | cls.snapshots_client = cls.manager.snapshots_client |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 92 | cls.interface_client = cls.manager.interfaces_client |
| 93 | # Neutron network client |
| 94 | cls.network_client = cls.manager.network_client |
Andrea Frittoli | 2e733b5 | 2014-07-16 14:12:11 +0100 | [diff] [blame] | 95 | |
| 96 | @classmethod |
| 97 | def _get_credentials(cls, get_creds, ctype): |
| 98 | if CONF.compute.allow_tenant_isolation: |
| 99 | creds = get_creds() |
| 100 | else: |
| 101 | creds = auth.get_default_credentials(ctype) |
| 102 | return creds |
| 103 | |
| 104 | @classmethod |
| 105 | def credentials(cls): |
| 106 | return cls._get_credentials(cls.isolated_creds.get_primary_creds, |
| 107 | 'user') |
| 108 | |
Masayuki Igawa | ccd6659 | 2014-07-17 00:42:42 +0900 | [diff] [blame] | 109 | @classmethod |
| 110 | def admin_credentials(cls): |
| 111 | return cls._get_credentials(cls.isolated_creds.get_admin_creds, |
| 112 | 'identity_admin') |
| 113 | |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 114 | # ## Methods to handle sync and async deletes |
| 115 | |
| 116 | def setUp(self): |
| 117 | super(ScenarioTest, self).setUp() |
| 118 | self.cleanup_waits = [] |
| 119 | # NOTE(mtreinish) This is safe to do in setUp instead of setUp class |
| 120 | # because scenario tests in the same test class should not share |
| 121 | # resources. If resources were shared between test cases then it |
| 122 | # should be a single scenario test instead of multiples. |
| 123 | |
| 124 | # NOTE(yfried): this list is cleaned at the end of test_methods and |
| 125 | # not at the end of the class |
| 126 | self.addCleanup(self._wait_for_cleanups) |
| 127 | |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 128 | def delete_wrapper(self, delete_thing, *args, **kwargs): |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 129 | """Ignores NotFound exceptions for delete operations. |
| 130 | |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 131 | @param delete_thing: delete method of a resource. method will be |
| 132 | executed as delete_thing(*args, **kwargs) |
| 133 | |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 134 | """ |
| 135 | try: |
| 136 | # Tempest clients return dicts, so there is no common delete |
| 137 | # method available. Using a callable instead |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 138 | delete_thing(*args, **kwargs) |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 139 | except exceptions.NotFound: |
| 140 | # If the resource is already missing, mission accomplished. |
| 141 | pass |
| 142 | |
| 143 | def addCleanup_with_wait(self, waiter_callable, thing_id, thing_id_param, |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 144 | cleanup_callable, cleanup_args=None, |
| 145 | cleanup_kwargs=None, ignore_error=True): |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 146 | """Adds wait for ansyc resource deletion at the end of cleanups |
| 147 | |
| 148 | @param waiter_callable: callable to wait for the resource to delete |
| 149 | @param thing_id: the id of the resource to be cleaned-up |
| 150 | @param thing_id_param: the name of the id param in the waiter |
| 151 | @param cleanup_callable: method to load pass to self.addCleanup with |
| 152 | the following *cleanup_args, **cleanup_kwargs. |
| 153 | usually a delete method. |
| 154 | """ |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 155 | if cleanup_args is None: |
| 156 | cleanup_args = [] |
| 157 | if cleanup_kwargs is None: |
| 158 | cleanup_kwargs = {} |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 159 | self.addCleanup(cleanup_callable, *cleanup_args, **cleanup_kwargs) |
| 160 | wait_dict = { |
| 161 | 'waiter_callable': waiter_callable, |
| 162 | thing_id_param: thing_id |
| 163 | } |
| 164 | self.cleanup_waits.append(wait_dict) |
| 165 | |
| 166 | def _wait_for_cleanups(self): |
| 167 | """To handle async delete actions, a list of waits is added |
| 168 | which will be iterated over as the last step of clearing the |
| 169 | cleanup queue. That way all the delete calls are made up front |
| 170 | and the tests won't succeed unless the deletes are eventually |
| 171 | successful. This is the same basic approach used in the api tests to |
| 172 | limit cleanup execution time except here it is multi-resource, |
| 173 | because of the nature of the scenario tests. |
| 174 | """ |
| 175 | for wait in self.cleanup_waits: |
| 176 | waiter_callable = wait.pop('waiter_callable') |
| 177 | waiter_callable(**wait) |
| 178 | |
| 179 | # ## Test functions library |
| 180 | # |
| 181 | # The create_[resource] functions only return body and discard the |
| 182 | # resp part which is not used in scenario tests |
| 183 | |
| 184 | def create_keypair(self): |
| 185 | name = data_utils.rand_name(self.__class__.__name__) |
| 186 | # We don't need to create a keypair by pubkey in scenario |
| 187 | resp, body = self.keypairs_client.create_keypair(name) |
| 188 | self.addCleanup(self.keypairs_client.delete_keypair, name) |
| 189 | return body |
| 190 | |
| 191 | def create_server(self, name=None, image=None, flavor=None, |
| 192 | wait_on_boot=True, wait_on_delete=True, |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 193 | create_kwargs=None): |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 194 | """Creates VM instance. |
| 195 | |
| 196 | @param image: image from which to create the instance |
| 197 | @param wait_on_boot: wait for status ACTIVE before continue |
| 198 | @param wait_on_delete: force synchronous delete on cleanup |
| 199 | @param create_kwargs: additional details for instance creation |
| 200 | @return: server dict |
| 201 | """ |
| 202 | if name is None: |
| 203 | name = data_utils.rand_name(self.__class__.__name__) |
| 204 | if image is None: |
| 205 | image = CONF.compute.image_ref |
| 206 | if flavor is None: |
| 207 | flavor = CONF.compute.flavor_ref |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 208 | if create_kwargs is None: |
| 209 | create_kwargs = {} |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 210 | |
| 211 | fixed_network_name = CONF.compute.fixed_network_name |
| 212 | if 'nics' not in create_kwargs and fixed_network_name: |
| 213 | _, networks = self.networks_client.list_networks() |
| 214 | # If several networks found, set the NetID on which to connect the |
| 215 | # server to avoid the following error "Multiple possible networks |
| 216 | # found, use a Network ID to be more specific." |
| 217 | # See Tempest #1250866 |
| 218 | if len(networks) > 1: |
| 219 | for network in networks: |
| 220 | if network['label'] == fixed_network_name: |
| 221 | create_kwargs['nics'] = [{'net-id': network['id']}] |
| 222 | break |
| 223 | # If we didn't find the network we were looking for : |
| 224 | else: |
| 225 | msg = ("The network on which the NIC of the server must " |
| 226 | "be connected can not be found : " |
| 227 | "fixed_network_name=%s. Starting instance without " |
| 228 | "specifying a network.") % fixed_network_name |
| 229 | LOG.info(msg) |
| 230 | |
| 231 | LOG.debug("Creating a server (name: %s, image: %s, flavor: %s)", |
| 232 | name, image, flavor) |
| 233 | _, server = self.servers_client.create_server(name, image, flavor, |
| 234 | **create_kwargs) |
| 235 | if wait_on_delete: |
| 236 | self.addCleanup(self.servers_client.wait_for_server_termination, |
| 237 | server['id']) |
| 238 | self.addCleanup_with_wait( |
| 239 | waiter_callable=self.servers_client.wait_for_server_termination, |
| 240 | thing_id=server['id'], thing_id_param='server_id', |
| 241 | cleanup_callable=self.delete_wrapper, |
| 242 | cleanup_args=[self.servers_client.delete_server, server['id']]) |
| 243 | if wait_on_boot: |
| 244 | self.servers_client.wait_for_server_status(server_id=server['id'], |
| 245 | status='ACTIVE') |
| 246 | # The instance retrieved on creation is missing network |
| 247 | # details, necessitating retrieval after it becomes active to |
| 248 | # ensure correct details. |
| 249 | _, server = self.servers_client.get_server(server['id']) |
| 250 | self.assertEqual(server['name'], name) |
| 251 | return server |
| 252 | |
| 253 | def create_volume(self, size=1, name=None, snapshot_id=None, |
| 254 | imageRef=None, volume_type=None, wait_on_delete=True): |
| 255 | if name is None: |
| 256 | name = data_utils.rand_name(self.__class__.__name__) |
| 257 | _, volume = self.volumes_client.create_volume( |
| 258 | size=size, display_name=name, snapshot_id=snapshot_id, |
| 259 | imageRef=imageRef, volume_type=volume_type) |
| 260 | if wait_on_delete: |
| 261 | self.addCleanup(self.volumes_client.wait_for_resource_deletion, |
| 262 | volume['id']) |
| 263 | self.addCleanup_with_wait( |
| 264 | waiter_callable=self.volumes_client.wait_for_resource_deletion, |
| 265 | thing_id=volume['id'], thing_id_param='id', |
| 266 | cleanup_callable=self.delete_wrapper, |
| 267 | cleanup_args=[self.volumes_client.delete_volume, volume['id']]) |
| 268 | |
| 269 | self.assertEqual(name, volume['display_name']) |
| 270 | self.volumes_client.wait_for_volume_status(volume['id'], 'available') |
| 271 | # The volume retrieved on creation has a non-up-to-date status. |
| 272 | # Retrieval after it becomes active ensures correct details. |
| 273 | _, volume = self.volumes_client.get_volume(volume['id']) |
| 274 | return volume |
| 275 | |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 276 | def _create_loginable_secgroup_rule(self, secgroup_id=None): |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 277 | _client = self.security_groups_client |
| 278 | if secgroup_id is None: |
| 279 | _, sgs = _client.list_security_groups() |
| 280 | for sg in sgs: |
| 281 | if sg['name'] == 'default': |
| 282 | secgroup_id = sg['id'] |
| 283 | |
| 284 | # These rules are intended to permit inbound ssh and icmp |
| 285 | # traffic from all sources, so no group_id is provided. |
| 286 | # Setting a group_id would only permit traffic from ports |
| 287 | # belonging to the same security group. |
| 288 | rulesets = [ |
| 289 | { |
| 290 | # ssh |
| 291 | 'ip_proto': 'tcp', |
| 292 | 'from_port': 22, |
| 293 | 'to_port': 22, |
| 294 | 'cidr': '0.0.0.0/0', |
| 295 | }, |
| 296 | { |
| 297 | # ping |
| 298 | 'ip_proto': 'icmp', |
| 299 | 'from_port': -1, |
| 300 | 'to_port': -1, |
| 301 | 'cidr': '0.0.0.0/0', |
| 302 | } |
| 303 | ] |
| 304 | rules = list() |
| 305 | for ruleset in rulesets: |
| 306 | _, sg_rule = _client.create_security_group_rule(secgroup_id, |
| 307 | **ruleset) |
| 308 | self.addCleanup(self.delete_wrapper, |
| 309 | _client.delete_security_group_rule, |
| 310 | sg_rule['id']) |
| 311 | rules.append(sg_rule) |
| 312 | return rules |
| 313 | |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 314 | def _create_security_group(self): |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 315 | # Create security group |
| 316 | sg_name = data_utils.rand_name(self.__class__.__name__) |
| 317 | sg_desc = sg_name + " description" |
| 318 | _, secgroup = self.security_groups_client.create_security_group( |
| 319 | sg_name, sg_desc) |
| 320 | self.assertEqual(secgroup['name'], sg_name) |
| 321 | self.assertEqual(secgroup['description'], sg_desc) |
| 322 | self.addCleanup(self.delete_wrapper, |
| 323 | self.security_groups_client.delete_security_group, |
| 324 | secgroup['id']) |
| 325 | |
| 326 | # Add rules to the security group |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 327 | self._create_loginable_secgroup_rule(secgroup['id']) |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 328 | |
| 329 | return secgroup |
| 330 | |
| 331 | def get_remote_client(self, server_or_ip, username=None, private_key=None): |
| 332 | if isinstance(server_or_ip, six.string_types): |
| 333 | ip = server_or_ip |
| 334 | else: |
| 335 | network_name_for_ssh = CONF.compute.network_for_ssh |
| 336 | ip = server_or_ip.networks[network_name_for_ssh][0] |
| 337 | if username is None: |
| 338 | username = CONF.scenario.ssh_user |
| 339 | if private_key is None: |
| 340 | private_key = self.keypair['private_key'] |
| 341 | linux_client = remote_client.RemoteClient(ip, username, |
| 342 | pkey=private_key) |
| 343 | try: |
| 344 | linux_client.validate_authentication() |
| 345 | except exceptions.SSHTimeout: |
| 346 | LOG.exception('ssh connection to %s failed' % ip) |
| 347 | debug.log_net_debug() |
| 348 | raise |
| 349 | |
| 350 | return linux_client |
| 351 | |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 352 | def _image_create(self, name, fmt, path, properties=None): |
| 353 | if properties is None: |
| 354 | properties = {} |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 355 | name = data_utils.rand_name('%s-' % name) |
| 356 | image_file = open(path, 'rb') |
| 357 | self.addCleanup(image_file.close) |
| 358 | params = { |
| 359 | 'name': name, |
| 360 | 'container_format': fmt, |
| 361 | 'disk_format': fmt, |
| 362 | 'is_public': 'False', |
| 363 | } |
| 364 | params.update(properties) |
| 365 | _, image = self.image_client.create_image(**params) |
| 366 | self.addCleanup(self.image_client.delete_image, image['id']) |
| 367 | self.assertEqual("queued", image['status']) |
| 368 | self.image_client.update_image(image['id'], data=image_file) |
| 369 | return image['id'] |
| 370 | |
| 371 | def glance_image_create(self): |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 372 | img_path = CONF.scenario.img_dir + "/" + CONF.scenario.img_file |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 373 | aki_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.aki_img_file |
| 374 | ari_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ari_img_file |
| 375 | ami_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ami_img_file |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 376 | img_container_format = CONF.scenario.img_container_format |
| 377 | img_disk_format = CONF.scenario.img_disk_format |
| 378 | LOG.debug("paths: img: %s, container_fomat: %s, disk_format: %s, " |
| 379 | "ami: %s, ari: %s, aki: %s" % |
| 380 | (img_path, img_container_format, img_disk_format, |
| 381 | ami_img_path, ari_img_path, aki_img_path)) |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 382 | try: |
| 383 | self.image = self._image_create('scenario-img', |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 384 | img_container_format, |
| 385 | img_path, |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 386 | properties={'disk_format': |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 387 | img_disk_format}) |
Andrea Frittoli | 247058f | 2014-07-16 16:09:22 +0100 | [diff] [blame] | 388 | except IOError: |
| 389 | LOG.debug("A qcow2 image was not found. Try to get a uec image.") |
| 390 | kernel = self._image_create('scenario-aki', 'aki', aki_img_path) |
| 391 | ramdisk = self._image_create('scenario-ari', 'ari', ari_img_path) |
| 392 | properties = { |
| 393 | 'properties': {'kernel_id': kernel, 'ramdisk_id': ramdisk} |
| 394 | } |
| 395 | self.image = self._image_create('scenario-ami', 'ami', |
| 396 | path=ami_img_path, |
| 397 | properties=properties) |
| 398 | LOG.debug("image:%s" % self.image) |
| 399 | |
| 400 | def _log_console_output(self, servers=None): |
| 401 | if not servers: |
| 402 | _, servers = self.servers_client.list_servers() |
| 403 | servers = servers['servers'] |
| 404 | for server in servers: |
| 405 | LOG.debug('Console output for %s', server['id']) |
| 406 | LOG.debug(self.servers_client.get_console_output(server['id'], |
| 407 | length=None)) |
| 408 | |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 409 | def create_server_snapshot(self, server, name=None): |
| 410 | # Glance client |
| 411 | _image_client = self.image_client |
| 412 | # Compute client |
| 413 | _images_client = self.images_client |
| 414 | if name is None: |
| 415 | name = data_utils.rand_name('scenario-snapshot-') |
| 416 | LOG.debug("Creating a snapshot image for server: %s", server['name']) |
| 417 | resp, image = _images_client.create_image(server['id'], name) |
| 418 | image_id = resp['location'].split('images/')[1] |
| 419 | _image_client.wait_for_image_status(image_id, 'active') |
| 420 | self.addCleanup_with_wait( |
| 421 | waiter_callable=_image_client.wait_for_resource_deletion, |
| 422 | thing_id=image_id, thing_id_param='id', |
| 423 | cleanup_callable=self.delete_wrapper, |
| 424 | cleanup_args=[_image_client.delete_image, image_id]) |
| 425 | _, snapshot_image = _image_client.get_image_meta(image_id) |
| 426 | image_name = snapshot_image['name'] |
| 427 | self.assertEqual(name, image_name) |
| 428 | LOG.debug("Created snapshot image %s for server %s", |
| 429 | image_name, server['name']) |
| 430 | return snapshot_image |
| 431 | |
Andrea Frittoli | 2e733b5 | 2014-07-16 14:12:11 +0100 | [diff] [blame] | 432 | |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 433 | # TODO(yfried): change this class name to NetworkScenarioTest once client |
| 434 | # migration is complete |
| 435 | class NeutronScenarioTest(ScenarioTest): |
| 436 | """Base class for network scenario tests. |
| 437 | This class provide helpers for network scenario tests, using the neutron |
| 438 | API. Helpers from ancestor which use the nova network API are overridden |
| 439 | with the neutron API. |
| 440 | |
| 441 | This Class also enforces using Neutron instead of novanetwork. |
| 442 | Subclassed tests will be skipped if Neutron is not enabled |
| 443 | |
| 444 | """ |
| 445 | |
| 446 | @classmethod |
| 447 | def check_preconditions(cls): |
| 448 | if CONF.service_available.neutron: |
| 449 | cls.enabled = True |
| 450 | # verify that neutron_available is telling the truth |
| 451 | try: |
| 452 | cls.network_client.list_networks() |
| 453 | except exc.EndpointNotFound: |
| 454 | cls.enabled = False |
| 455 | raise |
| 456 | else: |
| 457 | cls.enabled = False |
| 458 | msg = 'Neutron not available' |
| 459 | raise cls.skipException(msg) |
| 460 | |
| 461 | @classmethod |
| 462 | def setUpClass(cls): |
| 463 | super(NeutronScenarioTest, cls).setUpClass() |
| 464 | cls.tenant_id = cls.manager.identity_client.tenant_id |
| 465 | cls.check_preconditions() |
| 466 | |
| 467 | def _create_network(self, tenant_id, namestart='network-smoke-'): |
| 468 | name = data_utils.rand_name(namestart) |
| 469 | _, result = self.network_client.create_network(name=name, |
| 470 | tenant_id=tenant_id) |
| 471 | network = net_resources.DeletableNetwork(client=self.network_client, |
| 472 | **result['network']) |
| 473 | self.assertEqual(network.name, name) |
| 474 | self.addCleanup(self.delete_wrapper, network.delete) |
| 475 | return network |
| 476 | |
| 477 | def _list_networks(self, *args, **kwargs): |
| 478 | """List networks using admin creds """ |
| 479 | return self._admin_lister('networks')(*args, **kwargs) |
| 480 | |
| 481 | def _list_subnets(self, *args, **kwargs): |
| 482 | """List subnets using admin creds """ |
| 483 | return self._admin_lister('subnets')(*args, **kwargs) |
| 484 | |
| 485 | def _list_routers(self, *args, **kwargs): |
| 486 | """List routers using admin creds """ |
| 487 | return self._admin_lister('routers')(*args, **kwargs) |
| 488 | |
| 489 | def _list_ports(self, *args, **kwargs): |
| 490 | """List ports using admin creds """ |
| 491 | return self._admin_lister('ports')(*args, **kwargs) |
| 492 | |
| 493 | def _admin_lister(self, resource_type): |
| 494 | def temp(*args, **kwargs): |
| 495 | temp_method = self.admin_manager.network_client.__getattr__( |
| 496 | 'list_%s' % resource_type) |
| 497 | _, resource_list = temp_method(*args, **kwargs) |
| 498 | return resource_list[resource_type] |
| 499 | return temp |
| 500 | |
| 501 | def _create_subnet(self, network, namestart='subnet-smoke-', **kwargs): |
| 502 | """ |
| 503 | Create a subnet for the given network within the cidr block |
| 504 | configured for tenant networks. |
| 505 | """ |
| 506 | |
| 507 | def cidr_in_use(cidr, tenant_id): |
| 508 | """ |
| 509 | :return True if subnet with cidr already exist in tenant |
| 510 | False else |
| 511 | """ |
| 512 | cidr_in_use = self._list_subnets(tenant_id=tenant_id, cidr=cidr) |
| 513 | return len(cidr_in_use) != 0 |
| 514 | |
| 515 | tenant_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
| 516 | result = None |
| 517 | # Repeatedly attempt subnet creation with sequential cidr |
| 518 | # blocks until an unallocated block is found. |
| 519 | for subnet_cidr in tenant_cidr.subnet( |
| 520 | CONF.network.tenant_network_mask_bits): |
| 521 | str_cidr = str(subnet_cidr) |
| 522 | if cidr_in_use(str_cidr, tenant_id=network.tenant_id): |
| 523 | continue |
| 524 | |
| 525 | subnet = dict( |
| 526 | name=data_utils.rand_name(namestart), |
| 527 | ip_version=4, |
| 528 | network_id=network.id, |
| 529 | tenant_id=network.tenant_id, |
| 530 | cidr=str_cidr, |
| 531 | **kwargs |
| 532 | ) |
| 533 | try: |
| 534 | _, result = self.network_client.create_subnet(**subnet) |
| 535 | break |
| 536 | except exc.NeutronClientException as e: |
| 537 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 538 | if not is_overlapping_cidr: |
| 539 | raise |
| 540 | self.assertIsNotNone(result, 'Unable to allocate tenant network') |
| 541 | subnet = net_resources.DeletableSubnet(client=self.network_client, |
| 542 | **result['subnet']) |
| 543 | self.assertEqual(subnet.cidr, str_cidr) |
| 544 | self.addCleanup(self.delete_wrapper, subnet.delete) |
| 545 | return subnet |
| 546 | |
| 547 | def _create_port(self, network, namestart='port-quotatest'): |
| 548 | name = data_utils.rand_name(namestart) |
| 549 | _, result = self.network_client.create_port( |
| 550 | name=name, |
| 551 | network_id=network.id, |
| 552 | tenant_id=network.tenant_id) |
| 553 | self.assertIsNotNone(result, 'Unable to allocate port') |
| 554 | port = net_resources.DeletablePort(client=self.network_client, |
| 555 | **result['port']) |
| 556 | self.addCleanup(self.delete_wrapper, port.delete) |
| 557 | return port |
| 558 | |
| 559 | def _get_server_port_id(self, server, ip_addr=None): |
| 560 | ports = self._list_ports(device_id=server['id'], |
| 561 | fixed_ip=ip_addr) |
| 562 | self.assertEqual(len(ports), 1, |
| 563 | "Unable to determine which port to target.") |
| 564 | return ports[0]['id'] |
| 565 | |
| 566 | def _create_floating_ip(self, thing, external_network_id, port_id=None): |
| 567 | if not port_id: |
| 568 | port_id = self._get_server_port_id(thing) |
| 569 | _, result = self.network_client.create_floatingip( |
| 570 | floating_network_id=external_network_id, |
| 571 | port_id=port_id, |
| 572 | tenant_id=thing['tenant_id'] |
| 573 | ) |
| 574 | floating_ip = net_resources.DeletableFloatingIp( |
| 575 | client=self.network_client, |
| 576 | **result['floatingip']) |
| 577 | self.addCleanup(self.delete_wrapper, floating_ip.delete) |
| 578 | return floating_ip |
| 579 | |
| 580 | def _associate_floating_ip(self, floating_ip, server): |
| 581 | port_id = self._get_server_port_id(server) |
| 582 | floating_ip.update(port_id=port_id) |
| 583 | self.assertEqual(port_id, floating_ip.port_id) |
| 584 | return floating_ip |
| 585 | |
| 586 | def _disassociate_floating_ip(self, floating_ip): |
| 587 | """ |
| 588 | :param floating_ip: type DeletableFloatingIp |
| 589 | """ |
| 590 | floating_ip.update(port_id=None) |
| 591 | self.assertIsNone(floating_ip.port_id) |
| 592 | return floating_ip |
| 593 | |
| 594 | def _ping_ip_address(self, ip_address, should_succeed=True): |
| 595 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 596 | |
| 597 | def ping(): |
| 598 | proc = subprocess.Popen(cmd, |
| 599 | stdout=subprocess.PIPE, |
| 600 | stderr=subprocess.PIPE) |
| 601 | proc.wait() |
| 602 | return (proc.returncode == 0) == should_succeed |
| 603 | |
| 604 | return tempest.test.call_until_true( |
| 605 | ping, CONF.compute.ping_timeout, 1) |
| 606 | |
| 607 | def _check_vm_connectivity(self, ip_address, |
| 608 | username=None, |
| 609 | private_key=None, |
| 610 | should_connect=True): |
| 611 | """ |
| 612 | :param ip_address: server to test against |
| 613 | :param username: server's ssh username |
| 614 | :param private_key: server's ssh private key to be used |
| 615 | :param should_connect: True/False indicates positive/negative test |
| 616 | positive - attempt ping and ssh |
| 617 | negative - attempt ping and fail if succeed |
| 618 | |
| 619 | :raises: AssertError if the result of the connectivity check does |
| 620 | not match the value of the should_connect param |
| 621 | """ |
| 622 | if should_connect: |
| 623 | msg = "Timed out waiting for %s to become reachable" % ip_address |
| 624 | else: |
| 625 | msg = "ip address %s is reachable" % ip_address |
| 626 | self.assertTrue(self._ping_ip_address(ip_address, |
| 627 | should_succeed=should_connect), |
| 628 | msg=msg) |
| 629 | if should_connect: |
| 630 | # no need to check ssh for negative connectivity |
| 631 | self.get_remote_client(ip_address, username, private_key) |
| 632 | |
| 633 | def _check_public_network_connectivity(self, ip_address, username, |
| 634 | private_key, should_connect=True, |
| 635 | msg=None, servers=None): |
| 636 | # The target login is assumed to have been configured for |
| 637 | # key-based authentication by cloud-init. |
| 638 | LOG.debug('checking network connections to IP %s with user: %s' % |
| 639 | (ip_address, username)) |
| 640 | try: |
| 641 | self._check_vm_connectivity(ip_address, |
| 642 | username, |
| 643 | private_key, |
| 644 | should_connect=should_connect) |
| 645 | except Exception as e: |
| 646 | ex_msg = 'Public network connectivity check failed' |
| 647 | if msg: |
| 648 | ex_msg += ": " + msg |
| 649 | LOG.exception(ex_msg) |
| 650 | self._log_console_output(servers) |
| 651 | # network debug is called as part of ssh init |
| 652 | if not isinstance(e, exceptions.SSHTimeout): |
| 653 | debug.log_net_debug() |
| 654 | raise |
| 655 | |
| 656 | def _check_tenant_network_connectivity(self, server, |
| 657 | username, |
| 658 | private_key, |
| 659 | should_connect=True, |
| 660 | servers_for_debug=None): |
| 661 | if not CONF.network.tenant_networks_reachable: |
| 662 | msg = 'Tenant networks not configured to be reachable.' |
| 663 | LOG.info(msg) |
| 664 | return |
| 665 | # The target login is assumed to have been configured for |
| 666 | # key-based authentication by cloud-init. |
| 667 | try: |
| 668 | for net_name, ip_addresses in server['networks'].iteritems(): |
| 669 | for ip_address in ip_addresses: |
| 670 | self._check_vm_connectivity(ip_address, |
| 671 | username, |
| 672 | private_key, |
| 673 | should_connect=should_connect) |
| 674 | except Exception as e: |
| 675 | LOG.exception('Tenant network connectivity check failed') |
| 676 | self._log_console_output(servers_for_debug) |
| 677 | # network debug is called as part of ssh init |
| 678 | if not isinstance(e, exceptions.SSHTimeout): |
| 679 | debug.log_net_debug() |
| 680 | raise |
| 681 | |
| 682 | def _check_remote_connectivity(self, source, dest, should_succeed=True): |
| 683 | """ |
| 684 | check ping server via source ssh connection |
| 685 | |
| 686 | :param source: RemoteClient: an ssh connection from which to ping |
| 687 | :param dest: and IP to ping against |
| 688 | :param should_succeed: boolean should ping succeed or not |
| 689 | :returns: boolean -- should_succeed == ping |
| 690 | :returns: ping is false if ping failed |
| 691 | """ |
| 692 | def ping_remote(): |
| 693 | try: |
| 694 | source.ping_host(dest) |
| 695 | except exceptions.SSHExecCommandFailed: |
| 696 | LOG.warn('Failed to ping IP: %s via a ssh connection from: %s.' |
| 697 | % (dest, source.ssh_client.host)) |
| 698 | return not should_succeed |
| 699 | return should_succeed |
| 700 | |
| 701 | return tempest.test.call_until_true(ping_remote, |
| 702 | CONF.compute.ping_timeout, |
| 703 | 1) |
| 704 | |
| 705 | def _create_security_group(self, tenant_id, client=None, |
| 706 | namestart='secgroup-smoke'): |
| 707 | if client is None: |
| 708 | client = self.network_client |
| 709 | secgroup = self._create_empty_security_group(namestart=namestart, |
| 710 | client=client, |
| 711 | tenant_id=tenant_id) |
| 712 | |
| 713 | # Add rules to the security group |
| 714 | rules = self._create_loginable_secgroup_rule(secgroup=secgroup) |
| 715 | for rule in rules: |
| 716 | self.assertEqual(tenant_id, rule.tenant_id) |
| 717 | self.assertEqual(secgroup.id, rule.security_group_id) |
| 718 | return secgroup |
| 719 | |
| 720 | def _create_empty_security_group(self, tenant_id, client=None, |
| 721 | namestart='secgroup-smoke'): |
| 722 | """Create a security group without rules. |
| 723 | |
| 724 | Default rules will be created: |
| 725 | - IPv4 egress to any |
| 726 | - IPv6 egress to any |
| 727 | |
| 728 | :param tenant_id: secgroup will be created in this tenant |
| 729 | :returns: DeletableSecurityGroup -- containing the secgroup created |
| 730 | """ |
| 731 | if client is None: |
| 732 | client = self.network_client |
| 733 | sg_name = data_utils.rand_name(namestart) |
| 734 | sg_desc = sg_name + " description" |
| 735 | sg_dict = dict(name=sg_name, |
| 736 | description=sg_desc) |
| 737 | sg_dict['tenant_id'] = tenant_id |
| 738 | _, result = client.create_security_group(**sg_dict) |
| 739 | secgroup = net_resources.DeletableSecurityGroup( |
| 740 | client=client, |
| 741 | **result['security_group'] |
| 742 | ) |
| 743 | self.assertEqual(secgroup.name, sg_name) |
| 744 | self.assertEqual(tenant_id, secgroup.tenant_id) |
| 745 | self.assertEqual(secgroup.description, sg_desc) |
| 746 | self.addCleanup(self.delete_wrapper, secgroup.delete) |
| 747 | return secgroup |
| 748 | |
| 749 | def _default_security_group(self, tenant_id, client=None): |
| 750 | """Get default secgroup for given tenant_id. |
| 751 | |
| 752 | :returns: DeletableSecurityGroup -- default secgroup for given tenant |
| 753 | """ |
| 754 | if client is None: |
| 755 | client = self.network_client |
| 756 | sgs = [ |
| 757 | sg for sg in client.list_security_groups().values()[0] |
| 758 | if sg['tenant_id'] == tenant_id and sg['name'] == 'default' |
| 759 | ] |
| 760 | msg = "No default security group for tenant %s." % (tenant_id) |
| 761 | self.assertTrue(len(sgs) > 0, msg) |
| 762 | if len(sgs) > 1: |
| 763 | msg = "Found %d default security groups" % len(sgs) |
| 764 | raise exc.NeutronClientNoUniqueMatch(msg=msg) |
| 765 | return net_resources.DeletableSecurityGroup(client=client, |
| 766 | **sgs[0]) |
| 767 | |
| 768 | def _create_security_group_rule(self, client=None, secgroup=None, |
| 769 | tenant_id=None, **kwargs): |
| 770 | """Create a rule from a dictionary of rule parameters. |
| 771 | |
| 772 | Create a rule in a secgroup. if secgroup not defined will search for |
| 773 | default secgroup in tenant_id. |
| 774 | |
| 775 | :param secgroup: type DeletableSecurityGroup. |
| 776 | :param secgroup_id: search for secgroup by id |
| 777 | default -- choose default secgroup for given tenant_id |
| 778 | :param tenant_id: if secgroup not passed -- the tenant in which to |
| 779 | search for default secgroup |
| 780 | :param kwargs: a dictionary containing rule parameters: |
| 781 | for example, to allow incoming ssh: |
| 782 | rule = { |
| 783 | direction: 'ingress' |
| 784 | protocol:'tcp', |
| 785 | port_range_min: 22, |
| 786 | port_range_max: 22 |
| 787 | } |
| 788 | """ |
| 789 | if client is None: |
| 790 | client = self.network_client |
| 791 | if secgroup is None: |
| 792 | secgroup = self._default_security_group(tenant_id) |
| 793 | |
| 794 | ruleset = dict(security_group_id=secgroup.id, |
| 795 | tenant_id=secgroup.tenant_id) |
| 796 | ruleset.update(kwargs) |
| 797 | |
| 798 | _, sg_rule = client.create_security_group_rule(**ruleset) |
| 799 | sg_rule = net_resources.DeletableSecurityGroupRule( |
| 800 | client=client, |
| 801 | **sg_rule['security_group_rule'] |
| 802 | ) |
| 803 | self.addCleanup(self.delete_wrapper, sg_rule.delete) |
| 804 | self.assertEqual(secgroup.tenant_id, sg_rule.tenant_id) |
| 805 | self.assertEqual(secgroup.id, sg_rule.security_group_id) |
| 806 | |
| 807 | return sg_rule |
| 808 | |
| 809 | def _create_loginable_secgroup_rule(self, client=None, secgroup=None): |
| 810 | """These rules are intended to permit inbound ssh and icmp |
| 811 | traffic from all sources, so no group_id is provided. |
| 812 | Setting a group_id would only permit traffic from ports |
| 813 | belonging to the same security group. |
| 814 | """ |
| 815 | |
| 816 | if client is None: |
| 817 | client = self.network_client |
| 818 | rules = [] |
| 819 | rulesets = [ |
| 820 | dict( |
| 821 | # ssh |
| 822 | protocol='tcp', |
| 823 | port_range_min=22, |
| 824 | port_range_max=22, |
| 825 | ), |
| 826 | dict( |
| 827 | # ping |
| 828 | protocol='icmp', |
| 829 | ) |
| 830 | ] |
| 831 | for ruleset in rulesets: |
| 832 | for r_direction in ['ingress', 'egress']: |
| 833 | ruleset['direction'] = r_direction |
| 834 | try: |
| 835 | sg_rule = self._create_security_group_rule( |
| 836 | client=client, secgroup=secgroup, **ruleset) |
| 837 | except exceptions.Conflict as ex: |
| 838 | # if rule already exist - skip rule and continue |
| 839 | msg = 'Security group rule already exists' |
| 840 | if msg not in ex._error_string: |
| 841 | raise ex |
| 842 | else: |
| 843 | self.assertEqual(r_direction, sg_rule.direction) |
| 844 | rules.append(sg_rule) |
| 845 | |
| 846 | return rules |
| 847 | |
| 848 | def _ssh_to_server(self, server, private_key): |
| 849 | ssh_login = CONF.compute.image_ssh_user |
| 850 | return self.get_remote_client(server, |
| 851 | username=ssh_login, |
| 852 | private_key=private_key) |
| 853 | |
| 854 | def _get_router(self, tenant_id): |
| 855 | """Retrieve a router for the given tenant id. |
| 856 | |
| 857 | If a public router has been configured, it will be returned. |
| 858 | |
| 859 | If a public router has not been configured, but a public |
| 860 | network has, a tenant router will be created and returned that |
| 861 | routes traffic to the public network. |
| 862 | """ |
| 863 | router_id = CONF.network.public_router_id |
| 864 | network_id = CONF.network.public_network_id |
| 865 | if router_id: |
| 866 | result = self.network_client.show_router(router_id) |
| 867 | return net_resources.AttributeDict(**result['router']) |
| 868 | elif network_id: |
| 869 | router = self._create_router(tenant_id) |
| 870 | router.set_gateway(network_id) |
| 871 | return router |
| 872 | else: |
| 873 | raise Exception("Neither of 'public_router_id' or " |
| 874 | "'public_network_id' has been defined.") |
| 875 | |
| 876 | def _create_router(self, tenant_id, namestart='router-smoke-'): |
| 877 | name = data_utils.rand_name(namestart) |
| 878 | _, result = self.network_client.create_router(name=name, |
| 879 | admin_state_up=True, |
| 880 | tenant_id=tenant_id, ) |
| 881 | router = net_resources.DeletableRouter(client=self.network_client, |
| 882 | **result['router']) |
| 883 | self.assertEqual(router.name, name) |
| 884 | self.addCleanup(self.delete_wrapper, router.delete) |
| 885 | return router |
| 886 | |
| 887 | def _create_networks(self, tenant_id=None): |
| 888 | """Create a network with a subnet connected to a router. |
| 889 | |
| 890 | :returns: network, subnet, router |
| 891 | """ |
| 892 | if tenant_id is None: |
| 893 | tenant_id = self.tenant_id |
| 894 | network = self._create_network(tenant_id) |
| 895 | router = self._get_router(tenant_id) |
| 896 | subnet = self._create_subnet(network) |
| 897 | subnet.add_to_router(router.id) |
| 898 | return network, subnet, router |
| 899 | |
| 900 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 901 | class OfficialClientTest(tempest.test.BaseTestCase): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 902 | """ |
| 903 | Official Client test base class for scenario testing. |
| 904 | |
| 905 | Official Client tests are tests that have the following characteristics: |
| 906 | |
| 907 | * Test basic operations of an API, typically in an order that |
| 908 | a regular user would perform those operations |
| 909 | * Test only the correct inputs and action paths -- no fuzz or |
| 910 | random input data is sent, only valid inputs. |
| 911 | * Use only the default client tool for calling an API |
| 912 | """ |
| 913 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 914 | @classmethod |
| 915 | def setUpClass(cls): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 916 | super(OfficialClientTest, cls).setUpClass() |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 917 | cls.isolated_creds = isolated_creds.IsolatedCreds( |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 918 | cls.__name__, tempest_client=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 919 | network_resources=cls.network_resources) |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 920 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 921 | cls.manager = clients.OfficialClientManager( |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 922 | credentials=cls.credentials()) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 923 | cls.compute_client = cls.manager.compute_client |
| 924 | cls.image_client = cls.manager.image_client |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 925 | cls.baremetal_client = cls.manager.baremetal_client |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 926 | cls.identity_client = cls.manager.identity_client |
| 927 | cls.network_client = cls.manager.network_client |
| 928 | cls.volume_client = cls.manager.volume_client |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 929 | cls.object_storage_client = cls.manager.object_storage_client |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 930 | cls.orchestration_client = cls.manager.orchestration_client |
Sergey Lukjanov | 7409e2e | 2014-03-27 12:55:50 +0400 | [diff] [blame] | 931 | cls.data_processing_client = cls.manager.data_processing_client |
Artur Svechnikov | c3bf925 | 2014-05-05 16:37:37 +0400 | [diff] [blame] | 932 | cls.ceilometer_client = cls.manager.ceilometer_client |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 933 | |
| 934 | @classmethod |
Miguel Lavalle | da5f708 | 2014-07-16 19:18:22 -0500 | [diff] [blame] | 935 | def tearDownClass(cls): |
| 936 | cls.isolated_creds.clear_isolated_creds() |
| 937 | super(OfficialClientTest, cls).tearDownClass() |
| 938 | |
| 939 | @classmethod |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 940 | def _get_credentials(cls, get_creds, ctype): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 941 | if CONF.compute.allow_tenant_isolation: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 942 | creds = get_creds() |
Yair Fried | 769bbff | 2013-12-18 16:33:17 +0200 | [diff] [blame] | 943 | else: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 944 | creds = auth.get_default_credentials(ctype) |
| 945 | return creds |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 946 | |
| 947 | @classmethod |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 948 | def credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 949 | return cls._get_credentials(cls.isolated_creds.get_primary_creds, |
| 950 | 'user') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 951 | |
| 952 | @classmethod |
| 953 | def alt_credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 954 | return cls._get_credentials(cls.isolated_creds.get_alt_creds, |
| 955 | 'alt_user') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 956 | |
| 957 | @classmethod |
| 958 | def admin_credentials(cls): |
| 959 | return cls._get_credentials(cls.isolated_creds.get_admin_creds, |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 960 | 'identity_admin') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 961 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 962 | def setUp(self): |
| 963 | super(OfficialClientTest, self).setUp() |
| 964 | self.cleanup_waits = [] |
| 965 | # NOTE(mtreinish) This is safe to do in setUp instead of setUp class |
| 966 | # because scenario tests in the same test class should not share |
| 967 | # resources. If resources were shared between test cases then it |
| 968 | # should be a single scenario test instead of multiples. |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 969 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 970 | # NOTE(yfried): this list is cleaned at the end of test_methods and |
| 971 | # not at the end of the class |
| 972 | self.addCleanup(self._wait_for_cleanups) |
| 973 | |
| 974 | @staticmethod |
| 975 | def not_found_exception(exception): |
| 976 | """ |
| 977 | @return: True if exception is of NotFound type |
| 978 | """ |
| 979 | NOT_FOUND_LIST = ['NotFound', 'HTTPNotFound'] |
| 980 | return (exception.__class__.__name__ in NOT_FOUND_LIST |
| 981 | or |
| 982 | hasattr(exception, 'status_code') and |
| 983 | exception.status_code == 404) |
| 984 | |
| 985 | def delete_wrapper(self, thing): |
| 986 | """Ignores NotFound exceptions for delete operations. |
| 987 | |
| 988 | @param thing: object with delete() method. |
| 989 | OpenStack resources are assumed to have a delete() method which |
| 990 | destroys the resource |
| 991 | """ |
| 992 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 993 | try: |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 994 | thing.delete() |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 995 | except Exception as e: |
| 996 | # If the resource is already missing, mission accomplished. |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 997 | if not self.not_found_exception(e): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 998 | raise |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 999 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1000 | def _wait_for_cleanups(self): |
| 1001 | """To handle async delete actions, a list of waits is added |
| 1002 | which will be iterated over as the last step of clearing the |
| 1003 | cleanup queue. That way all the delete calls are made up front |
| 1004 | and the tests won't succeed unless the deletes are eventually |
| 1005 | successful. This is the same basic approach used in the api tests to |
| 1006 | limit cleanup execution time except here it is multi-resource, |
| 1007 | because of the nature of the scenario tests. |
| 1008 | """ |
| 1009 | for wait in self.cleanup_waits: |
| 1010 | self.delete_timeout(**wait) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 1011 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1012 | def addCleanup_with_wait(self, things, thing_id, |
| 1013 | error_status='ERROR', |
| 1014 | exc_type=nova_exceptions.NotFound, |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 1015 | cleanup_callable=None, cleanup_args=None, |
| 1016 | cleanup_kwargs=None): |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1017 | """Adds wait for ansyc resource deletion at the end of cleanups |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1018 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1019 | @param things: type of the resource to delete |
| 1020 | @param thing_id: |
| 1021 | @param error_status: see manager.delete_timeout() |
| 1022 | @param exc_type: see manager.delete_timeout() |
| 1023 | @param cleanup_callable: method to load pass to self.addCleanup with |
| 1024 | the following *cleanup_args, **cleanup_kwargs. |
| 1025 | usually a delete method. if not used, will try to use: |
| 1026 | things.delete(thing_id) |
| 1027 | """ |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 1028 | if cleanup_args is None: |
| 1029 | cleanup_args = [] |
| 1030 | if cleanup_kwargs is None: |
| 1031 | cleanup_kwargs = {} |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1032 | if cleanup_callable is None: |
| 1033 | LOG.debug("no delete method passed. using {rclass}.delete({id}) as" |
| 1034 | " default".format(rclass=things, id=thing_id)) |
| 1035 | self.addCleanup(things.delete, thing_id) |
| 1036 | else: |
| 1037 | self.addCleanup(cleanup_callable, *cleanup_args, **cleanup_kwargs) |
| 1038 | wait_dict = { |
| 1039 | 'things': things, |
| 1040 | 'thing_id': thing_id, |
| 1041 | 'error_status': error_status, |
| 1042 | 'not_found_exception': exc_type, |
| 1043 | } |
| 1044 | self.cleanup_waits.append(wait_dict) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1045 | |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 1046 | def status_timeout(self, things, thing_id, expected_status, |
| 1047 | error_status='ERROR', |
| 1048 | not_found_exception=nova_exceptions.NotFound): |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1049 | """ |
| 1050 | Given a thing and an expected status, do a loop, sleeping |
| 1051 | for a configurable amount of time, checking for the |
| 1052 | expected status to show. At any time, if the returned |
| 1053 | status of the thing is ERROR, fail out. |
| 1054 | """ |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 1055 | self._status_timeout(things, thing_id, |
| 1056 | expected_status=expected_status, |
| 1057 | error_status=error_status, |
| 1058 | not_found_exception=not_found_exception) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1059 | |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 1060 | def delete_timeout(self, things, thing_id, |
| 1061 | error_status='ERROR', |
| 1062 | not_found_exception=nova_exceptions.NotFound): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1063 | """ |
| 1064 | Given a thing, do a loop, sleeping |
| 1065 | for a configurable amount of time, checking for the |
| 1066 | deleted status to show. At any time, if the returned |
| 1067 | status of the thing is ERROR, fail out. |
| 1068 | """ |
| 1069 | self._status_timeout(things, |
| 1070 | thing_id, |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 1071 | allow_notfound=True, |
| 1072 | error_status=error_status, |
| 1073 | not_found_exception=not_found_exception) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1074 | |
| 1075 | def _status_timeout(self, |
| 1076 | things, |
| 1077 | thing_id, |
| 1078 | expected_status=None, |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 1079 | allow_notfound=False, |
| 1080 | error_status='ERROR', |
| 1081 | not_found_exception=nova_exceptions.NotFound): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1082 | |
| 1083 | log_status = expected_status if expected_status else '' |
| 1084 | if allow_notfound: |
| 1085 | log_status += ' or NotFound' if log_status != '' else 'NotFound' |
| 1086 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1087 | def check_status(): |
| 1088 | # python-novaclient has resources available to its client |
| 1089 | # that all implement a get() method taking an identifier |
| 1090 | # for the singular resource to retrieve. |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1091 | try: |
| 1092 | thing = things.get(thing_id) |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 1093 | except not_found_exception: |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1094 | if allow_notfound: |
| 1095 | return True |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1096 | raise |
| 1097 | except Exception as e: |
| 1098 | if allow_notfound and self.not_found_exception(e): |
| 1099 | return True |
| 1100 | raise |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1101 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1102 | new_status = thing.status |
Brent Eagles | c26d452 | 2013-12-02 13:28:49 -0500 | [diff] [blame] | 1103 | |
| 1104 | # Some components are reporting error status in lower case |
| 1105 | # so case sensitive comparisons can really mess things |
| 1106 | # up. |
| 1107 | if new_status.lower() == error_status.lower(): |
Masayuki Igawa | 2a8a812 | 2014-02-07 11:24:49 +0900 | [diff] [blame] | 1108 | message = ("%s failed to get to expected status (%s). " |
| 1109 | "In %s state.") % (thing, expected_status, |
| 1110 | new_status) |
Masayuki Igawa | a0e786a | 2014-01-27 15:25:06 +0900 | [diff] [blame] | 1111 | raise exceptions.BuildErrorException(message, |
| 1112 | server_id=thing_id) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1113 | elif new_status == expected_status and expected_status is not None: |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1114 | return True # All good. |
| 1115 | LOG.debug("Waiting for %s to get to %s status. " |
| 1116 | "Currently in %s status", |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 1117 | thing, log_status, new_status) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1118 | if not tempest.test.call_until_true( |
| 1119 | check_status, |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1120 | CONF.compute.build_timeout, |
| 1121 | CONF.compute.build_interval): |
Ken'ichi Ohmichi | ab1496f | 2013-12-12 22:17:57 +0900 | [diff] [blame] | 1122 | message = ("Timed out waiting for thing %s " |
| 1123 | "to become %s") % (thing_id, log_status) |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 1124 | raise exceptions.TimeoutException(message) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 1125 | |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1126 | def _create_loginable_secgroup_rule_nova(self, client=None, |
| 1127 | secgroup_id=None): |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 1128 | if client is None: |
| 1129 | client = self.compute_client |
| 1130 | if secgroup_id is None: |
| 1131 | sgs = client.security_groups.list() |
| 1132 | for sg in sgs: |
| 1133 | if sg.name == 'default': |
| 1134 | secgroup_id = sg.id |
| 1135 | |
| 1136 | # These rules are intended to permit inbound ssh and icmp |
| 1137 | # traffic from all sources, so no group_id is provided. |
| 1138 | # Setting a group_id would only permit traffic from ports |
| 1139 | # belonging to the same security group. |
| 1140 | rulesets = [ |
| 1141 | { |
| 1142 | # ssh |
| 1143 | 'ip_protocol': 'tcp', |
| 1144 | 'from_port': 22, |
| 1145 | 'to_port': 22, |
| 1146 | 'cidr': '0.0.0.0/0', |
| 1147 | }, |
| 1148 | { |
Kirill Shileev | 2f9111d | 2014-08-21 14:32:57 +0400 | [diff] [blame] | 1149 | # ssh -6 |
| 1150 | 'ip_protocol': 'tcp', |
| 1151 | 'from_port': 22, |
| 1152 | 'to_port': 22, |
| 1153 | 'cidr': '::/0', |
| 1154 | }, |
| 1155 | { |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 1156 | # ping |
| 1157 | 'ip_protocol': 'icmp', |
| 1158 | 'from_port': -1, |
| 1159 | 'to_port': -1, |
| 1160 | 'cidr': '0.0.0.0/0', |
Kirill Shileev | 2f9111d | 2014-08-21 14:32:57 +0400 | [diff] [blame] | 1161 | }, |
| 1162 | { |
| 1163 | # ping6 |
| 1164 | 'ip_protocol': 'icmp', |
| 1165 | 'from_port': -1, |
| 1166 | 'to_port': -1, |
| 1167 | 'cidr': '::/0', |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 1168 | } |
| 1169 | ] |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1170 | rules = list() |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 1171 | for ruleset in rulesets: |
| 1172 | sg_rule = client.security_group_rules.create(secgroup_id, |
| 1173 | **ruleset) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1174 | self.addCleanup(self.delete_wrapper, sg_rule) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1175 | rules.append(sg_rule) |
| 1176 | return rules |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 1177 | |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame] | 1178 | def _create_security_group_nova(self, client=None, |
| 1179 | namestart='secgroup-smoke-'): |
| 1180 | if client is None: |
| 1181 | client = self.compute_client |
| 1182 | # Create security group |
| 1183 | sg_name = data_utils.rand_name(namestart) |
| 1184 | sg_desc = sg_name + " description" |
| 1185 | secgroup = client.security_groups.create(sg_name, sg_desc) |
| 1186 | self.assertEqual(secgroup.name, sg_name) |
| 1187 | self.assertEqual(secgroup.description, sg_desc) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1188 | self.addCleanup(self.delete_wrapper, secgroup) |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame] | 1189 | |
| 1190 | # Add rules to the security group |
| 1191 | self._create_loginable_secgroup_rule_nova(client, secgroup.id) |
| 1192 | |
| 1193 | return secgroup |
| 1194 | |
David Shrewsbury | 0271936 | 2014-05-20 14:10:03 -0400 | [diff] [blame] | 1195 | def rebuild_server(self, server, client=None, image=None, |
| 1196 | preserve_ephemeral=False, wait=True, |
| 1197 | rebuild_kwargs=None): |
| 1198 | if client is None: |
| 1199 | client = self.compute_client |
| 1200 | if image is None: |
| 1201 | image = CONF.compute.image_ref |
| 1202 | rebuild_kwargs = rebuild_kwargs or {} |
| 1203 | |
| 1204 | LOG.debug("Rebuilding server (name: %s, image: %s, preserve eph: %s)", |
| 1205 | server.name, image, preserve_ephemeral) |
| 1206 | server.rebuild(image, preserve_ephemeral=preserve_ephemeral, |
| 1207 | **rebuild_kwargs) |
| 1208 | if wait: |
| 1209 | self.status_timeout(client.servers, server.id, 'ACTIVE') |
| 1210 | |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 1211 | def create_server(self, client=None, name=None, image=None, flavor=None, |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1212 | wait_on_boot=True, wait_on_delete=True, |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 1213 | create_kwargs=None): |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1214 | """Creates VM instance. |
| 1215 | |
| 1216 | @param client: compute client to create the instance |
| 1217 | @param image: image from which to create the instance |
| 1218 | @param wait_on_boot: wait for status ACTIVE before continue |
| 1219 | @param wait_on_delete: force synchronous delete on cleanup |
| 1220 | @param create_kwargs: additional details for instance creation |
| 1221 | @return: client.server object |
| 1222 | """ |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 1223 | if client is None: |
| 1224 | client = self.compute_client |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 1225 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1226 | name = data_utils.rand_name('scenario-server-') |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 1227 | if image is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1228 | image = CONF.compute.image_ref |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 1229 | if flavor is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1230 | flavor = CONF.compute.flavor_ref |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 1231 | if create_kwargs is None: |
| 1232 | create_kwargs = {} |
JordanP | 9c052aa | 2014-01-24 13:05:00 +0000 | [diff] [blame] | 1233 | |
| 1234 | fixed_network_name = CONF.compute.fixed_network_name |
| 1235 | if 'nics' not in create_kwargs and fixed_network_name: |
| 1236 | networks = client.networks.list() |
| 1237 | # If several networks found, set the NetID on which to connect the |
| 1238 | # server to avoid the following error "Multiple possible networks |
| 1239 | # found, use a Network ID to be more specific." |
| 1240 | # See Tempest #1250866 |
| 1241 | if len(networks) > 1: |
| 1242 | for network in networks: |
| 1243 | if network.label == fixed_network_name: |
| 1244 | create_kwargs['nics'] = [{'net-id': network.id}] |
| 1245 | break |
| 1246 | # If we didn't find the network we were looking for : |
| 1247 | else: |
| 1248 | msg = ("The network on which the NIC of the server must " |
| 1249 | "be connected can not be found : " |
| 1250 | "fixed_network_name=%s. Starting instance without " |
| 1251 | "specifying a network.") % fixed_network_name |
| 1252 | LOG.info(msg) |
| 1253 | |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 1254 | LOG.debug("Creating a server (name: %s, image: %s, flavor: %s)", |
| 1255 | name, image, flavor) |
| 1256 | server = client.servers.create(name, image, flavor, **create_kwargs) |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 1257 | self.assertEqual(server.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1258 | if wait_on_delete: |
| 1259 | self.addCleanup(self.delete_timeout, |
| 1260 | self.compute_client.servers, |
| 1261 | server.id) |
| 1262 | self.addCleanup_with_wait(self.compute_client.servers, server.id, |
| 1263 | cleanup_callable=self.delete_wrapper, |
| 1264 | cleanup_args=[server]) |
| 1265 | if wait_on_boot: |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 1266 | self.status_timeout(client.servers, server.id, 'ACTIVE') |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 1267 | # The instance retrieved on creation is missing network |
| 1268 | # details, necessitating retrieval after it becomes active to |
| 1269 | # ensure correct details. |
| 1270 | server = client.servers.get(server.id) |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 1271 | LOG.debug("Created server: %s", server) |
| 1272 | return server |
| 1273 | |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 1274 | def create_volume(self, client=None, size=1, name=None, |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1275 | snapshot_id=None, imageRef=None, volume_type=None, |
| 1276 | wait_on_delete=True): |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 1277 | if client is None: |
| 1278 | client = self.volume_client |
| 1279 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1280 | name = data_utils.rand_name('scenario-volume-') |
Eric Windisch | 2d26f1b | 2013-09-04 17:52:16 -0700 | [diff] [blame] | 1281 | LOG.debug("Creating a volume (size: %s, name: %s)", size, name) |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 1282 | volume = client.volumes.create(size=size, display_name=name, |
| 1283 | snapshot_id=snapshot_id, |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 1284 | imageRef=imageRef, |
| 1285 | volume_type=volume_type) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1286 | if wait_on_delete: |
| 1287 | self.addCleanup(self.delete_timeout, |
| 1288 | self.volume_client.volumes, |
| 1289 | volume.id) |
| 1290 | self.addCleanup_with_wait(self.volume_client.volumes, volume.id, |
| 1291 | exc_type=cinder_exceptions.NotFound) |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 1292 | self.assertEqual(name, volume.display_name) |
| 1293 | self.status_timeout(client.volumes, volume.id, 'available') |
| 1294 | LOG.debug("Created volume: %s", volume) |
| 1295 | return volume |
| 1296 | |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 1297 | def create_server_snapshot(self, server, compute_client=None, |
| 1298 | image_client=None, name=None): |
| 1299 | if compute_client is None: |
| 1300 | compute_client = self.compute_client |
| 1301 | if image_client is None: |
| 1302 | image_client = self.image_client |
| 1303 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1304 | name = data_utils.rand_name('scenario-snapshot-') |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 1305 | LOG.debug("Creating a snapshot image for server: %s", server.name) |
| 1306 | image_id = compute_client.servers.create_image(server, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1307 | self.addCleanup_with_wait(self.image_client.images, image_id, |
| 1308 | exc_type=glanceclient.exc.HTTPNotFound) |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 1309 | self.status_timeout(image_client.images, image_id, 'active') |
| 1310 | snapshot_image = image_client.images.get(image_id) |
Chang Bo Guo | fc77e93 | 2013-09-16 17:38:26 -0700 | [diff] [blame] | 1311 | self.assertEqual(name, snapshot_image.name) |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 1312 | LOG.debug("Created snapshot image %s for server %s", |
| 1313 | snapshot_image.name, server.name) |
| 1314 | return snapshot_image |
| 1315 | |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 1316 | def create_keypair(self, client=None, name=None): |
| 1317 | if client is None: |
| 1318 | client = self.compute_client |
| 1319 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1320 | name = data_utils.rand_name('scenario-keypair-') |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 1321 | keypair = client.keypairs.create(name) |
| 1322 | self.assertEqual(keypair.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1323 | self.addCleanup(self.delete_wrapper, keypair) |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 1324 | return keypair |
| 1325 | |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 1326 | def get_remote_client(self, server_or_ip, username=None, private_key=None): |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 1327 | if isinstance(server_or_ip, six.string_types): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 1328 | ip = server_or_ip |
| 1329 | else: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1330 | network_name_for_ssh = CONF.compute.network_for_ssh |
Adam Gandelman | 0d9508e | 2014-08-22 10:58:09 -0700 | [diff] [blame] | 1331 | ip = server_or_ip.networks[network_name_for_ssh][0] |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 1332 | if username is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1333 | username = CONF.scenario.ssh_user |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 1334 | if private_key is None: |
| 1335 | private_key = self.keypair.private_key |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1336 | linux_client = remote_client.RemoteClient(ip, username, |
| 1337 | pkey=private_key) |
| 1338 | try: |
| 1339 | linux_client.validate_authentication() |
| 1340 | except exceptions.SSHTimeout: |
| 1341 | LOG.exception('ssh connection to %s failed' % ip) |
| 1342 | debug.log_net_debug() |
| 1343 | raise |
| 1344 | |
| 1345 | return linux_client |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 1346 | |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 1347 | def _log_console_output(self, servers=None): |
Adam Gandelman | c6eefb4 | 2014-07-15 16:44:08 -0700 | [diff] [blame] | 1348 | if not CONF.compute_feature_enabled.console_output: |
| 1349 | LOG.debug('Console output not supported, cannot log') |
| 1350 | return |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 1351 | if not servers: |
| 1352 | servers = self.compute_client.servers.list() |
| 1353 | for server in servers: |
| 1354 | LOG.debug('Console output for %s', server.id) |
| 1355 | LOG.debug(server.get_console_output()) |
| 1356 | |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 1357 | def wait_for_volume_status(self, status): |
| 1358 | volume_id = self.volume.id |
| 1359 | self.status_timeout( |
| 1360 | self.volume_client.volumes, volume_id, status) |
| 1361 | |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 1362 | def _image_create(self, name, fmt, path, properties=None): |
| 1363 | if properties is None: |
| 1364 | properties = {} |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 1365 | name = data_utils.rand_name('%s-' % name) |
| 1366 | image_file = open(path, 'rb') |
| 1367 | self.addCleanup(image_file.close) |
| 1368 | params = { |
| 1369 | 'name': name, |
| 1370 | 'container_format': fmt, |
| 1371 | 'disk_format': fmt, |
Aaron Rosen | c772062 | 2014-05-20 10:38:10 -0700 | [diff] [blame] | 1372 | 'is_public': 'False', |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 1373 | } |
| 1374 | params.update(properties) |
| 1375 | image = self.image_client.images.create(**params) |
| 1376 | self.addCleanup(self.image_client.images.delete, image) |
| 1377 | self.assertEqual("queued", image.status) |
| 1378 | image.update(data=image_file) |
| 1379 | return image.id |
| 1380 | |
| 1381 | def glance_image_create(self): |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 1382 | img_path = CONF.scenario.img_dir + "/" + CONF.scenario.img_file |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 1383 | aki_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.aki_img_file |
| 1384 | ari_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ari_img_file |
| 1385 | ami_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ami_img_file |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 1386 | img_container_format = CONF.scenario.img_container_format |
| 1387 | img_disk_format = CONF.scenario.img_disk_format |
| 1388 | LOG.debug("paths: img: %s, container_fomat: %s, disk_format: %s, " |
| 1389 | "ami: %s, ari: %s, aki: %s" % |
| 1390 | (img_path, img_container_format, img_disk_format, |
| 1391 | ami_img_path, ari_img_path, aki_img_path)) |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 1392 | try: |
| 1393 | self.image = self._image_create('scenario-img', |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 1394 | img_container_format, |
| 1395 | img_path, |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 1396 | properties={'disk_format': |
Alessandro Pilotti | b7c1daa | 2014-08-16 14:24:13 +0300 | [diff] [blame] | 1397 | img_disk_format}) |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 1398 | except IOError: |
Masayuki Igawa | 188fc00 | 2014-02-23 06:42:44 +0900 | [diff] [blame] | 1399 | LOG.debug("A qcow2 image was not found. Try to get a uec image.") |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 1400 | kernel = self._image_create('scenario-aki', 'aki', aki_img_path) |
| 1401 | ramdisk = self._image_create('scenario-ari', 'ari', ari_img_path) |
| 1402 | properties = { |
| 1403 | 'properties': {'kernel_id': kernel, 'ramdisk_id': ramdisk} |
| 1404 | } |
| 1405 | self.image = self._image_create('scenario-ami', 'ami', |
| 1406 | path=ami_img_path, |
| 1407 | properties=properties) |
| 1408 | LOG.debug("image:%s" % self.image) |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 1409 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1410 | |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 1411 | # power/provision states as of icehouse |
| 1412 | class BaremetalPowerStates(object): |
| 1413 | """Possible power states of an Ironic node.""" |
| 1414 | POWER_ON = 'power on' |
| 1415 | POWER_OFF = 'power off' |
| 1416 | REBOOT = 'rebooting' |
| 1417 | SUSPEND = 'suspended' |
| 1418 | |
| 1419 | |
| 1420 | class BaremetalProvisionStates(object): |
| 1421 | """Possible provision states of an Ironic node.""" |
| 1422 | NOSTATE = None |
| 1423 | INIT = 'initializing' |
| 1424 | ACTIVE = 'active' |
| 1425 | BUILDING = 'building' |
| 1426 | DEPLOYWAIT = 'wait call-back' |
| 1427 | DEPLOYING = 'deploying' |
| 1428 | DEPLOYFAIL = 'deploy failed' |
| 1429 | DEPLOYDONE = 'deploy complete' |
| 1430 | DELETING = 'deleting' |
| 1431 | DELETED = 'deleted' |
| 1432 | ERROR = 'error' |
| 1433 | |
| 1434 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 1435 | class BaremetalScenarioTest(OfficialClientTest): |
| 1436 | @classmethod |
| 1437 | def setUpClass(cls): |
| 1438 | super(BaremetalScenarioTest, cls).setUpClass() |
| 1439 | |
| 1440 | if (not CONF.service_available.ironic or |
| 1441 | not CONF.baremetal.driver_enabled): |
| 1442 | msg = 'Ironic not available or Ironic compute driver not enabled' |
| 1443 | raise cls.skipException(msg) |
| 1444 | |
| 1445 | # use an admin client manager for baremetal client |
Adam Gandelman | acc13e6 | 2014-05-08 11:12:47 -0700 | [diff] [blame] | 1446 | admin_creds = cls.admin_credentials() |
| 1447 | manager = clients.OfficialClientManager(credentials=admin_creds) |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 1448 | cls.baremetal_client = manager.baremetal_client |
| 1449 | |
| 1450 | # allow any issues obtaining the node list to raise early |
| 1451 | cls.baremetal_client.node.list() |
| 1452 | |
| 1453 | def _node_state_timeout(self, node_id, state_attr, |
| 1454 | target_states, timeout=10, interval=1): |
| 1455 | if not isinstance(target_states, list): |
| 1456 | target_states = [target_states] |
| 1457 | |
| 1458 | def check_state(): |
| 1459 | node = self.get_node(node_id=node_id) |
| 1460 | if getattr(node, state_attr) in target_states: |
| 1461 | return True |
| 1462 | return False |
| 1463 | |
| 1464 | if not tempest.test.call_until_true( |
| 1465 | check_state, timeout, interval): |
| 1466 | msg = ("Timed out waiting for node %s to reach %s state(s) %s" % |
| 1467 | (node_id, state_attr, target_states)) |
| 1468 | raise exceptions.TimeoutException(msg) |
| 1469 | |
| 1470 | def wait_provisioning_state(self, node_id, state, timeout): |
| 1471 | self._node_state_timeout( |
| 1472 | node_id=node_id, state_attr='provision_state', |
| 1473 | target_states=state, timeout=timeout) |
| 1474 | |
| 1475 | def wait_power_state(self, node_id, state): |
| 1476 | self._node_state_timeout( |
| 1477 | node_id=node_id, state_attr='power_state', |
| 1478 | target_states=state, timeout=CONF.baremetal.power_timeout) |
| 1479 | |
| 1480 | def wait_node(self, instance_id): |
| 1481 | """Waits for a node to be associated with instance_id.""" |
Zhi Kun Liu | 4a8d1ea | 2014-04-15 22:08:21 -0500 | [diff] [blame] | 1482 | from ironicclient import exc as ironic_exceptions |
| 1483 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 1484 | def _get_node(): |
| 1485 | node = None |
| 1486 | try: |
| 1487 | node = self.get_node(instance_id=instance_id) |
| 1488 | except ironic_exceptions.HTTPNotFound: |
| 1489 | pass |
| 1490 | return node is not None |
| 1491 | |
| 1492 | if not tempest.test.call_until_true( |
| 1493 | _get_node, CONF.baremetal.association_timeout, 1): |
| 1494 | msg = ('Timed out waiting to get Ironic node by instance id %s' |
| 1495 | % instance_id) |
| 1496 | raise exceptions.TimeoutException(msg) |
| 1497 | |
| 1498 | def get_node(self, node_id=None, instance_id=None): |
| 1499 | if node_id: |
| 1500 | return self.baremetal_client.node.get(node_id) |
| 1501 | elif instance_id: |
| 1502 | return self.baremetal_client.node.get_by_instance_uuid(instance_id) |
| 1503 | |
| 1504 | def get_ports(self, node_id): |
| 1505 | ports = [] |
| 1506 | for port in self.baremetal_client.node.list_ports(node_id): |
| 1507 | ports.append(self.baremetal_client.port.get(port.uuid)) |
| 1508 | return ports |
| 1509 | |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 1510 | def add_keypair(self): |
| 1511 | self.keypair = self.create_keypair() |
| 1512 | |
| 1513 | def verify_connectivity(self, ip=None): |
| 1514 | if ip: |
| 1515 | dest = self.get_remote_client(ip) |
| 1516 | else: |
| 1517 | dest = self.get_remote_client(self.instance) |
| 1518 | dest.validate_authentication() |
| 1519 | |
| 1520 | def boot_instance(self): |
| 1521 | create_kwargs = { |
| 1522 | 'key_name': self.keypair.id |
| 1523 | } |
| 1524 | self.instance = self.create_server( |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1525 | wait_on_boot=False, create_kwargs=create_kwargs) |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 1526 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1527 | self.addCleanup_with_wait(self.compute_client.servers, |
| 1528 | self.instance.id, |
| 1529 | cleanup_callable=self.delete_wrapper, |
| 1530 | cleanup_args=[self.instance]) |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 1531 | |
| 1532 | self.wait_node(self.instance.id) |
| 1533 | self.node = self.get_node(instance_id=self.instance.id) |
| 1534 | |
| 1535 | self.wait_power_state(self.node.uuid, BaremetalPowerStates.POWER_ON) |
| 1536 | |
| 1537 | self.wait_provisioning_state( |
| 1538 | self.node.uuid, |
| 1539 | [BaremetalProvisionStates.DEPLOYWAIT, |
| 1540 | BaremetalProvisionStates.ACTIVE], |
| 1541 | timeout=15) |
| 1542 | |
| 1543 | self.wait_provisioning_state(self.node.uuid, |
| 1544 | BaremetalProvisionStates.ACTIVE, |
| 1545 | timeout=CONF.baremetal.active_timeout) |
| 1546 | |
| 1547 | self.status_timeout( |
| 1548 | self.compute_client.servers, self.instance.id, 'ACTIVE') |
| 1549 | |
| 1550 | self.node = self.get_node(instance_id=self.instance.id) |
| 1551 | self.instance = self.compute_client.servers.get(self.instance.id) |
| 1552 | |
| 1553 | def terminate_instance(self): |
| 1554 | self.instance.delete() |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 1555 | self.wait_power_state(self.node.uuid, BaremetalPowerStates.POWER_OFF) |
| 1556 | self.wait_provisioning_state( |
| 1557 | self.node.uuid, |
| 1558 | BaremetalProvisionStates.NOSTATE, |
| 1559 | timeout=CONF.baremetal.unprovision_timeout) |
| 1560 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 1561 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 1562 | class EncryptionScenarioTest(OfficialClientTest): |
| 1563 | """ |
| 1564 | Base class for encryption scenario tests |
| 1565 | """ |
| 1566 | |
| 1567 | @classmethod |
| 1568 | def setUpClass(cls): |
| 1569 | super(EncryptionScenarioTest, cls).setUpClass() |
| 1570 | |
| 1571 | # use admin credentials to create encrypted volume types |
| 1572 | admin_creds = cls.admin_credentials() |
| 1573 | manager = clients.OfficialClientManager(credentials=admin_creds) |
| 1574 | cls.admin_volume_client = manager.volume_client |
| 1575 | |
| 1576 | def _wait_for_volume_status(self, status): |
| 1577 | self.status_timeout( |
| 1578 | self.volume_client.volumes, self.volume.id, status) |
| 1579 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 1580 | def nova_boot(self): |
| 1581 | self.keypair = self.create_keypair() |
| 1582 | create_kwargs = {'key_name': self.keypair.name} |
| 1583 | self.server = self.create_server(self.compute_client, |
| 1584 | image=self.image, |
| 1585 | create_kwargs=create_kwargs) |
| 1586 | |
| 1587 | def create_volume_type(self, client=None, name=None): |
| 1588 | if not client: |
| 1589 | client = self.admin_volume_client |
| 1590 | if not name: |
| 1591 | name = 'generic' |
| 1592 | randomized_name = data_utils.rand_name('scenario-type-' + name + '-') |
| 1593 | LOG.debug("Creating a volume type: %s", randomized_name) |
| 1594 | volume_type = client.volume_types.create(randomized_name) |
| 1595 | self.addCleanup(client.volume_types.delete, volume_type.id) |
| 1596 | return volume_type |
| 1597 | |
| 1598 | def create_encryption_type(self, client=None, type_id=None, provider=None, |
| 1599 | key_size=None, cipher=None, |
| 1600 | control_location=None): |
| 1601 | if not client: |
| 1602 | client = self.admin_volume_client |
| 1603 | if not type_id: |
| 1604 | volume_type = self.create_volume_type() |
| 1605 | type_id = volume_type.id |
| 1606 | LOG.debug("Creating an encryption type for volume type: %s", type_id) |
| 1607 | client.volume_encryption_types.create(type_id, |
| 1608 | {'provider': provider, |
| 1609 | 'key_size': key_size, |
| 1610 | 'cipher': cipher, |
| 1611 | 'control_location': |
| 1612 | control_location}) |
| 1613 | |
| 1614 | def nova_volume_attach(self): |
| 1615 | attach_volume_client = self.compute_client.volumes.create_server_volume |
| 1616 | volume = attach_volume_client(self.server.id, |
| 1617 | self.volume.id, |
| 1618 | '/dev/vdb') |
| 1619 | self.assertEqual(self.volume.id, volume.id) |
| 1620 | self._wait_for_volume_status('in-use') |
| 1621 | |
| 1622 | def nova_volume_detach(self): |
| 1623 | detach_volume_client = self.compute_client.volumes.delete_server_volume |
| 1624 | detach_volume_client(self.server.id, self.volume.id) |
| 1625 | self._wait_for_volume_status('available') |
| 1626 | |
| 1627 | volume = self.volume_client.volumes.get(self.volume.id) |
| 1628 | self.assertEqual('available', volume.status) |
| 1629 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 1630 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1631 | class NetworkScenarioTest(OfficialClientTest): |
| 1632 | """ |
| 1633 | Base class for network scenario tests |
| 1634 | """ |
| 1635 | |
| 1636 | @classmethod |
| 1637 | def check_preconditions(cls): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1638 | if (CONF.service_available.neutron): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1639 | cls.enabled = True |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 1640 | # verify that neutron_available is telling the truth |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1641 | try: |
| 1642 | cls.network_client.list_networks() |
| 1643 | except exc.EndpointNotFound: |
| 1644 | cls.enabled = False |
| 1645 | raise |
| 1646 | else: |
| 1647 | cls.enabled = False |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 1648 | msg = 'Neutron not available' |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1649 | raise cls.skipException(msg) |
| 1650 | |
| 1651 | @classmethod |
| 1652 | def setUpClass(cls): |
| 1653 | super(NetworkScenarioTest, cls).setUpClass() |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 1654 | cls.tenant_id = cls.manager.identity_client.tenant_id |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1655 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1656 | def _create_network(self, tenant_id, namestart='network-smoke-'): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1657 | name = data_utils.rand_name(namestart) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1658 | body = dict( |
| 1659 | network=dict( |
| 1660 | name=name, |
| 1661 | tenant_id=tenant_id, |
| 1662 | ), |
| 1663 | ) |
| 1664 | result = self.network_client.create_network(body=body) |
| 1665 | network = net_common.DeletableNetwork(client=self.network_client, |
| 1666 | **result['network']) |
| 1667 | self.assertEqual(network.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1668 | self.addCleanup(self.delete_wrapper, network) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1669 | return network |
| 1670 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1671 | def _list_networks(self, **kwargs): |
| 1672 | nets = self.network_client.list_networks(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1673 | return nets['networks'] |
| 1674 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1675 | def _list_subnets(self, **kwargs): |
| 1676 | subnets = self.network_client.list_subnets(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1677 | return subnets['subnets'] |
| 1678 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1679 | def _list_routers(self, **kwargs): |
| 1680 | routers = self.network_client.list_routers(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1681 | return routers['routers'] |
| 1682 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1683 | def _list_ports(self, **kwargs): |
| 1684 | ports = self.network_client.list_ports(**kwargs) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 1685 | return ports['ports'] |
| 1686 | |
| 1687 | def _get_tenant_own_network_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1688 | nets = self._list_networks(tenant_id=tenant_id) |
| 1689 | return len(nets) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 1690 | |
| 1691 | def _get_tenant_own_subnet_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1692 | subnets = self._list_subnets(tenant_id=tenant_id) |
| 1693 | return len(subnets) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 1694 | |
| 1695 | def _get_tenant_own_port_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1696 | ports = self._list_ports(tenant_id=tenant_id) |
| 1697 | return len(ports) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 1698 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 1699 | def _create_subnet(self, network, namestart='subnet-smoke-', **kwargs): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1700 | """ |
| 1701 | Create a subnet for the given network within the cidr block |
| 1702 | configured for tenant networks. |
| 1703 | """ |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 1704 | |
| 1705 | def cidr_in_use(cidr, tenant_id): |
| 1706 | """ |
| 1707 | :return True if subnet with cidr already exist in tenant |
| 1708 | False else |
| 1709 | """ |
| 1710 | cidr_in_use = self._list_subnets(tenant_id=tenant_id, cidr=cidr) |
| 1711 | return len(cidr_in_use) != 0 |
| 1712 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1713 | tenant_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1714 | result = None |
| 1715 | # Repeatedly attempt subnet creation with sequential cidr |
| 1716 | # blocks until an unallocated block is found. |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1717 | for subnet_cidr in tenant_cidr.subnet( |
| 1718 | CONF.network.tenant_network_mask_bits): |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 1719 | str_cidr = str(subnet_cidr) |
| 1720 | if cidr_in_use(str_cidr, tenant_id=network.tenant_id): |
| 1721 | continue |
| 1722 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1723 | body = dict( |
| 1724 | subnet=dict( |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 1725 | name=data_utils.rand_name(namestart), |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1726 | ip_version=4, |
| 1727 | network_id=network.id, |
| 1728 | tenant_id=network.tenant_id, |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 1729 | cidr=str_cidr, |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1730 | ), |
| 1731 | ) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 1732 | body['subnet'].update(kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1733 | try: |
| 1734 | result = self.network_client.create_subnet(body=body) |
| 1735 | break |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 1736 | except exc.NeutronClientException as e: |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1737 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 1738 | if not is_overlapping_cidr: |
| 1739 | raise |
| 1740 | self.assertIsNotNone(result, 'Unable to allocate tenant network') |
| 1741 | subnet = net_common.DeletableSubnet(client=self.network_client, |
| 1742 | **result['subnet']) |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 1743 | self.assertEqual(subnet.cidr, str_cidr) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1744 | self.addCleanup(self.delete_wrapper, subnet) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1745 | return subnet |
| 1746 | |
| 1747 | def _create_port(self, network, namestart='port-quotatest-'): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1748 | name = data_utils.rand_name(namestart) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1749 | body = dict( |
| 1750 | port=dict(name=name, |
| 1751 | network_id=network.id, |
| 1752 | tenant_id=network.tenant_id)) |
| 1753 | result = self.network_client.create_port(body=body) |
| 1754 | self.assertIsNotNone(result, 'Unable to allocate port') |
| 1755 | port = net_common.DeletablePort(client=self.network_client, |
| 1756 | **result['port']) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1757 | self.addCleanup(self.delete_wrapper, port) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1758 | return port |
| 1759 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1760 | def _get_server_port_id(self, server, ip_addr=None): |
| 1761 | ports = self._list_ports(device_id=server.id, fixed_ip=ip_addr) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1762 | self.assertEqual(len(ports), 1, |
| 1763 | "Unable to determine which port to target.") |
Yair Fried | 05db252 | 2013-11-18 11:02:10 +0200 | [diff] [blame] | 1764 | return ports[0]['id'] |
| 1765 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 1766 | def _create_floating_ip(self, thing, external_network_id, port_id=None): |
| 1767 | if not port_id: |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 1768 | port_id = self._get_server_port_id(thing) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1769 | body = dict( |
| 1770 | floatingip=dict( |
| 1771 | floating_network_id=external_network_id, |
| 1772 | port_id=port_id, |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 1773 | tenant_id=thing.tenant_id, |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1774 | ) |
| 1775 | ) |
| 1776 | result = self.network_client.create_floatingip(body=body) |
| 1777 | floating_ip = net_common.DeletableFloatingIp( |
| 1778 | client=self.network_client, |
| 1779 | **result['floatingip']) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1780 | self.addCleanup(self.delete_wrapper, floating_ip) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1781 | return floating_ip |
| 1782 | |
Yair Fried | 05db252 | 2013-11-18 11:02:10 +0200 | [diff] [blame] | 1783 | def _associate_floating_ip(self, floating_ip, server): |
| 1784 | port_id = self._get_server_port_id(server) |
| 1785 | floating_ip.update(port_id=port_id) |
| 1786 | self.assertEqual(port_id, floating_ip.port_id) |
| 1787 | return floating_ip |
| 1788 | |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 1789 | def _disassociate_floating_ip(self, floating_ip): |
| 1790 | """ |
| 1791 | :param floating_ip: type DeletableFloatingIp |
| 1792 | """ |
| 1793 | floating_ip.update(port_id=None) |
llg8212 | e4cd392 | 2014-02-15 12:14:21 +0800 | [diff] [blame] | 1794 | self.assertIsNone(floating_ip.port_id) |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 1795 | return floating_ip |
| 1796 | |
| 1797 | def _ping_ip_address(self, ip_address, should_succeed=True): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1798 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 1799 | |
| 1800 | def ping(): |
| 1801 | proc = subprocess.Popen(cmd, |
| 1802 | stdout=subprocess.PIPE, |
| 1803 | stderr=subprocess.PIPE) |
| 1804 | proc.wait() |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 1805 | return (proc.returncode == 0) == should_succeed |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 1806 | |
Nachi Ueno | 6d580be | 2013-07-24 10:58:11 -0700 | [diff] [blame] | 1807 | return tempest.test.call_until_true( |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1808 | ping, CONF.compute.ping_timeout, 1) |
Maru Newby | af292e8 | 2013-05-20 21:32:28 +0000 | [diff] [blame] | 1809 | |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 1810 | def _create_pool(self, lb_method, protocol, subnet_id): |
| 1811 | """Wrapper utility that returns a test pool.""" |
| 1812 | name = data_utils.rand_name('pool-') |
| 1813 | body = { |
| 1814 | "pool": { |
| 1815 | "protocol": protocol, |
| 1816 | "name": name, |
| 1817 | "subnet_id": subnet_id, |
| 1818 | "lb_method": lb_method |
| 1819 | } |
| 1820 | } |
| 1821 | resp = self.network_client.create_pool(body=body) |
| 1822 | pool = net_common.DeletablePool(client=self.network_client, |
| 1823 | **resp['pool']) |
| 1824 | self.assertEqual(pool['name'], name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1825 | self.addCleanup(self.delete_wrapper, pool) |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 1826 | return pool |
| 1827 | |
| 1828 | def _create_member(self, address, protocol_port, pool_id): |
| 1829 | """Wrapper utility that returns a test member.""" |
| 1830 | body = { |
| 1831 | "member": { |
| 1832 | "protocol_port": protocol_port, |
| 1833 | "pool_id": pool_id, |
| 1834 | "address": address |
| 1835 | } |
| 1836 | } |
| 1837 | resp = self.network_client.create_member(body) |
| 1838 | member = net_common.DeletableMember(client=self.network_client, |
| 1839 | **resp['member']) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1840 | self.addCleanup(self.delete_wrapper, member) |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 1841 | return member |
| 1842 | |
| 1843 | def _create_vip(self, protocol, protocol_port, subnet_id, pool_id): |
| 1844 | """Wrapper utility that returns a test vip.""" |
| 1845 | name = data_utils.rand_name('vip-') |
| 1846 | body = { |
| 1847 | "vip": { |
| 1848 | "protocol": protocol, |
| 1849 | "name": name, |
| 1850 | "subnet_id": subnet_id, |
| 1851 | "pool_id": pool_id, |
| 1852 | "protocol_port": protocol_port |
| 1853 | } |
| 1854 | } |
| 1855 | resp = self.network_client.create_vip(body) |
| 1856 | vip = net_common.DeletableVip(client=self.network_client, |
| 1857 | **resp['vip']) |
| 1858 | self.assertEqual(vip['name'], name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1859 | self.addCleanup(self.delete_wrapper, vip) |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 1860 | return vip |
| 1861 | |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 1862 | def _check_vm_connectivity(self, ip_address, |
| 1863 | username=None, |
| 1864 | private_key=None, |
| 1865 | should_connect=True): |
| 1866 | """ |
| 1867 | :param ip_address: server to test against |
| 1868 | :param username: server's ssh username |
| 1869 | :param private_key: server's ssh private key to be used |
| 1870 | :param should_connect: True/False indicates positive/negative test |
| 1871 | positive - attempt ping and ssh |
| 1872 | negative - attempt ping and fail if succeed |
| 1873 | |
| 1874 | :raises: AssertError if the result of the connectivity check does |
| 1875 | not match the value of the should_connect param |
| 1876 | """ |
| 1877 | if should_connect: |
| 1878 | msg = "Timed out waiting for %s to become reachable" % ip_address |
| 1879 | else: |
| 1880 | msg = "ip address %s is reachable" % ip_address |
| 1881 | self.assertTrue(self._ping_ip_address(ip_address, |
| 1882 | should_succeed=should_connect), |
| 1883 | msg=msg) |
| 1884 | if should_connect: |
| 1885 | # no need to check ssh for negative connectivity |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1886 | self.get_remote_client(ip_address, username, private_key) |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1887 | |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 1888 | def _check_public_network_connectivity(self, ip_address, username, |
| 1889 | private_key, should_connect=True, |
| 1890 | msg=None, servers=None): |
| 1891 | # The target login is assumed to have been configured for |
| 1892 | # key-based authentication by cloud-init. |
| 1893 | LOG.debug('checking network connections to IP %s with user: %s' % |
| 1894 | (ip_address, username)) |
| 1895 | try: |
| 1896 | self._check_vm_connectivity(ip_address, |
| 1897 | username, |
| 1898 | private_key, |
| 1899 | should_connect=should_connect) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1900 | except Exception as e: |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 1901 | ex_msg = 'Public network connectivity check failed' |
| 1902 | if msg: |
| 1903 | ex_msg += ": " + msg |
| 1904 | LOG.exception(ex_msg) |
| 1905 | self._log_console_output(servers) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1906 | # network debug is called as part of ssh init |
| 1907 | if not isinstance(e, exceptions.SSHTimeout): |
| 1908 | debug.log_net_debug() |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 1909 | raise |
| 1910 | |
Matt Riedemann | 2d005be | 2014-05-27 10:52:35 -0700 | [diff] [blame] | 1911 | def _check_tenant_network_connectivity(self, server, |
| 1912 | username, |
| 1913 | private_key, |
| 1914 | should_connect=True, |
| 1915 | servers_for_debug=None): |
| 1916 | if not CONF.network.tenant_networks_reachable: |
| 1917 | msg = 'Tenant networks not configured to be reachable.' |
| 1918 | LOG.info(msg) |
| 1919 | return |
| 1920 | # The target login is assumed to have been configured for |
| 1921 | # key-based authentication by cloud-init. |
| 1922 | try: |
| 1923 | for net_name, ip_addresses in server.networks.iteritems(): |
| 1924 | for ip_address in ip_addresses: |
| 1925 | self._check_vm_connectivity(ip_address, |
| 1926 | username, |
| 1927 | private_key, |
| 1928 | should_connect=should_connect) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1929 | except Exception as e: |
Matt Riedemann | 2d005be | 2014-05-27 10:52:35 -0700 | [diff] [blame] | 1930 | LOG.exception('Tenant network connectivity check failed') |
| 1931 | self._log_console_output(servers_for_debug) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1932 | # network debug is called as part of ssh init |
| 1933 | if not isinstance(e, exceptions.SSHTimeout): |
| 1934 | debug.log_net_debug() |
Matt Riedemann | 2d005be | 2014-05-27 10:52:35 -0700 | [diff] [blame] | 1935 | raise |
| 1936 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 1937 | def _check_remote_connectivity(self, source, dest, should_succeed=True): |
| 1938 | """ |
| 1939 | check ping server via source ssh connection |
| 1940 | |
| 1941 | :param source: RemoteClient: an ssh connection from which to ping |
| 1942 | :param dest: and IP to ping against |
| 1943 | :param should_succeed: boolean should ping succeed or not |
| 1944 | :returns: boolean -- should_succeed == ping |
| 1945 | :returns: ping is false if ping failed |
| 1946 | """ |
| 1947 | def ping_remote(): |
| 1948 | try: |
| 1949 | source.ping_host(dest) |
| 1950 | except exceptions.SSHExecCommandFailed: |
Matthew Treinish | c4131d8 | 2014-05-27 11:59:28 -0400 | [diff] [blame] | 1951 | LOG.warn('Failed to ping IP: %s via a ssh connection from: %s.' |
| 1952 | % (dest, source.ssh_client.host)) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 1953 | return not should_succeed |
| 1954 | return should_succeed |
| 1955 | |
| 1956 | return tempest.test.call_until_true(ping_remote, |
| 1957 | CONF.compute.ping_timeout, |
| 1958 | 1) |
| 1959 | |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1960 | def _create_security_group_neutron(self, tenant_id, client=None, |
| 1961 | namestart='secgroup-smoke-'): |
| 1962 | if client is None: |
| 1963 | client = self.network_client |
| 1964 | secgroup = self._create_empty_security_group(namestart=namestart, |
| 1965 | client=client, |
| 1966 | tenant_id=tenant_id) |
| 1967 | |
| 1968 | # Add rules to the security group |
| 1969 | rules = self._create_loginable_secgroup_rule_neutron(secgroup=secgroup) |
| 1970 | for rule in rules: |
| 1971 | self.assertEqual(tenant_id, rule.tenant_id) |
| 1972 | self.assertEqual(secgroup.id, rule.security_group_id) |
| 1973 | return secgroup |
| 1974 | |
| 1975 | def _create_empty_security_group(self, tenant_id, client=None, |
| 1976 | namestart='secgroup-smoke-'): |
| 1977 | """Create a security group without rules. |
| 1978 | |
| 1979 | Default rules will be created: |
| 1980 | - IPv4 egress to any |
| 1981 | - IPv6 egress to any |
| 1982 | |
| 1983 | :param tenant_id: secgroup will be created in this tenant |
| 1984 | :returns: DeletableSecurityGroup -- containing the secgroup created |
| 1985 | """ |
| 1986 | if client is None: |
| 1987 | client = self.network_client |
| 1988 | sg_name = data_utils.rand_name(namestart) |
| 1989 | sg_desc = sg_name + " description" |
| 1990 | sg_dict = dict(name=sg_name, |
| 1991 | description=sg_desc) |
| 1992 | sg_dict['tenant_id'] = tenant_id |
| 1993 | body = dict(security_group=sg_dict) |
| 1994 | result = client.create_security_group(body=body) |
| 1995 | secgroup = net_common.DeletableSecurityGroup( |
| 1996 | client=client, |
| 1997 | **result['security_group'] |
| 1998 | ) |
| 1999 | self.assertEqual(secgroup.name, sg_name) |
| 2000 | self.assertEqual(tenant_id, secgroup.tenant_id) |
| 2001 | self.assertEqual(secgroup.description, sg_desc) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 2002 | self.addCleanup(self.delete_wrapper, secgroup) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 2003 | return secgroup |
| 2004 | |
| 2005 | def _default_security_group(self, tenant_id, client=None): |
| 2006 | """Get default secgroup for given tenant_id. |
| 2007 | |
| 2008 | :returns: DeletableSecurityGroup -- default secgroup for given tenant |
| 2009 | """ |
| 2010 | if client is None: |
| 2011 | client = self.network_client |
| 2012 | sgs = [ |
| 2013 | sg for sg in client.list_security_groups().values()[0] |
| 2014 | if sg['tenant_id'] == tenant_id and sg['name'] == 'default' |
| 2015 | ] |
| 2016 | msg = "No default security group for tenant %s." % (tenant_id) |
| 2017 | self.assertTrue(len(sgs) > 0, msg) |
| 2018 | if len(sgs) > 1: |
| 2019 | msg = "Found %d default security groups" % len(sgs) |
| 2020 | raise exc.NeutronClientNoUniqueMatch(msg=msg) |
| 2021 | return net_common.DeletableSecurityGroup(client=client, |
| 2022 | **sgs[0]) |
| 2023 | |
| 2024 | def _create_security_group_rule(self, client=None, secgroup=None, |
| 2025 | tenant_id=None, **kwargs): |
| 2026 | """Create a rule from a dictionary of rule parameters. |
| 2027 | |
| 2028 | Create a rule in a secgroup. if secgroup not defined will search for |
| 2029 | default secgroup in tenant_id. |
| 2030 | |
| 2031 | :param secgroup: type DeletableSecurityGroup. |
| 2032 | :param secgroup_id: search for secgroup by id |
| 2033 | default -- choose default secgroup for given tenant_id |
| 2034 | :param tenant_id: if secgroup not passed -- the tenant in which to |
| 2035 | search for default secgroup |
| 2036 | :param kwargs: a dictionary containing rule parameters: |
| 2037 | for example, to allow incoming ssh: |
| 2038 | rule = { |
| 2039 | direction: 'ingress' |
| 2040 | protocol:'tcp', |
| 2041 | port_range_min: 22, |
| 2042 | port_range_max: 22 |
| 2043 | } |
| 2044 | """ |
| 2045 | if client is None: |
| 2046 | client = self.network_client |
| 2047 | if secgroup is None: |
| 2048 | secgroup = self._default_security_group(tenant_id) |
| 2049 | |
| 2050 | ruleset = dict(security_group_id=secgroup.id, |
| 2051 | tenant_id=secgroup.tenant_id, |
| 2052 | ) |
| 2053 | ruleset.update(kwargs) |
| 2054 | |
| 2055 | body = dict(security_group_rule=dict(ruleset)) |
| 2056 | sg_rule = client.create_security_group_rule(body=body) |
| 2057 | sg_rule = net_common.DeletableSecurityGroupRule( |
| 2058 | client=client, |
| 2059 | **sg_rule['security_group_rule'] |
| 2060 | ) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 2061 | self.addCleanup(self.delete_wrapper, sg_rule) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 2062 | self.assertEqual(secgroup.tenant_id, sg_rule.tenant_id) |
| 2063 | self.assertEqual(secgroup.id, sg_rule.security_group_id) |
| 2064 | |
| 2065 | return sg_rule |
| 2066 | |
| 2067 | def _create_loginable_secgroup_rule_neutron(self, client=None, |
| 2068 | secgroup=None): |
| 2069 | """These rules are intended to permit inbound ssh and icmp |
| 2070 | traffic from all sources, so no group_id is provided. |
| 2071 | Setting a group_id would only permit traffic from ports |
| 2072 | belonging to the same security group. |
| 2073 | """ |
| 2074 | |
| 2075 | if client is None: |
| 2076 | client = self.network_client |
| 2077 | rules = [] |
| 2078 | rulesets = [ |
| 2079 | dict( |
| 2080 | # ssh |
| 2081 | protocol='tcp', |
| 2082 | port_range_min=22, |
| 2083 | port_range_max=22, |
| 2084 | ), |
| 2085 | dict( |
| 2086 | # ping |
| 2087 | protocol='icmp', |
| 2088 | ) |
| 2089 | ] |
| 2090 | for ruleset in rulesets: |
| 2091 | for r_direction in ['ingress', 'egress']: |
| 2092 | ruleset['direction'] = r_direction |
| 2093 | try: |
| 2094 | sg_rule = self._create_security_group_rule( |
| 2095 | client=client, secgroup=secgroup, **ruleset) |
| 2096 | except exc.NeutronClientException as ex: |
| 2097 | # if rule already exist - skip rule and continue |
| 2098 | if not (ex.status_code is 409 and 'Security group rule' |
| 2099 | ' already exists' in ex.message): |
| 2100 | raise ex |
| 2101 | else: |
| 2102 | self.assertEqual(r_direction, sg_rule.direction) |
| 2103 | rules.append(sg_rule) |
| 2104 | |
| 2105 | return rules |
| 2106 | |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 2107 | def _ssh_to_server(self, server, private_key): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 2108 | ssh_login = CONF.compute.image_ssh_user |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 2109 | return self.get_remote_client(server, |
| 2110 | username=ssh_login, |
| 2111 | private_key=private_key) |
| 2112 | |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 2113 | def _show_quota_network(self, tenant_id): |
| 2114 | quota = self.network_client.show_quota(tenant_id) |
| 2115 | return quota['quota']['network'] |
| 2116 | |
| 2117 | def _show_quota_subnet(self, tenant_id): |
| 2118 | quota = self.network_client.show_quota(tenant_id) |
| 2119 | return quota['quota']['subnet'] |
| 2120 | |
| 2121 | def _show_quota_port(self, tenant_id): |
| 2122 | quota = self.network_client.show_quota(tenant_id) |
| 2123 | return quota['quota']['port'] |
| 2124 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 2125 | def _get_router(self, tenant_id): |
| 2126 | """Retrieve a router for the given tenant id. |
| 2127 | |
| 2128 | If a public router has been configured, it will be returned. |
| 2129 | |
| 2130 | If a public router has not been configured, but a public |
| 2131 | network has, a tenant router will be created and returned that |
| 2132 | routes traffic to the public network. |
| 2133 | """ |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 2134 | router_id = CONF.network.public_router_id |
| 2135 | network_id = CONF.network.public_network_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 2136 | if router_id: |
| 2137 | result = self.network_client.show_router(router_id) |
| 2138 | return net_common.AttributeDict(**result['router']) |
| 2139 | elif network_id: |
| 2140 | router = self._create_router(tenant_id) |
| 2141 | router.add_gateway(network_id) |
| 2142 | return router |
| 2143 | else: |
| 2144 | raise Exception("Neither of 'public_router_id' or " |
| 2145 | "'public_network_id' has been defined.") |
| 2146 | |
| 2147 | def _create_router(self, tenant_id, namestart='router-smoke-'): |
| 2148 | name = data_utils.rand_name(namestart) |
| 2149 | body = dict( |
| 2150 | router=dict( |
| 2151 | name=name, |
| 2152 | admin_state_up=True, |
| 2153 | tenant_id=tenant_id, |
| 2154 | ), |
| 2155 | ) |
| 2156 | result = self.network_client.create_router(body=body) |
| 2157 | router = net_common.DeletableRouter(client=self.network_client, |
| 2158 | **result['router']) |
| 2159 | self.assertEqual(router.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 2160 | self.addCleanup(self.delete_wrapper, router) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 2161 | return router |
| 2162 | |
| 2163 | def _create_networks(self, tenant_id=None): |
| 2164 | """Create a network with a subnet connected to a router. |
| 2165 | |
| 2166 | :returns: network, subnet, router |
| 2167 | """ |
| 2168 | if tenant_id is None: |
| 2169 | tenant_id = self.tenant_id |
| 2170 | network = self._create_network(tenant_id) |
| 2171 | router = self._get_router(tenant_id) |
| 2172 | subnet = self._create_subnet(network) |
| 2173 | subnet.add_to_router(router.id) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 2174 | return network, subnet, router |
| 2175 | |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 2176 | |
| 2177 | class OrchestrationScenarioTest(OfficialClientTest): |
| 2178 | """ |
| 2179 | Base class for orchestration scenario tests |
| 2180 | """ |
| 2181 | |
| 2182 | @classmethod |
Matt Riedemann | 11c5b64 | 2013-08-24 08:45:38 -0700 | [diff] [blame] | 2183 | def setUpClass(cls): |
| 2184 | super(OrchestrationScenarioTest, cls).setUpClass() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 2185 | if not CONF.service_available.heat: |
Matt Riedemann | 11c5b64 | 2013-08-24 08:45:38 -0700 | [diff] [blame] | 2186 | raise cls.skipException("Heat support is required") |
| 2187 | |
| 2188 | @classmethod |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 2189 | def credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 2190 | admin_creds = auth.get_default_credentials('identity_admin') |
| 2191 | creds = auth.get_default_credentials('user') |
| 2192 | admin_creds.tenant_name = creds.tenant_name |
| 2193 | return admin_creds |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 2194 | |
| 2195 | def _load_template(self, base_file, file_name): |
| 2196 | filepath = os.path.join(os.path.dirname(os.path.realpath(base_file)), |
| 2197 | file_name) |
| 2198 | with open(filepath) as f: |
| 2199 | return f.read() |
| 2200 | |
| 2201 | @classmethod |
| 2202 | def _stack_rand_name(cls): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 2203 | return data_utils.rand_name(cls.__name__ + '-') |
Steve Baker | 80252da | 2013-09-25 13:29:10 +1200 | [diff] [blame] | 2204 | |
| 2205 | @classmethod |
| 2206 | def _get_default_network(cls): |
| 2207 | networks = cls.network_client.list_networks() |
| 2208 | for net in networks['networks']: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 2209 | if net['name'] == CONF.compute.fixed_network_name: |
Steve Baker | 80252da | 2013-09-25 13:29:10 +1200 | [diff] [blame] | 2210 | return net |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 2211 | |
| 2212 | @staticmethod |
| 2213 | def _stack_output(stack, output_key): |
| 2214 | """Return a stack output value for a given key.""" |
| 2215 | return next((o['output_value'] for o in stack.outputs |
| 2216 | if o['output_key'] == output_key), None) |
| 2217 | |
| 2218 | def _ping_ip_address(self, ip_address, should_succeed=True): |
| 2219 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 2220 | |
| 2221 | def ping(): |
| 2222 | proc = subprocess.Popen(cmd, |
| 2223 | stdout=subprocess.PIPE, |
| 2224 | stderr=subprocess.PIPE) |
| 2225 | proc.wait() |
| 2226 | return (proc.returncode == 0) == should_succeed |
| 2227 | |
| 2228 | return tempest.test.call_until_true( |
| 2229 | ping, CONF.orchestration.build_timeout, 1) |
| 2230 | |
| 2231 | def _wait_for_resource_status(self, stack_identifier, resource_name, |
| 2232 | status, failure_pattern='^.*_FAILED$'): |
| 2233 | """Waits for a Resource to reach a given status.""" |
| 2234 | fail_regexp = re.compile(failure_pattern) |
| 2235 | build_timeout = CONF.orchestration.build_timeout |
| 2236 | build_interval = CONF.orchestration.build_interval |
| 2237 | |
| 2238 | start = timeutils.utcnow() |
| 2239 | while timeutils.delta_seconds(start, |
| 2240 | timeutils.utcnow()) < build_timeout: |
| 2241 | try: |
| 2242 | res = self.client.resources.get( |
| 2243 | stack_identifier, resource_name) |
| 2244 | except heat_exceptions.HTTPNotFound: |
| 2245 | # ignore this, as the resource may not have |
| 2246 | # been created yet |
| 2247 | pass |
| 2248 | else: |
| 2249 | if res.resource_status == status: |
| 2250 | return |
| 2251 | if fail_regexp.search(res.resource_status): |
| 2252 | raise exceptions.StackResourceBuildErrorException( |
| 2253 | resource_name=res.resource_name, |
| 2254 | stack_identifier=stack_identifier, |
| 2255 | resource_status=res.resource_status, |
| 2256 | resource_status_reason=res.resource_status_reason) |
| 2257 | time.sleep(build_interval) |
| 2258 | |
| 2259 | message = ('Resource %s failed to reach %s status within ' |
| 2260 | 'the required time (%s s).' % |
| 2261 | (res.resource_name, status, build_timeout)) |
| 2262 | raise exceptions.TimeoutException(message) |
| 2263 | |
| 2264 | def _wait_for_stack_status(self, stack_identifier, status, |
| 2265 | failure_pattern='^.*_FAILED$'): |
| 2266 | """ |
| 2267 | Waits for a Stack to reach a given status. |
| 2268 | |
| 2269 | Note this compares the full $action_$status, e.g |
| 2270 | CREATE_COMPLETE, not just COMPLETE which is exposed |
| 2271 | via the status property of Stack in heatclient |
| 2272 | """ |
| 2273 | fail_regexp = re.compile(failure_pattern) |
| 2274 | build_timeout = CONF.orchestration.build_timeout |
| 2275 | build_interval = CONF.orchestration.build_interval |
| 2276 | |
| 2277 | start = timeutils.utcnow() |
| 2278 | while timeutils.delta_seconds(start, |
| 2279 | timeutils.utcnow()) < build_timeout: |
| 2280 | try: |
| 2281 | stack = self.client.stacks.get(stack_identifier) |
| 2282 | except heat_exceptions.HTTPNotFound: |
| 2283 | # ignore this, as the stackource may not have |
| 2284 | # been created yet |
| 2285 | pass |
| 2286 | else: |
| 2287 | if stack.stack_status == status: |
| 2288 | return |
| 2289 | if fail_regexp.search(stack.stack_status): |
| 2290 | raise exceptions.StackBuildErrorException( |
| 2291 | stack_identifier=stack_identifier, |
| 2292 | stack_status=stack.stack_status, |
| 2293 | stack_status_reason=stack.stack_status_reason) |
| 2294 | time.sleep(build_interval) |
| 2295 | |
| 2296 | message = ('Stack %s failed to reach %s status within ' |
| 2297 | 'the required time (%s s).' % |
| 2298 | (stack.stack_name, status, build_timeout)) |
| 2299 | raise exceptions.TimeoutException(message) |
| 2300 | |
| 2301 | def _stack_delete(self, stack_identifier): |
| 2302 | try: |
| 2303 | self.client.stacks.delete(stack_identifier) |
| 2304 | except heat_exceptions.HTTPNotFound: |
| 2305 | pass |
Chris Dent | 0d49411 | 2014-08-26 13:48:30 +0100 | [diff] [blame] | 2306 | |
| 2307 | |
| 2308 | class SwiftScenarioTest(ScenarioTest): |
| 2309 | """ |
| 2310 | Provide harness to do Swift scenario tests. |
| 2311 | |
| 2312 | Subclasses implement the tests that use the methods provided by this |
| 2313 | class. |
| 2314 | """ |
| 2315 | |
| 2316 | @classmethod |
| 2317 | def setUpClass(cls): |
| 2318 | cls.set_network_resources() |
| 2319 | super(SwiftScenarioTest, cls).setUpClass() |
| 2320 | if not CONF.service_available.swift: |
| 2321 | skip_msg = ("%s skipped as swift is not available" % |
| 2322 | cls.__name__) |
| 2323 | raise cls.skipException(skip_msg) |
| 2324 | # Clients for Swift |
| 2325 | cls.account_client = cls.manager.account_client |
| 2326 | cls.container_client = cls.manager.container_client |
| 2327 | cls.object_client = cls.manager.object_client |
| 2328 | |
| 2329 | def _get_swift_stat(self): |
| 2330 | """get swift status for our user account.""" |
| 2331 | self.account_client.list_account_containers() |
| 2332 | LOG.debug('Swift status information obtained successfully') |
| 2333 | |
| 2334 | def _create_container(self, container_name=None): |
| 2335 | name = container_name or data_utils.rand_name( |
| 2336 | 'swift-scenario-container') |
| 2337 | self.container_client.create_container(name) |
| 2338 | # look for the container to assure it is created |
| 2339 | self._list_and_check_container_objects(name) |
| 2340 | LOG.debug('Container %s created' % (name)) |
| 2341 | return name |
| 2342 | |
| 2343 | def _delete_container(self, container_name): |
| 2344 | self.container_client.delete_container(container_name) |
| 2345 | LOG.debug('Container %s deleted' % (container_name)) |
| 2346 | |
| 2347 | def _upload_object_to_container(self, container_name, obj_name=None): |
| 2348 | obj_name = obj_name or data_utils.rand_name('swift-scenario-object') |
| 2349 | obj_data = data_utils.arbitrary_string() |
| 2350 | self.object_client.create_object(container_name, obj_name, obj_data) |
| 2351 | return obj_name, obj_data |
| 2352 | |
| 2353 | def _delete_object(self, container_name, filename): |
| 2354 | self.object_client.delete_object(container_name, filename) |
| 2355 | self._list_and_check_container_objects(container_name, |
| 2356 | not_present_obj=[filename]) |
| 2357 | |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 2358 | def _list_and_check_container_objects(self, container_name, |
| 2359 | present_obj=None, |
| 2360 | not_present_obj=None): |
Chris Dent | 0d49411 | 2014-08-26 13:48:30 +0100 | [diff] [blame] | 2361 | """ |
| 2362 | List objects for a given container and assert which are present and |
| 2363 | which are not. |
| 2364 | """ |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 2365 | if present_obj is None: |
| 2366 | present_obj = [] |
| 2367 | if not_present_obj is None: |
| 2368 | not_present_obj = [] |
Chris Dent | 0d49411 | 2014-08-26 13:48:30 +0100 | [diff] [blame] | 2369 | _, object_list = self.container_client.list_container_contents( |
| 2370 | container_name) |
| 2371 | if present_obj: |
| 2372 | for obj in present_obj: |
| 2373 | self.assertIn(obj, object_list) |
| 2374 | if not_present_obj: |
| 2375 | for obj in not_present_obj: |
| 2376 | self.assertNotIn(obj, object_list) |
| 2377 | |
| 2378 | def _change_container_acl(self, container_name, acl): |
| 2379 | metadata_param = {'metadata_prefix': 'x-container-', |
| 2380 | 'metadata': {'read': acl}} |
| 2381 | self.container_client.update_container_metadata(container_name, |
| 2382 | **metadata_param) |
| 2383 | resp, _ = self.container_client.list_container_metadata(container_name) |
| 2384 | self.assertEqual(resp['x-container-read'], acl) |
| 2385 | |
| 2386 | def _download_and_verify(self, container_name, obj_name, expected_data): |
| 2387 | _, obj = self.object_client.get_object(container_name, obj_name) |
| 2388 | self.assertEqual(obj, expected_data) |