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 |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 19 | import six |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 20 | import subprocess |
| 21 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 22 | import netaddr |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 23 | from neutronclient.common import exceptions as exc |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 24 | from novaclient import exceptions as nova_exceptions |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 25 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 26 | from tempest.api.network import common as net_common |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 27 | from tempest import auth |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 28 | from tempest import clients |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 29 | from tempest.common import isolated_creds |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 30 | from tempest.common.utils import data_utils |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 31 | from tempest.common.utils.linux import remote_client |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 32 | from tempest import config |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 33 | from tempest import exceptions |
Attila Fazekas | fb7552a | 2013-08-27 13:02:26 +0200 | [diff] [blame] | 34 | from tempest.openstack.common import log |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 35 | import tempest.test |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 36 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 37 | CONF = config.CONF |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 38 | |
Attila Fazekas | fb7552a | 2013-08-27 13:02:26 +0200 | [diff] [blame] | 39 | LOG = log.getLogger(__name__) |
| 40 | |
| 41 | # NOTE(afazekas): Workaround for the stdout logging |
| 42 | LOG_nova_client = logging.getLogger('novaclient.client') |
| 43 | LOG_nova_client.addHandler(log.NullHandler()) |
| 44 | |
| 45 | LOG_cinder_client = logging.getLogger('cinderclient.client') |
| 46 | LOG_cinder_client.addHandler(log.NullHandler()) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 47 | |
| 48 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 49 | class OfficialClientTest(tempest.test.BaseTestCase): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 50 | """ |
| 51 | Official Client test base class for scenario testing. |
| 52 | |
| 53 | Official Client tests are tests that have the following characteristics: |
| 54 | |
| 55 | * Test basic operations of an API, typically in an order that |
| 56 | a regular user would perform those operations |
| 57 | * Test only the correct inputs and action paths -- no fuzz or |
| 58 | random input data is sent, only valid inputs. |
| 59 | * Use only the default client tool for calling an API |
| 60 | """ |
| 61 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 62 | @classmethod |
| 63 | def setUpClass(cls): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 64 | super(OfficialClientTest, cls).setUpClass() |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 65 | cls.isolated_creds = isolated_creds.IsolatedCreds( |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 66 | cls.__name__, tempest_client=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 67 | network_resources=cls.network_resources) |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 68 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 69 | cls.manager = clients.OfficialClientManager( |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 70 | credentials=cls.credentials()) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 71 | cls.compute_client = cls.manager.compute_client |
| 72 | cls.image_client = cls.manager.image_client |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 73 | cls.baremetal_client = cls.manager.baremetal_client |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 74 | cls.identity_client = cls.manager.identity_client |
| 75 | cls.network_client = cls.manager.network_client |
| 76 | cls.volume_client = cls.manager.volume_client |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 77 | cls.object_storage_client = cls.manager.object_storage_client |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 78 | cls.orchestration_client = cls.manager.orchestration_client |
Sergey Lukjanov | 7409e2e | 2014-03-27 12:55:50 +0400 | [diff] [blame] | 79 | cls.data_processing_client = cls.manager.data_processing_client |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 80 | cls.resource_keys = {} |
| 81 | cls.os_resources = [] |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 82 | |
| 83 | @classmethod |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 84 | def _get_credentials(cls, get_creds, ctype): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 85 | if CONF.compute.allow_tenant_isolation: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 86 | creds = get_creds() |
Yair Fried | 769bbff | 2013-12-18 16:33:17 +0200 | [diff] [blame] | 87 | else: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 88 | creds = auth.get_default_credentials(ctype) |
| 89 | return creds |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 90 | |
| 91 | @classmethod |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 92 | def credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 93 | return cls._get_credentials(cls.isolated_creds.get_primary_creds, |
| 94 | 'user') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 95 | |
| 96 | @classmethod |
| 97 | def alt_credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 98 | return cls._get_credentials(cls.isolated_creds.get_alt_creds, |
| 99 | 'alt_user') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 100 | |
| 101 | @classmethod |
| 102 | def admin_credentials(cls): |
| 103 | return cls._get_credentials(cls.isolated_creds.get_admin_creds, |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 104 | 'identity_admin') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 105 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 106 | @staticmethod |
| 107 | def cleanup_resource(resource, test_name): |
| 108 | |
| 109 | LOG.debug("Deleting %r from shared resources of %s" % |
| 110 | (resource, test_name)) |
| 111 | try: |
| 112 | # OpenStack resources are assumed to have a delete() |
| 113 | # method which destroys the resource... |
| 114 | resource.delete() |
| 115 | except Exception as e: |
| 116 | # If the resource is already missing, mission accomplished. |
| 117 | # add status code as workaround for bug 1247568 |
| 118 | if (e.__class__.__name__ == 'NotFound' or |
| 119 | (hasattr(e, 'status_code') and e.status_code == 404)): |
| 120 | return |
| 121 | raise |
| 122 | |
| 123 | def is_deletion_complete(): |
| 124 | # Deletion testing is only required for objects whose |
| 125 | # existence cannot be checked via retrieval. |
| 126 | if isinstance(resource, dict): |
| 127 | return True |
| 128 | try: |
| 129 | resource.get() |
| 130 | except Exception as e: |
| 131 | # Clients are expected to return an exception |
| 132 | # called 'NotFound' if retrieval fails. |
| 133 | if e.__class__.__name__ == 'NotFound': |
| 134 | return True |
| 135 | raise |
| 136 | return False |
| 137 | |
| 138 | # Block until resource deletion has completed or timed-out |
| 139 | tempest.test.call_until_true(is_deletion_complete, 10, 1) |
| 140 | |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 141 | @classmethod |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 142 | def tearDownClass(cls): |
| 143 | # NOTE(jaypipes): Because scenario tests are typically run in a |
| 144 | # specific order, and because test methods in scenario tests |
| 145 | # generally create resources in a particular order, we destroy |
| 146 | # resources in the reverse order in which resources are added to |
| 147 | # the scenario test class object |
| 148 | while cls.os_resources: |
| 149 | thing = cls.os_resources.pop() |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 150 | cls.cleanup_resource(thing, cls.__name__) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 151 | cls.isolated_creds.clear_isolated_creds() |
| 152 | super(OfficialClientTest, cls).tearDownClass() |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 153 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 154 | @classmethod |
| 155 | def set_resource(cls, key, thing): |
| 156 | LOG.debug("Adding %r to shared resources of %s" % |
| 157 | (thing, cls.__name__)) |
| 158 | cls.resource_keys[key] = thing |
| 159 | cls.os_resources.append(thing) |
| 160 | |
| 161 | @classmethod |
| 162 | def get_resource(cls, key): |
| 163 | return cls.resource_keys[key] |
| 164 | |
| 165 | @classmethod |
| 166 | def remove_resource(cls, key): |
| 167 | thing = cls.resource_keys[key] |
| 168 | cls.os_resources.remove(thing) |
| 169 | del cls.resource_keys[key] |
| 170 | |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 171 | def status_timeout(self, things, thing_id, expected_status, |
| 172 | error_status='ERROR', |
| 173 | not_found_exception=nova_exceptions.NotFound): |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 174 | """ |
| 175 | Given a thing and an expected status, do a loop, sleeping |
| 176 | for a configurable amount of time, checking for the |
| 177 | expected status to show. At any time, if the returned |
| 178 | status of the thing is ERROR, fail out. |
| 179 | """ |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 180 | self._status_timeout(things, thing_id, |
| 181 | expected_status=expected_status, |
| 182 | error_status=error_status, |
| 183 | not_found_exception=not_found_exception) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 184 | |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 185 | def delete_timeout(self, things, thing_id, |
| 186 | error_status='ERROR', |
| 187 | not_found_exception=nova_exceptions.NotFound): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 188 | """ |
| 189 | Given a thing, do a loop, sleeping |
| 190 | for a configurable amount of time, checking for the |
| 191 | deleted status to show. At any time, if the returned |
| 192 | status of the thing is ERROR, fail out. |
| 193 | """ |
| 194 | self._status_timeout(things, |
| 195 | thing_id, |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 196 | allow_notfound=True, |
| 197 | error_status=error_status, |
| 198 | not_found_exception=not_found_exception) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 199 | |
| 200 | def _status_timeout(self, |
| 201 | things, |
| 202 | thing_id, |
| 203 | expected_status=None, |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 204 | allow_notfound=False, |
| 205 | error_status='ERROR', |
| 206 | not_found_exception=nova_exceptions.NotFound): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 207 | |
| 208 | log_status = expected_status if expected_status else '' |
| 209 | if allow_notfound: |
| 210 | log_status += ' or NotFound' if log_status != '' else 'NotFound' |
| 211 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 212 | def check_status(): |
| 213 | # python-novaclient has resources available to its client |
| 214 | # that all implement a get() method taking an identifier |
| 215 | # for the singular resource to retrieve. |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 216 | try: |
| 217 | thing = things.get(thing_id) |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 218 | except not_found_exception: |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 219 | if allow_notfound: |
| 220 | return True |
| 221 | else: |
| 222 | raise |
| 223 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 224 | new_status = thing.status |
Brent Eagles | c26d452 | 2013-12-02 13:28:49 -0500 | [diff] [blame] | 225 | |
| 226 | # Some components are reporting error status in lower case |
| 227 | # so case sensitive comparisons can really mess things |
| 228 | # up. |
| 229 | if new_status.lower() == error_status.lower(): |
Masayuki Igawa | 2a8a812 | 2014-02-07 11:24:49 +0900 | [diff] [blame] | 230 | message = ("%s failed to get to expected status (%s). " |
| 231 | "In %s state.") % (thing, expected_status, |
| 232 | new_status) |
Masayuki Igawa | a0e786a | 2014-01-27 15:25:06 +0900 | [diff] [blame] | 233 | raise exceptions.BuildErrorException(message, |
| 234 | server_id=thing_id) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 235 | elif new_status == expected_status and expected_status is not None: |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 236 | return True # All good. |
| 237 | LOG.debug("Waiting for %s to get to %s status. " |
| 238 | "Currently in %s status", |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 239 | thing, log_status, new_status) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 240 | if not tempest.test.call_until_true( |
| 241 | check_status, |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 242 | CONF.compute.build_timeout, |
| 243 | CONF.compute.build_interval): |
Ken'ichi Ohmichi | ab1496f | 2013-12-12 22:17:57 +0900 | [diff] [blame] | 244 | message = ("Timed out waiting for thing %s " |
| 245 | "to become %s") % (thing_id, log_status) |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 246 | raise exceptions.TimeoutException(message) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 247 | |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 248 | def _create_loginable_secgroup_rule_nova(self, client=None, |
| 249 | secgroup_id=None): |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 250 | if client is None: |
| 251 | client = self.compute_client |
| 252 | if secgroup_id is None: |
| 253 | sgs = client.security_groups.list() |
| 254 | for sg in sgs: |
| 255 | if sg.name == 'default': |
| 256 | secgroup_id = sg.id |
| 257 | |
| 258 | # These rules are intended to permit inbound ssh and icmp |
| 259 | # traffic from all sources, so no group_id is provided. |
| 260 | # Setting a group_id would only permit traffic from ports |
| 261 | # belonging to the same security group. |
| 262 | rulesets = [ |
| 263 | { |
| 264 | # ssh |
| 265 | 'ip_protocol': 'tcp', |
| 266 | 'from_port': 22, |
| 267 | 'to_port': 22, |
| 268 | 'cidr': '0.0.0.0/0', |
| 269 | }, |
| 270 | { |
| 271 | # ping |
| 272 | 'ip_protocol': 'icmp', |
| 273 | 'from_port': -1, |
| 274 | 'to_port': -1, |
| 275 | 'cidr': '0.0.0.0/0', |
| 276 | } |
| 277 | ] |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 278 | rules = list() |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 279 | for ruleset in rulesets: |
| 280 | sg_rule = client.security_group_rules.create(secgroup_id, |
| 281 | **ruleset) |
| 282 | self.set_resource(sg_rule.id, sg_rule) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 283 | rules.append(sg_rule) |
| 284 | return rules |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 285 | |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame^] | 286 | def _create_security_group_nova(self, client=None, |
| 287 | namestart='secgroup-smoke-'): |
| 288 | if client is None: |
| 289 | client = self.compute_client |
| 290 | # Create security group |
| 291 | sg_name = data_utils.rand_name(namestart) |
| 292 | sg_desc = sg_name + " description" |
| 293 | secgroup = client.security_groups.create(sg_name, sg_desc) |
| 294 | self.assertEqual(secgroup.name, sg_name) |
| 295 | self.assertEqual(secgroup.description, sg_desc) |
| 296 | self.set_resource(sg_name, secgroup) |
| 297 | |
| 298 | # Add rules to the security group |
| 299 | self._create_loginable_secgroup_rule_nova(client, secgroup.id) |
| 300 | |
| 301 | return secgroup |
| 302 | |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 303 | def create_server(self, client=None, name=None, image=None, flavor=None, |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 304 | wait=True, create_kwargs={}): |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 305 | if client is None: |
| 306 | client = self.compute_client |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 307 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 308 | name = data_utils.rand_name('scenario-server-') |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 309 | if image is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 310 | image = CONF.compute.image_ref |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 311 | if flavor is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 312 | flavor = CONF.compute.flavor_ref |
JordanP | 9c052aa | 2014-01-24 13:05:00 +0000 | [diff] [blame] | 313 | |
| 314 | fixed_network_name = CONF.compute.fixed_network_name |
| 315 | if 'nics' not in create_kwargs and fixed_network_name: |
| 316 | networks = client.networks.list() |
| 317 | # If several networks found, set the NetID on which to connect the |
| 318 | # server to avoid the following error "Multiple possible networks |
| 319 | # found, use a Network ID to be more specific." |
| 320 | # See Tempest #1250866 |
| 321 | if len(networks) > 1: |
| 322 | for network in networks: |
| 323 | if network.label == fixed_network_name: |
| 324 | create_kwargs['nics'] = [{'net-id': network.id}] |
| 325 | break |
| 326 | # If we didn't find the network we were looking for : |
| 327 | else: |
| 328 | msg = ("The network on which the NIC of the server must " |
| 329 | "be connected can not be found : " |
| 330 | "fixed_network_name=%s. Starting instance without " |
| 331 | "specifying a network.") % fixed_network_name |
| 332 | LOG.info(msg) |
| 333 | |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 334 | LOG.debug("Creating a server (name: %s, image: %s, flavor: %s)", |
| 335 | name, image, flavor) |
| 336 | server = client.servers.create(name, image, flavor, **create_kwargs) |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 337 | self.assertEqual(server.name, name) |
| 338 | self.set_resource(name, server) |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 339 | if wait: |
| 340 | self.status_timeout(client.servers, server.id, 'ACTIVE') |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 341 | # The instance retrieved on creation is missing network |
| 342 | # details, necessitating retrieval after it becomes active to |
| 343 | # ensure correct details. |
| 344 | server = client.servers.get(server.id) |
| 345 | self.set_resource(name, server) |
| 346 | LOG.debug("Created server: %s", server) |
| 347 | return server |
| 348 | |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 349 | def create_volume(self, client=None, size=1, name=None, |
| 350 | snapshot_id=None, imageRef=None): |
| 351 | if client is None: |
| 352 | client = self.volume_client |
| 353 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 354 | name = data_utils.rand_name('scenario-volume-') |
Eric Windisch | 2d26f1b | 2013-09-04 17:52:16 -0700 | [diff] [blame] | 355 | LOG.debug("Creating a volume (size: %s, name: %s)", size, name) |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 356 | volume = client.volumes.create(size=size, display_name=name, |
| 357 | snapshot_id=snapshot_id, |
| 358 | imageRef=imageRef) |
| 359 | self.set_resource(name, volume) |
| 360 | self.assertEqual(name, volume.display_name) |
| 361 | self.status_timeout(client.volumes, volume.id, 'available') |
| 362 | LOG.debug("Created volume: %s", volume) |
| 363 | return volume |
| 364 | |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 365 | def create_server_snapshot(self, server, compute_client=None, |
| 366 | image_client=None, name=None): |
| 367 | if compute_client is None: |
| 368 | compute_client = self.compute_client |
| 369 | if image_client is None: |
| 370 | image_client = self.image_client |
| 371 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 372 | name = data_utils.rand_name('scenario-snapshot-') |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 373 | LOG.debug("Creating a snapshot image for server: %s", server.name) |
| 374 | image_id = compute_client.servers.create_image(server, name) |
| 375 | self.addCleanup(image_client.images.delete, image_id) |
| 376 | self.status_timeout(image_client.images, image_id, 'active') |
| 377 | snapshot_image = image_client.images.get(image_id) |
Chang Bo Guo | fc77e93 | 2013-09-16 17:38:26 -0700 | [diff] [blame] | 378 | self.assertEqual(name, snapshot_image.name) |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 379 | LOG.debug("Created snapshot image %s for server %s", |
| 380 | snapshot_image.name, server.name) |
| 381 | return snapshot_image |
| 382 | |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 383 | def create_keypair(self, client=None, name=None): |
| 384 | if client is None: |
| 385 | client = self.compute_client |
| 386 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 387 | name = data_utils.rand_name('scenario-keypair-') |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 388 | keypair = client.keypairs.create(name) |
| 389 | self.assertEqual(keypair.name, name) |
| 390 | self.set_resource(name, keypair) |
| 391 | return keypair |
| 392 | |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 393 | def get_remote_client(self, server_or_ip, username=None, private_key=None): |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 394 | if isinstance(server_or_ip, six.string_types): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 395 | ip = server_or_ip |
| 396 | else: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 397 | network_name_for_ssh = CONF.compute.network_for_ssh |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 398 | ip = server_or_ip.networks[network_name_for_ssh][0] |
| 399 | if username is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 400 | username = CONF.scenario.ssh_user |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 401 | if private_key is None: |
| 402 | private_key = self.keypair.private_key |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 403 | return remote_client.RemoteClient(ip, username, pkey=private_key) |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 404 | |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 405 | def _log_console_output(self, servers=None): |
| 406 | if not servers: |
| 407 | servers = self.compute_client.servers.list() |
| 408 | for server in servers: |
| 409 | LOG.debug('Console output for %s', server.id) |
| 410 | LOG.debug(server.get_console_output()) |
| 411 | |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 412 | def wait_for_volume_status(self, status): |
| 413 | volume_id = self.volume.id |
| 414 | self.status_timeout( |
| 415 | self.volume_client.volumes, volume_id, status) |
| 416 | |
| 417 | def _image_create(self, name, fmt, path, properties={}): |
| 418 | name = data_utils.rand_name('%s-' % name) |
| 419 | image_file = open(path, 'rb') |
| 420 | self.addCleanup(image_file.close) |
| 421 | params = { |
| 422 | 'name': name, |
| 423 | 'container_format': fmt, |
| 424 | 'disk_format': fmt, |
| 425 | 'is_public': 'True', |
| 426 | } |
| 427 | params.update(properties) |
| 428 | image = self.image_client.images.create(**params) |
| 429 | self.addCleanup(self.image_client.images.delete, image) |
| 430 | self.assertEqual("queued", image.status) |
| 431 | image.update(data=image_file) |
| 432 | return image.id |
| 433 | |
| 434 | def glance_image_create(self): |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 435 | qcow2_img_path = (CONF.scenario.img_dir + "/" + |
| 436 | CONF.scenario.qcow2_img_file) |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 437 | aki_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.aki_img_file |
| 438 | ari_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ari_img_file |
| 439 | ami_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ami_img_file |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 440 | LOG.debug("paths: img: %s, ami: %s, ari: %s, aki: %s" |
| 441 | % (qcow2_img_path, ami_img_path, ari_img_path, aki_img_path)) |
| 442 | try: |
| 443 | self.image = self._image_create('scenario-img', |
| 444 | 'bare', |
| 445 | qcow2_img_path, |
| 446 | properties={'disk_format': |
| 447 | 'qcow2'}) |
| 448 | except IOError: |
Masayuki Igawa | 188fc00 | 2014-02-23 06:42:44 +0900 | [diff] [blame] | 449 | 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] | 450 | kernel = self._image_create('scenario-aki', 'aki', aki_img_path) |
| 451 | ramdisk = self._image_create('scenario-ari', 'ari', ari_img_path) |
| 452 | properties = { |
| 453 | 'properties': {'kernel_id': kernel, 'ramdisk_id': ramdisk} |
| 454 | } |
| 455 | self.image = self._image_create('scenario-ami', 'ami', |
| 456 | path=ami_img_path, |
| 457 | properties=properties) |
| 458 | LOG.debug("image:%s" % self.image) |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 459 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 460 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 461 | class BaremetalScenarioTest(OfficialClientTest): |
| 462 | @classmethod |
| 463 | def setUpClass(cls): |
| 464 | super(BaremetalScenarioTest, cls).setUpClass() |
| 465 | |
| 466 | if (not CONF.service_available.ironic or |
| 467 | not CONF.baremetal.driver_enabled): |
| 468 | msg = 'Ironic not available or Ironic compute driver not enabled' |
| 469 | raise cls.skipException(msg) |
| 470 | |
| 471 | # use an admin client manager for baremetal client |
Adam Gandelman | acc13e6 | 2014-05-08 11:12:47 -0700 | [diff] [blame] | 472 | admin_creds = cls.admin_credentials() |
| 473 | manager = clients.OfficialClientManager(credentials=admin_creds) |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 474 | cls.baremetal_client = manager.baremetal_client |
| 475 | |
| 476 | # allow any issues obtaining the node list to raise early |
| 477 | cls.baremetal_client.node.list() |
| 478 | |
| 479 | def _node_state_timeout(self, node_id, state_attr, |
| 480 | target_states, timeout=10, interval=1): |
| 481 | if not isinstance(target_states, list): |
| 482 | target_states = [target_states] |
| 483 | |
| 484 | def check_state(): |
| 485 | node = self.get_node(node_id=node_id) |
| 486 | if getattr(node, state_attr) in target_states: |
| 487 | return True |
| 488 | return False |
| 489 | |
| 490 | if not tempest.test.call_until_true( |
| 491 | check_state, timeout, interval): |
| 492 | msg = ("Timed out waiting for node %s to reach %s state(s) %s" % |
| 493 | (node_id, state_attr, target_states)) |
| 494 | raise exceptions.TimeoutException(msg) |
| 495 | |
| 496 | def wait_provisioning_state(self, node_id, state, timeout): |
| 497 | self._node_state_timeout( |
| 498 | node_id=node_id, state_attr='provision_state', |
| 499 | target_states=state, timeout=timeout) |
| 500 | |
| 501 | def wait_power_state(self, node_id, state): |
| 502 | self._node_state_timeout( |
| 503 | node_id=node_id, state_attr='power_state', |
| 504 | target_states=state, timeout=CONF.baremetal.power_timeout) |
| 505 | |
| 506 | def wait_node(self, instance_id): |
| 507 | """Waits for a node to be associated with instance_id.""" |
Zhi Kun Liu | 4a8d1ea | 2014-04-15 22:08:21 -0500 | [diff] [blame] | 508 | from ironicclient import exc as ironic_exceptions |
| 509 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 510 | def _get_node(): |
| 511 | node = None |
| 512 | try: |
| 513 | node = self.get_node(instance_id=instance_id) |
| 514 | except ironic_exceptions.HTTPNotFound: |
| 515 | pass |
| 516 | return node is not None |
| 517 | |
| 518 | if not tempest.test.call_until_true( |
| 519 | _get_node, CONF.baremetal.association_timeout, 1): |
| 520 | msg = ('Timed out waiting to get Ironic node by instance id %s' |
| 521 | % instance_id) |
| 522 | raise exceptions.TimeoutException(msg) |
| 523 | |
| 524 | def get_node(self, node_id=None, instance_id=None): |
| 525 | if node_id: |
| 526 | return self.baremetal_client.node.get(node_id) |
| 527 | elif instance_id: |
| 528 | return self.baremetal_client.node.get_by_instance_uuid(instance_id) |
| 529 | |
| 530 | def get_ports(self, node_id): |
| 531 | ports = [] |
| 532 | for port in self.baremetal_client.node.list_ports(node_id): |
| 533 | ports.append(self.baremetal_client.port.get(port.uuid)) |
| 534 | return ports |
| 535 | |
| 536 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 537 | class NetworkScenarioTest(OfficialClientTest): |
| 538 | """ |
| 539 | Base class for network scenario tests |
| 540 | """ |
| 541 | |
| 542 | @classmethod |
| 543 | def check_preconditions(cls): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 544 | if (CONF.service_available.neutron): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 545 | cls.enabled = True |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 546 | # verify that neutron_available is telling the truth |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 547 | try: |
| 548 | cls.network_client.list_networks() |
| 549 | except exc.EndpointNotFound: |
| 550 | cls.enabled = False |
| 551 | raise |
| 552 | else: |
| 553 | cls.enabled = False |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 554 | msg = 'Neutron not available' |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 555 | raise cls.skipException(msg) |
| 556 | |
| 557 | @classmethod |
| 558 | def setUpClass(cls): |
| 559 | super(NetworkScenarioTest, cls).setUpClass() |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 560 | cls.tenant_id = cls.manager.identity_client.tenant_id |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 561 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 562 | def _create_network(self, tenant_id, namestart='network-smoke-'): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 563 | name = data_utils.rand_name(namestart) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 564 | body = dict( |
| 565 | network=dict( |
| 566 | name=name, |
| 567 | tenant_id=tenant_id, |
| 568 | ), |
| 569 | ) |
| 570 | result = self.network_client.create_network(body=body) |
| 571 | network = net_common.DeletableNetwork(client=self.network_client, |
| 572 | **result['network']) |
| 573 | self.assertEqual(network.name, name) |
| 574 | self.set_resource(name, network) |
| 575 | return network |
| 576 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 577 | def _list_networks(self, **kwargs): |
| 578 | nets = self.network_client.list_networks(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 579 | return nets['networks'] |
| 580 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 581 | def _list_subnets(self, **kwargs): |
| 582 | subnets = self.network_client.list_subnets(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 583 | return subnets['subnets'] |
| 584 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 585 | def _list_routers(self, **kwargs): |
| 586 | routers = self.network_client.list_routers(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 587 | return routers['routers'] |
| 588 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 589 | def _list_ports(self, **kwargs): |
| 590 | ports = self.network_client.list_ports(**kwargs) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 591 | return ports['ports'] |
| 592 | |
| 593 | def _get_tenant_own_network_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 594 | nets = self._list_networks(tenant_id=tenant_id) |
| 595 | return len(nets) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 596 | |
| 597 | def _get_tenant_own_subnet_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 598 | subnets = self._list_subnets(tenant_id=tenant_id) |
| 599 | return len(subnets) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 600 | |
| 601 | def _get_tenant_own_port_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 602 | ports = self._list_ports(tenant_id=tenant_id) |
| 603 | return len(ports) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 604 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 605 | def _create_subnet(self, network, namestart='subnet-smoke-', **kwargs): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 606 | """ |
| 607 | Create a subnet for the given network within the cidr block |
| 608 | configured for tenant networks. |
| 609 | """ |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 610 | |
| 611 | def cidr_in_use(cidr, tenant_id): |
| 612 | """ |
| 613 | :return True if subnet with cidr already exist in tenant |
| 614 | False else |
| 615 | """ |
| 616 | cidr_in_use = self._list_subnets(tenant_id=tenant_id, cidr=cidr) |
| 617 | return len(cidr_in_use) != 0 |
| 618 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 619 | tenant_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 620 | result = None |
| 621 | # Repeatedly attempt subnet creation with sequential cidr |
| 622 | # blocks until an unallocated block is found. |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 623 | for subnet_cidr in tenant_cidr.subnet( |
| 624 | CONF.network.tenant_network_mask_bits): |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 625 | str_cidr = str(subnet_cidr) |
| 626 | if cidr_in_use(str_cidr, tenant_id=network.tenant_id): |
| 627 | continue |
| 628 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 629 | body = dict( |
| 630 | subnet=dict( |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 631 | name=data_utils.rand_name(namestart), |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 632 | ip_version=4, |
| 633 | network_id=network.id, |
| 634 | tenant_id=network.tenant_id, |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 635 | cidr=str_cidr, |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 636 | ), |
| 637 | ) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 638 | body['subnet'].update(kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 639 | try: |
| 640 | result = self.network_client.create_subnet(body=body) |
| 641 | break |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 642 | except exc.NeutronClientException as e: |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 643 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 644 | if not is_overlapping_cidr: |
| 645 | raise |
| 646 | self.assertIsNotNone(result, 'Unable to allocate tenant network') |
| 647 | subnet = net_common.DeletableSubnet(client=self.network_client, |
| 648 | **result['subnet']) |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 649 | self.assertEqual(subnet.cidr, str_cidr) |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 650 | self.set_resource(data_utils.rand_name(namestart), subnet) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 651 | return subnet |
| 652 | |
| 653 | def _create_port(self, network, namestart='port-quotatest-'): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 654 | name = data_utils.rand_name(namestart) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 655 | body = dict( |
| 656 | port=dict(name=name, |
| 657 | network_id=network.id, |
| 658 | tenant_id=network.tenant_id)) |
| 659 | result = self.network_client.create_port(body=body) |
| 660 | self.assertIsNotNone(result, 'Unable to allocate port') |
| 661 | port = net_common.DeletablePort(client=self.network_client, |
| 662 | **result['port']) |
| 663 | self.set_resource(name, port) |
| 664 | return port |
| 665 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 666 | def _get_server_port_id(self, server, ip_addr=None): |
| 667 | ports = self._list_ports(device_id=server.id, fixed_ip=ip_addr) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 668 | self.assertEqual(len(ports), 1, |
| 669 | "Unable to determine which port to target.") |
Yair Fried | 05db252 | 2013-11-18 11:02:10 +0200 | [diff] [blame] | 670 | return ports[0]['id'] |
| 671 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 672 | def _create_floating_ip(self, thing, external_network_id, port_id=None): |
| 673 | if not port_id: |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 674 | port_id = self._get_server_port_id(thing) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 675 | body = dict( |
| 676 | floatingip=dict( |
| 677 | floating_network_id=external_network_id, |
| 678 | port_id=port_id, |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 679 | tenant_id=thing.tenant_id, |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 680 | ) |
| 681 | ) |
| 682 | result = self.network_client.create_floatingip(body=body) |
| 683 | floating_ip = net_common.DeletableFloatingIp( |
| 684 | client=self.network_client, |
| 685 | **result['floatingip']) |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 686 | self.set_resource(data_utils.rand_name('floatingip-'), floating_ip) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 687 | return floating_ip |
| 688 | |
Yair Fried | 05db252 | 2013-11-18 11:02:10 +0200 | [diff] [blame] | 689 | def _associate_floating_ip(self, floating_ip, server): |
| 690 | port_id = self._get_server_port_id(server) |
| 691 | floating_ip.update(port_id=port_id) |
| 692 | self.assertEqual(port_id, floating_ip.port_id) |
| 693 | return floating_ip |
| 694 | |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 695 | def _disassociate_floating_ip(self, floating_ip): |
| 696 | """ |
| 697 | :param floating_ip: type DeletableFloatingIp |
| 698 | """ |
| 699 | floating_ip.update(port_id=None) |
llg8212 | e4cd392 | 2014-02-15 12:14:21 +0800 | [diff] [blame] | 700 | self.assertIsNone(floating_ip.port_id) |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 701 | return floating_ip |
| 702 | |
| 703 | def _ping_ip_address(self, ip_address, should_succeed=True): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 704 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 705 | |
| 706 | def ping(): |
| 707 | proc = subprocess.Popen(cmd, |
| 708 | stdout=subprocess.PIPE, |
| 709 | stderr=subprocess.PIPE) |
| 710 | proc.wait() |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 711 | return (proc.returncode == 0) == should_succeed |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 712 | |
Nachi Ueno | 6d580be | 2013-07-24 10:58:11 -0700 | [diff] [blame] | 713 | return tempest.test.call_until_true( |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 714 | ping, CONF.compute.ping_timeout, 1) |
Maru Newby | af292e8 | 2013-05-20 21:32:28 +0000 | [diff] [blame] | 715 | |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 716 | def _create_pool(self, lb_method, protocol, subnet_id): |
| 717 | """Wrapper utility that returns a test pool.""" |
| 718 | name = data_utils.rand_name('pool-') |
| 719 | body = { |
| 720 | "pool": { |
| 721 | "protocol": protocol, |
| 722 | "name": name, |
| 723 | "subnet_id": subnet_id, |
| 724 | "lb_method": lb_method |
| 725 | } |
| 726 | } |
| 727 | resp = self.network_client.create_pool(body=body) |
| 728 | pool = net_common.DeletablePool(client=self.network_client, |
| 729 | **resp['pool']) |
| 730 | self.assertEqual(pool['name'], name) |
| 731 | self.set_resource(name, pool) |
| 732 | return pool |
| 733 | |
| 734 | def _create_member(self, address, protocol_port, pool_id): |
| 735 | """Wrapper utility that returns a test member.""" |
| 736 | body = { |
| 737 | "member": { |
| 738 | "protocol_port": protocol_port, |
| 739 | "pool_id": pool_id, |
| 740 | "address": address |
| 741 | } |
| 742 | } |
| 743 | resp = self.network_client.create_member(body) |
| 744 | member = net_common.DeletableMember(client=self.network_client, |
| 745 | **resp['member']) |
| 746 | self.set_resource(data_utils.rand_name('member-'), member) |
| 747 | return member |
| 748 | |
| 749 | def _create_vip(self, protocol, protocol_port, subnet_id, pool_id): |
| 750 | """Wrapper utility that returns a test vip.""" |
| 751 | name = data_utils.rand_name('vip-') |
| 752 | body = { |
| 753 | "vip": { |
| 754 | "protocol": protocol, |
| 755 | "name": name, |
| 756 | "subnet_id": subnet_id, |
| 757 | "pool_id": pool_id, |
| 758 | "protocol_port": protocol_port |
| 759 | } |
| 760 | } |
| 761 | resp = self.network_client.create_vip(body) |
| 762 | vip = net_common.DeletableVip(client=self.network_client, |
| 763 | **resp['vip']) |
| 764 | self.assertEqual(vip['name'], name) |
| 765 | self.set_resource(name, vip) |
| 766 | return vip |
| 767 | |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 768 | def _check_vm_connectivity(self, ip_address, |
| 769 | username=None, |
| 770 | private_key=None, |
| 771 | should_connect=True): |
| 772 | """ |
| 773 | :param ip_address: server to test against |
| 774 | :param username: server's ssh username |
| 775 | :param private_key: server's ssh private key to be used |
| 776 | :param should_connect: True/False indicates positive/negative test |
| 777 | positive - attempt ping and ssh |
| 778 | negative - attempt ping and fail if succeed |
| 779 | |
| 780 | :raises: AssertError if the result of the connectivity check does |
| 781 | not match the value of the should_connect param |
| 782 | """ |
| 783 | if should_connect: |
| 784 | msg = "Timed out waiting for %s to become reachable" % ip_address |
| 785 | else: |
| 786 | msg = "ip address %s is reachable" % ip_address |
| 787 | self.assertTrue(self._ping_ip_address(ip_address, |
| 788 | should_succeed=should_connect), |
| 789 | msg=msg) |
| 790 | if should_connect: |
| 791 | # no need to check ssh for negative connectivity |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 792 | linux_client = self.get_remote_client(ip_address, username, |
| 793 | private_key) |
| 794 | linux_client.validate_authentication() |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 795 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 796 | def _check_remote_connectivity(self, source, dest, should_succeed=True): |
| 797 | """ |
| 798 | check ping server via source ssh connection |
| 799 | |
| 800 | :param source: RemoteClient: an ssh connection from which to ping |
| 801 | :param dest: and IP to ping against |
| 802 | :param should_succeed: boolean should ping succeed or not |
| 803 | :returns: boolean -- should_succeed == ping |
| 804 | :returns: ping is false if ping failed |
| 805 | """ |
| 806 | def ping_remote(): |
| 807 | try: |
| 808 | source.ping_host(dest) |
| 809 | except exceptions.SSHExecCommandFailed: |
| 810 | LOG.exception('Failed to ping host via ssh connection') |
| 811 | return not should_succeed |
| 812 | return should_succeed |
| 813 | |
| 814 | return tempest.test.call_until_true(ping_remote, |
| 815 | CONF.compute.ping_timeout, |
| 816 | 1) |
| 817 | |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 818 | def _create_security_group_neutron(self, tenant_id, client=None, |
| 819 | namestart='secgroup-smoke-'): |
| 820 | if client is None: |
| 821 | client = self.network_client |
| 822 | secgroup = self._create_empty_security_group(namestart=namestart, |
| 823 | client=client, |
| 824 | tenant_id=tenant_id) |
| 825 | |
| 826 | # Add rules to the security group |
| 827 | rules = self._create_loginable_secgroup_rule_neutron(secgroup=secgroup) |
| 828 | for rule in rules: |
| 829 | self.assertEqual(tenant_id, rule.tenant_id) |
| 830 | self.assertEqual(secgroup.id, rule.security_group_id) |
| 831 | return secgroup |
| 832 | |
| 833 | def _create_empty_security_group(self, tenant_id, client=None, |
| 834 | namestart='secgroup-smoke-'): |
| 835 | """Create a security group without rules. |
| 836 | |
| 837 | Default rules will be created: |
| 838 | - IPv4 egress to any |
| 839 | - IPv6 egress to any |
| 840 | |
| 841 | :param tenant_id: secgroup will be created in this tenant |
| 842 | :returns: DeletableSecurityGroup -- containing the secgroup created |
| 843 | """ |
| 844 | if client is None: |
| 845 | client = self.network_client |
| 846 | sg_name = data_utils.rand_name(namestart) |
| 847 | sg_desc = sg_name + " description" |
| 848 | sg_dict = dict(name=sg_name, |
| 849 | description=sg_desc) |
| 850 | sg_dict['tenant_id'] = tenant_id |
| 851 | body = dict(security_group=sg_dict) |
| 852 | result = client.create_security_group(body=body) |
| 853 | secgroup = net_common.DeletableSecurityGroup( |
| 854 | client=client, |
| 855 | **result['security_group'] |
| 856 | ) |
| 857 | self.assertEqual(secgroup.name, sg_name) |
| 858 | self.assertEqual(tenant_id, secgroup.tenant_id) |
| 859 | self.assertEqual(secgroup.description, sg_desc) |
| 860 | self.set_resource(sg_name, secgroup) |
| 861 | return secgroup |
| 862 | |
| 863 | def _default_security_group(self, tenant_id, client=None): |
| 864 | """Get default secgroup for given tenant_id. |
| 865 | |
| 866 | :returns: DeletableSecurityGroup -- default secgroup for given tenant |
| 867 | """ |
| 868 | if client is None: |
| 869 | client = self.network_client |
| 870 | sgs = [ |
| 871 | sg for sg in client.list_security_groups().values()[0] |
| 872 | if sg['tenant_id'] == tenant_id and sg['name'] == 'default' |
| 873 | ] |
| 874 | msg = "No default security group for tenant %s." % (tenant_id) |
| 875 | self.assertTrue(len(sgs) > 0, msg) |
| 876 | if len(sgs) > 1: |
| 877 | msg = "Found %d default security groups" % len(sgs) |
| 878 | raise exc.NeutronClientNoUniqueMatch(msg=msg) |
| 879 | return net_common.DeletableSecurityGroup(client=client, |
| 880 | **sgs[0]) |
| 881 | |
| 882 | def _create_security_group_rule(self, client=None, secgroup=None, |
| 883 | tenant_id=None, **kwargs): |
| 884 | """Create a rule from a dictionary of rule parameters. |
| 885 | |
| 886 | Create a rule in a secgroup. if secgroup not defined will search for |
| 887 | default secgroup in tenant_id. |
| 888 | |
| 889 | :param secgroup: type DeletableSecurityGroup. |
| 890 | :param secgroup_id: search for secgroup by id |
| 891 | default -- choose default secgroup for given tenant_id |
| 892 | :param tenant_id: if secgroup not passed -- the tenant in which to |
| 893 | search for default secgroup |
| 894 | :param kwargs: a dictionary containing rule parameters: |
| 895 | for example, to allow incoming ssh: |
| 896 | rule = { |
| 897 | direction: 'ingress' |
| 898 | protocol:'tcp', |
| 899 | port_range_min: 22, |
| 900 | port_range_max: 22 |
| 901 | } |
| 902 | """ |
| 903 | if client is None: |
| 904 | client = self.network_client |
| 905 | if secgroup is None: |
| 906 | secgroup = self._default_security_group(tenant_id) |
| 907 | |
| 908 | ruleset = dict(security_group_id=secgroup.id, |
| 909 | tenant_id=secgroup.tenant_id, |
| 910 | ) |
| 911 | ruleset.update(kwargs) |
| 912 | |
| 913 | body = dict(security_group_rule=dict(ruleset)) |
| 914 | sg_rule = client.create_security_group_rule(body=body) |
| 915 | sg_rule = net_common.DeletableSecurityGroupRule( |
| 916 | client=client, |
| 917 | **sg_rule['security_group_rule'] |
| 918 | ) |
| 919 | self.set_resource(sg_rule.id, sg_rule) |
| 920 | self.assertEqual(secgroup.tenant_id, sg_rule.tenant_id) |
| 921 | self.assertEqual(secgroup.id, sg_rule.security_group_id) |
| 922 | |
| 923 | return sg_rule |
| 924 | |
| 925 | def _create_loginable_secgroup_rule_neutron(self, client=None, |
| 926 | secgroup=None): |
| 927 | """These rules are intended to permit inbound ssh and icmp |
| 928 | traffic from all sources, so no group_id is provided. |
| 929 | Setting a group_id would only permit traffic from ports |
| 930 | belonging to the same security group. |
| 931 | """ |
| 932 | |
| 933 | if client is None: |
| 934 | client = self.network_client |
| 935 | rules = [] |
| 936 | rulesets = [ |
| 937 | dict( |
| 938 | # ssh |
| 939 | protocol='tcp', |
| 940 | port_range_min=22, |
| 941 | port_range_max=22, |
| 942 | ), |
| 943 | dict( |
| 944 | # ping |
| 945 | protocol='icmp', |
| 946 | ) |
| 947 | ] |
| 948 | for ruleset in rulesets: |
| 949 | for r_direction in ['ingress', 'egress']: |
| 950 | ruleset['direction'] = r_direction |
| 951 | try: |
| 952 | sg_rule = self._create_security_group_rule( |
| 953 | client=client, secgroup=secgroup, **ruleset) |
| 954 | except exc.NeutronClientException as ex: |
| 955 | # if rule already exist - skip rule and continue |
| 956 | if not (ex.status_code is 409 and 'Security group rule' |
| 957 | ' already exists' in ex.message): |
| 958 | raise ex |
| 959 | else: |
| 960 | self.assertEqual(r_direction, sg_rule.direction) |
| 961 | rules.append(sg_rule) |
| 962 | |
| 963 | return rules |
| 964 | |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 965 | def _ssh_to_server(self, server, private_key): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 966 | ssh_login = CONF.compute.image_ssh_user |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 967 | return self.get_remote_client(server, |
| 968 | username=ssh_login, |
| 969 | private_key=private_key) |
| 970 | |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 971 | def _show_quota_network(self, tenant_id): |
| 972 | quota = self.network_client.show_quota(tenant_id) |
| 973 | return quota['quota']['network'] |
| 974 | |
| 975 | def _show_quota_subnet(self, tenant_id): |
| 976 | quota = self.network_client.show_quota(tenant_id) |
| 977 | return quota['quota']['subnet'] |
| 978 | |
| 979 | def _show_quota_port(self, tenant_id): |
| 980 | quota = self.network_client.show_quota(tenant_id) |
| 981 | return quota['quota']['port'] |
| 982 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 983 | def _get_router(self, tenant_id): |
| 984 | """Retrieve a router for the given tenant id. |
| 985 | |
| 986 | If a public router has been configured, it will be returned. |
| 987 | |
| 988 | If a public router has not been configured, but a public |
| 989 | network has, a tenant router will be created and returned that |
| 990 | routes traffic to the public network. |
| 991 | """ |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 992 | router_id = CONF.network.public_router_id |
| 993 | network_id = CONF.network.public_network_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 994 | if router_id: |
| 995 | result = self.network_client.show_router(router_id) |
| 996 | return net_common.AttributeDict(**result['router']) |
| 997 | elif network_id: |
| 998 | router = self._create_router(tenant_id) |
| 999 | router.add_gateway(network_id) |
| 1000 | return router |
| 1001 | else: |
| 1002 | raise Exception("Neither of 'public_router_id' or " |
| 1003 | "'public_network_id' has been defined.") |
| 1004 | |
| 1005 | def _create_router(self, tenant_id, namestart='router-smoke-'): |
| 1006 | name = data_utils.rand_name(namestart) |
| 1007 | body = dict( |
| 1008 | router=dict( |
| 1009 | name=name, |
| 1010 | admin_state_up=True, |
| 1011 | tenant_id=tenant_id, |
| 1012 | ), |
| 1013 | ) |
| 1014 | result = self.network_client.create_router(body=body) |
| 1015 | router = net_common.DeletableRouter(client=self.network_client, |
| 1016 | **result['router']) |
| 1017 | self.assertEqual(router.name, name) |
| 1018 | self.set_resource(name, router) |
| 1019 | return router |
| 1020 | |
| 1021 | def _create_networks(self, tenant_id=None): |
| 1022 | """Create a network with a subnet connected to a router. |
| 1023 | |
| 1024 | :returns: network, subnet, router |
| 1025 | """ |
| 1026 | if tenant_id is None: |
| 1027 | tenant_id = self.tenant_id |
| 1028 | network = self._create_network(tenant_id) |
| 1029 | router = self._get_router(tenant_id) |
| 1030 | subnet = self._create_subnet(network) |
| 1031 | subnet.add_to_router(router.id) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 1032 | return network, subnet, router |
| 1033 | |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1034 | |
| 1035 | class OrchestrationScenarioTest(OfficialClientTest): |
| 1036 | """ |
| 1037 | Base class for orchestration scenario tests |
| 1038 | """ |
| 1039 | |
| 1040 | @classmethod |
Matt Riedemann | 11c5b64 | 2013-08-24 08:45:38 -0700 | [diff] [blame] | 1041 | def setUpClass(cls): |
| 1042 | super(OrchestrationScenarioTest, cls).setUpClass() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1043 | if not CONF.service_available.heat: |
Matt Riedemann | 11c5b64 | 2013-08-24 08:45:38 -0700 | [diff] [blame] | 1044 | raise cls.skipException("Heat support is required") |
| 1045 | |
| 1046 | @classmethod |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1047 | def credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 1048 | admin_creds = auth.get_default_credentials('identity_admin') |
| 1049 | creds = auth.get_default_credentials('user') |
| 1050 | admin_creds.tenant_name = creds.tenant_name |
| 1051 | return admin_creds |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1052 | |
| 1053 | def _load_template(self, base_file, file_name): |
| 1054 | filepath = os.path.join(os.path.dirname(os.path.realpath(base_file)), |
| 1055 | file_name) |
| 1056 | with open(filepath) as f: |
| 1057 | return f.read() |
| 1058 | |
| 1059 | @classmethod |
| 1060 | def _stack_rand_name(cls): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1061 | return data_utils.rand_name(cls.__name__ + '-') |
Steve Baker | 80252da | 2013-09-25 13:29:10 +1200 | [diff] [blame] | 1062 | |
| 1063 | @classmethod |
| 1064 | def _get_default_network(cls): |
| 1065 | networks = cls.network_client.list_networks() |
| 1066 | for net in networks['networks']: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1067 | if net['name'] == CONF.compute.fixed_network_name: |
Steve Baker | 80252da | 2013-09-25 13:29:10 +1200 | [diff] [blame] | 1068 | return net |