blob: 2e39cf9451801dbf00cc8f7f7228f0d2a3435a4d [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
dwalleck5d734432012-10-04 01:11:47 -050016
Masayuki Igawa911148b2014-02-17 15:00:54 +090017from tempest.api.identity import base
Matthew Treinish481466b2012-12-20 17:16:01 -050018from tempest import clients
Daisuke Morita8e1f8612013-11-26 15:43:21 +090019from tempest.common import custom_matchers
Matthew Treinish3fdb80c2013-08-15 11:13:19 -040020from tempest.common import isolated_creds
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000021from tempest import config
dwalleck5d734432012-10-04 01:11:47 -050022from tempest import exceptions
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
30 @classmethod
Andrea Frittoli5f13f712014-09-15 13:14:54 +010031 def resource_setup(cls):
Sylvain Afchain11b99d02014-01-16 00:42:33 +010032 cls.set_network_resources()
Andrea Frittoli5f13f712014-09-15 13:14:54 +010033 super(BaseObjectTest, cls).resource_setup()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000034 if not CONF.service_available.swift:
Matthew Treinish61e332b2013-07-19 16:42:31 -040035 skip_msg = ("%s skipped as swift is not available" % cls.__name__)
36 raise cls.skipException(skip_msg)
Matthew Treinish9f756a02014-01-15 10:26:07 -050037 cls.isolated_creds = isolated_creds.IsolatedCreds(
38 cls.__name__, network_resources=cls.network_resources)
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010039 # Get isolated creds for normal user
40 cls.os = clients.Manager(cls.isolated_creds.get_primary_creds())
41 # Get isolated creds for admin user
42 cls.os_admin = clients.Manager(cls.isolated_creds.get_admin_creds())
43 # Get isolated creds for alt user
44 cls.os_alt = clients.Manager(cls.isolated_creds.get_alt_creds())
Matthew Treinish3fdb80c2013-08-15 11:13:19 -040045
dwalleck5d734432012-10-04 01:11:47 -050046 cls.object_client = cls.os.object_client
47 cls.container_client = cls.os.container_client
48 cls.account_client = cls.os.account_client
harika-vakadi1a9ad612012-12-14 19:12:08 +053049 cls.custom_object_client = cls.os.custom_object_client
harika-vakadi2daed0a2013-01-01 20:51:39 +053050 cls.token_client = cls.os_admin.token_client
Attila Fazekas407b6db2013-01-19 12:48:36 +010051 cls.identity_admin_client = cls.os_admin.identity_client
harika-vakadi2daed0a2013-01-01 20:51:39 +053052 cls.custom_account_client = cls.os.custom_account_client
harika-vakadi2daed0a2013-01-01 20:51:39 +053053 cls.object_client_alt = cls.os_alt.object_client
54 cls.container_client_alt = cls.os_alt.container_client
Attila Fazekas407b6db2013-01-19 12:48:36 +010055 cls.identity_client_alt = cls.os_alt.identity_client
harika-vakadi2daed0a2013-01-01 20:51:39 +053056
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000057 # Make sure we get fresh auth data after assigning swift role
58 cls.object_client.auth_provider.clear_auth()
59 cls.container_client.auth_provider.clear_auth()
60 cls.account_client.auth_provider.clear_auth()
61 cls.custom_object_client.auth_provider.clear_auth()
62 cls.custom_account_client.auth_provider.clear_auth()
63 cls.object_client_alt.auth_provider.clear_auth()
64 cls.container_client_alt.auth_provider.clear_auth()
65
Daisuke Morita41a5cf42014-08-26 19:39:15 +090066 cls.data = SwiftDataGenerator(cls.identity_admin_client)
dwalleck5d734432012-10-04 01:11:47 -050067
Martina Kollarovafd850b92013-05-20 15:40:13 +020068 @classmethod
Andrea Frittoli5f13f712014-09-15 13:14:54 +010069 def resource_cleanup(cls):
Daisuke Morita41a5cf42014-08-26 19:39:15 +090070 cls.data.teardown_all()
Matthew Treinishb7360f72013-09-13 15:40:11 +000071 cls.isolated_creds.clear_isolated_creds()
Andrea Frittoli5f13f712014-09-15 13:14:54 +010072 super(BaseObjectTest, cls).resource_cleanup()
Matthew Treinishb7360f72013-09-13 15:40:11 +000073
74 @classmethod
Martina Kollarovafd850b92013-05-20 15:40:13 +020075 def delete_containers(cls, containers, container_client=None,
76 object_client=None):
77 """Remove given containers and all objects in them.
78
79 The containers should be visible from the container_client given.
80 Will not throw any error if the containers don't exist.
Fabien Boucherc3a9ba82014-01-03 13:17:05 +010081 Will not check that object and container deletions succeed.
Martina Kollarovafd850b92013-05-20 15:40:13 +020082
83 :param containers: list of container names to remove
84 :param container_client: if None, use cls.container_client, this means
85 that the default testing user will be used (see 'username' in
86 'etc/tempest.conf')
87 :param object_client: if None, use cls.object_client
88 """
89 if container_client is None:
90 container_client = cls.container_client
91 if object_client is None:
92 object_client = cls.object_client
93 for cont in containers:
94 try:
95 objlist = container_client.list_all_container_objects(cont)
96 # delete every object in the container
97 for obj in objlist:
Ala Rezmeritaa81af8c2014-02-18 16:01:48 +010098 try:
99 object_client.delete_object(cont, obj['name'])
100 except exceptions.NotFound:
101 pass
Martina Kollarovafd850b92013-05-20 15:40:13 +0200102 container_client.delete_container(cont)
103 except exceptions.NotFound:
104 pass
Daisuke Morita8e1f8612013-11-26 15:43:21 +0900105
106 def assertHeaders(self, resp, target, method):
107 """
108 Common method to check the existence and the format of common response
109 headers
110 """
111 self.assertThat(resp, custom_matchers.ExistsAllResponseHeaders(
112 target, method))
113 self.assertThat(resp, custom_matchers.AreAllWellFormatted())
Daisuke Morita41a5cf42014-08-26 19:39:15 +0900114
115
116class SwiftDataGenerator(base.DataGenerator):
117
118 def setup_test_user(self, reseller=False):
119 super(SwiftDataGenerator, self).setup_test_user()
120 if reseller:
121 role_name = CONF.object_storage.reseller_admin_role
122 else:
123 role_name = CONF.object_storage.operator_role
124 role_id = self._get_role_id(role_name)
125 self._assign_role(role_id)
126
127 def _get_role_id(self, role_name):
128 try:
129 _, roles = self.client.list_roles()
130 return next(r['id'] for r in roles if r['name'] == role_name)
131 except StopIteration:
132 msg = "Role name '%s' is not found" % role_name
133 raise exceptions.NotFound(msg)
134
135 def _assign_role(self, role_id):
136 self.client.assign_user_role(self.tenant['id'],
137 self.user['id'],
138 role_id)