blob: cd16c604e37dd90a13651577fd032682405ace80 [file] [log] [blame]
Marc Koderer0abc93b2015-07-15 09:18:35 +02001# 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
16import copy
17import inspect
Valeriy Ponomaryov2abf5d72016-06-01 18:30:12 +030018import re
Marc Koderer0abc93b2015-07-15 09:18:35 +020019import traceback
20
21from oslo_concurrency import lockutils
22from oslo_log import log
23import six
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +030024from tempest import clients
Sam Wanc7b7f1f2015-11-25 00:22:28 -050025from tempest.common import credentials_factory as common_creds
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +020026from tempest.common import dynamic_creds
27from tempest import config
Ben Swartzlander1c4ff522016-03-02 22:16:23 -050028from tempest.lib.common.utils import data_utils
29from tempest.lib import exceptions
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +020030from tempest import test
Marc Koderer0abc93b2015-07-15 09:18:35 +020031
Yogeshbdb88102015-09-29 23:41:02 -040032from manila_tempest_tests.common import constants
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +030033from manila_tempest_tests.services.share.json import shares_client
34from manila_tempest_tests.services.share.v2.json import (
35 shares_client as shares_v2_client)
Marc Koderer0abc93b2015-07-15 09:18:35 +020036from manila_tempest_tests import share_exceptions
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020037from manila_tempest_tests import utils
Marc Koderer0abc93b2015-07-15 09:18:35 +020038
39CONF = config.CONF
40LOG = log.getLogger(__name__)
41
Valeriy Ponomaryov2abf5d72016-06-01 18:30:12 +030042# Test tags related to test direction
43TAG_POSITIVE = "positive"
44TAG_NEGATIVE = "negative"
45
46# Test tags related to service involvement
47TAG_API = "api"
48TAG_BACKEND = "backend"
49TAG_API_WITH_BACKEND = "api_with_backend"
50
51TAGS_MAPPER = {
52 "p": TAG_POSITIVE,
53 "n": TAG_NEGATIVE,
54 "a": TAG_API,
55 "b": TAG_BACKEND,
56 "ab": TAG_API_WITH_BACKEND,
57}
58TAGS_PATTERN = re.compile(
59 r"(?=.*\[.*\b(%(p)s|%(n)s)\b.*\])(?=.*\[.*\b(%(a)s|%(b)s|%(ab)s)\b.*\])" %
60 TAGS_MAPPER)
61
62
63def 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 Koderer0abc93b2015-07-15 09:18:35 +020073
74class 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
97def 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 Ponomaryovfcde7712015-12-14 18:06:13 +0200117skip_if_microversion_not_supported = utils.skip_if_microversion_not_supported
Xing Yang69b00b52015-11-22 16:10:44 -0500118skip_if_microversion_lt = utils.skip_if_microversion_lt
Valeriy Ponomaryova14c2252015-10-29 13:34:32 +0200119
120
Marc Koderer0abc93b2015-07-15 09:18:35 +0200121class BaseSharesTest(test.BaseTestCase):
122 """Base test case class for all Manila API tests."""
123
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300124 credentials = ('primary', )
Marc Koderer0abc93b2015-07-15 09:18:35 +0200125 force_tenant_isolation = False
John Spray061b1452015-11-18 13:15:32 +0000126 protocols = ["nfs", "cifs", "glusterfs", "hdfs", "cephfs"]
Marc Koderer0abc93b2015-07-15 09:18:35 +0200127
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 Ponomaryova14c2252015-10-29 13:34:32 +0200140 def skip_if_microversion_not_supported(self, microversion):
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +0200141 if not utils.is_microversion_supported(microversion):
Valeriy Ponomaryova14c2252015-10-29 13:34:32 +0200142 raise self.skipException(
143 "Microversion '%s' is not supported." % microversion)
144
Xing Yang69b00b52015-11-22 16:10:44 -0500145 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 Koderer0abc93b2015-07-15 09:18:35 +0200152 @classmethod
153 def get_client_with_isolated_creds(cls,
154 name=None,
155 type_of_creds="admin",
Clinton Knighte5c8f092015-08-27 15:00:23 -0400156 cleanup_in_class=False,
157 client_version='1'):
Marc Koderer0abc93b2015-07-15 09:18:35 +0200158 """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 Ponomaryov48a2bd72015-11-05 13:22:44 +0200174 ic = dynamic_creds.DynamicCredentialProvider(
175 identity_version=CONF.identity.auth_version,
176 name=name,
Sam Wanc7b7f1f2015-11-25 00:22:28 -0500177 admin_role=CONF.identity.admin_role,
Valeriy Ponomaryov0ddd29b2016-06-07 17:49:31 +0300178 admin_creds=common_creds.get_configured_admin_credentials())
Marc Koderer0abc93b2015-07-15 09:18:35 +0200179 if "admin" in type_of_creds:
180 creds = ic.get_admin_creds()
181 elif "alt" in type_of_creds:
182 creds = ic.get_alt_creds()
183 else:
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300184 creds = ic.get_credentials(type_of_creds)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200185 ic.type_of_creds = type_of_creds
186
187 # create client with isolated creds
188 os = clients.Manager(credentials=creds)
Clinton Knighte5c8f092015-08-27 15:00:23 -0400189 if client_version == '1':
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300190 client = shares_client.SharesClient(os.auth_provider)
Clinton Knighte5c8f092015-08-27 15:00:23 -0400191 elif client_version == '2':
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300192 client = shares_v2_client.SharesV2Client(os.auth_provider)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200193
194 # Set place where will be deleted isolated creds
195 ic_res = {
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +0200196 "method": ic.clear_creds,
Marc Koderer0abc93b2015-07-15 09:18:35 +0200197 "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:
206 if not CONF.service_available.neutron:
207 raise cls.skipException("Neutron support is required")
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +0200208 nc = os.networks_client
Marc Koderer0abc93b2015-07-15 09:18:35 +0200209 share_network_id = cls.provide_share_network(client, nc, ic)
210 client.share_network_id = share_network_id
211 resource = {
212 "type": "share_network",
213 "id": client.share_network_id,
214 "client": client,
215 }
216 if cleanup_in_class:
217 cls.class_resources.insert(0, resource)
218 else:
219 cls.method_resources.insert(0, resource)
220 return client
221
222 @classmethod
223 def verify_nonempty(cls, *args):
224 if not all(args):
225 msg = "Missing API credentials in configuration."
226 raise cls.skipException(msg)
227
228 @classmethod
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300229 def setup_clients(cls):
230 super(BaseSharesTest, cls).setup_clients()
231 os = getattr(cls, 'os_%s' % cls.credentials[0])
232 os.shares_client = shares_client.SharesClient(os.auth_provider)
233 cls.shares_client = os.shares_client
234 os.shares_v2_client = shares_v2_client.SharesV2Client(
235 os.auth_provider)
236 cls.shares_v2_client = os.shares_v2_client
237 if CONF.share.multitenancy_enabled:
238 if not CONF.service_available.neutron:
239 raise cls.skipException("Neutron support is required")
240 share_network_id = cls.provide_share_network(
241 cls.shares_v2_client, os.networks_client)
242 cls.shares_client.share_network_id = share_network_id
243 cls.shares_v2_client.share_network_id = share_network_id
244
245 @classmethod
Marc Koderer0abc93b2015-07-15 09:18:35 +0200246 def resource_setup(cls):
247 if not (any(p in CONF.share.enable_protocols
248 for p in cls.protocols) and
249 CONF.service_available.manila):
250 skip_msg = "Manila is disabled"
251 raise cls.skipException(skip_msg)
252 super(BaseSharesTest, cls).resource_setup()
Marc Koderer0abc93b2015-07-15 09:18:35 +0200253
254 def setUp(self):
255 super(BaseSharesTest, self).setUp()
Marc Koderer0abc93b2015-07-15 09:18:35 +0200256 self.addCleanup(self.clear_isolated_creds)
Valeriy Ponomaryovdd162cb2016-01-20 19:09:49 +0200257 self.addCleanup(self.clear_resources)
Valeriy Ponomaryov2abf5d72016-06-01 18:30:12 +0300258 verify_test_has_appropriate_tags(self)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200259
260 @classmethod
261 def resource_cleanup(cls):
262 super(BaseSharesTest, cls).resource_cleanup()
263 cls.clear_resources(cls.class_resources)
264 cls.clear_isolated_creds(cls.class_isolated_creds)
265
266 @classmethod
267 @network_synchronized
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +0200268 def provide_share_network(cls, shares_client, networks_client,
Marc Koderer0abc93b2015-07-15 09:18:35 +0200269 isolated_creds_client=None):
270 """Used for finding/creating share network for multitenant driver.
271
272 This method creates/gets entity share-network for one tenant. This
273 share-network will be used for creation of service vm.
274
275 :param shares_client: shares client, which requires share-network
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +0200276 :param networks_client: network client from same tenant as shares
277 :param isolated_creds_client: DynamicCredentialProvider instance
Marc Koderer0abc93b2015-07-15 09:18:35 +0200278 If provided, then its networking will be used if needed.
279 If not provided, then common network will be used if needed.
280 :returns: str -- share network id for shares_client tenant
281 :returns: None -- if single-tenant driver used
282 """
283
284 sc = shares_client
285
286 if not CONF.share.multitenancy_enabled:
287 # Assumed usage of a single-tenant driver
288 share_network_id = None
289 elif sc.share_network_id:
290 # Share-network already exists, use it
291 share_network_id = sc.share_network_id
292 else:
293 net_id = subnet_id = share_network_id = None
294
295 if not isolated_creds_client:
296 # Search for networks, created in previous runs
297 search_word = "reusable"
298 sn_name = "autogenerated_by_tempest_%s" % search_word
299 service_net_name = "share-service"
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +0200300 networks = networks_client.list_networks()
Marc Koderer0abc93b2015-07-15 09:18:35 +0200301 if "networks" in networks.keys():
302 networks = networks["networks"]
303 for network in networks:
304 if (service_net_name in network["name"] and
305 sc.tenant_id == network['tenant_id']):
306 net_id = network["id"]
307 if len(network["subnets"]) > 0:
308 subnet_id = network["subnets"][0]
309 break
310
311 # Create suitable network
312 if (net_id is None or subnet_id is None):
Valeriy Ponomaryov48a2bd72015-11-05 13:22:44 +0200313 ic = dynamic_creds.DynamicCredentialProvider(
314 identity_version=CONF.identity.auth_version,
315 name=service_net_name,
316 admin_role=CONF.identity.admin_role,
Valeriy Ponomaryov0ddd29b2016-06-07 17:49:31 +0300317 admin_creds=(
318 common_creds.get_configured_admin_credentials()))
Marc Koderer0abc93b2015-07-15 09:18:35 +0200319 net_data = ic._create_network_resources(sc.tenant_id)
320 network, subnet, router = net_data
321 net_id = network["id"]
322 subnet_id = subnet["id"]
323
324 # Try get suitable share-network
325 share_networks = sc.list_share_networks_with_detail()
326 for sn in share_networks:
327 if (net_id == sn["neutron_net_id"] and
328 subnet_id == sn["neutron_subnet_id"] and
329 sn["name"] and search_word in sn["name"]):
330 share_network_id = sn["id"]
331 break
332 else:
333 sn_name = "autogenerated_by_tempest_for_isolated_creds"
334 # Use precreated network and subnet from isolated creds
335 net_id = isolated_creds_client.get_credentials(
336 isolated_creds_client.type_of_creds).network['id']
337 subnet_id = isolated_creds_client.get_credentials(
338 isolated_creds_client.type_of_creds).subnet['id']
339
340 # Create suitable share-network
341 if share_network_id is None:
342 sn_desc = "This share-network was created by tempest"
343 sn = sc.create_share_network(name=sn_name,
344 description=sn_desc,
345 neutron_net_id=net_id,
346 neutron_subnet_id=subnet_id)
347 share_network_id = sn["id"]
348
349 return share_network_id
350
351 @classmethod
352 def _create_share(cls, share_protocol=None, size=1, name=None,
353 snapshot_id=None, description=None, metadata=None,
354 share_network_id=None, share_type_id=None,
Andrew Kerrbf31e912015-07-29 10:39:38 -0400355 consistency_group_id=None, client=None,
Clinton Knighte5c8f092015-08-27 15:00:23 -0400356 cleanup_in_class=True, is_public=False, **kwargs):
Valeriy Ponomaryov1aaa72d2015-09-08 12:59:41 +0300357 client = client or cls.shares_v2_client
Marc Koderer0abc93b2015-07-15 09:18:35 +0200358 description = description or "Tempest's share"
359 share_network_id = share_network_id or client.share_network_id or None
360 metadata = metadata or {}
Clinton Knighte5c8f092015-08-27 15:00:23 -0400361 kwargs.update({
Marc Koderer0abc93b2015-07-15 09:18:35 +0200362 'share_protocol': share_protocol,
363 'size': size,
364 'name': name,
365 'snapshot_id': snapshot_id,
366 'description': description,
367 'metadata': metadata,
368 'share_network_id': share_network_id,
369 'share_type_id': share_type_id,
370 'is_public': is_public,
Clinton Knighte5c8f092015-08-27 15:00:23 -0400371 })
Andrew Kerrbf31e912015-07-29 10:39:38 -0400372 if consistency_group_id:
373 kwargs['consistency_group_id'] = consistency_group_id
374
Marc Koderer0abc93b2015-07-15 09:18:35 +0200375 share = client.create_share(**kwargs)
Andrew Kerrbf31e912015-07-29 10:39:38 -0400376 resource = {"type": "share", "id": share["id"], "client": client,
377 "consistency_group_id": consistency_group_id}
Marc Koderer0abc93b2015-07-15 09:18:35 +0200378 cleanup_list = (cls.class_resources if cleanup_in_class else
379 cls.method_resources)
380 cleanup_list.insert(0, resource)
381 return share
382
383 @classmethod
Rodrigo Barbierie3305122016-02-03 14:32:24 -0200384 def migrate_share(cls, share_id, dest_host, client=None, notify=True,
385 wait_for_status='migration_success', **kwargs):
Clinton Knighte5c8f092015-08-27 15:00:23 -0400386 client = client or cls.shares_v2_client
Rodrigo Barbierie3305122016-02-03 14:32:24 -0200387 client.migrate_share(share_id, dest_host, notify, **kwargs)
388 share = client.wait_for_migration_status(
389 share_id, dest_host, wait_for_status,
390 version=kwargs.get('version'))
391 return share
392
393 @classmethod
394 def migration_complete(cls, share_id, dest_host, client=None, **kwargs):
395 client = client or cls.shares_v2_client
396 client.migration_complete(share_id, **kwargs)
397 share = client.wait_for_migration_status(
398 share_id, dest_host, 'migration_success',
399 version=kwargs.get('version'))
Rodrigo Barbierib7137ad2015-09-06 22:53:16 -0300400 return share
401
402 @classmethod
Marc Koderer0abc93b2015-07-15 09:18:35 +0200403 def create_share(cls, *args, **kwargs):
404 """Create one share and wait for available state. Retry if allowed."""
405 result = cls.create_shares([{"args": args, "kwargs": kwargs}])
406 return result[0]
407
408 @classmethod
409 def create_shares(cls, share_data_list):
410 """Creates several shares in parallel with retries.
411
412 Use this method when you want to create more than one share at same
413 time. Especially if config option 'share.share_creation_retry_number'
414 has value more than zero (0).
415 All shares will be expected to have 'available' status with or without
416 recreation else error will be raised.
417
418 :param share_data_list: list -- list of dictionaries with 'args' and
419 'kwargs' for '_create_share' method of this base class.
420 example of data:
421 share_data_list=[{'args': ['quuz'], 'kwargs': {'foo': 'bar'}}}]
422 :returns: list -- list of shares created using provided data.
423 """
424
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300425 for d in share_data_list:
Marc Koderer0abc93b2015-07-15 09:18:35 +0200426 if not isinstance(d, dict):
427 raise exceptions.TempestException(
428 "Expected 'dict', got '%s'" % type(d))
429 if "args" not in d:
430 d["args"] = []
431 if "kwargs" not in d:
432 d["kwargs"] = {}
433 if len(d) > 2:
434 raise exceptions.TempestException(
435 "Expected only 'args' and 'kwargs' keys. "
436 "Provided %s" % list(d))
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300437
438 data = []
439 for d in share_data_list:
440 client = d["kwargs"].pop("client", cls.shares_v2_client)
441 local_d = {
442 "args": d["args"],
443 "kwargs": copy.deepcopy(d["kwargs"]),
444 }
445 local_d["kwargs"]["client"] = client
446 local_d["share"] = cls._create_share(
447 *local_d["args"], **local_d["kwargs"])
448 local_d["cnt"] = 0
449 local_d["available"] = False
450 data.append(local_d)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200451
452 while not all(d["available"] for d in data):
453 for d in data:
454 if d["available"]:
455 continue
456 try:
457 d["kwargs"]["client"].wait_for_share_status(
458 d["share"]["id"], "available")
459 d["available"] = True
460 except (share_exceptions.ShareBuildErrorException,
461 exceptions.TimeoutException) as e:
462 if CONF.share.share_creation_retry_number > d["cnt"]:
463 d["cnt"] += 1
464 msg = ("Share '%s' failed to be built. "
465 "Trying create another." % d["share"]["id"])
466 LOG.error(msg)
467 LOG.error(e)
468 d["share"] = cls._create_share(
469 *d["args"], **d["kwargs"])
470 else:
471 raise e
472
473 return [d["share"] for d in data]
474
475 @classmethod
Andrew Kerrbf31e912015-07-29 10:39:38 -0400476 def create_consistency_group(cls, client=None, cleanup_in_class=True,
477 share_network_id=None, **kwargs):
Clinton Knighte5c8f092015-08-27 15:00:23 -0400478 client = client or cls.shares_v2_client
Goutham Pacha Ravi9221f5e2016-04-21 13:17:49 -0400479 if kwargs.get('source_cgsnapshot_id') is None:
480 kwargs['share_network_id'] = (share_network_id or
481 client.share_network_id or None)
Andrew Kerrbf31e912015-07-29 10:39:38 -0400482 consistency_group = client.create_consistency_group(**kwargs)
483 resource = {
484 "type": "consistency_group",
485 "id": consistency_group["id"],
486 "client": client}
487 if cleanup_in_class:
488 cls.class_resources.insert(0, resource)
489 else:
490 cls.method_resources.insert(0, resource)
491
492 if kwargs.get('source_cgsnapshot_id'):
493 new_cg_shares = client.list_shares(
494 detailed=True,
495 params={'consistency_group_id': consistency_group['id']})
496
497 for share in new_cg_shares:
498 resource = {"type": "share",
499 "id": share["id"],
500 "client": client,
501 "consistency_group_id": share.get(
502 'consistency_group_id')}
503 if cleanup_in_class:
504 cls.class_resources.insert(0, resource)
505 else:
506 cls.method_resources.insert(0, resource)
507
508 client.wait_for_consistency_group_status(consistency_group['id'],
509 'available')
510 return consistency_group
511
512 @classmethod
Marc Koderer0abc93b2015-07-15 09:18:35 +0200513 def create_snapshot_wait_for_active(cls, share_id, name=None,
514 description=None, force=False,
515 client=None, cleanup_in_class=True):
516 if client is None:
Yogesh1f931ff2015-09-29 23:41:02 -0400517 client = cls.shares_v2_client
Marc Koderer0abc93b2015-07-15 09:18:35 +0200518 if description is None:
519 description = "Tempest's snapshot"
520 snapshot = client.create_snapshot(share_id, name, description, force)
521 resource = {
522 "type": "snapshot",
523 "id": snapshot["id"],
524 "client": client,
525 }
526 if cleanup_in_class:
527 cls.class_resources.insert(0, resource)
528 else:
529 cls.method_resources.insert(0, resource)
530 client.wait_for_snapshot_status(snapshot["id"], "available")
531 return snapshot
532
533 @classmethod
Andrew Kerrbf31e912015-07-29 10:39:38 -0400534 def create_cgsnapshot_wait_for_active(cls, consistency_group_id,
535 name=None, description=None,
Clinton Knighte5c8f092015-08-27 15:00:23 -0400536 client=None, cleanup_in_class=True,
537 **kwargs):
538 client = client or cls.shares_v2_client
Andrew Kerrbf31e912015-07-29 10:39:38 -0400539 if description is None:
540 description = "Tempest's cgsnapshot"
Clinton Knighte5c8f092015-08-27 15:00:23 -0400541 cgsnapshot = client.create_cgsnapshot(consistency_group_id,
542 name=name,
543 description=description,
544 **kwargs)
Andrew Kerrbf31e912015-07-29 10:39:38 -0400545 resource = {
546 "type": "cgsnapshot",
547 "id": cgsnapshot["id"],
548 "client": client,
549 }
550 if cleanup_in_class:
551 cls.class_resources.insert(0, resource)
552 else:
553 cls.method_resources.insert(0, resource)
554 client.wait_for_cgsnapshot_status(cgsnapshot["id"], "available")
555 return cgsnapshot
556
557 @classmethod
Yogeshbdb88102015-09-29 23:41:02 -0400558 def get_availability_zones(cls, client=None):
559 """List the availability zones for "manila-share" services
560
561 that are currently in "up" state.
562 """
563 client = client or cls.shares_v2_client
564 cls.services = client.list_services()
565 zones = [service['zone'] for service in cls.services if
566 service['binary'] == "manila-share" and
567 service['state'] == 'up']
568 return zones
569
Yogesh1f931ff2015-09-29 23:41:02 -0400570 def get_pools_for_replication_domain(self):
571 # Get the list of pools for the replication domain
572 pools = self.admin_client.list_pools(detail=True)['pools']
573 instance_host = self.shares[0]['host']
574 host_pool = [p for p in pools if p['name'] == instance_host][0]
575 rep_domain = host_pool['capabilities']['replication_domain']
576 pools_in_rep_domain = [p for p in pools if p['capabilities'][
577 'replication_domain'] == rep_domain]
578 return rep_domain, pools_in_rep_domain
579
Yogeshbdb88102015-09-29 23:41:02 -0400580 @classmethod
581 def create_share_replica(cls, share_id, availability_zone, client=None,
582 cleanup_in_class=False, cleanup=True):
583 client = client or cls.shares_v2_client
584 replica = client.create_share_replica(share_id, availability_zone)
585 resource = {
586 "type": "share_replica",
587 "id": replica["id"],
588 "client": client,
589 "share_id": share_id,
590 }
591 # NOTE(Yogi1): Cleanup needs to be disabled during promotion tests.
592 if cleanup:
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_share_replica_status(
598 replica["id"], constants.STATUS_AVAILABLE)
599 return replica
600
601 @classmethod
602 def delete_share_replica(cls, replica_id, client=None):
603 client = client or cls.shares_v2_client
Yogesh1f931ff2015-09-29 23:41:02 -0400604 try:
605 client.delete_share_replica(replica_id)
606 client.wait_for_resource_deletion(replica_id=replica_id)
607 except exceptions.NotFound:
608 pass
Yogeshbdb88102015-09-29 23:41:02 -0400609
610 @classmethod
611 def promote_share_replica(cls, replica_id, client=None):
612 client = client or cls.shares_v2_client
613 replica = client.promote_share_replica(replica_id)
614 client.wait_for_share_replica_status(
615 replica["id"],
616 constants.REPLICATION_STATE_ACTIVE,
617 status_attr="replica_state")
618 return replica
619
620 @classmethod
Marc Koderer0abc93b2015-07-15 09:18:35 +0200621 def create_share_network(cls, client=None,
622 cleanup_in_class=False, **kwargs):
623 if client is None:
624 client = cls.shares_client
625 share_network = client.create_share_network(**kwargs)
626 resource = {
627 "type": "share_network",
628 "id": share_network["id"],
629 "client": client,
630 }
631 if cleanup_in_class:
632 cls.class_resources.insert(0, resource)
633 else:
634 cls.method_resources.insert(0, resource)
635 return share_network
636
637 @classmethod
638 def create_security_service(cls, ss_type="ldap", client=None,
639 cleanup_in_class=False, **kwargs):
640 if client is None:
641 client = cls.shares_client
642 security_service = client.create_security_service(ss_type, **kwargs)
643 resource = {
644 "type": "security_service",
645 "id": security_service["id"],
646 "client": client,
647 }
648 if cleanup_in_class:
649 cls.class_resources.insert(0, resource)
650 else:
651 cls.method_resources.insert(0, resource)
652 return security_service
653
654 @classmethod
655 def create_share_type(cls, name, is_public=True, client=None,
656 cleanup_in_class=True, **kwargs):
657 if client is None:
Valeriy Ponomaryova14c2252015-10-29 13:34:32 +0200658 client = cls.shares_v2_client
Marc Koderer0abc93b2015-07-15 09:18:35 +0200659 share_type = client.create_share_type(name, is_public, **kwargs)
660 resource = {
661 "type": "share_type",
662 "id": share_type["share_type"]["id"],
663 "client": client,
664 }
665 if cleanup_in_class:
666 cls.class_resources.insert(0, resource)
667 else:
668 cls.method_resources.insert(0, resource)
669 return share_type
670
671 @staticmethod
672 def add_required_extra_specs_to_dict(extra_specs=None):
Valeriy Ponomaryovad55dc52015-09-23 13:54:00 +0300673 dhss = six.text_type(CONF.share.multitenancy_enabled)
674 snapshot_support = six.text_type(
675 CONF.share.capability_snapshot_support)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200676 required = {
Valeriy Ponomaryovad55dc52015-09-23 13:54:00 +0300677 "driver_handles_share_servers": dhss,
678 "snapshot_support": snapshot_support,
Marc Koderer0abc93b2015-07-15 09:18:35 +0200679 }
680 if extra_specs:
681 required.update(extra_specs)
682 return required
683
684 @classmethod
685 def clear_isolated_creds(cls, creds=None):
686 if creds is None:
687 creds = cls.method_isolated_creds
688 for ic in creds:
689 if "deleted" not in ic.keys():
690 ic["deleted"] = False
691 if not ic["deleted"]:
692 with handle_cleanup_exceptions():
693 ic["method"]()
694 ic["deleted"] = True
695
696 @classmethod
Yogesh1f931ff2015-09-29 23:41:02 -0400697 def clear_share_replicas(cls, share_id, client=None):
698 client = client or cls.shares_v2_client
699 share_replicas = client.list_share_replicas(
700 share_id=share_id)
701
702 for replica in share_replicas:
703 try:
704 cls.delete_share_replica(replica['id'])
705 except exceptions.BadRequest:
706 # Ignore the exception due to deletion of last active replica
707 pass
708
709 @classmethod
Marc Koderer0abc93b2015-07-15 09:18:35 +0200710 def clear_resources(cls, resources=None):
711 """Deletes resources, that were created in test suites.
712
713 This method tries to remove resources from resource list,
714 if it is not found, assumed it was deleted in test itself.
715 It is expected, that all resources were added as LIFO
716 due to restriction of deletion resources, that is in the chain.
717
718 :param resources: dict with keys 'type','id','client' and 'deleted'
719 """
720
721 if resources is None:
722 resources = cls.method_resources
723 for res in resources:
724 if "deleted" not in res.keys():
725 res["deleted"] = False
726 if "client" not in res.keys():
727 res["client"] = cls.shares_client
728 if not(res["deleted"]):
729 res_id = res['id']
730 client = res["client"]
731 with handle_cleanup_exceptions():
732 if res["type"] is "share":
Yogesh1f931ff2015-09-29 23:41:02 -0400733 cls.clear_share_replicas(res_id)
Andrew Kerrbf31e912015-07-29 10:39:38 -0400734 cg_id = res.get('consistency_group_id')
735 if cg_id:
736 params = {'consistency_group_id': cg_id}
Clinton Knighte5c8f092015-08-27 15:00:23 -0400737 client.delete_share(res_id, params=params)
738 else:
739 client.delete_share(res_id)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200740 client.wait_for_resource_deletion(share_id=res_id)
741 elif res["type"] is "snapshot":
742 client.delete_snapshot(res_id)
743 client.wait_for_resource_deletion(snapshot_id=res_id)
744 elif res["type"] is "share_network":
745 client.delete_share_network(res_id)
746 client.wait_for_resource_deletion(sn_id=res_id)
747 elif res["type"] is "security_service":
748 client.delete_security_service(res_id)
749 client.wait_for_resource_deletion(ss_id=res_id)
750 elif res["type"] is "share_type":
751 client.delete_share_type(res_id)
752 client.wait_for_resource_deletion(st_id=res_id)
Andrew Kerrbf31e912015-07-29 10:39:38 -0400753 elif res["type"] is "consistency_group":
754 client.delete_consistency_group(res_id)
755 client.wait_for_resource_deletion(cg_id=res_id)
756 elif res["type"] is "cgsnapshot":
757 client.delete_cgsnapshot(res_id)
758 client.wait_for_resource_deletion(cgsnapshot_id=res_id)
Yogeshbdb88102015-09-29 23:41:02 -0400759 elif res["type"] is "share_replica":
760 client.delete_share_replica(res_id)
761 client.wait_for_resource_deletion(replica_id=res_id)
Marc Koderer0abc93b2015-07-15 09:18:35 +0200762 else:
huayue97bacbf2016-01-04 09:57:39 +0800763 LOG.warning("Provided unsupported resource type for "
764 "cleanup '%s'. Skipping." % res["type"])
Marc Koderer0abc93b2015-07-15 09:18:35 +0200765 res["deleted"] = True
766
767 @classmethod
768 def generate_share_network_data(self):
769 data = {
770 "name": data_utils.rand_name("sn-name"),
771 "description": data_utils.rand_name("sn-desc"),
772 "neutron_net_id": data_utils.rand_name("net-id"),
773 "neutron_subnet_id": data_utils.rand_name("subnet-id"),
774 }
775 return data
776
777 @classmethod
778 def generate_security_service_data(self):
779 data = {
780 "name": data_utils.rand_name("ss-name"),
781 "description": data_utils.rand_name("ss-desc"),
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +0200782 "dns_ip": utils.rand_ip(),
783 "server": utils.rand_ip(),
Marc Koderer0abc93b2015-07-15 09:18:35 +0200784 "domain": data_utils.rand_name("ss-domain"),
785 "user": data_utils.rand_name("ss-user"),
786 "password": data_utils.rand_name("ss-password"),
787 }
788 return data
789
790 # Useful assertions
791 def assertDictMatch(self, d1, d2, approx_equal=False, tolerance=0.001):
792 """Assert two dicts are equivalent.
793
794 This is a 'deep' match in the sense that it handles nested
795 dictionaries appropriately.
796
797 NOTE:
798
799 If you don't care (or don't know) a given value, you can specify
800 the string DONTCARE as the value. This will cause that dict-item
801 to be skipped.
802
803 """
804 def raise_assertion(msg):
805 d1str = str(d1)
806 d2str = str(d2)
807 base_msg = ('Dictionaries do not match. %(msg)s d1: %(d1str)s '
808 'd2: %(d2str)s' %
809 {"msg": msg, "d1str": d1str, "d2str": d2str})
810 raise AssertionError(base_msg)
811
812 d1keys = set(d1.keys())
813 d2keys = set(d2.keys())
814 if d1keys != d2keys:
815 d1only = d1keys - d2keys
816 d2only = d2keys - d1keys
817 raise_assertion('Keys in d1 and not d2: %(d1only)s. '
818 'Keys in d2 and not d1: %(d2only)s' %
819 {"d1only": d1only, "d2only": d2only})
820
821 for key in d1keys:
822 d1value = d1[key]
823 d2value = d2[key]
824 try:
825 error = abs(float(d1value) - float(d2value))
826 within_tolerance = error <= tolerance
827 except (ValueError, TypeError):
daiki kato6914b1a2016-03-16 17:16:57 +0900828 # If both values aren't convertible to float, just ignore
Marc Koderer0abc93b2015-07-15 09:18:35 +0200829 # ValueError if arg is a str, TypeError if it's something else
830 # (like None)
831 within_tolerance = False
832
833 if hasattr(d1value, 'keys') and hasattr(d2value, 'keys'):
834 self.assertDictMatch(d1value, d2value)
835 elif 'DONTCARE' in (d1value, d2value):
836 continue
837 elif approx_equal and within_tolerance:
838 continue
839 elif d1value != d2value:
840 raise_assertion("d1['%(key)s']=%(d1value)s != "
841 "d2['%(key)s']=%(d2value)s" %
842 {
843 "key": key,
844 "d1value": d1value,
845 "d2value": d2value
846 })
847
848
849class BaseSharesAltTest(BaseSharesTest):
850 """Base test case class for all Shares Alt API tests."""
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300851 credentials = ('alt', )
Marc Koderer0abc93b2015-07-15 09:18:35 +0200852
853
854class BaseSharesAdminTest(BaseSharesTest):
855 """Base test case class for all Shares Admin API tests."""
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300856 credentials = ('admin', )
857
858
859class BaseSharesMixedTest(BaseSharesTest):
860 """Base test case class for all Shares API tests with all user roles."""
861 credentials = ('primary', 'alt', 'admin')
Marc Koderer0abc93b2015-07-15 09:18:35 +0200862
863 @classmethod
Valeriy Ponomaryov39cdf722016-05-30 18:16:15 +0300864 def setup_clients(cls):
865 super(BaseSharesMixedTest, cls).setup_clients()
866 cls.admin_shares_client = shares_client.SharesClient(
867 cls.os_admin.auth_provider)
868 cls.admin_shares_v2_client = shares_v2_client.SharesV2Client(
869 cls.os_admin.auth_provider)
870 cls.alt_shares_client = shares_client.SharesClient(
871 cls.os_alt.auth_provider)
872 cls.alt_shares_v2_client = shares_v2_client.SharesV2Client(
873 cls.os_alt.auth_provider)
874
875 if CONF.share.multitenancy_enabled:
876 admin_share_network_id = cls.provide_share_network(
877 cls.admin_shares_v2_client, cls.os_admin.networks_client)
878 cls.admin_shares_client.share_network_id = admin_share_network_id
879 cls.admin_shares_v2_client.share_network_id = (
880 admin_share_network_id)
881
882 alt_share_network_id = cls.provide_share_network(
883 cls.alt_shares_v2_client, cls.os_alt.networks_client)
884 cls.alt_shares_client.share_network_id = alt_share_network_id
885 cls.alt_shares_v2_client.share_network_id = alt_share_network_id