blob: 642c1acbe9b9d2c1064f40ab99ded7fe92f3840e [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwalleck5d734432012-10-04 01:11:47 -05002# 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 Cruz7301e422016-12-01 17:27:03 +010016import time
17
Daisuke Morita8e1f8612013-11-26 15:43:21 +090018from tempest.common import custom_matchers
Joshua White02446972017-01-27 13:02:27 -080019from tempest.common.utils import data_utils
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000020from tempest import config
Jordan Pittier9e227c52016-02-09 14:35:18 +010021from tempest.lib.common.utils import test_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050022from tempest.lib import exceptions as lib_exc
Attila Fazekasdc216422013-01-29 15:12:14 +010023import tempest.test
dwalleck5d734432012-10-04 01:11:47 -050024
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000025CONF = config.CONF
26
dwalleck5d734432012-10-04 01:11:47 -050027
Andrea Frittolid3b8e672016-12-16 15:30:44 +000028def 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 Fazekasdc216422013-01-29 15:12:14 +010060class BaseObjectTest(tempest.test.BaseTestCase):
dwalleck5d734432012-10-04 01:11:47 -050061
Andrea Frittoli (andreaf)825b2d32015-04-08 20:58:01 +010062 credentials = [['operator', CONF.object_storage.operator_role]]
63
dwalleck5d734432012-10-04 01:11:47 -050064 @classmethod
Rohan Kanadef30a5692015-02-18 01:43:31 +053065 def skip_checks(cls):
66 super(BaseObjectTest, cls).skip_checks()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000067 if not CONF.service_available.swift:
Matthew Treinish61e332b2013-07-19 16:42:31 -040068 skip_msg = ("%s skipped as swift is not available" % cls.__name__)
69 raise cls.skipException(skip_msg)
Rohan Kanadef30a5692015-02-18 01:43:31 +053070
71 @classmethod
72 def setup_credentials(cls):
73 cls.set_network_resources()
74 super(BaseObjectTest, cls).setup_credentials()
Andrea Frittoli (andreaf)825b2d32015-04-08 20:58:01 +010075 # credentials may be overwritten by children classes
76 if hasattr(cls, 'os_roles_operator'):
77 cls.os = cls.os_roles_operator
Matthew Treinish3fdb80c2013-08-15 11:13:19 -040078
Rohan Kanadef30a5692015-02-18 01:43:31 +053079 @classmethod
80 def setup_clients(cls):
81 super(BaseObjectTest, cls).setup_clients()
dwalleck5d734432012-10-04 01:11:47 -050082 cls.object_client = cls.os.object_client
83 cls.container_client = cls.os.container_client
84 cls.account_client = cls.os.account_client
ghanshyamf29831d2016-12-12 18:45:23 +090085 cls.capabilities_client = cls.os.capabilities_client
harika-vakadi2daed0a2013-01-01 20:51:39 +053086
Rohan Kanadef30a5692015-02-18 01:43:31 +053087 @classmethod
88 def resource_setup(cls):
89 super(BaseObjectTest, cls).resource_setup()
90
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000091 # 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 Frittoli8bbdb162014-01-06 11:06:13 +000095
Brian Obera212c4a2016-04-16 18:30:12 -050096 # 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:
ghanshyamf29831d2016-12-12 18:45:23 +0900101 _, body = cls.capabilities_client.list_capabilities()
Brian Obera212c4a2016-04-16 18:30:12 -0500102
103 if 'swift' in body and 'policies' in body['swift']:
104 cls.policies = body['swift']['policies']
105
Cindy Lu6bdc3ed2016-05-31 16:07:43 -0700106 cls.containers = []
107
Martina Kollarovafd850b92013-05-20 15:40:13 +0200108 @classmethod
Cindy Lu6bdc3ed2016-05-31 16:07:43 -0700109 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 Pittierb84f2d42016-12-21 19:02:15 +0100124 data = data_utils.random_bytes()
Cindy Lu6bdc3ed2016-05-31 16:07:43 -0700125 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 Frittolid3b8e672016-12-16 15:30:44 +0000133 def delete_containers(cls, container_client=None, object_client=None):
Martina Kollarovafd850b92013-05-20 15:40:13 +0200134 if container_client is None:
135 container_client = cls.container_client
136 if object_client is None:
137 object_client = cls.object_client
Andrea Frittolid3b8e672016-12-16 15:30:44 +0000138 delete_containers(cls.containers, container_client, object_client)
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900139
140 def assertHeaders(self, resp, target, method):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000141 """Check the existence and the format of response headers"""
142
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900143 self.assertThat(resp, custom_matchers.ExistsAllResponseHeaders(
Brian Obera212c4a2016-04-16 18:30:12 -0500144 target, method, self.policies))
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900145 self.assertThat(resp, custom_matchers.AreAllWellFormatted())