blob: eb313d25e3fddd65408ab4c3126c6af3fe24a85a [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
Cindy Lu6bdc3ed2016-05-31 16:07:43 -070020from 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
Attila Fazekasdc216422013-01-29 15:12:14 +010028class BaseObjectTest(tempest.test.BaseTestCase):
dwalleck5d734432012-10-04 01:11:47 -050029
Andrea Frittoli (andreaf)825b2d32015-04-08 20:58:01 +010030 credentials = [['operator', CONF.object_storage.operator_role]]
31
dwalleck5d734432012-10-04 01:11:47 -050032 @classmethod
Rohan Kanadef30a5692015-02-18 01:43:31 +053033 def skip_checks(cls):
34 super(BaseObjectTest, cls).skip_checks()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000035 if not CONF.service_available.swift:
Matthew Treinish61e332b2013-07-19 16:42:31 -040036 skip_msg = ("%s skipped as swift is not available" % cls.__name__)
37 raise cls.skipException(skip_msg)
Rohan Kanadef30a5692015-02-18 01:43:31 +053038
39 @classmethod
40 def setup_credentials(cls):
41 cls.set_network_resources()
42 super(BaseObjectTest, cls).setup_credentials()
Andrea Frittoli (andreaf)825b2d32015-04-08 20:58:01 +010043 # credentials may be overwritten by children classes
44 if hasattr(cls, 'os_roles_operator'):
45 cls.os = cls.os_roles_operator
Matthew Treinish3fdb80c2013-08-15 11:13:19 -040046
Rohan Kanadef30a5692015-02-18 01:43:31 +053047 @classmethod
48 def setup_clients(cls):
49 super(BaseObjectTest, cls).setup_clients()
dwalleck5d734432012-10-04 01:11:47 -050050 cls.object_client = cls.os.object_client
51 cls.container_client = cls.os.container_client
52 cls.account_client = cls.os.account_client
ghanshyamf29831d2016-12-12 18:45:23 +090053 cls.capabilities_client = cls.os.capabilities_client
harika-vakadi2daed0a2013-01-01 20:51:39 +053054
Rohan Kanadef30a5692015-02-18 01:43:31 +053055 @classmethod
56 def resource_setup(cls):
57 super(BaseObjectTest, cls).resource_setup()
58
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000059 # Make sure we get fresh auth data after assigning swift role
60 cls.object_client.auth_provider.clear_auth()
61 cls.container_client.auth_provider.clear_auth()
62 cls.account_client.auth_provider.clear_auth()
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000063
Brian Obera212c4a2016-04-16 18:30:12 -050064 # make sure that discoverability is enabled and that the sections
65 # have not been disallowed by Swift
66 cls.policies = None
67
68 if CONF.object_storage_feature_enabled.discoverability:
ghanshyamf29831d2016-12-12 18:45:23 +090069 _, body = cls.capabilities_client.list_capabilities()
Brian Obera212c4a2016-04-16 18:30:12 -050070
71 if 'swift' in body and 'policies' in body['swift']:
72 cls.policies = body['swift']['policies']
73
Cindy Lu6bdc3ed2016-05-31 16:07:43 -070074 cls.containers = []
75
Martina Kollarovafd850b92013-05-20 15:40:13 +020076 @classmethod
Cindy Lu6bdc3ed2016-05-31 16:07:43 -070077 def create_container(cls):
78 # wrapper that returns a test container
79 container_name = data_utils.rand_name(name='TestContainer')
80 cls.container_client.create_container(container_name)
81 cls.containers.append(container_name)
82
83 return container_name
84
85 @classmethod
86 def create_object(cls, container_name, object_name=None,
87 data=None, metadata=None):
88 # wrapper that returns a test object
89 if object_name is None:
90 object_name = data_utils.rand_name(name='TestObject')
91 if data is None:
92 data = data_utils.arbitrary_string()
93 cls.object_client.create_object(container_name,
94 object_name,
95 data,
96 metadata=metadata)
97
98 return object_name, data
99
100 @classmethod
101 def delete_containers(cls, container_client=None,
Martina Kollarovafd850b92013-05-20 15:40:13 +0200102 object_client=None):
Cindy Lu6bdc3ed2016-05-31 16:07:43 -0700103 """Remove containers and all objects in them.
Martina Kollarovafd850b92013-05-20 15:40:13 +0200104
105 The containers should be visible from the container_client given.
106 Will not throw any error if the containers don't exist.
Fabien Boucherc3a9ba82014-01-03 13:17:05 +0100107 Will not check that object and container deletions succeed.
Arx Cruz7301e422016-12-01 17:27:03 +0100108 After delete all the objects from a container, it will wait 2
109 seconds before delete the container itself, in order to deployments
110 using HA proxy sync the deletion properly, otherwise, the container
111 might fail to be deleted because it's not empty.
Martina Kollarovafd850b92013-05-20 15:40:13 +0200112
Martina Kollarovafd850b92013-05-20 15:40:13 +0200113 :param container_client: if None, use cls.container_client, this means
114 that the default testing user will be used (see 'username' in
115 'etc/tempest.conf')
116 :param object_client: if None, use cls.object_client
117 """
118 if container_client is None:
119 container_client = cls.container_client
120 if object_client is None:
121 object_client = cls.object_client
Cindy Lu6bdc3ed2016-05-31 16:07:43 -0700122 for cont in cls.containers:
Martina Kollarovafd850b92013-05-20 15:40:13 +0200123 try:
zheng yin6736c2b2016-09-06 19:21:44 +0800124 params = {'limit': 9999, 'format': 'json'}
125 resp, objlist = container_client.list_container_contents(
126 cont, params)
Martina Kollarovafd850b92013-05-20 15:40:13 +0200127 # delete every object in the container
128 for obj in objlist:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100129 test_utils.call_and_ignore_notfound_exc(
130 object_client.delete_object, cont, obj['name'])
Arx Cruz7301e422016-12-01 17:27:03 +0100131 # sleep 2 seconds to sync the deletion of the objects
132 # in HA deployment
133 time.sleep(2)
Martina Kollarovafd850b92013-05-20 15:40:13 +0200134 container_client.delete_container(cont)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900135 except lib_exc.NotFound:
Martina Kollarovafd850b92013-05-20 15:40:13 +0200136 pass
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900137
138 def assertHeaders(self, resp, target, method):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000139 """Check the existence and the format of response headers"""
140
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900141 self.assertThat(resp, custom_matchers.ExistsAllResponseHeaders(
Brian Obera212c4a2016-04-16 18:30:12 -0500142 target, method, self.policies))
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900143 self.assertThat(resp, custom_matchers.AreAllWellFormatted())