ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 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 | |
Arx Cruz | 7301e42 | 2016-12-01 17:27:03 +0100 | [diff] [blame] | 16 | import time |
| 17 | |
Daisuke Morita | 8e1f861 | 2013-11-26 15:43:21 +0900 | [diff] [blame] | 18 | from tempest.common import custom_matchers |
Joshua White | 0244697 | 2017-01-27 13:02:27 -0800 | [diff] [blame] | 19 | from tempest.common.utils import data_utils |
Matthew Treinish | cb09bbb | 2014-01-29 18:20:25 +0000 | [diff] [blame] | 20 | from tempest import config |
Jordan Pittier | 9e227c5 | 2016-02-09 14:35:18 +0100 | [diff] [blame] | 21 | from tempest.lib.common.utils import test_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 23 | import tempest.test |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 24 | |
Matthew Treinish | cb09bbb | 2014-01-29 18:20:25 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 27 | |
Andrea Frittoli | d3b8e67 | 2016-12-16 15:30:44 +0000 | [diff] [blame] | 28 | def delete_containers(containers, container_client, object_client): |
| 29 | """Remove containers and all objects in them. |
| 30 | |
| 31 | The containers should be visible from the container_client given. |
| 32 | Will not throw any error if the containers don't exist. |
| 33 | Will not check that object and container deletions succeed. |
| 34 | After delete all the objects from a container, it will wait 2 |
| 35 | seconds before delete the container itself, in order to deployments |
| 36 | using HA proxy sync the deletion properly, otherwise, the container |
| 37 | might fail to be deleted because it's not empty. |
| 38 | |
| 39 | :param containers: List of containers to be deleted |
| 40 | :param container_client: Client to be used to delete containers |
| 41 | :param object_client: Client to be used to delete objects |
| 42 | """ |
| 43 | for cont in containers: |
| 44 | try: |
| 45 | params = {'limit': 9999, 'format': 'json'} |
| 46 | resp, objlist = container_client.list_container_contents( |
| 47 | cont, params) |
| 48 | # delete every object in the container |
| 49 | for obj in objlist: |
| 50 | test_utils.call_and_ignore_notfound_exc( |
| 51 | object_client.delete_object, cont, obj['name']) |
| 52 | # sleep 2 seconds to sync the deletion of the objects |
| 53 | # in HA deployment |
| 54 | time.sleep(2) |
| 55 | container_client.delete_container(cont) |
| 56 | except lib_exc.NotFound: |
| 57 | pass |
| 58 | |
| 59 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 60 | class BaseObjectTest(tempest.test.BaseTestCase): |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 61 | |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 62 | credentials = [['operator', CONF.object_storage.operator_role]] |
| 63 | |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 64 | @classmethod |
Rohan Kanade | f30a569 | 2015-02-18 01:43:31 +0530 | [diff] [blame] | 65 | def skip_checks(cls): |
| 66 | super(BaseObjectTest, cls).skip_checks() |
Matthew Treinish | cb09bbb | 2014-01-29 18:20:25 +0000 | [diff] [blame] | 67 | if not CONF.service_available.swift: |
Matthew Treinish | 61e332b | 2013-07-19 16:42:31 -0400 | [diff] [blame] | 68 | skip_msg = ("%s skipped as swift is not available" % cls.__name__) |
| 69 | raise cls.skipException(skip_msg) |
Rohan Kanade | f30a569 | 2015-02-18 01:43:31 +0530 | [diff] [blame] | 70 | |
| 71 | @classmethod |
| 72 | def setup_credentials(cls): |
| 73 | cls.set_network_resources() |
| 74 | super(BaseObjectTest, cls).setup_credentials() |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 75 | # credentials may be overwritten by children classes |
| 76 | if hasattr(cls, 'os_roles_operator'): |
| 77 | cls.os = cls.os_roles_operator |
Matthew Treinish | 3fdb80c | 2013-08-15 11:13:19 -0400 | [diff] [blame] | 78 | |
Rohan Kanade | f30a569 | 2015-02-18 01:43:31 +0530 | [diff] [blame] | 79 | @classmethod |
| 80 | def setup_clients(cls): |
| 81 | super(BaseObjectTest, cls).setup_clients() |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 82 | cls.object_client = cls.os.object_client |
| 83 | cls.container_client = cls.os.container_client |
| 84 | cls.account_client = cls.os.account_client |
ghanshyam | f29831d | 2016-12-12 18:45:23 +0900 | [diff] [blame] | 85 | cls.capabilities_client = cls.os.capabilities_client |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 86 | |
Rohan Kanade | f30a569 | 2015-02-18 01:43:31 +0530 | [diff] [blame] | 87 | @classmethod |
| 88 | def resource_setup(cls): |
| 89 | super(BaseObjectTest, cls).resource_setup() |
| 90 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 91 | # Make sure we get fresh auth data after assigning swift role |
| 92 | cls.object_client.auth_provider.clear_auth() |
| 93 | cls.container_client.auth_provider.clear_auth() |
| 94 | cls.account_client.auth_provider.clear_auth() |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 95 | |
Brian Ober | a212c4a | 2016-04-16 18:30:12 -0500 | [diff] [blame] | 96 | # make sure that discoverability is enabled and that the sections |
| 97 | # have not been disallowed by Swift |
| 98 | cls.policies = None |
| 99 | |
| 100 | if CONF.object_storage_feature_enabled.discoverability: |
ghanshyam | f29831d | 2016-12-12 18:45:23 +0900 | [diff] [blame] | 101 | _, body = cls.capabilities_client.list_capabilities() |
Brian Ober | a212c4a | 2016-04-16 18:30:12 -0500 | [diff] [blame] | 102 | |
| 103 | if 'swift' in body and 'policies' in body['swift']: |
| 104 | cls.policies = body['swift']['policies'] |
| 105 | |
Cindy Lu | 6bdc3ed | 2016-05-31 16:07:43 -0700 | [diff] [blame] | 106 | cls.containers = [] |
| 107 | |
Martina Kollarova | fd850b9 | 2013-05-20 15:40:13 +0200 | [diff] [blame] | 108 | @classmethod |
Cindy Lu | 6bdc3ed | 2016-05-31 16:07:43 -0700 | [diff] [blame] | 109 | def create_container(cls): |
| 110 | # wrapper that returns a test container |
| 111 | container_name = data_utils.rand_name(name='TestContainer') |
| 112 | cls.container_client.create_container(container_name) |
| 113 | cls.containers.append(container_name) |
| 114 | |
| 115 | return container_name |
| 116 | |
| 117 | @classmethod |
| 118 | def create_object(cls, container_name, object_name=None, |
| 119 | data=None, metadata=None): |
| 120 | # wrapper that returns a test object |
| 121 | if object_name is None: |
| 122 | object_name = data_utils.rand_name(name='TestObject') |
| 123 | if data is None: |
Jordan Pittier | b84f2d4 | 2016-12-21 19:02:15 +0100 | [diff] [blame] | 124 | data = data_utils.random_bytes() |
Cindy Lu | 6bdc3ed | 2016-05-31 16:07:43 -0700 | [diff] [blame] | 125 | cls.object_client.create_object(container_name, |
| 126 | object_name, |
| 127 | data, |
| 128 | metadata=metadata) |
| 129 | |
| 130 | return object_name, data |
| 131 | |
| 132 | @classmethod |
Andrea Frittoli | d3b8e67 | 2016-12-16 15:30:44 +0000 | [diff] [blame] | 133 | def delete_containers(cls, container_client=None, object_client=None): |
Martina Kollarova | fd850b9 | 2013-05-20 15:40:13 +0200 | [diff] [blame] | 134 | if container_client is None: |
| 135 | container_client = cls.container_client |
| 136 | if object_client is None: |
| 137 | object_client = cls.object_client |
Andrea Frittoli | d3b8e67 | 2016-12-16 15:30:44 +0000 | [diff] [blame] | 138 | delete_containers(cls.containers, container_client, object_client) |
Daisuke Morita | 8e1f861 | 2013-11-26 15:43:21 +0900 | [diff] [blame] | 139 | |
| 140 | def assertHeaders(self, resp, target, method): |
Ken'ichi Ohmichi | 9e3dac0 | 2015-11-19 07:01:07 +0000 | [diff] [blame] | 141 | """Check the existence and the format of response headers""" |
| 142 | |
Daisuke Morita | 8e1f861 | 2013-11-26 15:43:21 +0900 | [diff] [blame] | 143 | self.assertThat(resp, custom_matchers.ExistsAllResponseHeaders( |
Brian Ober | a212c4a | 2016-04-16 18:30:12 -0500 | [diff] [blame] | 144 | target, method, self.policies)) |
Daisuke Morita | 8e1f861 | 2013-11-26 15:43:21 +0900 | [diff] [blame] | 145 | self.assertThat(resp, custom_matchers.AreAllWellFormatted()) |