blob: 11273e4594c7961f9369a596618e24db836ea5c6 [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
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000019from tempest import config
Ken'ichi Ohmichie8873232017-03-10 10:59:16 -080020from tempest.lib.common.utils import data_utils
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'}
Ferenc Horváthe52bee72017-06-14 15:02:23 +020046 _, objlist = container_client.list_container_contents(cont, params)
Andrea Frittolid3b8e672016-12-16 15:30:44 +000047 # delete every object in the container
48 for obj in objlist:
49 test_utils.call_and_ignore_notfound_exc(
50 object_client.delete_object, cont, obj['name'])
51 # sleep 2 seconds to sync the deletion of the objects
52 # in HA deployment
53 time.sleep(2)
54 container_client.delete_container(cont)
55 except lib_exc.NotFound:
56 pass
57
58
Attila Fazekasdc216422013-01-29 15:12:14 +010059class BaseObjectTest(tempest.test.BaseTestCase):
dwalleck5d734432012-10-04 01:11:47 -050060
Andrea Frittoli (andreaf)825b2d32015-04-08 20:58:01 +010061 credentials = [['operator', CONF.object_storage.operator_role]]
62
dwalleck5d734432012-10-04 01:11:47 -050063 @classmethod
Rohan Kanadef30a5692015-02-18 01:43:31 +053064 def skip_checks(cls):
65 super(BaseObjectTest, cls).skip_checks()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000066 if not CONF.service_available.swift:
Matthew Treinish61e332b2013-07-19 16:42:31 -040067 skip_msg = ("%s skipped as swift is not available" % cls.__name__)
68 raise cls.skipException(skip_msg)
Rohan Kanadef30a5692015-02-18 01:43:31 +053069
70 @classmethod
71 def setup_credentials(cls):
72 cls.set_network_resources()
73 super(BaseObjectTest, cls).setup_credentials()
Andrea Frittoli (andreaf)825b2d32015-04-08 20:58:01 +010074 # credentials may be overwritten by children classes
75 if hasattr(cls, 'os_roles_operator'):
76 cls.os = cls.os_roles_operator
Matthew Treinish3fdb80c2013-08-15 11:13:19 -040077
Rohan Kanadef30a5692015-02-18 01:43:31 +053078 @classmethod
79 def setup_clients(cls):
80 super(BaseObjectTest, cls).setup_clients()
Jordan Pittier8160d312017-04-18 11:52:23 +020081 cls.object_client = cls.os_roles_operator.object_client
82 cls.bulk_client = cls.os_roles_operator.bulk_client
83 cls.container_client = cls.os_roles_operator.container_client
84 cls.account_client = cls.os_roles_operator.account_client
85 cls.capabilities_client = cls.os_roles_operator.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:
ghanshyam17d8a482017-07-21 03:10:48 +0000101 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())