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 |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 20 | import six |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 21 | import subprocess |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 22 | import time |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 23 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 24 | from cinderclient import exceptions as cinder_exceptions |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 25 | import glanceclient |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 26 | from heatclient import exc as heat_exceptions |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 27 | import netaddr |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 28 | from neutronclient.common import exceptions as exc |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 29 | from novaclient import exceptions as nova_exceptions |
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 |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 42 | import tempest.test |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 43 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 44 | CONF = config.CONF |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 45 | |
Attila Fazekas | fb7552a | 2013-08-27 13:02:26 +0200 | [diff] [blame] | 46 | LOG = log.getLogger(__name__) |
| 47 | |
| 48 | # NOTE(afazekas): Workaround for the stdout logging |
| 49 | LOG_nova_client = logging.getLogger('novaclient.client') |
| 50 | LOG_nova_client.addHandler(log.NullHandler()) |
| 51 | |
| 52 | LOG_cinder_client = logging.getLogger('cinderclient.client') |
| 53 | LOG_cinder_client.addHandler(log.NullHandler()) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 54 | |
| 55 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 56 | class OfficialClientTest(tempest.test.BaseTestCase): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 57 | """ |
| 58 | Official Client test base class for scenario testing. |
| 59 | |
| 60 | Official Client tests are tests that have the following characteristics: |
| 61 | |
| 62 | * Test basic operations of an API, typically in an order that |
| 63 | a regular user would perform those operations |
| 64 | * Test only the correct inputs and action paths -- no fuzz or |
| 65 | random input data is sent, only valid inputs. |
| 66 | * Use only the default client tool for calling an API |
| 67 | """ |
| 68 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 69 | @classmethod |
| 70 | def setUpClass(cls): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 71 | super(OfficialClientTest, cls).setUpClass() |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 72 | cls.isolated_creds = isolated_creds.IsolatedCreds( |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 73 | cls.__name__, tempest_client=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 74 | network_resources=cls.network_resources) |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 75 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 76 | cls.manager = clients.OfficialClientManager( |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 77 | credentials=cls.credentials()) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 78 | cls.compute_client = cls.manager.compute_client |
| 79 | cls.image_client = cls.manager.image_client |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 80 | cls.baremetal_client = cls.manager.baremetal_client |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 81 | cls.identity_client = cls.manager.identity_client |
| 82 | cls.network_client = cls.manager.network_client |
| 83 | cls.volume_client = cls.manager.volume_client |
Mauro S. M. Rodrigues | e86ed04 | 2013-12-12 18:56:00 +0000 | [diff] [blame] | 84 | cls.object_storage_client = cls.manager.object_storage_client |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 85 | cls.orchestration_client = cls.manager.orchestration_client |
Sergey Lukjanov | 7409e2e | 2014-03-27 12:55:50 +0400 | [diff] [blame] | 86 | cls.data_processing_client = cls.manager.data_processing_client |
Artur Svechnikov | c3bf925 | 2014-05-05 16:37:37 +0400 | [diff] [blame] | 87 | cls.ceilometer_client = cls.manager.ceilometer_client |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 88 | |
| 89 | @classmethod |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 90 | def _get_credentials(cls, get_creds, ctype): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 91 | if CONF.compute.allow_tenant_isolation: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 92 | creds = get_creds() |
Yair Fried | 769bbff | 2013-12-18 16:33:17 +0200 | [diff] [blame] | 93 | else: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 94 | creds = auth.get_default_credentials(ctype) |
| 95 | return creds |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 96 | |
| 97 | @classmethod |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 98 | def credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 99 | return cls._get_credentials(cls.isolated_creds.get_primary_creds, |
| 100 | 'user') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 101 | |
| 102 | @classmethod |
| 103 | def alt_credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 104 | return cls._get_credentials(cls.isolated_creds.get_alt_creds, |
| 105 | 'alt_user') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 106 | |
| 107 | @classmethod |
| 108 | def admin_credentials(cls): |
| 109 | return cls._get_credentials(cls.isolated_creds.get_admin_creds, |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 110 | 'identity_admin') |
Yair Fried | a71cc44 | 2013-12-18 13:32:36 +0200 | [diff] [blame] | 111 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 112 | def setUp(self): |
| 113 | super(OfficialClientTest, self).setUp() |
| 114 | self.cleanup_waits = [] |
| 115 | # NOTE(mtreinish) This is safe to do in setUp instead of setUp class |
| 116 | # because scenario tests in the same test class should not share |
| 117 | # resources. If resources were shared between test cases then it |
| 118 | # should be a single scenario test instead of multiples. |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 119 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 120 | # NOTE(yfried): this list is cleaned at the end of test_methods and |
| 121 | # not at the end of the class |
| 122 | self.addCleanup(self._wait_for_cleanups) |
| 123 | |
| 124 | @staticmethod |
| 125 | def not_found_exception(exception): |
| 126 | """ |
| 127 | @return: True if exception is of NotFound type |
| 128 | """ |
| 129 | NOT_FOUND_LIST = ['NotFound', 'HTTPNotFound'] |
| 130 | return (exception.__class__.__name__ in NOT_FOUND_LIST |
| 131 | or |
| 132 | hasattr(exception, 'status_code') and |
| 133 | exception.status_code == 404) |
| 134 | |
| 135 | def delete_wrapper(self, thing): |
| 136 | """Ignores NotFound exceptions for delete operations. |
| 137 | |
| 138 | @param thing: object with delete() method. |
| 139 | OpenStack resources are assumed to have a delete() method which |
| 140 | destroys the resource |
| 141 | """ |
| 142 | |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 143 | try: |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 144 | thing.delete() |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 145 | except Exception as e: |
| 146 | # If the resource is already missing, mission accomplished. |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 147 | if not self.not_found_exception(e): |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 148 | raise |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 149 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 150 | def _wait_for_cleanups(self): |
| 151 | """To handle async delete actions, a list of waits is added |
| 152 | which will be iterated over as the last step of clearing the |
| 153 | cleanup queue. That way all the delete calls are made up front |
| 154 | and the tests won't succeed unless the deletes are eventually |
| 155 | successful. This is the same basic approach used in the api tests to |
| 156 | limit cleanup execution time except here it is multi-resource, |
| 157 | because of the nature of the scenario tests. |
| 158 | """ |
| 159 | for wait in self.cleanup_waits: |
| 160 | self.delete_timeout(**wait) |
Yair Fried | bf2e2c4 | 2014-01-28 12:06:38 +0200 | [diff] [blame] | 161 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 162 | def addCleanup_with_wait(self, things, thing_id, |
| 163 | error_status='ERROR', |
| 164 | exc_type=nova_exceptions.NotFound, |
| 165 | cleanup_callable=None, cleanup_args=[], |
| 166 | cleanup_kwargs={}): |
| 167 | """Adds wait for ansyc resource deletion at the end of cleanups |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 168 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 169 | @param things: type of the resource to delete |
| 170 | @param thing_id: |
| 171 | @param error_status: see manager.delete_timeout() |
| 172 | @param exc_type: see manager.delete_timeout() |
| 173 | @param cleanup_callable: method to load pass to self.addCleanup with |
| 174 | the following *cleanup_args, **cleanup_kwargs. |
| 175 | usually a delete method. if not used, will try to use: |
| 176 | things.delete(thing_id) |
| 177 | """ |
| 178 | if cleanup_callable is None: |
| 179 | LOG.debug("no delete method passed. using {rclass}.delete({id}) as" |
| 180 | " default".format(rclass=things, id=thing_id)) |
| 181 | self.addCleanup(things.delete, thing_id) |
| 182 | else: |
| 183 | self.addCleanup(cleanup_callable, *cleanup_args, **cleanup_kwargs) |
| 184 | wait_dict = { |
| 185 | 'things': things, |
| 186 | 'thing_id': thing_id, |
| 187 | 'error_status': error_status, |
| 188 | 'not_found_exception': exc_type, |
| 189 | } |
| 190 | self.cleanup_waits.append(wait_dict) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 191 | |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 192 | def status_timeout(self, things, thing_id, expected_status, |
| 193 | error_status='ERROR', |
| 194 | not_found_exception=nova_exceptions.NotFound): |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 195 | """ |
| 196 | Given a thing and an expected status, do a loop, sleeping |
| 197 | for a configurable amount of time, checking for the |
| 198 | expected status to show. At any time, if the returned |
| 199 | status of the thing is ERROR, fail out. |
| 200 | """ |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 201 | self._status_timeout(things, thing_id, |
| 202 | expected_status=expected_status, |
| 203 | error_status=error_status, |
| 204 | not_found_exception=not_found_exception) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 205 | |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 206 | def delete_timeout(self, things, thing_id, |
| 207 | error_status='ERROR', |
| 208 | not_found_exception=nova_exceptions.NotFound): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 209 | """ |
| 210 | Given a thing, do a loop, sleeping |
| 211 | for a configurable amount of time, checking for the |
| 212 | deleted status to show. At any time, if the returned |
| 213 | status of the thing is ERROR, fail out. |
| 214 | """ |
| 215 | self._status_timeout(things, |
| 216 | thing_id, |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 217 | allow_notfound=True, |
| 218 | error_status=error_status, |
| 219 | not_found_exception=not_found_exception) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 220 | |
| 221 | def _status_timeout(self, |
| 222 | things, |
| 223 | thing_id, |
| 224 | expected_status=None, |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 225 | allow_notfound=False, |
| 226 | error_status='ERROR', |
| 227 | not_found_exception=nova_exceptions.NotFound): |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 228 | |
| 229 | log_status = expected_status if expected_status else '' |
| 230 | if allow_notfound: |
| 231 | log_status += ' or NotFound' if log_status != '' else 'NotFound' |
| 232 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 233 | def check_status(): |
| 234 | # python-novaclient has resources available to its client |
| 235 | # that all implement a get() method taking an identifier |
| 236 | # for the singular resource to retrieve. |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 237 | try: |
| 238 | thing = things.get(thing_id) |
Steve Baker | efde761 | 2013-09-30 11:29:23 +1300 | [diff] [blame] | 239 | except not_found_exception: |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 240 | if allow_notfound: |
| 241 | return True |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 242 | raise |
| 243 | except Exception as e: |
| 244 | if allow_notfound and self.not_found_exception(e): |
| 245 | return True |
| 246 | raise |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 247 | |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 248 | new_status = thing.status |
Brent Eagles | c26d452 | 2013-12-02 13:28:49 -0500 | [diff] [blame] | 249 | |
| 250 | # Some components are reporting error status in lower case |
| 251 | # so case sensitive comparisons can really mess things |
| 252 | # up. |
| 253 | if new_status.lower() == error_status.lower(): |
Masayuki Igawa | 2a8a812 | 2014-02-07 11:24:49 +0900 | [diff] [blame] | 254 | message = ("%s failed to get to expected status (%s). " |
| 255 | "In %s state.") % (thing, expected_status, |
| 256 | new_status) |
Masayuki Igawa | a0e786a | 2014-01-27 15:25:06 +0900 | [diff] [blame] | 257 | raise exceptions.BuildErrorException(message, |
| 258 | server_id=thing_id) |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 259 | elif new_status == expected_status and expected_status is not None: |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 260 | return True # All good. |
| 261 | LOG.debug("Waiting for %s to get to %s status. " |
| 262 | "Currently in %s status", |
fujioka yuuichi | 636f8db | 2013-08-09 12:05:24 +0900 | [diff] [blame] | 263 | thing, log_status, new_status) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 264 | if not tempest.test.call_until_true( |
| 265 | check_status, |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 266 | CONF.compute.build_timeout, |
| 267 | CONF.compute.build_interval): |
Ken'ichi Ohmichi | ab1496f | 2013-12-12 22:17:57 +0900 | [diff] [blame] | 268 | message = ("Timed out waiting for thing %s " |
| 269 | "to become %s") % (thing_id, log_status) |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 270 | raise exceptions.TimeoutException(message) |
Matthew Treinish | 0ae79ce | 2013-08-08 14:31:05 -0400 | [diff] [blame] | 271 | |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 272 | def _create_loginable_secgroup_rule_nova(self, client=None, |
| 273 | secgroup_id=None): |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 274 | if client is None: |
| 275 | client = self.compute_client |
| 276 | if secgroup_id is None: |
| 277 | sgs = client.security_groups.list() |
| 278 | for sg in sgs: |
| 279 | if sg.name == 'default': |
| 280 | secgroup_id = sg.id |
| 281 | |
| 282 | # These rules are intended to permit inbound ssh and icmp |
| 283 | # traffic from all sources, so no group_id is provided. |
| 284 | # Setting a group_id would only permit traffic from ports |
| 285 | # belonging to the same security group. |
| 286 | rulesets = [ |
| 287 | { |
| 288 | # ssh |
| 289 | 'ip_protocol': 'tcp', |
| 290 | 'from_port': 22, |
| 291 | 'to_port': 22, |
| 292 | 'cidr': '0.0.0.0/0', |
| 293 | }, |
| 294 | { |
| 295 | # ping |
| 296 | 'ip_protocol': 'icmp', |
| 297 | 'from_port': -1, |
| 298 | 'to_port': -1, |
| 299 | 'cidr': '0.0.0.0/0', |
| 300 | } |
| 301 | ] |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 302 | rules = list() |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 303 | for ruleset in rulesets: |
| 304 | sg_rule = client.security_group_rules.create(secgroup_id, |
| 305 | **ruleset) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 306 | self.addCleanup(self.delete_wrapper, sg_rule) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 307 | rules.append(sg_rule) |
| 308 | return rules |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 309 | |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame] | 310 | def _create_security_group_nova(self, client=None, |
| 311 | namestart='secgroup-smoke-'): |
| 312 | if client is None: |
| 313 | client = self.compute_client |
| 314 | # Create security group |
| 315 | sg_name = data_utils.rand_name(namestart) |
| 316 | sg_desc = sg_name + " description" |
| 317 | secgroup = client.security_groups.create(sg_name, sg_desc) |
| 318 | self.assertEqual(secgroup.name, sg_name) |
| 319 | self.assertEqual(secgroup.description, sg_desc) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 320 | self.addCleanup(self.delete_wrapper, secgroup) |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame] | 321 | |
| 322 | # Add rules to the security group |
| 323 | self._create_loginable_secgroup_rule_nova(client, secgroup.id) |
| 324 | |
| 325 | return secgroup |
| 326 | |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 327 | def create_server(self, client=None, name=None, image=None, flavor=None, |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 328 | wait_on_boot=True, wait_on_delete=True, |
| 329 | create_kwargs={}): |
| 330 | """Creates VM instance. |
| 331 | |
| 332 | @param client: compute client to create the instance |
| 333 | @param image: image from which to create the instance |
| 334 | @param wait_on_boot: wait for status ACTIVE before continue |
| 335 | @param wait_on_delete: force synchronous delete on cleanup |
| 336 | @param create_kwargs: additional details for instance creation |
| 337 | @return: client.server object |
| 338 | """ |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 339 | if client is None: |
| 340 | client = self.compute_client |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 341 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 342 | name = data_utils.rand_name('scenario-server-') |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 343 | if image is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 344 | image = CONF.compute.image_ref |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 345 | if flavor is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 346 | flavor = CONF.compute.flavor_ref |
JordanP | 9c052aa | 2014-01-24 13:05:00 +0000 | [diff] [blame] | 347 | |
| 348 | fixed_network_name = CONF.compute.fixed_network_name |
| 349 | if 'nics' not in create_kwargs and fixed_network_name: |
| 350 | networks = client.networks.list() |
| 351 | # If several networks found, set the NetID on which to connect the |
| 352 | # server to avoid the following error "Multiple possible networks |
| 353 | # found, use a Network ID to be more specific." |
| 354 | # See Tempest #1250866 |
| 355 | if len(networks) > 1: |
| 356 | for network in networks: |
| 357 | if network.label == fixed_network_name: |
| 358 | create_kwargs['nics'] = [{'net-id': network.id}] |
| 359 | break |
| 360 | # If we didn't find the network we were looking for : |
| 361 | else: |
| 362 | msg = ("The network on which the NIC of the server must " |
| 363 | "be connected can not be found : " |
| 364 | "fixed_network_name=%s. Starting instance without " |
| 365 | "specifying a network.") % fixed_network_name |
| 366 | LOG.info(msg) |
| 367 | |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 368 | LOG.debug("Creating a server (name: %s, image: %s, flavor: %s)", |
| 369 | name, image, flavor) |
| 370 | server = client.servers.create(name, image, flavor, **create_kwargs) |
Giulio Fidente | 92f7719 | 2013-08-26 17:13:28 +0200 | [diff] [blame] | 371 | self.assertEqual(server.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 372 | if wait_on_delete: |
| 373 | self.addCleanup(self.delete_timeout, |
| 374 | self.compute_client.servers, |
| 375 | server.id) |
| 376 | self.addCleanup_with_wait(self.compute_client.servers, server.id, |
| 377 | cleanup_callable=self.delete_wrapper, |
| 378 | cleanup_args=[server]) |
| 379 | if wait_on_boot: |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 380 | self.status_timeout(client.servers, server.id, 'ACTIVE') |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 381 | # The instance retrieved on creation is missing network |
| 382 | # details, necessitating retrieval after it becomes active to |
| 383 | # ensure correct details. |
| 384 | server = client.servers.get(server.id) |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 385 | LOG.debug("Created server: %s", server) |
| 386 | return server |
| 387 | |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 388 | def create_volume(self, client=None, size=1, name=None, |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 389 | snapshot_id=None, imageRef=None, volume_type=None, |
| 390 | wait_on_delete=True): |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 391 | if client is None: |
| 392 | client = self.volume_client |
| 393 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 394 | name = data_utils.rand_name('scenario-volume-') |
Eric Windisch | 2d26f1b | 2013-09-04 17:52:16 -0700 | [diff] [blame] | 395 | LOG.debug("Creating a volume (size: %s, name: %s)", size, name) |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 396 | volume = client.volumes.create(size=size, display_name=name, |
| 397 | snapshot_id=snapshot_id, |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 398 | imageRef=imageRef, |
| 399 | volume_type=volume_type) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 400 | if wait_on_delete: |
| 401 | self.addCleanup(self.delete_timeout, |
| 402 | self.volume_client.volumes, |
| 403 | volume.id) |
| 404 | self.addCleanup_with_wait(self.volume_client.volumes, volume.id, |
| 405 | exc_type=cinder_exceptions.NotFound) |
Ken'ichi Ohmichi | 70672df | 2013-08-19 18:35:19 +0900 | [diff] [blame] | 406 | self.assertEqual(name, volume.display_name) |
| 407 | self.status_timeout(client.volumes, volume.id, 'available') |
| 408 | LOG.debug("Created volume: %s", volume) |
| 409 | return volume |
| 410 | |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 411 | def create_server_snapshot(self, server, compute_client=None, |
| 412 | image_client=None, name=None): |
| 413 | if compute_client is None: |
| 414 | compute_client = self.compute_client |
| 415 | if image_client is None: |
| 416 | image_client = self.image_client |
| 417 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 418 | name = data_utils.rand_name('scenario-snapshot-') |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 419 | LOG.debug("Creating a snapshot image for server: %s", server.name) |
| 420 | image_id = compute_client.servers.create_image(server, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 421 | self.addCleanup_with_wait(self.image_client.images, image_id, |
| 422 | exc_type=glanceclient.exc.HTTPNotFound) |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 423 | self.status_timeout(image_client.images, image_id, 'active') |
| 424 | snapshot_image = image_client.images.get(image_id) |
Chang Bo Guo | fc77e93 | 2013-09-16 17:38:26 -0700 | [diff] [blame] | 425 | self.assertEqual(name, snapshot_image.name) |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 426 | LOG.debug("Created snapshot image %s for server %s", |
| 427 | snapshot_image.name, server.name) |
| 428 | return snapshot_image |
| 429 | |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 430 | def create_keypair(self, client=None, name=None): |
| 431 | if client is None: |
| 432 | client = self.compute_client |
| 433 | if name is None: |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 434 | name = data_utils.rand_name('scenario-keypair-') |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 435 | keypair = client.keypairs.create(name) |
| 436 | self.assertEqual(keypair.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 437 | self.addCleanup(self.delete_wrapper, keypair) |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 438 | return keypair |
| 439 | |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 440 | def get_remote_client(self, server_or_ip, username=None, private_key=None): |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 441 | if isinstance(server_or_ip, six.string_types): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 442 | ip = server_or_ip |
| 443 | else: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 444 | network_name_for_ssh = CONF.compute.network_for_ssh |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 445 | ip = server_or_ip.networks[network_name_for_ssh][0] |
| 446 | if username is None: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 447 | username = CONF.scenario.ssh_user |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 448 | if private_key is None: |
| 449 | private_key = self.keypair.private_key |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 450 | linux_client = remote_client.RemoteClient(ip, username, |
| 451 | pkey=private_key) |
| 452 | try: |
| 453 | linux_client.validate_authentication() |
| 454 | except exceptions.SSHTimeout: |
| 455 | LOG.exception('ssh connection to %s failed' % ip) |
| 456 | debug.log_net_debug() |
| 457 | raise |
| 458 | |
| 459 | return linux_client |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 460 | |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 461 | def _log_console_output(self, servers=None): |
| 462 | if not servers: |
| 463 | servers = self.compute_client.servers.list() |
| 464 | for server in servers: |
| 465 | LOG.debug('Console output for %s', server.id) |
| 466 | LOG.debug(server.get_console_output()) |
| 467 | |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 468 | def wait_for_volume_status(self, status): |
| 469 | volume_id = self.volume.id |
| 470 | self.status_timeout( |
| 471 | self.volume_client.volumes, volume_id, status) |
| 472 | |
| 473 | def _image_create(self, name, fmt, path, properties={}): |
| 474 | name = data_utils.rand_name('%s-' % name) |
| 475 | image_file = open(path, 'rb') |
| 476 | self.addCleanup(image_file.close) |
| 477 | params = { |
| 478 | 'name': name, |
| 479 | 'container_format': fmt, |
| 480 | 'disk_format': fmt, |
Aaron Rosen | c772062 | 2014-05-20 10:38:10 -0700 | [diff] [blame] | 481 | 'is_public': 'False', |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 482 | } |
| 483 | params.update(properties) |
| 484 | image = self.image_client.images.create(**params) |
| 485 | self.addCleanup(self.image_client.images.delete, image) |
| 486 | self.assertEqual("queued", image.status) |
| 487 | image.update(data=image_file) |
| 488 | return image.id |
| 489 | |
| 490 | def glance_image_create(self): |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 491 | qcow2_img_path = (CONF.scenario.img_dir + "/" + |
| 492 | CONF.scenario.qcow2_img_file) |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 493 | aki_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.aki_img_file |
| 494 | ari_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ari_img_file |
| 495 | ami_img_path = CONF.scenario.img_dir + "/" + CONF.scenario.ami_img_file |
Masayuki Igawa | 4f71bf0 | 2014-02-21 14:02:29 +0900 | [diff] [blame] | 496 | LOG.debug("paths: img: %s, ami: %s, ari: %s, aki: %s" |
| 497 | % (qcow2_img_path, ami_img_path, ari_img_path, aki_img_path)) |
| 498 | try: |
| 499 | self.image = self._image_create('scenario-img', |
| 500 | 'bare', |
| 501 | qcow2_img_path, |
| 502 | properties={'disk_format': |
| 503 | 'qcow2'}) |
| 504 | except IOError: |
Masayuki Igawa | 188fc00 | 2014-02-23 06:42:44 +0900 | [diff] [blame] | 505 | 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] | 506 | kernel = self._image_create('scenario-aki', 'aki', aki_img_path) |
| 507 | ramdisk = self._image_create('scenario-ari', 'ari', ari_img_path) |
| 508 | properties = { |
| 509 | 'properties': {'kernel_id': kernel, 'ramdisk_id': ramdisk} |
| 510 | } |
| 511 | self.image = self._image_create('scenario-ami', 'ami', |
| 512 | path=ami_img_path, |
| 513 | properties=properties) |
| 514 | LOG.debug("image:%s" % self.image) |
Masayuki Igawa | 5cf3190 | 2014-02-21 17:30:25 +0900 | [diff] [blame] | 515 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 516 | |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 517 | # power/provision states as of icehouse |
| 518 | class BaremetalPowerStates(object): |
| 519 | """Possible power states of an Ironic node.""" |
| 520 | POWER_ON = 'power on' |
| 521 | POWER_OFF = 'power off' |
| 522 | REBOOT = 'rebooting' |
| 523 | SUSPEND = 'suspended' |
| 524 | |
| 525 | |
| 526 | class BaremetalProvisionStates(object): |
| 527 | """Possible provision states of an Ironic node.""" |
| 528 | NOSTATE = None |
| 529 | INIT = 'initializing' |
| 530 | ACTIVE = 'active' |
| 531 | BUILDING = 'building' |
| 532 | DEPLOYWAIT = 'wait call-back' |
| 533 | DEPLOYING = 'deploying' |
| 534 | DEPLOYFAIL = 'deploy failed' |
| 535 | DEPLOYDONE = 'deploy complete' |
| 536 | DELETING = 'deleting' |
| 537 | DELETED = 'deleted' |
| 538 | ERROR = 'error' |
| 539 | |
| 540 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 541 | class BaremetalScenarioTest(OfficialClientTest): |
| 542 | @classmethod |
| 543 | def setUpClass(cls): |
| 544 | super(BaremetalScenarioTest, cls).setUpClass() |
| 545 | |
| 546 | if (not CONF.service_available.ironic or |
| 547 | not CONF.baremetal.driver_enabled): |
| 548 | msg = 'Ironic not available or Ironic compute driver not enabled' |
| 549 | raise cls.skipException(msg) |
| 550 | |
| 551 | # use an admin client manager for baremetal client |
Adam Gandelman | acc13e6 | 2014-05-08 11:12:47 -0700 | [diff] [blame] | 552 | admin_creds = cls.admin_credentials() |
| 553 | manager = clients.OfficialClientManager(credentials=admin_creds) |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 554 | cls.baremetal_client = manager.baremetal_client |
| 555 | |
| 556 | # allow any issues obtaining the node list to raise early |
| 557 | cls.baremetal_client.node.list() |
| 558 | |
| 559 | def _node_state_timeout(self, node_id, state_attr, |
| 560 | target_states, timeout=10, interval=1): |
| 561 | if not isinstance(target_states, list): |
| 562 | target_states = [target_states] |
| 563 | |
| 564 | def check_state(): |
| 565 | node = self.get_node(node_id=node_id) |
| 566 | if getattr(node, state_attr) in target_states: |
| 567 | return True |
| 568 | return False |
| 569 | |
| 570 | if not tempest.test.call_until_true( |
| 571 | check_state, timeout, interval): |
| 572 | msg = ("Timed out waiting for node %s to reach %s state(s) %s" % |
| 573 | (node_id, state_attr, target_states)) |
| 574 | raise exceptions.TimeoutException(msg) |
| 575 | |
| 576 | def wait_provisioning_state(self, node_id, state, timeout): |
| 577 | self._node_state_timeout( |
| 578 | node_id=node_id, state_attr='provision_state', |
| 579 | target_states=state, timeout=timeout) |
| 580 | |
| 581 | def wait_power_state(self, node_id, state): |
| 582 | self._node_state_timeout( |
| 583 | node_id=node_id, state_attr='power_state', |
| 584 | target_states=state, timeout=CONF.baremetal.power_timeout) |
| 585 | |
| 586 | def wait_node(self, instance_id): |
| 587 | """Waits for a node to be associated with instance_id.""" |
Zhi Kun Liu | 4a8d1ea | 2014-04-15 22:08:21 -0500 | [diff] [blame] | 588 | from ironicclient import exc as ironic_exceptions |
| 589 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 590 | def _get_node(): |
| 591 | node = None |
| 592 | try: |
| 593 | node = self.get_node(instance_id=instance_id) |
| 594 | except ironic_exceptions.HTTPNotFound: |
| 595 | pass |
| 596 | return node is not None |
| 597 | |
| 598 | if not tempest.test.call_until_true( |
| 599 | _get_node, CONF.baremetal.association_timeout, 1): |
| 600 | msg = ('Timed out waiting to get Ironic node by instance id %s' |
| 601 | % instance_id) |
| 602 | raise exceptions.TimeoutException(msg) |
| 603 | |
| 604 | def get_node(self, node_id=None, instance_id=None): |
| 605 | if node_id: |
| 606 | return self.baremetal_client.node.get(node_id) |
| 607 | elif instance_id: |
| 608 | return self.baremetal_client.node.get_by_instance_uuid(instance_id) |
| 609 | |
| 610 | def get_ports(self, node_id): |
| 611 | ports = [] |
| 612 | for port in self.baremetal_client.node.list_ports(node_id): |
| 613 | ports.append(self.baremetal_client.port.get(port.uuid)) |
| 614 | return ports |
| 615 | |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 616 | def add_keypair(self): |
| 617 | self.keypair = self.create_keypair() |
| 618 | |
| 619 | def verify_connectivity(self, ip=None): |
| 620 | if ip: |
| 621 | dest = self.get_remote_client(ip) |
| 622 | else: |
| 623 | dest = self.get_remote_client(self.instance) |
| 624 | dest.validate_authentication() |
| 625 | |
| 626 | def boot_instance(self): |
| 627 | create_kwargs = { |
| 628 | 'key_name': self.keypair.id |
| 629 | } |
| 630 | self.instance = self.create_server( |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 631 | wait_on_boot=False, create_kwargs=create_kwargs) |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 632 | |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 633 | self.addCleanup_with_wait(self.compute_client.servers, |
| 634 | self.instance.id, |
| 635 | cleanup_callable=self.delete_wrapper, |
| 636 | cleanup_args=[self.instance]) |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 637 | |
| 638 | self.wait_node(self.instance.id) |
| 639 | self.node = self.get_node(instance_id=self.instance.id) |
| 640 | |
| 641 | self.wait_power_state(self.node.uuid, BaremetalPowerStates.POWER_ON) |
| 642 | |
| 643 | self.wait_provisioning_state( |
| 644 | self.node.uuid, |
| 645 | [BaremetalProvisionStates.DEPLOYWAIT, |
| 646 | BaremetalProvisionStates.ACTIVE], |
| 647 | timeout=15) |
| 648 | |
| 649 | self.wait_provisioning_state(self.node.uuid, |
| 650 | BaremetalProvisionStates.ACTIVE, |
| 651 | timeout=CONF.baremetal.active_timeout) |
| 652 | |
| 653 | self.status_timeout( |
| 654 | self.compute_client.servers, self.instance.id, 'ACTIVE') |
| 655 | |
| 656 | self.node = self.get_node(instance_id=self.instance.id) |
| 657 | self.instance = self.compute_client.servers.get(self.instance.id) |
| 658 | |
| 659 | def terminate_instance(self): |
| 660 | self.instance.delete() |
David Shrewsbury | 06f7f8a | 2014-05-20 13:55:57 -0400 | [diff] [blame] | 661 | self.wait_power_state(self.node.uuid, BaremetalPowerStates.POWER_OFF) |
| 662 | self.wait_provisioning_state( |
| 663 | self.node.uuid, |
| 664 | BaremetalProvisionStates.NOSTATE, |
| 665 | timeout=CONF.baremetal.unprovision_timeout) |
| 666 | |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 667 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 668 | class EncryptionScenarioTest(OfficialClientTest): |
| 669 | """ |
| 670 | Base class for encryption scenario tests |
| 671 | """ |
| 672 | |
| 673 | @classmethod |
| 674 | def setUpClass(cls): |
| 675 | super(EncryptionScenarioTest, cls).setUpClass() |
| 676 | |
| 677 | # use admin credentials to create encrypted volume types |
| 678 | admin_creds = cls.admin_credentials() |
| 679 | manager = clients.OfficialClientManager(credentials=admin_creds) |
| 680 | cls.admin_volume_client = manager.volume_client |
| 681 | |
| 682 | def _wait_for_volume_status(self, status): |
| 683 | self.status_timeout( |
| 684 | self.volume_client.volumes, self.volume.id, status) |
| 685 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 686 | def nova_boot(self): |
| 687 | self.keypair = self.create_keypair() |
| 688 | create_kwargs = {'key_name': self.keypair.name} |
| 689 | self.server = self.create_server(self.compute_client, |
| 690 | image=self.image, |
| 691 | create_kwargs=create_kwargs) |
| 692 | |
| 693 | def create_volume_type(self, client=None, name=None): |
| 694 | if not client: |
| 695 | client = self.admin_volume_client |
| 696 | if not name: |
| 697 | name = 'generic' |
| 698 | randomized_name = data_utils.rand_name('scenario-type-' + name + '-') |
| 699 | LOG.debug("Creating a volume type: %s", randomized_name) |
| 700 | volume_type = client.volume_types.create(randomized_name) |
| 701 | self.addCleanup(client.volume_types.delete, volume_type.id) |
| 702 | return volume_type |
| 703 | |
| 704 | def create_encryption_type(self, client=None, type_id=None, provider=None, |
| 705 | key_size=None, cipher=None, |
| 706 | control_location=None): |
| 707 | if not client: |
| 708 | client = self.admin_volume_client |
| 709 | if not type_id: |
| 710 | volume_type = self.create_volume_type() |
| 711 | type_id = volume_type.id |
| 712 | LOG.debug("Creating an encryption type for volume type: %s", type_id) |
| 713 | client.volume_encryption_types.create(type_id, |
| 714 | {'provider': provider, |
| 715 | 'key_size': key_size, |
| 716 | 'cipher': cipher, |
| 717 | 'control_location': |
| 718 | control_location}) |
| 719 | |
| 720 | def nova_volume_attach(self): |
| 721 | attach_volume_client = self.compute_client.volumes.create_server_volume |
| 722 | volume = attach_volume_client(self.server.id, |
| 723 | self.volume.id, |
| 724 | '/dev/vdb') |
| 725 | self.assertEqual(self.volume.id, volume.id) |
| 726 | self._wait_for_volume_status('in-use') |
| 727 | |
| 728 | def nova_volume_detach(self): |
| 729 | detach_volume_client = self.compute_client.volumes.delete_server_volume |
| 730 | detach_volume_client(self.server.id, self.volume.id) |
| 731 | self._wait_for_volume_status('available') |
| 732 | |
| 733 | volume = self.volume_client.volumes.get(self.volume.id) |
| 734 | self.assertEqual('available', volume.status) |
| 735 | |
Kaitlin Farr | 366a51f | 2014-04-21 12:43:54 -0400 | [diff] [blame] | 736 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 737 | class NetworkScenarioTest(OfficialClientTest): |
| 738 | """ |
| 739 | Base class for network scenario tests |
| 740 | """ |
| 741 | |
| 742 | @classmethod |
| 743 | def check_preconditions(cls): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 744 | if (CONF.service_available.neutron): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 745 | cls.enabled = True |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 746 | # verify that neutron_available is telling the truth |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 747 | try: |
| 748 | cls.network_client.list_networks() |
| 749 | except exc.EndpointNotFound: |
| 750 | cls.enabled = False |
| 751 | raise |
| 752 | else: |
| 753 | cls.enabled = False |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 754 | msg = 'Neutron not available' |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 755 | raise cls.skipException(msg) |
| 756 | |
| 757 | @classmethod |
| 758 | def setUpClass(cls): |
| 759 | super(NetworkScenarioTest, cls).setUpClass() |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 760 | cls.tenant_id = cls.manager.identity_client.tenant_id |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 761 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 762 | def _create_network(self, tenant_id, namestart='network-smoke-'): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 763 | name = data_utils.rand_name(namestart) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 764 | body = dict( |
| 765 | network=dict( |
| 766 | name=name, |
| 767 | tenant_id=tenant_id, |
| 768 | ), |
| 769 | ) |
| 770 | result = self.network_client.create_network(body=body) |
| 771 | network = net_common.DeletableNetwork(client=self.network_client, |
| 772 | **result['network']) |
| 773 | self.assertEqual(network.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 774 | self.addCleanup(self.delete_wrapper, network) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 775 | return network |
| 776 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 777 | def _list_networks(self, **kwargs): |
| 778 | nets = self.network_client.list_networks(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 779 | return nets['networks'] |
| 780 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 781 | def _list_subnets(self, **kwargs): |
| 782 | subnets = self.network_client.list_subnets(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 783 | return subnets['subnets'] |
| 784 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 785 | def _list_routers(self, **kwargs): |
| 786 | routers = self.network_client.list_routers(**kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 787 | return routers['routers'] |
| 788 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 789 | def _list_ports(self, **kwargs): |
| 790 | ports = self.network_client.list_ports(**kwargs) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 791 | return ports['ports'] |
| 792 | |
| 793 | def _get_tenant_own_network_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 794 | nets = self._list_networks(tenant_id=tenant_id) |
| 795 | return len(nets) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 796 | |
| 797 | def _get_tenant_own_subnet_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 798 | subnets = self._list_subnets(tenant_id=tenant_id) |
| 799 | return len(subnets) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 800 | |
| 801 | def _get_tenant_own_port_num(self, tenant_id): |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 802 | ports = self._list_ports(tenant_id=tenant_id) |
| 803 | return len(ports) |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 804 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 805 | def _create_subnet(self, network, namestart='subnet-smoke-', **kwargs): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 806 | """ |
| 807 | Create a subnet for the given network within the cidr block |
| 808 | configured for tenant networks. |
| 809 | """ |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 810 | |
| 811 | def cidr_in_use(cidr, tenant_id): |
| 812 | """ |
| 813 | :return True if subnet with cidr already exist in tenant |
| 814 | False else |
| 815 | """ |
| 816 | cidr_in_use = self._list_subnets(tenant_id=tenant_id, cidr=cidr) |
| 817 | return len(cidr_in_use) != 0 |
| 818 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 819 | tenant_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 820 | result = None |
| 821 | # Repeatedly attempt subnet creation with sequential cidr |
| 822 | # blocks until an unallocated block is found. |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 823 | for subnet_cidr in tenant_cidr.subnet( |
| 824 | CONF.network.tenant_network_mask_bits): |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 825 | str_cidr = str(subnet_cidr) |
| 826 | if cidr_in_use(str_cidr, tenant_id=network.tenant_id): |
| 827 | continue |
| 828 | |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 829 | body = dict( |
| 830 | subnet=dict( |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 831 | name=data_utils.rand_name(namestart), |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 832 | ip_version=4, |
| 833 | network_id=network.id, |
| 834 | tenant_id=network.tenant_id, |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 835 | cidr=str_cidr, |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 836 | ), |
| 837 | ) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 838 | body['subnet'].update(kwargs) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 839 | try: |
| 840 | result = self.network_client.create_subnet(body=body) |
| 841 | break |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 842 | except exc.NeutronClientException as e: |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 843 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 844 | if not is_overlapping_cidr: |
| 845 | raise |
| 846 | self.assertIsNotNone(result, 'Unable to allocate tenant network') |
| 847 | subnet = net_common.DeletableSubnet(client=self.network_client, |
| 848 | **result['subnet']) |
Attila Fazekas | e857bd6 | 2013-10-21 21:02:44 +0200 | [diff] [blame] | 849 | self.assertEqual(subnet.cidr, str_cidr) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 850 | self.addCleanup(self.delete_wrapper, subnet) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 851 | return subnet |
| 852 | |
| 853 | def _create_port(self, network, namestart='port-quotatest-'): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 854 | name = data_utils.rand_name(namestart) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 855 | body = dict( |
| 856 | port=dict(name=name, |
| 857 | network_id=network.id, |
| 858 | tenant_id=network.tenant_id)) |
| 859 | result = self.network_client.create_port(body=body) |
| 860 | self.assertIsNotNone(result, 'Unable to allocate port') |
| 861 | port = net_common.DeletablePort(client=self.network_client, |
| 862 | **result['port']) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 863 | self.addCleanup(self.delete_wrapper, port) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 864 | return port |
| 865 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 866 | def _get_server_port_id(self, server, ip_addr=None): |
| 867 | ports = self._list_ports(device_id=server.id, fixed_ip=ip_addr) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 868 | self.assertEqual(len(ports), 1, |
| 869 | "Unable to determine which port to target.") |
Yair Fried | 05db252 | 2013-11-18 11:02:10 +0200 | [diff] [blame] | 870 | return ports[0]['id'] |
| 871 | |
Yair Fried | a2e3b2c | 2014-02-17 10:56:10 +0200 | [diff] [blame] | 872 | def _create_floating_ip(self, thing, external_network_id, port_id=None): |
| 873 | if not port_id: |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 874 | port_id = self._get_server_port_id(thing) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 875 | body = dict( |
| 876 | floatingip=dict( |
| 877 | floating_network_id=external_network_id, |
| 878 | port_id=port_id, |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 879 | tenant_id=thing.tenant_id, |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 880 | ) |
| 881 | ) |
| 882 | result = self.network_client.create_floatingip(body=body) |
| 883 | floating_ip = net_common.DeletableFloatingIp( |
| 884 | client=self.network_client, |
| 885 | **result['floatingip']) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 886 | self.addCleanup(self.delete_wrapper, floating_ip) |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 887 | return floating_ip |
| 888 | |
Yair Fried | 05db252 | 2013-11-18 11:02:10 +0200 | [diff] [blame] | 889 | def _associate_floating_ip(self, floating_ip, server): |
| 890 | port_id = self._get_server_port_id(server) |
| 891 | floating_ip.update(port_id=port_id) |
| 892 | self.assertEqual(port_id, floating_ip.port_id) |
| 893 | return floating_ip |
| 894 | |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 895 | def _disassociate_floating_ip(self, floating_ip): |
| 896 | """ |
| 897 | :param floating_ip: type DeletableFloatingIp |
| 898 | """ |
| 899 | floating_ip.update(port_id=None) |
llg8212 | e4cd392 | 2014-02-15 12:14:21 +0800 | [diff] [blame] | 900 | self.assertIsNone(floating_ip.port_id) |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 901 | return floating_ip |
| 902 | |
| 903 | def _ping_ip_address(self, ip_address, should_succeed=True): |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 904 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 905 | |
| 906 | def ping(): |
| 907 | proc = subprocess.Popen(cmd, |
| 908 | stdout=subprocess.PIPE, |
| 909 | stderr=subprocess.PIPE) |
| 910 | proc.wait() |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 911 | return (proc.returncode == 0) == should_succeed |
Sean Dague | 6dbc6da | 2013-05-08 17:49:46 -0400 | [diff] [blame] | 912 | |
Nachi Ueno | 6d580be | 2013-07-24 10:58:11 -0700 | [diff] [blame] | 913 | return tempest.test.call_until_true( |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 914 | ping, CONF.compute.ping_timeout, 1) |
Maru Newby | af292e8 | 2013-05-20 21:32:28 +0000 | [diff] [blame] | 915 | |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 916 | def _create_pool(self, lb_method, protocol, subnet_id): |
| 917 | """Wrapper utility that returns a test pool.""" |
| 918 | name = data_utils.rand_name('pool-') |
| 919 | body = { |
| 920 | "pool": { |
| 921 | "protocol": protocol, |
| 922 | "name": name, |
| 923 | "subnet_id": subnet_id, |
| 924 | "lb_method": lb_method |
| 925 | } |
| 926 | } |
| 927 | resp = self.network_client.create_pool(body=body) |
| 928 | pool = net_common.DeletablePool(client=self.network_client, |
| 929 | **resp['pool']) |
| 930 | self.assertEqual(pool['name'], name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 931 | self.addCleanup(self.delete_wrapper, pool) |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 932 | return pool |
| 933 | |
| 934 | def _create_member(self, address, protocol_port, pool_id): |
| 935 | """Wrapper utility that returns a test member.""" |
| 936 | body = { |
| 937 | "member": { |
| 938 | "protocol_port": protocol_port, |
| 939 | "pool_id": pool_id, |
| 940 | "address": address |
| 941 | } |
| 942 | } |
| 943 | resp = self.network_client.create_member(body) |
| 944 | member = net_common.DeletableMember(client=self.network_client, |
| 945 | **resp['member']) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 946 | self.addCleanup(self.delete_wrapper, member) |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 947 | return member |
| 948 | |
| 949 | def _create_vip(self, protocol, protocol_port, subnet_id, pool_id): |
| 950 | """Wrapper utility that returns a test vip.""" |
| 951 | name = data_utils.rand_name('vip-') |
| 952 | body = { |
| 953 | "vip": { |
| 954 | "protocol": protocol, |
| 955 | "name": name, |
| 956 | "subnet_id": subnet_id, |
| 957 | "pool_id": pool_id, |
| 958 | "protocol_port": protocol_port |
| 959 | } |
| 960 | } |
| 961 | resp = self.network_client.create_vip(body) |
| 962 | vip = net_common.DeletableVip(client=self.network_client, |
| 963 | **resp['vip']) |
| 964 | self.assertEqual(vip['name'], name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 965 | self.addCleanup(self.delete_wrapper, vip) |
Elena Ezhova | a5105e6 | 2013-11-26 20:46:52 +0400 | [diff] [blame] | 966 | return vip |
| 967 | |
Yair Fried | 9a551c4 | 2013-12-15 14:59:34 +0200 | [diff] [blame] | 968 | def _check_vm_connectivity(self, ip_address, |
| 969 | username=None, |
| 970 | private_key=None, |
| 971 | should_connect=True): |
| 972 | """ |
| 973 | :param ip_address: server to test against |
| 974 | :param username: server's ssh username |
| 975 | :param private_key: server's ssh private key to be used |
| 976 | :param should_connect: True/False indicates positive/negative test |
| 977 | positive - attempt ping and ssh |
| 978 | negative - attempt ping and fail if succeed |
| 979 | |
| 980 | :raises: AssertError if the result of the connectivity check does |
| 981 | not match the value of the should_connect param |
| 982 | """ |
| 983 | if should_connect: |
| 984 | msg = "Timed out waiting for %s to become reachable" % ip_address |
| 985 | else: |
| 986 | msg = "ip address %s is reachable" % ip_address |
| 987 | self.assertTrue(self._ping_ip_address(ip_address, |
| 988 | should_succeed=should_connect), |
| 989 | msg=msg) |
| 990 | if should_connect: |
| 991 | # no need to check ssh for negative connectivity |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 992 | self.get_remote_client(ip_address, username, private_key) |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 993 | |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 994 | def _check_public_network_connectivity(self, ip_address, username, |
| 995 | private_key, should_connect=True, |
| 996 | msg=None, servers=None): |
| 997 | # The target login is assumed to have been configured for |
| 998 | # key-based authentication by cloud-init. |
| 999 | LOG.debug('checking network connections to IP %s with user: %s' % |
| 1000 | (ip_address, username)) |
| 1001 | try: |
| 1002 | self._check_vm_connectivity(ip_address, |
| 1003 | username, |
| 1004 | private_key, |
| 1005 | should_connect=should_connect) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1006 | except Exception as e: |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 1007 | ex_msg = 'Public network connectivity check failed' |
| 1008 | if msg: |
| 1009 | ex_msg += ": " + msg |
| 1010 | LOG.exception(ex_msg) |
| 1011 | self._log_console_output(servers) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1012 | # network debug is called as part of ssh init |
| 1013 | if not isinstance(e, exceptions.SSHTimeout): |
| 1014 | debug.log_net_debug() |
Matt Riedemann | 343305f | 2014-05-27 09:55:03 -0700 | [diff] [blame] | 1015 | raise |
| 1016 | |
Matt Riedemann | 2d005be | 2014-05-27 10:52:35 -0700 | [diff] [blame] | 1017 | def _check_tenant_network_connectivity(self, server, |
| 1018 | username, |
| 1019 | private_key, |
| 1020 | should_connect=True, |
| 1021 | servers_for_debug=None): |
| 1022 | if not CONF.network.tenant_networks_reachable: |
| 1023 | msg = 'Tenant networks not configured to be reachable.' |
| 1024 | LOG.info(msg) |
| 1025 | return |
| 1026 | # The target login is assumed to have been configured for |
| 1027 | # key-based authentication by cloud-init. |
| 1028 | try: |
| 1029 | for net_name, ip_addresses in server.networks.iteritems(): |
| 1030 | for ip_address in ip_addresses: |
| 1031 | self._check_vm_connectivity(ip_address, |
| 1032 | username, |
| 1033 | private_key, |
| 1034 | should_connect=should_connect) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1035 | except Exception as e: |
Matt Riedemann | 2d005be | 2014-05-27 10:52:35 -0700 | [diff] [blame] | 1036 | LOG.exception('Tenant network connectivity check failed') |
| 1037 | self._log_console_output(servers_for_debug) |
Yair Fried | 3960c4d | 2014-05-07 15:20:30 +0300 | [diff] [blame] | 1038 | # network debug is called as part of ssh init |
| 1039 | if not isinstance(e, exceptions.SSHTimeout): |
| 1040 | debug.log_net_debug() |
Matt Riedemann | 2d005be | 2014-05-27 10:52:35 -0700 | [diff] [blame] | 1041 | raise |
| 1042 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 1043 | def _check_remote_connectivity(self, source, dest, should_succeed=True): |
| 1044 | """ |
| 1045 | check ping server via source ssh connection |
| 1046 | |
| 1047 | :param source: RemoteClient: an ssh connection from which to ping |
| 1048 | :param dest: and IP to ping against |
| 1049 | :param should_succeed: boolean should ping succeed or not |
| 1050 | :returns: boolean -- should_succeed == ping |
| 1051 | :returns: ping is false if ping failed |
| 1052 | """ |
| 1053 | def ping_remote(): |
| 1054 | try: |
| 1055 | source.ping_host(dest) |
| 1056 | except exceptions.SSHExecCommandFailed: |
| 1057 | LOG.exception('Failed to ping host via ssh connection') |
| 1058 | return not should_succeed |
| 1059 | return should_succeed |
| 1060 | |
| 1061 | return tempest.test.call_until_true(ping_remote, |
| 1062 | CONF.compute.ping_timeout, |
| 1063 | 1) |
| 1064 | |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1065 | def _create_security_group_neutron(self, tenant_id, client=None, |
| 1066 | namestart='secgroup-smoke-'): |
| 1067 | if client is None: |
| 1068 | client = self.network_client |
| 1069 | secgroup = self._create_empty_security_group(namestart=namestart, |
| 1070 | client=client, |
| 1071 | tenant_id=tenant_id) |
| 1072 | |
| 1073 | # Add rules to the security group |
| 1074 | rules = self._create_loginable_secgroup_rule_neutron(secgroup=secgroup) |
| 1075 | for rule in rules: |
| 1076 | self.assertEqual(tenant_id, rule.tenant_id) |
| 1077 | self.assertEqual(secgroup.id, rule.security_group_id) |
| 1078 | return secgroup |
| 1079 | |
| 1080 | def _create_empty_security_group(self, tenant_id, client=None, |
| 1081 | namestart='secgroup-smoke-'): |
| 1082 | """Create a security group without rules. |
| 1083 | |
| 1084 | Default rules will be created: |
| 1085 | - IPv4 egress to any |
| 1086 | - IPv6 egress to any |
| 1087 | |
| 1088 | :param tenant_id: secgroup will be created in this tenant |
| 1089 | :returns: DeletableSecurityGroup -- containing the secgroup created |
| 1090 | """ |
| 1091 | if client is None: |
| 1092 | client = self.network_client |
| 1093 | sg_name = data_utils.rand_name(namestart) |
| 1094 | sg_desc = sg_name + " description" |
| 1095 | sg_dict = dict(name=sg_name, |
| 1096 | description=sg_desc) |
| 1097 | sg_dict['tenant_id'] = tenant_id |
| 1098 | body = dict(security_group=sg_dict) |
| 1099 | result = client.create_security_group(body=body) |
| 1100 | secgroup = net_common.DeletableSecurityGroup( |
| 1101 | client=client, |
| 1102 | **result['security_group'] |
| 1103 | ) |
| 1104 | self.assertEqual(secgroup.name, sg_name) |
| 1105 | self.assertEqual(tenant_id, secgroup.tenant_id) |
| 1106 | self.assertEqual(secgroup.description, sg_desc) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1107 | self.addCleanup(self.delete_wrapper, secgroup) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1108 | return secgroup |
| 1109 | |
| 1110 | def _default_security_group(self, tenant_id, client=None): |
| 1111 | """Get default secgroup for given tenant_id. |
| 1112 | |
| 1113 | :returns: DeletableSecurityGroup -- default secgroup for given tenant |
| 1114 | """ |
| 1115 | if client is None: |
| 1116 | client = self.network_client |
| 1117 | sgs = [ |
| 1118 | sg for sg in client.list_security_groups().values()[0] |
| 1119 | if sg['tenant_id'] == tenant_id and sg['name'] == 'default' |
| 1120 | ] |
| 1121 | msg = "No default security group for tenant %s." % (tenant_id) |
| 1122 | self.assertTrue(len(sgs) > 0, msg) |
| 1123 | if len(sgs) > 1: |
| 1124 | msg = "Found %d default security groups" % len(sgs) |
| 1125 | raise exc.NeutronClientNoUniqueMatch(msg=msg) |
| 1126 | return net_common.DeletableSecurityGroup(client=client, |
| 1127 | **sgs[0]) |
| 1128 | |
| 1129 | def _create_security_group_rule(self, client=None, secgroup=None, |
| 1130 | tenant_id=None, **kwargs): |
| 1131 | """Create a rule from a dictionary of rule parameters. |
| 1132 | |
| 1133 | Create a rule in a secgroup. if secgroup not defined will search for |
| 1134 | default secgroup in tenant_id. |
| 1135 | |
| 1136 | :param secgroup: type DeletableSecurityGroup. |
| 1137 | :param secgroup_id: search for secgroup by id |
| 1138 | default -- choose default secgroup for given tenant_id |
| 1139 | :param tenant_id: if secgroup not passed -- the tenant in which to |
| 1140 | search for default secgroup |
| 1141 | :param kwargs: a dictionary containing rule parameters: |
| 1142 | for example, to allow incoming ssh: |
| 1143 | rule = { |
| 1144 | direction: 'ingress' |
| 1145 | protocol:'tcp', |
| 1146 | port_range_min: 22, |
| 1147 | port_range_max: 22 |
| 1148 | } |
| 1149 | """ |
| 1150 | if client is None: |
| 1151 | client = self.network_client |
| 1152 | if secgroup is None: |
| 1153 | secgroup = self._default_security_group(tenant_id) |
| 1154 | |
| 1155 | ruleset = dict(security_group_id=secgroup.id, |
| 1156 | tenant_id=secgroup.tenant_id, |
| 1157 | ) |
| 1158 | ruleset.update(kwargs) |
| 1159 | |
| 1160 | body = dict(security_group_rule=dict(ruleset)) |
| 1161 | sg_rule = client.create_security_group_rule(body=body) |
| 1162 | sg_rule = net_common.DeletableSecurityGroupRule( |
| 1163 | client=client, |
| 1164 | **sg_rule['security_group_rule'] |
| 1165 | ) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1166 | self.addCleanup(self.delete_wrapper, sg_rule) |
Yair Fried | eb69f3f | 2013-10-10 13:18:16 +0300 | [diff] [blame] | 1167 | self.assertEqual(secgroup.tenant_id, sg_rule.tenant_id) |
| 1168 | self.assertEqual(secgroup.id, sg_rule.security_group_id) |
| 1169 | |
| 1170 | return sg_rule |
| 1171 | |
| 1172 | def _create_loginable_secgroup_rule_neutron(self, client=None, |
| 1173 | secgroup=None): |
| 1174 | """These rules are intended to permit inbound ssh and icmp |
| 1175 | traffic from all sources, so no group_id is provided. |
| 1176 | Setting a group_id would only permit traffic from ports |
| 1177 | belonging to the same security group. |
| 1178 | """ |
| 1179 | |
| 1180 | if client is None: |
| 1181 | client = self.network_client |
| 1182 | rules = [] |
| 1183 | rulesets = [ |
| 1184 | dict( |
| 1185 | # ssh |
| 1186 | protocol='tcp', |
| 1187 | port_range_min=22, |
| 1188 | port_range_max=22, |
| 1189 | ), |
| 1190 | dict( |
| 1191 | # ping |
| 1192 | protocol='icmp', |
| 1193 | ) |
| 1194 | ] |
| 1195 | for ruleset in rulesets: |
| 1196 | for r_direction in ['ingress', 'egress']: |
| 1197 | ruleset['direction'] = r_direction |
| 1198 | try: |
| 1199 | sg_rule = self._create_security_group_rule( |
| 1200 | client=client, secgroup=secgroup, **ruleset) |
| 1201 | except exc.NeutronClientException as ex: |
| 1202 | # if rule already exist - skip rule and continue |
| 1203 | if not (ex.status_code is 409 and 'Security group rule' |
| 1204 | ' already exists' in ex.message): |
| 1205 | raise ex |
| 1206 | else: |
| 1207 | self.assertEqual(r_direction, sg_rule.direction) |
| 1208 | rules.append(sg_rule) |
| 1209 | |
| 1210 | return rules |
| 1211 | |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 1212 | def _ssh_to_server(self, server, private_key): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1213 | ssh_login = CONF.compute.image_ssh_user |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 1214 | return self.get_remote_client(server, |
| 1215 | username=ssh_login, |
| 1216 | private_key=private_key) |
| 1217 | |
Yuiko Takada | 7f4b1b3 | 2013-11-20 08:06:26 +0000 | [diff] [blame] | 1218 | def _show_quota_network(self, tenant_id): |
| 1219 | quota = self.network_client.show_quota(tenant_id) |
| 1220 | return quota['quota']['network'] |
| 1221 | |
| 1222 | def _show_quota_subnet(self, tenant_id): |
| 1223 | quota = self.network_client.show_quota(tenant_id) |
| 1224 | return quota['quota']['subnet'] |
| 1225 | |
| 1226 | def _show_quota_port(self, tenant_id): |
| 1227 | quota = self.network_client.show_quota(tenant_id) |
| 1228 | return quota['quota']['port'] |
| 1229 | |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 1230 | def _get_router(self, tenant_id): |
| 1231 | """Retrieve a router for the given tenant id. |
| 1232 | |
| 1233 | If a public router has been configured, it will be returned. |
| 1234 | |
| 1235 | If a public router has not been configured, but a public |
| 1236 | network has, a tenant router will be created and returned that |
| 1237 | routes traffic to the public network. |
| 1238 | """ |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1239 | router_id = CONF.network.public_router_id |
| 1240 | network_id = CONF.network.public_network_id |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 1241 | if router_id: |
| 1242 | result = self.network_client.show_router(router_id) |
| 1243 | return net_common.AttributeDict(**result['router']) |
| 1244 | elif network_id: |
| 1245 | router = self._create_router(tenant_id) |
| 1246 | router.add_gateway(network_id) |
| 1247 | return router |
| 1248 | else: |
| 1249 | raise Exception("Neither of 'public_router_id' or " |
| 1250 | "'public_network_id' has been defined.") |
| 1251 | |
| 1252 | def _create_router(self, tenant_id, namestart='router-smoke-'): |
| 1253 | name = data_utils.rand_name(namestart) |
| 1254 | body = dict( |
| 1255 | router=dict( |
| 1256 | name=name, |
| 1257 | admin_state_up=True, |
| 1258 | tenant_id=tenant_id, |
| 1259 | ), |
| 1260 | ) |
| 1261 | result = self.network_client.create_router(body=body) |
| 1262 | router = net_common.DeletableRouter(client=self.network_client, |
| 1263 | **result['router']) |
| 1264 | self.assertEqual(router.name, name) |
Matthew Treinish | b7144eb | 2013-12-13 22:57:35 +0000 | [diff] [blame] | 1265 | self.addCleanup(self.delete_wrapper, router) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 1266 | return router |
| 1267 | |
| 1268 | def _create_networks(self, tenant_id=None): |
| 1269 | """Create a network with a subnet connected to a router. |
| 1270 | |
| 1271 | :returns: network, subnet, router |
| 1272 | """ |
| 1273 | if tenant_id is None: |
| 1274 | tenant_id = self.tenant_id |
| 1275 | network = self._create_network(tenant_id) |
| 1276 | router = self._get_router(tenant_id) |
| 1277 | subnet = self._create_subnet(network) |
| 1278 | subnet.add_to_router(router.id) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 1279 | return network, subnet, router |
| 1280 | |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1281 | |
| 1282 | class OrchestrationScenarioTest(OfficialClientTest): |
| 1283 | """ |
| 1284 | Base class for orchestration scenario tests |
| 1285 | """ |
| 1286 | |
| 1287 | @classmethod |
Matt Riedemann | 11c5b64 | 2013-08-24 08:45:38 -0700 | [diff] [blame] | 1288 | def setUpClass(cls): |
| 1289 | super(OrchestrationScenarioTest, cls).setUpClass() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1290 | if not CONF.service_available.heat: |
Matt Riedemann | 11c5b64 | 2013-08-24 08:45:38 -0700 | [diff] [blame] | 1291 | raise cls.skipException("Heat support is required") |
| 1292 | |
| 1293 | @classmethod |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1294 | def credentials(cls): |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 1295 | admin_creds = auth.get_default_credentials('identity_admin') |
| 1296 | creds = auth.get_default_credentials('user') |
| 1297 | admin_creds.tenant_name = creds.tenant_name |
| 1298 | return admin_creds |
Steve Baker | dd7c6ce | 2013-06-24 14:46:47 +1200 | [diff] [blame] | 1299 | |
| 1300 | def _load_template(self, base_file, file_name): |
| 1301 | filepath = os.path.join(os.path.dirname(os.path.realpath(base_file)), |
| 1302 | file_name) |
| 1303 | with open(filepath) as f: |
| 1304 | return f.read() |
| 1305 | |
| 1306 | @classmethod |
| 1307 | def _stack_rand_name(cls): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 1308 | return data_utils.rand_name(cls.__name__ + '-') |
Steve Baker | 80252da | 2013-09-25 13:29:10 +1200 | [diff] [blame] | 1309 | |
| 1310 | @classmethod |
| 1311 | def _get_default_network(cls): |
| 1312 | networks = cls.network_client.list_networks() |
| 1313 | for net in networks['networks']: |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 1314 | if net['name'] == CONF.compute.fixed_network_name: |
Steve Baker | 80252da | 2013-09-25 13:29:10 +1200 | [diff] [blame] | 1315 | return net |
Steve Baker | 22c1660 | 2014-05-05 13:34:19 +1200 | [diff] [blame] | 1316 | |
| 1317 | @staticmethod |
| 1318 | def _stack_output(stack, output_key): |
| 1319 | """Return a stack output value for a given key.""" |
| 1320 | return next((o['output_value'] for o in stack.outputs |
| 1321 | if o['output_key'] == output_key), None) |
| 1322 | |
| 1323 | def _ping_ip_address(self, ip_address, should_succeed=True): |
| 1324 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 1325 | |
| 1326 | def ping(): |
| 1327 | proc = subprocess.Popen(cmd, |
| 1328 | stdout=subprocess.PIPE, |
| 1329 | stderr=subprocess.PIPE) |
| 1330 | proc.wait() |
| 1331 | return (proc.returncode == 0) == should_succeed |
| 1332 | |
| 1333 | return tempest.test.call_until_true( |
| 1334 | ping, CONF.orchestration.build_timeout, 1) |
| 1335 | |
| 1336 | def _wait_for_resource_status(self, stack_identifier, resource_name, |
| 1337 | status, failure_pattern='^.*_FAILED$'): |
| 1338 | """Waits for a Resource to reach a given status.""" |
| 1339 | fail_regexp = re.compile(failure_pattern) |
| 1340 | build_timeout = CONF.orchestration.build_timeout |
| 1341 | build_interval = CONF.orchestration.build_interval |
| 1342 | |
| 1343 | start = timeutils.utcnow() |
| 1344 | while timeutils.delta_seconds(start, |
| 1345 | timeutils.utcnow()) < build_timeout: |
| 1346 | try: |
| 1347 | res = self.client.resources.get( |
| 1348 | stack_identifier, resource_name) |
| 1349 | except heat_exceptions.HTTPNotFound: |
| 1350 | # ignore this, as the resource may not have |
| 1351 | # been created yet |
| 1352 | pass |
| 1353 | else: |
| 1354 | if res.resource_status == status: |
| 1355 | return |
| 1356 | if fail_regexp.search(res.resource_status): |
| 1357 | raise exceptions.StackResourceBuildErrorException( |
| 1358 | resource_name=res.resource_name, |
| 1359 | stack_identifier=stack_identifier, |
| 1360 | resource_status=res.resource_status, |
| 1361 | resource_status_reason=res.resource_status_reason) |
| 1362 | time.sleep(build_interval) |
| 1363 | |
| 1364 | message = ('Resource %s failed to reach %s status within ' |
| 1365 | 'the required time (%s s).' % |
| 1366 | (res.resource_name, status, build_timeout)) |
| 1367 | raise exceptions.TimeoutException(message) |
| 1368 | |
| 1369 | def _wait_for_stack_status(self, stack_identifier, status, |
| 1370 | failure_pattern='^.*_FAILED$'): |
| 1371 | """ |
| 1372 | Waits for a Stack to reach a given status. |
| 1373 | |
| 1374 | Note this compares the full $action_$status, e.g |
| 1375 | CREATE_COMPLETE, not just COMPLETE which is exposed |
| 1376 | via the status property of Stack in heatclient |
| 1377 | """ |
| 1378 | fail_regexp = re.compile(failure_pattern) |
| 1379 | build_timeout = CONF.orchestration.build_timeout |
| 1380 | build_interval = CONF.orchestration.build_interval |
| 1381 | |
| 1382 | start = timeutils.utcnow() |
| 1383 | while timeutils.delta_seconds(start, |
| 1384 | timeutils.utcnow()) < build_timeout: |
| 1385 | try: |
| 1386 | stack = self.client.stacks.get(stack_identifier) |
| 1387 | except heat_exceptions.HTTPNotFound: |
| 1388 | # ignore this, as the stackource may not have |
| 1389 | # been created yet |
| 1390 | pass |
| 1391 | else: |
| 1392 | if stack.stack_status == status: |
| 1393 | return |
| 1394 | if fail_regexp.search(stack.stack_status): |
| 1395 | raise exceptions.StackBuildErrorException( |
| 1396 | stack_identifier=stack_identifier, |
| 1397 | stack_status=stack.stack_status, |
| 1398 | stack_status_reason=stack.stack_status_reason) |
| 1399 | time.sleep(build_interval) |
| 1400 | |
| 1401 | message = ('Stack %s failed to reach %s status within ' |
| 1402 | 'the required time (%s s).' % |
| 1403 | (stack.stack_name, status, build_timeout)) |
| 1404 | raise exceptions.TimeoutException(message) |
| 1405 | |
| 1406 | def _stack_delete(self, stack_identifier): |
| 1407 | try: |
| 1408 | self.client.stacks.delete(stack_identifier) |
| 1409 | except heat_exceptions.HTTPNotFound: |
| 1410 | pass |