| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 1 | # Copyright 2014 Mirantis Inc. | 
|  | 2 | # All Rights Reserved. | 
|  | 3 | # | 
|  | 4 | #    Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 5 | #    not use this file except in compliance with the License. You may obtain | 
|  | 6 | #    a copy of the License at | 
|  | 7 | # | 
|  | 8 | #         http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | # | 
|  | 10 | #    Unless required by applicable law or agreed to in writing, software | 
|  | 11 | #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 12 | #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 
|  | 13 | #    License for the specific language governing permissions and limitations | 
|  | 14 | #    under the License. | 
|  | 15 |  | 
|  | 16 | import copy | 
|  | 17 | import inspect | 
| Valeriy Ponomaryov | 2abf5d7 | 2016-06-01 18:30:12 +0300 | [diff] [blame] | 18 | import re | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 19 | import traceback | 
|  | 20 |  | 
|  | 21 | from oslo_concurrency import lockutils | 
|  | 22 | from oslo_log import log | 
|  | 23 | import six | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 24 | from tempest import clients | 
| Sam Wan | c7b7f1f | 2015-11-25 00:22:28 -0500 | [diff] [blame] | 25 | from tempest.common import credentials_factory as common_creds | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 26 | from tempest.common import dynamic_creds | 
|  | 27 | from tempest import config | 
| Ben Swartzlander | 1c4ff52 | 2016-03-02 22:16:23 -0500 | [diff] [blame] | 28 | from tempest.lib.common.utils import data_utils | 
|  | 29 | from tempest.lib import exceptions | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 30 | from tempest import test | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 31 |  | 
| Yogesh | bdb8810 | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 32 | from manila_tempest_tests.common import constants | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 33 | from manila_tempest_tests.services.share.json import shares_client | 
|  | 34 | from manila_tempest_tests.services.share.v2.json import ( | 
|  | 35 | shares_client as shares_v2_client) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 36 | from manila_tempest_tests import share_exceptions | 
| Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 37 | from manila_tempest_tests import utils | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 38 |  | 
|  | 39 | CONF = config.CONF | 
|  | 40 | LOG = log.getLogger(__name__) | 
|  | 41 |  | 
| Valeriy Ponomaryov | 2abf5d7 | 2016-06-01 18:30:12 +0300 | [diff] [blame] | 42 | # Test tags related to test direction | 
|  | 43 | TAG_POSITIVE = "positive" | 
|  | 44 | TAG_NEGATIVE = "negative" | 
|  | 45 |  | 
|  | 46 | # Test tags related to service involvement | 
|  | 47 | TAG_API = "api" | 
|  | 48 | TAG_BACKEND = "backend" | 
|  | 49 | TAG_API_WITH_BACKEND = "api_with_backend" | 
|  | 50 |  | 
|  | 51 | TAGS_MAPPER = { | 
|  | 52 | "p": TAG_POSITIVE, | 
|  | 53 | "n": TAG_NEGATIVE, | 
|  | 54 | "a": TAG_API, | 
|  | 55 | "b": TAG_BACKEND, | 
|  | 56 | "ab": TAG_API_WITH_BACKEND, | 
|  | 57 | } | 
|  | 58 | TAGS_PATTERN = re.compile( | 
|  | 59 | r"(?=.*\[.*\b(%(p)s|%(n)s)\b.*\])(?=.*\[.*\b(%(a)s|%(b)s|%(ab)s)\b.*\])" % | 
|  | 60 | TAGS_MAPPER) | 
|  | 61 |  | 
|  | 62 |  | 
|  | 63 | def verify_test_has_appropriate_tags(self): | 
|  | 64 | if not TAGS_PATTERN.match(self.id()): | 
|  | 65 | msg = ( | 
|  | 66 | "Required attributes either not set or set improperly. " | 
|  | 67 | "Two test attributes are expected:\n" | 
|  | 68 | " - one of '%(p)s' or '%(n)s' and \n" | 
|  | 69 | " - one of '%(a)s', '%(b)s' or '%(ab)s'." | 
|  | 70 | ) % TAGS_MAPPER | 
|  | 71 | raise self.failureException(msg) | 
|  | 72 |  | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 73 |  | 
|  | 74 | class handle_cleanup_exceptions(object): | 
|  | 75 | """Handle exceptions raised with cleanup operations. | 
|  | 76 |  | 
|  | 77 | Always suppress errors when exceptions.NotFound or exceptions.Forbidden | 
|  | 78 | are raised. | 
|  | 79 | Suppress all other exceptions only in case config opt | 
|  | 80 | 'suppress_errors_in_cleanup' in config group 'share' is True. | 
|  | 81 | """ | 
|  | 82 |  | 
|  | 83 | def __enter__(self): | 
|  | 84 | return self | 
|  | 85 |  | 
|  | 86 | def __exit__(self, exc_type, exc_value, exc_traceback): | 
|  | 87 | if not (isinstance(exc_value, | 
|  | 88 | (exceptions.NotFound, exceptions.Forbidden)) or | 
|  | 89 | CONF.share.suppress_errors_in_cleanup): | 
|  | 90 | return False  # Do not suppress error if any | 
|  | 91 | if exc_traceback: | 
|  | 92 | LOG.error("Suppressed cleanup error in Manila: " | 
|  | 93 | "\n%s" % traceback.format_exc()) | 
|  | 94 | return True  # Suppress error if any | 
|  | 95 |  | 
|  | 96 |  | 
|  | 97 | def network_synchronized(f): | 
|  | 98 |  | 
|  | 99 | def wrapped_func(self, *args, **kwargs): | 
|  | 100 | with_isolated_creds = True if len(args) > 2 else False | 
|  | 101 | no_lock_required = kwargs.get( | 
|  | 102 | "isolated_creds_client", with_isolated_creds) | 
|  | 103 | if no_lock_required: | 
|  | 104 | # Usage of not reusable network. No need in lock. | 
|  | 105 | return f(self, *args, **kwargs) | 
|  | 106 |  | 
|  | 107 | # Use lock assuming reusage of common network. | 
|  | 108 | @lockutils.synchronized("manila_network_lock", external=True) | 
|  | 109 | def source_func(self, *args, **kwargs): | 
|  | 110 | return f(self, *args, **kwargs) | 
|  | 111 |  | 
|  | 112 | return source_func(self, *args, **kwargs) | 
|  | 113 |  | 
|  | 114 | return wrapped_func | 
|  | 115 |  | 
|  | 116 |  | 
| Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 117 | skip_if_microversion_not_supported = utils.skip_if_microversion_not_supported | 
| Xing Yang | 69b00b5 | 2015-11-22 16:10:44 -0500 | [diff] [blame] | 118 | skip_if_microversion_lt = utils.skip_if_microversion_lt | 
| Valeriy Ponomaryov | a14c225 | 2015-10-29 13:34:32 +0200 | [diff] [blame] | 119 |  | 
|  | 120 |  | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 121 | class BaseSharesTest(test.BaseTestCase): | 
|  | 122 | """Base test case class for all Manila API tests.""" | 
|  | 123 |  | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 124 | credentials = ('primary', ) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 125 | force_tenant_isolation = False | 
| Vitaliy Levitksi | cfebfff | 2016-12-15 16:16:35 +0200 | [diff] [blame] | 126 | protocols = ["nfs", "cifs", "glusterfs", "hdfs", "cephfs", "maprfs"] | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 127 |  | 
|  | 128 | # Will be cleaned up in resource_cleanup | 
|  | 129 | class_resources = [] | 
|  | 130 |  | 
|  | 131 | # Will be cleaned up in tearDown method | 
|  | 132 | method_resources = [] | 
|  | 133 |  | 
|  | 134 | # Will be cleaned up in resource_cleanup | 
|  | 135 | class_isolated_creds = [] | 
|  | 136 |  | 
|  | 137 | # Will be cleaned up in tearDown method | 
|  | 138 | method_isolated_creds = [] | 
|  | 139 |  | 
| Valeriy Ponomaryov | a14c225 | 2015-10-29 13:34:32 +0200 | [diff] [blame] | 140 | def skip_if_microversion_not_supported(self, microversion): | 
| Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 141 | if not utils.is_microversion_supported(microversion): | 
| Valeriy Ponomaryov | a14c225 | 2015-10-29 13:34:32 +0200 | [diff] [blame] | 142 | raise self.skipException( | 
|  | 143 | "Microversion '%s' is not supported." % microversion) | 
|  | 144 |  | 
| Xing Yang | 69b00b5 | 2015-11-22 16:10:44 -0500 | [diff] [blame] | 145 | def skip_if_microversion_lt(self, microversion): | 
|  | 146 | if utils.is_microversion_lt(CONF.share.max_api_microversion, | 
|  | 147 | microversion): | 
|  | 148 | raise self.skipException( | 
|  | 149 | "Microversion must be greater than or equal to '%s'." % | 
|  | 150 | microversion) | 
|  | 151 |  | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 152 | @classmethod | 
|  | 153 | def get_client_with_isolated_creds(cls, | 
|  | 154 | name=None, | 
|  | 155 | type_of_creds="admin", | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 156 | cleanup_in_class=False, | 
|  | 157 | client_version='1'): | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 158 | """Creates isolated creds. | 
|  | 159 |  | 
|  | 160 | :param name: name, will be used for naming ic and related stuff | 
|  | 161 | :param type_of_creds: admin, alt or primary | 
|  | 162 | :param cleanup_in_class: defines place where to delete | 
|  | 163 | :returns: SharesClient -- shares client with isolated creds. | 
|  | 164 | :returns: To client added dict attr 'creds' with | 
|  | 165 | :returns: key elements 'tenant' and 'user'. | 
|  | 166 | """ | 
|  | 167 | if name is None: | 
|  | 168 | # Get name of test method | 
|  | 169 | name = inspect.stack()[1][3] | 
|  | 170 | if len(name) > 32: | 
|  | 171 | name = name[0:32] | 
|  | 172 |  | 
|  | 173 | # Choose type of isolated creds | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 174 | ic = dynamic_creds.DynamicCredentialProvider( | 
|  | 175 | identity_version=CONF.identity.auth_version, | 
|  | 176 | name=name, | 
| Sam Wan | c7b7f1f | 2015-11-25 00:22:28 -0500 | [diff] [blame] | 177 | admin_role=CONF.identity.admin_role, | 
| Valeriy Ponomaryov | 0ddd29b | 2016-06-07 17:49:31 +0300 | [diff] [blame] | 178 | admin_creds=common_creds.get_configured_admin_credentials()) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 179 | if "admin" in type_of_creds: | 
| Marc Koderer | 5880b36 | 2016-07-06 10:59:07 +0200 | [diff] [blame] | 180 | creds = ic.get_admin_creds().credentials | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 181 | elif "alt" in type_of_creds: | 
| Marc Koderer | 5880b36 | 2016-07-06 10:59:07 +0200 | [diff] [blame] | 182 | creds = ic.get_alt_creds().credentials | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 183 | else: | 
| Marc Koderer | 5880b36 | 2016-07-06 10:59:07 +0200 | [diff] [blame] | 184 | creds = ic.get_credentials(type_of_creds).credentials | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 185 | ic.type_of_creds = type_of_creds | 
|  | 186 |  | 
|  | 187 | # create client with isolated creds | 
|  | 188 | os = clients.Manager(credentials=creds) | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 189 | if client_version == '1': | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 190 | client = shares_client.SharesClient(os.auth_provider) | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 191 | elif client_version == '2': | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 192 | client = shares_v2_client.SharesV2Client(os.auth_provider) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 193 |  | 
|  | 194 | # Set place where will be deleted isolated creds | 
|  | 195 | ic_res = { | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 196 | "method": ic.clear_creds, | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 197 | "deleted": False, | 
|  | 198 | } | 
|  | 199 | if cleanup_in_class: | 
|  | 200 | cls.class_isolated_creds.insert(0, ic_res) | 
|  | 201 | else: | 
|  | 202 | cls.method_isolated_creds.insert(0, ic_res) | 
|  | 203 |  | 
|  | 204 | # Provide share network | 
|  | 205 | if CONF.share.multitenancy_enabled: | 
| Valeriy Ponomaryov | c5dae27 | 2016-06-10 18:29:24 +0300 | [diff] [blame] | 206 | if (not CONF.service_available.neutron and | 
|  | 207 | CONF.share.create_networks_when_multitenancy_enabled): | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 208 | raise cls.skipException("Neutron support is required") | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 209 | nc = os.networks_client | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 210 | share_network_id = cls.provide_share_network(client, nc, ic) | 
|  | 211 | client.share_network_id = share_network_id | 
|  | 212 | resource = { | 
|  | 213 | "type": "share_network", | 
|  | 214 | "id": client.share_network_id, | 
|  | 215 | "client": client, | 
|  | 216 | } | 
|  | 217 | if cleanup_in_class: | 
|  | 218 | cls.class_resources.insert(0, resource) | 
|  | 219 | else: | 
|  | 220 | cls.method_resources.insert(0, resource) | 
|  | 221 | return client | 
|  | 222 |  | 
|  | 223 | @classmethod | 
| Daniel Mellado | e526914 | 2017-01-12 12:17:58 +0000 | [diff] [blame] | 224 | def skip_checks(cls): | 
|  | 225 | super(BaseSharesTest, cls).skip_checks() | 
|  | 226 | if not CONF.service_available.manila: | 
|  | 227 | raise cls.skipException("Manila support is required") | 
|  | 228 |  | 
|  | 229 | @classmethod | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 230 | def verify_nonempty(cls, *args): | 
|  | 231 | if not all(args): | 
|  | 232 | msg = "Missing API credentials in configuration." | 
|  | 233 | raise cls.skipException(msg) | 
|  | 234 |  | 
|  | 235 | @classmethod | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 236 | def setup_clients(cls): | 
|  | 237 | super(BaseSharesTest, cls).setup_clients() | 
|  | 238 | os = getattr(cls, 'os_%s' % cls.credentials[0]) | 
|  | 239 | os.shares_client = shares_client.SharesClient(os.auth_provider) | 
| Valeriy Ponomaryov | 4fb305f | 2016-10-21 13:46:47 +0300 | [diff] [blame] | 240 |  | 
|  | 241 | if CONF.identity.auth_version == 'v3': | 
|  | 242 | project_id = os.auth_provider.auth_data[1]['project']['id'] | 
|  | 243 | else: | 
|  | 244 | project_id = os.auth_provider.auth_data[1]['token']['tenant']['id'] | 
|  | 245 | cls.tenant_id = project_id | 
|  | 246 | cls.user_id = os.auth_provider.auth_data[1]['user']['id'] | 
|  | 247 |  | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 248 | cls.shares_client = os.shares_client | 
|  | 249 | os.shares_v2_client = shares_v2_client.SharesV2Client( | 
|  | 250 | os.auth_provider) | 
|  | 251 | cls.shares_v2_client = os.shares_v2_client | 
|  | 252 | if CONF.share.multitenancy_enabled: | 
| Valeriy Ponomaryov | c5dae27 | 2016-06-10 18:29:24 +0300 | [diff] [blame] | 253 | if (not CONF.service_available.neutron and | 
|  | 254 | CONF.share.create_networks_when_multitenancy_enabled): | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 255 | raise cls.skipException("Neutron support is required") | 
|  | 256 | share_network_id = cls.provide_share_network( | 
|  | 257 | cls.shares_v2_client, os.networks_client) | 
|  | 258 | cls.shares_client.share_network_id = share_network_id | 
|  | 259 | cls.shares_v2_client.share_network_id = share_network_id | 
|  | 260 |  | 
|  | 261 | @classmethod | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 262 | def resource_setup(cls): | 
|  | 263 | if not (any(p in CONF.share.enable_protocols | 
|  | 264 | for p in cls.protocols) and | 
|  | 265 | CONF.service_available.manila): | 
|  | 266 | skip_msg = "Manila is disabled" | 
|  | 267 | raise cls.skipException(skip_msg) | 
|  | 268 | super(BaseSharesTest, cls).resource_setup() | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 269 |  | 
|  | 270 | def setUp(self): | 
|  | 271 | super(BaseSharesTest, self).setUp() | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 272 | self.addCleanup(self.clear_isolated_creds) | 
| Valeriy Ponomaryov | dd162cb | 2016-01-20 19:09:49 +0200 | [diff] [blame] | 273 | self.addCleanup(self.clear_resources) | 
| Valeriy Ponomaryov | 2abf5d7 | 2016-06-01 18:30:12 +0300 | [diff] [blame] | 274 | verify_test_has_appropriate_tags(self) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 275 |  | 
|  | 276 | @classmethod | 
|  | 277 | def resource_cleanup(cls): | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 278 | cls.clear_resources(cls.class_resources) | 
|  | 279 | cls.clear_isolated_creds(cls.class_isolated_creds) | 
| Sam Wan | 241029c | 2016-07-26 03:37:42 -0400 | [diff] [blame] | 280 | super(BaseSharesTest, cls).resource_cleanup() | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 281 |  | 
|  | 282 | @classmethod | 
|  | 283 | @network_synchronized | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 284 | def provide_share_network(cls, shares_client, networks_client, | 
| Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 285 | isolated_creds_client=None, | 
|  | 286 | ignore_multitenancy_config=False): | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 287 | """Used for finding/creating share network for multitenant driver. | 
|  | 288 |  | 
|  | 289 | This method creates/gets entity share-network for one tenant. This | 
|  | 290 | share-network will be used for creation of service vm. | 
|  | 291 |  | 
|  | 292 | :param shares_client: shares client, which requires share-network | 
| Valeriy Ponomaryov | 48a2bd7 | 2015-11-05 13:22:44 +0200 | [diff] [blame] | 293 | :param networks_client: network client from same tenant as shares | 
|  | 294 | :param isolated_creds_client: DynamicCredentialProvider instance | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 295 | If provided, then its networking will be used if needed. | 
|  | 296 | If not provided, then common network will be used if needed. | 
| Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 297 | :param ignore_multitenancy_config: provide a share network regardless | 
|  | 298 | of 'multitenancy_enabled' configuration value. | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 299 | :returns: str -- share network id for shares_client tenant | 
|  | 300 | :returns: None -- if single-tenant driver used | 
|  | 301 | """ | 
|  | 302 |  | 
|  | 303 | sc = shares_client | 
| Valeriy Ponomaryov | c5dae27 | 2016-06-10 18:29:24 +0300 | [diff] [blame] | 304 | search_word = "reusable" | 
|  | 305 | sn_name = "autogenerated_by_tempest_%s" % search_word | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 306 |  | 
| Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 307 | if (not ignore_multitenancy_config and | 
|  | 308 | not CONF.share.multitenancy_enabled): | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 309 | # Assumed usage of a single-tenant driver | 
|  | 310 | share_network_id = None | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 311 | else: | 
| Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 312 | if sc.share_network_id: | 
|  | 313 | # Share-network already exists, use it | 
|  | 314 | share_network_id = sc.share_network_id | 
|  | 315 | elif not CONF.share.create_networks_when_multitenancy_enabled: | 
|  | 316 | share_network_id = None | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 317 |  | 
|  | 318 | # Try get suitable share-network | 
|  | 319 | share_networks = sc.list_share_networks_with_detail() | 
|  | 320 | for sn in share_networks: | 
| Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 321 | if (sn["neutron_net_id"] is None and | 
|  | 322 | sn["neutron_subnet_id"] is None and | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 323 | sn["name"] and search_word in sn["name"]): | 
|  | 324 | share_network_id = sn["id"] | 
|  | 325 | break | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 326 |  | 
| Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 327 | # Create new share-network if one was not found | 
|  | 328 | if share_network_id is None: | 
|  | 329 | sn_desc = "This share-network was created by tempest" | 
|  | 330 | sn = sc.create_share_network(name=sn_name, | 
|  | 331 | description=sn_desc) | 
|  | 332 | share_network_id = sn["id"] | 
|  | 333 | else: | 
|  | 334 | net_id = subnet_id = share_network_id = None | 
|  | 335 |  | 
|  | 336 | if not isolated_creds_client: | 
|  | 337 | # Search for networks, created in previous runs | 
|  | 338 | service_net_name = "share-service" | 
|  | 339 | networks = networks_client.list_networks() | 
|  | 340 | if "networks" in networks.keys(): | 
|  | 341 | networks = networks["networks"] | 
|  | 342 | for network in networks: | 
|  | 343 | if (service_net_name in network["name"] and | 
|  | 344 | sc.tenant_id == network['tenant_id']): | 
|  | 345 | net_id = network["id"] | 
|  | 346 | if len(network["subnets"]) > 0: | 
|  | 347 | subnet_id = network["subnets"][0] | 
|  | 348 | break | 
|  | 349 |  | 
|  | 350 | # Create suitable network | 
|  | 351 | if net_id is None or subnet_id is None: | 
|  | 352 | ic = dynamic_creds.DynamicCredentialProvider( | 
|  | 353 | identity_version=CONF.identity.auth_version, | 
|  | 354 | name=service_net_name, | 
|  | 355 | admin_role=CONF.identity.admin_role, | 
|  | 356 | admin_creds=( | 
|  | 357 | common_creds. | 
|  | 358 | get_configured_admin_credentials())) | 
|  | 359 | net_data = ic._create_network_resources(sc.tenant_id) | 
|  | 360 | network, subnet, router = net_data | 
|  | 361 | net_id = network["id"] | 
|  | 362 | subnet_id = subnet["id"] | 
|  | 363 |  | 
|  | 364 | # Try get suitable share-network | 
|  | 365 | share_networks = sc.list_share_networks_with_detail() | 
|  | 366 | for sn in share_networks: | 
|  | 367 | if (net_id == sn["neutron_net_id"] and | 
|  | 368 | subnet_id == sn["neutron_subnet_id"] and | 
|  | 369 | sn["name"] and search_word in sn["name"]): | 
|  | 370 | share_network_id = sn["id"] | 
|  | 371 | break | 
|  | 372 | else: | 
|  | 373 | sn_name = "autogenerated_by_tempest_for_isolated_creds" | 
|  | 374 | # Use precreated network and subnet from isolated creds | 
|  | 375 | net_id = isolated_creds_client.get_credentials( | 
|  | 376 | isolated_creds_client.type_of_creds).network['id'] | 
|  | 377 | subnet_id = isolated_creds_client.get_credentials( | 
|  | 378 | isolated_creds_client.type_of_creds).subnet['id'] | 
|  | 379 |  | 
|  | 380 | # Create suitable share-network | 
|  | 381 | if share_network_id is None: | 
|  | 382 | sn_desc = "This share-network was created by tempest" | 
|  | 383 | sn = sc.create_share_network(name=sn_name, | 
|  | 384 | description=sn_desc, | 
|  | 385 | neutron_net_id=net_id, | 
|  | 386 | neutron_subnet_id=subnet_id) | 
|  | 387 | share_network_id = sn["id"] | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 388 |  | 
|  | 389 | return share_network_id | 
|  | 390 |  | 
|  | 391 | @classmethod | 
| marcusvrn | e0d7cfd | 2016-06-24 12:27:55 -0300 | [diff] [blame] | 392 | def _create_share(cls, share_protocol=None, size=None, name=None, | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 393 | snapshot_id=None, description=None, metadata=None, | 
|  | 394 | share_network_id=None, share_type_id=None, | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 395 | consistency_group_id=None, client=None, | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 396 | cleanup_in_class=True, is_public=False, **kwargs): | 
| Valeriy Ponomaryov | 1aaa72d | 2015-09-08 12:59:41 +0300 | [diff] [blame] | 397 | client = client or cls.shares_v2_client | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 398 | description = description or "Tempest's share" | 
|  | 399 | share_network_id = share_network_id or client.share_network_id or None | 
|  | 400 | metadata = metadata or {} | 
| marcusvrn | e0d7cfd | 2016-06-24 12:27:55 -0300 | [diff] [blame] | 401 | size = size or CONF.share.share_size | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 402 | kwargs.update({ | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 403 | 'share_protocol': share_protocol, | 
|  | 404 | 'size': size, | 
|  | 405 | 'name': name, | 
|  | 406 | 'snapshot_id': snapshot_id, | 
|  | 407 | 'description': description, | 
|  | 408 | 'metadata': metadata, | 
|  | 409 | 'share_network_id': share_network_id, | 
|  | 410 | 'share_type_id': share_type_id, | 
|  | 411 | 'is_public': is_public, | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 412 | }) | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 413 | if consistency_group_id: | 
|  | 414 | kwargs['consistency_group_id'] = consistency_group_id | 
|  | 415 |  | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 416 | share = client.create_share(**kwargs) | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 417 | resource = {"type": "share", "id": share["id"], "client": client, | 
|  | 418 | "consistency_group_id": consistency_group_id} | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 419 | cleanup_list = (cls.class_resources if cleanup_in_class else | 
|  | 420 | cls.method_resources) | 
|  | 421 | cleanup_list.insert(0, resource) | 
|  | 422 | return share | 
|  | 423 |  | 
|  | 424 | @classmethod | 
| Rodrigo Barbieri | 427bc05 | 2016-06-06 17:10:06 -0300 | [diff] [blame] | 425 | def migrate_share( | 
|  | 426 | cls, share_id, dest_host, wait_for_status, client=None, | 
| Rodrigo Barbieri | 027df98 | 2016-11-24 15:52:03 -0200 | [diff] [blame^] | 427 | force_host_assisted_migration=False, writable=False, | 
|  | 428 | nondisruptive=False, preserve_metadata=False, | 
|  | 429 | preserve_snapshots=False, new_share_network_id=None, | 
| Rodrigo Barbieri | d38d2f5 | 2016-07-19 22:24:56 -0300 | [diff] [blame] | 430 | new_share_type_id=None, **kwargs): | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 431 | client = client or cls.shares_v2_client | 
| Rodrigo Barbieri | 427bc05 | 2016-06-06 17:10:06 -0300 | [diff] [blame] | 432 | client.migrate_share( | 
|  | 433 | share_id, dest_host, | 
|  | 434 | force_host_assisted_migration=force_host_assisted_migration, | 
| Rodrigo Barbieri | 027df98 | 2016-11-24 15:52:03 -0200 | [diff] [blame^] | 435 | writable=writable, preserve_metadata=preserve_metadata, | 
|  | 436 | nondisruptive=nondisruptive, preserve_snapshots=preserve_snapshots, | 
| Rodrigo Barbieri | 427bc05 | 2016-06-06 17:10:06 -0300 | [diff] [blame] | 437 | new_share_network_id=new_share_network_id, | 
| Rodrigo Barbieri | d38d2f5 | 2016-07-19 22:24:56 -0300 | [diff] [blame] | 438 | new_share_type_id=new_share_type_id, **kwargs) | 
| Rodrigo Barbieri | e330512 | 2016-02-03 14:32:24 -0200 | [diff] [blame] | 439 | share = client.wait_for_migration_status( | 
| Rodrigo Barbieri | 427bc05 | 2016-06-06 17:10:06 -0300 | [diff] [blame] | 440 | share_id, dest_host, wait_for_status, **kwargs) | 
| Rodrigo Barbieri | e330512 | 2016-02-03 14:32:24 -0200 | [diff] [blame] | 441 | return share | 
|  | 442 |  | 
|  | 443 | @classmethod | 
|  | 444 | def migration_complete(cls, share_id, dest_host, client=None, **kwargs): | 
|  | 445 | client = client or cls.shares_v2_client | 
|  | 446 | client.migration_complete(share_id, **kwargs) | 
|  | 447 | share = client.wait_for_migration_status( | 
| Rodrigo Barbieri | 427bc05 | 2016-06-06 17:10:06 -0300 | [diff] [blame] | 448 | share_id, dest_host, 'migration_success', **kwargs) | 
| Rodrigo Barbieri | b7137ad | 2015-09-06 22:53:16 -0300 | [diff] [blame] | 449 | return share | 
|  | 450 |  | 
|  | 451 | @classmethod | 
| Rodrigo Barbieri | c9abf28 | 2016-08-24 22:01:31 -0300 | [diff] [blame] | 452 | def migration_cancel(cls, share_id, dest_host, client=None, **kwargs): | 
|  | 453 | client = client or cls.shares_v2_client | 
|  | 454 | client.migration_cancel(share_id, **kwargs) | 
|  | 455 | share = client.wait_for_migration_status( | 
|  | 456 | share_id, dest_host, 'migration_cancelled', **kwargs) | 
|  | 457 | return share | 
|  | 458 |  | 
|  | 459 | @classmethod | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 460 | def create_share(cls, *args, **kwargs): | 
|  | 461 | """Create one share and wait for available state. Retry if allowed.""" | 
|  | 462 | result = cls.create_shares([{"args": args, "kwargs": kwargs}]) | 
|  | 463 | return result[0] | 
|  | 464 |  | 
|  | 465 | @classmethod | 
|  | 466 | def create_shares(cls, share_data_list): | 
|  | 467 | """Creates several shares in parallel with retries. | 
|  | 468 |  | 
|  | 469 | Use this method when you want to create more than one share at same | 
|  | 470 | time. Especially if config option 'share.share_creation_retry_number' | 
|  | 471 | has value more than zero (0). | 
|  | 472 | All shares will be expected to have 'available' status with or without | 
|  | 473 | recreation else error will be raised. | 
|  | 474 |  | 
|  | 475 | :param share_data_list: list -- list of dictionaries with 'args' and | 
|  | 476 | 'kwargs' for '_create_share' method of this base class. | 
|  | 477 | example of data: | 
|  | 478 | share_data_list=[{'args': ['quuz'], 'kwargs': {'foo': 'bar'}}}] | 
|  | 479 | :returns: list -- list of shares created using provided data. | 
|  | 480 | """ | 
|  | 481 |  | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 482 | for d in share_data_list: | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 483 | if not isinstance(d, dict): | 
|  | 484 | raise exceptions.TempestException( | 
|  | 485 | "Expected 'dict', got '%s'" % type(d)) | 
|  | 486 | if "args" not in d: | 
|  | 487 | d["args"] = [] | 
|  | 488 | if "kwargs" not in d: | 
|  | 489 | d["kwargs"] = {} | 
|  | 490 | if len(d) > 2: | 
|  | 491 | raise exceptions.TempestException( | 
|  | 492 | "Expected only 'args' and 'kwargs' keys. " | 
|  | 493 | "Provided %s" % list(d)) | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 494 |  | 
|  | 495 | data = [] | 
|  | 496 | for d in share_data_list: | 
|  | 497 | client = d["kwargs"].pop("client", cls.shares_v2_client) | 
|  | 498 | local_d = { | 
|  | 499 | "args": d["args"], | 
|  | 500 | "kwargs": copy.deepcopy(d["kwargs"]), | 
|  | 501 | } | 
|  | 502 | local_d["kwargs"]["client"] = client | 
|  | 503 | local_d["share"] = cls._create_share( | 
|  | 504 | *local_d["args"], **local_d["kwargs"]) | 
|  | 505 | local_d["cnt"] = 0 | 
|  | 506 | local_d["available"] = False | 
|  | 507 | data.append(local_d) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 508 |  | 
|  | 509 | while not all(d["available"] for d in data): | 
|  | 510 | for d in data: | 
|  | 511 | if d["available"]: | 
|  | 512 | continue | 
| Valeriy Ponomaryov | 1a3e338 | 2016-06-08 15:17:16 +0300 | [diff] [blame] | 513 | client = d["kwargs"]["client"] | 
|  | 514 | share_id = d["share"]["id"] | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 515 | try: | 
| Valeriy Ponomaryov | 1a3e338 | 2016-06-08 15:17:16 +0300 | [diff] [blame] | 516 | client.wait_for_share_status(share_id, "available") | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 517 | d["available"] = True | 
|  | 518 | except (share_exceptions.ShareBuildErrorException, | 
|  | 519 | exceptions.TimeoutException) as e: | 
|  | 520 | if CONF.share.share_creation_retry_number > d["cnt"]: | 
|  | 521 | d["cnt"] += 1 | 
|  | 522 | msg = ("Share '%s' failed to be built. " | 
| Valeriy Ponomaryov | 1a3e338 | 2016-06-08 15:17:16 +0300 | [diff] [blame] | 523 | "Trying create another." % share_id) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 524 | LOG.error(msg) | 
|  | 525 | LOG.error(e) | 
| Valeriy Ponomaryov | 1a3e338 | 2016-06-08 15:17:16 +0300 | [diff] [blame] | 526 | cg_id = d["kwargs"].get("consistency_group_id") | 
|  | 527 | if cg_id: | 
|  | 528 | # NOTE(vponomaryov): delete errored share | 
|  | 529 | # immediately in case share is part of CG. | 
|  | 530 | client.delete_share( | 
|  | 531 | share_id, | 
|  | 532 | params={"consistency_group_id": cg_id}) | 
|  | 533 | client.wait_for_resource_deletion( | 
|  | 534 | share_id=share_id) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 535 | d["share"] = cls._create_share( | 
|  | 536 | *d["args"], **d["kwargs"]) | 
|  | 537 | else: | 
| gecong1973 | 5866380 | 2016-08-25 11:08:45 +0800 | [diff] [blame] | 538 | raise | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 539 |  | 
|  | 540 | return [d["share"] for d in data] | 
|  | 541 |  | 
|  | 542 | @classmethod | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 543 | def create_consistency_group(cls, client=None, cleanup_in_class=True, | 
|  | 544 | share_network_id=None, **kwargs): | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 545 | client = client or cls.shares_v2_client | 
| Goutham Pacha Ravi | 9221f5e | 2016-04-21 13:17:49 -0400 | [diff] [blame] | 546 | if kwargs.get('source_cgsnapshot_id') is None: | 
|  | 547 | kwargs['share_network_id'] = (share_network_id or | 
|  | 548 | client.share_network_id or None) | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 549 | consistency_group = client.create_consistency_group(**kwargs) | 
|  | 550 | resource = { | 
|  | 551 | "type": "consistency_group", | 
|  | 552 | "id": consistency_group["id"], | 
|  | 553 | "client": client} | 
|  | 554 | if cleanup_in_class: | 
|  | 555 | cls.class_resources.insert(0, resource) | 
|  | 556 | else: | 
|  | 557 | cls.method_resources.insert(0, resource) | 
|  | 558 |  | 
|  | 559 | if kwargs.get('source_cgsnapshot_id'): | 
|  | 560 | new_cg_shares = client.list_shares( | 
|  | 561 | detailed=True, | 
|  | 562 | params={'consistency_group_id': consistency_group['id']}) | 
|  | 563 |  | 
|  | 564 | for share in new_cg_shares: | 
|  | 565 | resource = {"type": "share", | 
|  | 566 | "id": share["id"], | 
|  | 567 | "client": client, | 
|  | 568 | "consistency_group_id": share.get( | 
|  | 569 | 'consistency_group_id')} | 
|  | 570 | if cleanup_in_class: | 
|  | 571 | cls.class_resources.insert(0, resource) | 
|  | 572 | else: | 
|  | 573 | cls.method_resources.insert(0, resource) | 
|  | 574 |  | 
|  | 575 | client.wait_for_consistency_group_status(consistency_group['id'], | 
|  | 576 | 'available') | 
|  | 577 | return consistency_group | 
|  | 578 |  | 
|  | 579 | @classmethod | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 580 | def create_snapshot_wait_for_active(cls, share_id, name=None, | 
|  | 581 | description=None, force=False, | 
|  | 582 | client=None, cleanup_in_class=True): | 
|  | 583 | if client is None: | 
| Yogesh | 1f931ff | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 584 | client = cls.shares_v2_client | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 585 | if description is None: | 
|  | 586 | description = "Tempest's snapshot" | 
|  | 587 | snapshot = client.create_snapshot(share_id, name, description, force) | 
|  | 588 | resource = { | 
|  | 589 | "type": "snapshot", | 
|  | 590 | "id": snapshot["id"], | 
|  | 591 | "client": client, | 
|  | 592 | } | 
|  | 593 | if cleanup_in_class: | 
|  | 594 | cls.class_resources.insert(0, resource) | 
|  | 595 | else: | 
|  | 596 | cls.method_resources.insert(0, resource) | 
|  | 597 | client.wait_for_snapshot_status(snapshot["id"], "available") | 
|  | 598 | return snapshot | 
|  | 599 |  | 
|  | 600 | @classmethod | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 601 | def create_cgsnapshot_wait_for_active(cls, consistency_group_id, | 
|  | 602 | name=None, description=None, | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 603 | client=None, cleanup_in_class=True, | 
|  | 604 | **kwargs): | 
|  | 605 | client = client or cls.shares_v2_client | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 606 | if description is None: | 
|  | 607 | description = "Tempest's cgsnapshot" | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 608 | cgsnapshot = client.create_cgsnapshot(consistency_group_id, | 
|  | 609 | name=name, | 
|  | 610 | description=description, | 
|  | 611 | **kwargs) | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 612 | resource = { | 
|  | 613 | "type": "cgsnapshot", | 
|  | 614 | "id": cgsnapshot["id"], | 
|  | 615 | "client": client, | 
|  | 616 | } | 
|  | 617 | if cleanup_in_class: | 
|  | 618 | cls.class_resources.insert(0, resource) | 
|  | 619 | else: | 
|  | 620 | cls.method_resources.insert(0, resource) | 
|  | 621 | client.wait_for_cgsnapshot_status(cgsnapshot["id"], "available") | 
|  | 622 | return cgsnapshot | 
|  | 623 |  | 
|  | 624 | @classmethod | 
| Yogesh | bdb8810 | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 625 | def get_availability_zones(cls, client=None): | 
|  | 626 | """List the availability zones for "manila-share" services | 
|  | 627 |  | 
|  | 628 | that are currently in "up" state. | 
|  | 629 | """ | 
|  | 630 | client = client or cls.shares_v2_client | 
|  | 631 | cls.services = client.list_services() | 
|  | 632 | zones = [service['zone'] for service in cls.services if | 
|  | 633 | service['binary'] == "manila-share" and | 
|  | 634 | service['state'] == 'up'] | 
|  | 635 | return zones | 
|  | 636 |  | 
| Yogesh | 1f931ff | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 637 | def get_pools_for_replication_domain(self): | 
|  | 638 | # Get the list of pools for the replication domain | 
|  | 639 | pools = self.admin_client.list_pools(detail=True)['pools'] | 
|  | 640 | instance_host = self.shares[0]['host'] | 
|  | 641 | host_pool = [p for p in pools if p['name'] == instance_host][0] | 
|  | 642 | rep_domain = host_pool['capabilities']['replication_domain'] | 
|  | 643 | pools_in_rep_domain = [p for p in pools if p['capabilities'][ | 
|  | 644 | 'replication_domain'] == rep_domain] | 
|  | 645 | return rep_domain, pools_in_rep_domain | 
|  | 646 |  | 
| Yogesh | bdb8810 | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 647 | @classmethod | 
|  | 648 | def create_share_replica(cls, share_id, availability_zone, client=None, | 
|  | 649 | cleanup_in_class=False, cleanup=True): | 
|  | 650 | client = client or cls.shares_v2_client | 
|  | 651 | replica = client.create_share_replica(share_id, availability_zone) | 
|  | 652 | resource = { | 
|  | 653 | "type": "share_replica", | 
|  | 654 | "id": replica["id"], | 
|  | 655 | "client": client, | 
|  | 656 | "share_id": share_id, | 
|  | 657 | } | 
|  | 658 | # NOTE(Yogi1): Cleanup needs to be disabled during promotion tests. | 
|  | 659 | if cleanup: | 
|  | 660 | if cleanup_in_class: | 
|  | 661 | cls.class_resources.insert(0, resource) | 
|  | 662 | else: | 
|  | 663 | cls.method_resources.insert(0, resource) | 
|  | 664 | client.wait_for_share_replica_status( | 
|  | 665 | replica["id"], constants.STATUS_AVAILABLE) | 
|  | 666 | return replica | 
|  | 667 |  | 
|  | 668 | @classmethod | 
|  | 669 | def delete_share_replica(cls, replica_id, client=None): | 
|  | 670 | client = client or cls.shares_v2_client | 
| Yogesh | 1f931ff | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 671 | try: | 
|  | 672 | client.delete_share_replica(replica_id) | 
|  | 673 | client.wait_for_resource_deletion(replica_id=replica_id) | 
|  | 674 | except exceptions.NotFound: | 
|  | 675 | pass | 
| Yogesh | bdb8810 | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 676 |  | 
|  | 677 | @classmethod | 
|  | 678 | def promote_share_replica(cls, replica_id, client=None): | 
|  | 679 | client = client or cls.shares_v2_client | 
|  | 680 | replica = client.promote_share_replica(replica_id) | 
|  | 681 | client.wait_for_share_replica_status( | 
|  | 682 | replica["id"], | 
|  | 683 | constants.REPLICATION_STATE_ACTIVE, | 
|  | 684 | status_attr="replica_state") | 
|  | 685 | return replica | 
|  | 686 |  | 
|  | 687 | @classmethod | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 688 | def create_share_network(cls, client=None, | 
|  | 689 | cleanup_in_class=False, **kwargs): | 
|  | 690 | if client is None: | 
|  | 691 | client = cls.shares_client | 
|  | 692 | share_network = client.create_share_network(**kwargs) | 
|  | 693 | resource = { | 
|  | 694 | "type": "share_network", | 
|  | 695 | "id": share_network["id"], | 
|  | 696 | "client": client, | 
|  | 697 | } | 
|  | 698 | if cleanup_in_class: | 
|  | 699 | cls.class_resources.insert(0, resource) | 
|  | 700 | else: | 
|  | 701 | cls.method_resources.insert(0, resource) | 
|  | 702 | return share_network | 
|  | 703 |  | 
|  | 704 | @classmethod | 
|  | 705 | def create_security_service(cls, ss_type="ldap", client=None, | 
|  | 706 | cleanup_in_class=False, **kwargs): | 
|  | 707 | if client is None: | 
|  | 708 | client = cls.shares_client | 
|  | 709 | security_service = client.create_security_service(ss_type, **kwargs) | 
|  | 710 | resource = { | 
|  | 711 | "type": "security_service", | 
|  | 712 | "id": security_service["id"], | 
|  | 713 | "client": client, | 
|  | 714 | } | 
|  | 715 | if cleanup_in_class: | 
|  | 716 | cls.class_resources.insert(0, resource) | 
|  | 717 | else: | 
|  | 718 | cls.method_resources.insert(0, resource) | 
|  | 719 | return security_service | 
|  | 720 |  | 
|  | 721 | @classmethod | 
|  | 722 | def create_share_type(cls, name, is_public=True, client=None, | 
|  | 723 | cleanup_in_class=True, **kwargs): | 
|  | 724 | if client is None: | 
| Valeriy Ponomaryov | a14c225 | 2015-10-29 13:34:32 +0200 | [diff] [blame] | 725 | client = cls.shares_v2_client | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 726 | share_type = client.create_share_type(name, is_public, **kwargs) | 
|  | 727 | resource = { | 
|  | 728 | "type": "share_type", | 
|  | 729 | "id": share_type["share_type"]["id"], | 
|  | 730 | "client": client, | 
|  | 731 | } | 
|  | 732 | if cleanup_in_class: | 
|  | 733 | cls.class_resources.insert(0, resource) | 
|  | 734 | else: | 
|  | 735 | cls.method_resources.insert(0, resource) | 
|  | 736 | return share_type | 
|  | 737 |  | 
|  | 738 | @staticmethod | 
| Clinton Knight | 4699a8c | 2016-08-16 22:36:13 -0400 | [diff] [blame] | 739 | def add_extra_specs_to_dict(extra_specs=None): | 
|  | 740 | """Add any required extra-specs to share type dictionary""" | 
| Valeriy Ponomaryov | ad55dc5 | 2015-09-23 13:54:00 +0300 | [diff] [blame] | 741 | dhss = six.text_type(CONF.share.multitenancy_enabled) | 
|  | 742 | snapshot_support = six.text_type( | 
|  | 743 | CONF.share.capability_snapshot_support) | 
| Clinton Knight | 4699a8c | 2016-08-16 22:36:13 -0400 | [diff] [blame] | 744 | create_from_snapshot_support = six.text_type( | 
|  | 745 | CONF.share.capability_create_share_from_snapshot_support) | 
| Clinton Knight | 7f16b8c | 2016-06-08 13:46:51 -0700 | [diff] [blame] | 746 | revert_to_snapshot_support = six.text_type( | 
|  | 747 | CONF.share.capability_revert_to_snapshot_support) | 
| Clinton Knight | 4699a8c | 2016-08-16 22:36:13 -0400 | [diff] [blame] | 748 |  | 
|  | 749 | extra_specs_dict = { | 
| Valeriy Ponomaryov | ad55dc5 | 2015-09-23 13:54:00 +0300 | [diff] [blame] | 750 | "driver_handles_share_servers": dhss, | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 751 | } | 
| Clinton Knight | 4699a8c | 2016-08-16 22:36:13 -0400 | [diff] [blame] | 752 |  | 
|  | 753 | optional = { | 
|  | 754 | "snapshot_support": snapshot_support, | 
|  | 755 | "create_share_from_snapshot_support": create_from_snapshot_support, | 
| Clinton Knight | 7f16b8c | 2016-06-08 13:46:51 -0700 | [diff] [blame] | 756 | "revert_to_snapshot_support": revert_to_snapshot_support, | 
| Clinton Knight | 4699a8c | 2016-08-16 22:36:13 -0400 | [diff] [blame] | 757 | } | 
|  | 758 | # NOTE(gouthamr): In micro-versions < 2.24, snapshot_support is a | 
|  | 759 | # required extra-spec | 
|  | 760 | extra_specs_dict.update(optional) | 
|  | 761 |  | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 762 | if extra_specs: | 
| Clinton Knight | 4699a8c | 2016-08-16 22:36:13 -0400 | [diff] [blame] | 763 | extra_specs_dict.update(extra_specs) | 
|  | 764 |  | 
|  | 765 | return extra_specs_dict | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 766 |  | 
|  | 767 | @classmethod | 
|  | 768 | def clear_isolated_creds(cls, creds=None): | 
|  | 769 | if creds is None: | 
|  | 770 | creds = cls.method_isolated_creds | 
|  | 771 | for ic in creds: | 
|  | 772 | if "deleted" not in ic.keys(): | 
|  | 773 | ic["deleted"] = False | 
|  | 774 | if not ic["deleted"]: | 
|  | 775 | with handle_cleanup_exceptions(): | 
|  | 776 | ic["method"]() | 
|  | 777 | ic["deleted"] = True | 
|  | 778 |  | 
|  | 779 | @classmethod | 
| Yogesh | 1f931ff | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 780 | def clear_share_replicas(cls, share_id, client=None): | 
|  | 781 | client = client or cls.shares_v2_client | 
|  | 782 | share_replicas = client.list_share_replicas( | 
|  | 783 | share_id=share_id) | 
|  | 784 |  | 
|  | 785 | for replica in share_replicas: | 
|  | 786 | try: | 
|  | 787 | cls.delete_share_replica(replica['id']) | 
|  | 788 | except exceptions.BadRequest: | 
|  | 789 | # Ignore the exception due to deletion of last active replica | 
|  | 790 | pass | 
|  | 791 |  | 
|  | 792 | @classmethod | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 793 | def clear_resources(cls, resources=None): | 
|  | 794 | """Deletes resources, that were created in test suites. | 
|  | 795 |  | 
|  | 796 | This method tries to remove resources from resource list, | 
|  | 797 | if it is not found, assumed it was deleted in test itself. | 
|  | 798 | It is expected, that all resources were added as LIFO | 
|  | 799 | due to restriction of deletion resources, that is in the chain. | 
|  | 800 |  | 
|  | 801 | :param resources: dict with keys 'type','id','client' and 'deleted' | 
|  | 802 | """ | 
|  | 803 |  | 
|  | 804 | if resources is None: | 
|  | 805 | resources = cls.method_resources | 
|  | 806 | for res in resources: | 
|  | 807 | if "deleted" not in res.keys(): | 
|  | 808 | res["deleted"] = False | 
|  | 809 | if "client" not in res.keys(): | 
|  | 810 | res["client"] = cls.shares_client | 
|  | 811 | if not(res["deleted"]): | 
|  | 812 | res_id = res['id'] | 
|  | 813 | client = res["client"] | 
|  | 814 | with handle_cleanup_exceptions(): | 
|  | 815 | if res["type"] is "share": | 
| Yogesh | 1f931ff | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 816 | cls.clear_share_replicas(res_id) | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 817 | cg_id = res.get('consistency_group_id') | 
|  | 818 | if cg_id: | 
|  | 819 | params = {'consistency_group_id': cg_id} | 
| Clinton Knight | e5c8f09 | 2015-08-27 15:00:23 -0400 | [diff] [blame] | 820 | client.delete_share(res_id, params=params) | 
|  | 821 | else: | 
|  | 822 | client.delete_share(res_id) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 823 | client.wait_for_resource_deletion(share_id=res_id) | 
|  | 824 | elif res["type"] is "snapshot": | 
|  | 825 | client.delete_snapshot(res_id) | 
|  | 826 | client.wait_for_resource_deletion(snapshot_id=res_id) | 
|  | 827 | elif res["type"] is "share_network": | 
|  | 828 | client.delete_share_network(res_id) | 
|  | 829 | client.wait_for_resource_deletion(sn_id=res_id) | 
|  | 830 | elif res["type"] is "security_service": | 
|  | 831 | client.delete_security_service(res_id) | 
|  | 832 | client.wait_for_resource_deletion(ss_id=res_id) | 
|  | 833 | elif res["type"] is "share_type": | 
|  | 834 | client.delete_share_type(res_id) | 
|  | 835 | client.wait_for_resource_deletion(st_id=res_id) | 
| Andrew Kerr | bf31e91 | 2015-07-29 10:39:38 -0400 | [diff] [blame] | 836 | elif res["type"] is "consistency_group": | 
|  | 837 | client.delete_consistency_group(res_id) | 
|  | 838 | client.wait_for_resource_deletion(cg_id=res_id) | 
|  | 839 | elif res["type"] is "cgsnapshot": | 
|  | 840 | client.delete_cgsnapshot(res_id) | 
|  | 841 | client.wait_for_resource_deletion(cgsnapshot_id=res_id) | 
| Yogesh | bdb8810 | 2015-09-29 23:41:02 -0400 | [diff] [blame] | 842 | elif res["type"] is "share_replica": | 
|  | 843 | client.delete_share_replica(res_id) | 
|  | 844 | client.wait_for_resource_deletion(replica_id=res_id) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 845 | else: | 
| huayue | 97bacbf | 2016-01-04 09:57:39 +0800 | [diff] [blame] | 846 | LOG.warning("Provided unsupported resource type for " | 
|  | 847 | "cleanup '%s'. Skipping." % res["type"]) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 848 | res["deleted"] = True | 
|  | 849 |  | 
|  | 850 | @classmethod | 
|  | 851 | def generate_share_network_data(self): | 
|  | 852 | data = { | 
|  | 853 | "name": data_utils.rand_name("sn-name"), | 
|  | 854 | "description": data_utils.rand_name("sn-desc"), | 
|  | 855 | "neutron_net_id": data_utils.rand_name("net-id"), | 
|  | 856 | "neutron_subnet_id": data_utils.rand_name("subnet-id"), | 
|  | 857 | } | 
|  | 858 | return data | 
|  | 859 |  | 
|  | 860 | @classmethod | 
|  | 861 | def generate_security_service_data(self): | 
|  | 862 | data = { | 
|  | 863 | "name": data_utils.rand_name("ss-name"), | 
|  | 864 | "description": data_utils.rand_name("ss-desc"), | 
| Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 865 | "dns_ip": utils.rand_ip(), | 
|  | 866 | "server": utils.rand_ip(), | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 867 | "domain": data_utils.rand_name("ss-domain"), | 
|  | 868 | "user": data_utils.rand_name("ss-user"), | 
|  | 869 | "password": data_utils.rand_name("ss-password"), | 
|  | 870 | } | 
|  | 871 | return data | 
|  | 872 |  | 
|  | 873 | # Useful assertions | 
|  | 874 | def assertDictMatch(self, d1, d2, approx_equal=False, tolerance=0.001): | 
|  | 875 | """Assert two dicts are equivalent. | 
|  | 876 |  | 
|  | 877 | This is a 'deep' match in the sense that it handles nested | 
|  | 878 | dictionaries appropriately. | 
|  | 879 |  | 
|  | 880 | NOTE: | 
|  | 881 |  | 
|  | 882 | If you don't care (or don't know) a given value, you can specify | 
|  | 883 | the string DONTCARE as the value. This will cause that dict-item | 
|  | 884 | to be skipped. | 
|  | 885 |  | 
|  | 886 | """ | 
|  | 887 | def raise_assertion(msg): | 
|  | 888 | d1str = str(d1) | 
|  | 889 | d2str = str(d2) | 
|  | 890 | base_msg = ('Dictionaries do not match. %(msg)s d1: %(d1str)s ' | 
|  | 891 | 'd2: %(d2str)s' % | 
|  | 892 | {"msg": msg, "d1str": d1str, "d2str": d2str}) | 
|  | 893 | raise AssertionError(base_msg) | 
|  | 894 |  | 
|  | 895 | d1keys = set(d1.keys()) | 
|  | 896 | d2keys = set(d2.keys()) | 
|  | 897 | if d1keys != d2keys: | 
|  | 898 | d1only = d1keys - d2keys | 
|  | 899 | d2only = d2keys - d1keys | 
|  | 900 | raise_assertion('Keys in d1 and not d2: %(d1only)s. ' | 
|  | 901 | 'Keys in d2 and not d1: %(d2only)s' % | 
|  | 902 | {"d1only": d1only, "d2only": d2only}) | 
|  | 903 |  | 
|  | 904 | for key in d1keys: | 
|  | 905 | d1value = d1[key] | 
|  | 906 | d2value = d2[key] | 
|  | 907 | try: | 
|  | 908 | error = abs(float(d1value) - float(d2value)) | 
|  | 909 | within_tolerance = error <= tolerance | 
|  | 910 | except (ValueError, TypeError): | 
| daiki kato | 6914b1a | 2016-03-16 17:16:57 +0900 | [diff] [blame] | 911 | # If both values aren't convertible to float, just ignore | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 912 | # ValueError if arg is a str, TypeError if it's something else | 
|  | 913 | # (like None) | 
|  | 914 | within_tolerance = False | 
|  | 915 |  | 
|  | 916 | if hasattr(d1value, 'keys') and hasattr(d2value, 'keys'): | 
|  | 917 | self.assertDictMatch(d1value, d2value) | 
|  | 918 | elif 'DONTCARE' in (d1value, d2value): | 
|  | 919 | continue | 
|  | 920 | elif approx_equal and within_tolerance: | 
|  | 921 | continue | 
|  | 922 | elif d1value != d2value: | 
|  | 923 | raise_assertion("d1['%(key)s']=%(d1value)s != " | 
|  | 924 | "d2['%(key)s']=%(d2value)s" % | 
|  | 925 | { | 
|  | 926 | "key": key, | 
|  | 927 | "d1value": d1value, | 
|  | 928 | "d2value": d2value | 
|  | 929 | }) | 
|  | 930 |  | 
|  | 931 |  | 
|  | 932 | class BaseSharesAltTest(BaseSharesTest): | 
|  | 933 | """Base test case class for all Shares Alt API tests.""" | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 934 | credentials = ('alt', ) | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 935 |  | 
|  | 936 |  | 
|  | 937 | class BaseSharesAdminTest(BaseSharesTest): | 
|  | 938 | """Base test case class for all Shares Admin API tests.""" | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 939 | credentials = ('admin', ) | 
|  | 940 |  | 
|  | 941 |  | 
|  | 942 | class BaseSharesMixedTest(BaseSharesTest): | 
|  | 943 | """Base test case class for all Shares API tests with all user roles.""" | 
|  | 944 | credentials = ('primary', 'alt', 'admin') | 
| Marc Koderer | 0abc93b | 2015-07-15 09:18:35 +0200 | [diff] [blame] | 945 |  | 
|  | 946 | @classmethod | 
| Valeriy Ponomaryov | 39cdf72 | 2016-05-30 18:16:15 +0300 | [diff] [blame] | 947 | def setup_clients(cls): | 
|  | 948 | super(BaseSharesMixedTest, cls).setup_clients() | 
|  | 949 | cls.admin_shares_client = shares_client.SharesClient( | 
|  | 950 | cls.os_admin.auth_provider) | 
|  | 951 | cls.admin_shares_v2_client = shares_v2_client.SharesV2Client( | 
|  | 952 | cls.os_admin.auth_provider) | 
|  | 953 | cls.alt_shares_client = shares_client.SharesClient( | 
|  | 954 | cls.os_alt.auth_provider) | 
|  | 955 | cls.alt_shares_v2_client = shares_v2_client.SharesV2Client( | 
|  | 956 | cls.os_alt.auth_provider) | 
|  | 957 |  | 
|  | 958 | if CONF.share.multitenancy_enabled: | 
|  | 959 | admin_share_network_id = cls.provide_share_network( | 
|  | 960 | cls.admin_shares_v2_client, cls.os_admin.networks_client) | 
|  | 961 | cls.admin_shares_client.share_network_id = admin_share_network_id | 
|  | 962 | cls.admin_shares_v2_client.share_network_id = ( | 
|  | 963 | admin_share_network_id) | 
|  | 964 |  | 
|  | 965 | alt_share_network_id = cls.provide_share_network( | 
|  | 966 | cls.alt_shares_v2_client, cls.os_alt.networks_client) | 
|  | 967 | cls.alt_shares_client.share_network_id = alt_share_network_id | 
|  | 968 | cls.alt_shares_v2_client.share_network_id = alt_share_network_id |