blob: 9515788f24a4bf48ec33a6c22a503cb83c828e72 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipesf38eaac2012-06-21 13:37:35 -04002# 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
Fei Long Wangd39431f2015-05-14 11:30:48 +120016from tempest.common.utils import data_utils
Matthew Treinishdb2c5972014-01-31 22:18:59 +000017from tempest import config
Attila Fazekasdc216422013-01-29 15:12:14 +010018import tempest.test
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070019
Matthew Treinishdb2c5972014-01-31 22:18:59 +000020CONF = config.CONF
21
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070022
Chris Hoge4f6117a2015-03-20 12:39:33 -050023class BaseIdentityTest(tempest.test.BaseTestCase):
Jay Pipesf38eaac2012-06-21 13:37:35 -040024
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070025 @classmethod
Jordan Pittierf7da5e52016-09-06 18:00:52 +020026 def setup_credentials(cls):
27 # Create no network resources for these test.
28 cls.set_network_resources()
29 super(BaseIdentityTest, cls).setup_credentials()
30
31 @classmethod
Matthew Treinishdb2c5972014-01-31 22:18:59 +000032 def disable_user(cls, user_name):
33 user = cls.get_user_by_name(user_name)
ghanshyam9c257a72016-06-21 10:15:10 +090034 cls.users_client.update_user_enabled(user['id'], enabled=False)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070035
Matthew Treinishdb2c5972014-01-31 22:18:59 +000036 @classmethod
37 def disable_tenant(cls, tenant_name):
38 tenant = cls.get_tenant_by_name(tenant_name)
Daniel Melladob04da902015-11-20 17:43:12 +010039 cls.tenants_client.update_tenant(tenant['id'], enabled=False)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070040
Matthew Treinishdb2c5972014-01-31 22:18:59 +000041 @classmethod
Tom Cocozzello5544c172016-02-23 17:50:28 -060042 def get_user_by_name(cls, name, domain_id=None):
43 if domain_id:
44 params = {'domain_id': domain_id}
ghanshyam7f817db2016-08-01 18:37:13 +090045 users = cls.users_client.list_users(**params)['users']
Tom Cocozzello5544c172016-02-23 17:50:28 -060046 else:
47 users = cls.users_client.list_users()['users']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070048 user = [u for u in users if u['name'] == name]
49 if len(user) > 0:
50 return user[0]
51
Matthew Treinishdb2c5972014-01-31 22:18:59 +000052 @classmethod
53 def get_tenant_by_name(cls, name):
54 try:
Daniel Melladob04da902015-11-20 17:43:12 +010055 tenants = cls.tenants_client.list_tenants()['tenants']
Matthew Treinishdb2c5972014-01-31 22:18:59 +000056 except AttributeError:
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -060057 tenants = cls.projects_client.list_projects()['projects']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070058 tenant = [t for t in tenants if t['name'] == name]
59 if len(tenant) > 0:
60 return tenant[0]
61
Matthew Treinishdb2c5972014-01-31 22:18:59 +000062 @classmethod
63 def get_role_by_name(cls, name):
Daniel Mellado6b16b922015-12-07 12:43:08 +000064 roles = cls.roles_client.list_roles()['roles']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070065 role = [r for r in roles if r['name'] == name]
66 if len(role) > 0:
67 return role[0]
68
Castulo J. Martineze3adee42016-07-14 10:40:08 -070069 def _create_test_user(self, **kwargs):
70 if kwargs['password'] is None:
71 user_password = data_utils.rand_password()
72 kwargs['password'] = user_password
73 user = self.users_client.create_user(**kwargs)['user']
74 # Delete the user at the end of the test
75 self.addCleanup(self.users_client.delete_user, user['id'])
76 return user
77
78 def setup_test_role(self):
79 """Set up a test role."""
80 role = self.roles_client.create_role(
81 name=data_utils.rand_name('test_role'))['role']
82 # Delete the role at the end of the test
83 self.addCleanup(self.roles_client.delete_role, role['id'])
84 return role
85
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070086
Chris Hoge4f6117a2015-03-20 12:39:33 -050087class BaseIdentityV2Test(BaseIdentityTest):
88
Andrea Frittolib21de6c2015-02-06 20:12:38 +000089 credentials = ['primary']
90
Andrea Frittoli (andreaf)41601412015-05-12 16:39:03 +010091 # identity v2 tests should obtain tokens and create accounts via v2
92 # regardless of the configured CONF.identity.auth_version
93 identity_version = 'v2'
Rohan Kanadeb645e172015-02-05 17:38:59 +053094
95 @classmethod
96 def setup_clients(cls):
Chris Hoge4f6117a2015-03-20 12:39:33 -050097 super(BaseIdentityV2Test, cls).setup_clients()
Jane Zadorozhnabfc72372015-06-16 17:32:59 +030098 cls.non_admin_client = cls.os.identity_public_client
Chris Hoge4f6117a2015-03-20 12:39:33 -050099 cls.non_admin_token_client = cls.os.token_client
Daniel Melladob04da902015-11-20 17:43:12 +0100100 cls.non_admin_tenants_client = cls.os.tenants_public_client
Daniel Mellado82c83a52015-12-09 15:16:49 +0000101 cls.non_admin_users_client = cls.os.users_public_client
Chris Hoge4f6117a2015-03-20 12:39:33 -0500102
Chris Hoge4f6117a2015-03-20 12:39:33 -0500103
104class BaseIdentityV2AdminTest(BaseIdentityV2Test):
105
Andrea Frittoli (andreaf)41601412015-05-12 16:39:03 +0100106 credentials = ['primary', 'admin']
Chris Hoge4f6117a2015-03-20 12:39:33 -0500107
Andrea Frittoli00882b62016-12-19 23:22:44 +0000108 # NOTE(andreaf) Identity tests work with credentials, so it is safer
109 # for them to always use disposable credentials. Forcing dynamic creds
110 # on regular identity tests would be however to restrictive, since it
111 # would prevent any identity test from being executed against clouds where
112 # admin credentials are not available.
113 # Since All admin tests require admin credentials to be
114 # executed, so this will not impact the ability to execute tests.
115 force_tenant_isolation = True
116
Chris Hoge4f6117a2015-03-20 12:39:33 -0500117 @classmethod
118 def setup_clients(cls):
Rohan Kanadeb645e172015-02-05 17:38:59 +0530119 super(BaseIdentityV2AdminTest, cls).setup_clients()
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000120 cls.client = cls.os_adm.identity_client
Jane Zadorozhnabfc72372015-06-16 17:32:59 +0300121 cls.non_admin_client = cls.os.identity_client
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000122 cls.token_client = cls.os_adm.token_client
Daniel Melladob04da902015-11-20 17:43:12 +0100123 cls.tenants_client = cls.os_adm.tenants_client
124 cls.non_admin_tenants_client = cls.os.tenants_client
Daniel Mellado6b16b922015-12-07 12:43:08 +0000125 cls.roles_client = cls.os_adm.roles_client
126 cls.non_admin_roles_client = cls.os.roles_client
Daniel Mellado82c83a52015-12-09 15:16:49 +0000127 cls.users_client = cls.os_adm.users_client
128 cls.non_admin_users_client = cls.os.users_client
Yaroslav Lobankovf6906e12016-02-26 19:44:53 -0600129 cls.services_client = cls.os_adm.identity_services_client
130 cls.endpoints_client = cls.os_adm.endpoints_client
Rohan Kanadeb645e172015-02-05 17:38:59 +0530131
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000132 @classmethod
Rohan Kanadeb645e172015-02-05 17:38:59 +0530133 def resource_setup(cls):
134 super(BaseIdentityV2AdminTest, cls).resource_setup()
Castulo J. Martineze3adee42016-07-14 10:40:08 -0700135 cls.projects_client = cls.tenants_client
Rohan Kanadeb645e172015-02-05 17:38:59 +0530136
Castulo J. Martineze3adee42016-07-14 10:40:08 -0700137 def setup_test_user(self, password=None):
138 """Set up a test user."""
139 tenant = self.setup_test_tenant()
140 username = data_utils.rand_name('test_user')
141 email = username + '@testmail.tm'
142 user = self._create_test_user(name=username, email=email,
143 tenantId=tenant['id'], password=password)
144 return user
145
146 def setup_test_tenant(self):
147 """Set up a test tenant."""
148 tenant = self.projects_client.create_tenant(
149 name=data_utils.rand_name('test_tenant'),
150 description=data_utils.rand_name('desc'))['tenant']
151 # Delete the tenant at the end of the test
152 self.addCleanup(self.tenants_client.delete_tenant, tenant['id'])
153 return tenant
154
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000155
Chris Hoge4f6117a2015-03-20 12:39:33 -0500156class BaseIdentityV3Test(BaseIdentityTest):
157
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000158 credentials = ['primary']
159
Andrea Frittoli (andreaf)41601412015-05-12 16:39:03 +0100160 # identity v3 tests should obtain tokens and create accounts via v3
161 # regardless of the configured CONF.identity.auth_version
162 identity_version = 'v3'
Rohan Kanadeb645e172015-02-05 17:38:59 +0530163
164 @classmethod
165 def setup_clients(cls):
Chris Hoge4f6117a2015-03-20 12:39:33 -0500166 super(BaseIdentityV3Test, cls).setup_clients()
167 cls.non_admin_client = cls.os.identity_v3_client
Daniel Mellado7aea5342016-02-09 09:10:12 +0000168 cls.non_admin_users_client = cls.os.users_v3_client
Chris Hoge4f6117a2015-03-20 12:39:33 -0500169 cls.non_admin_token = cls.os.token_v3_client
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -0600170 cls.non_admin_projects_client = cls.os.projects_client
Chris Hoge4f6117a2015-03-20 12:39:33 -0500171
Chris Hoge4f6117a2015-03-20 12:39:33 -0500172
173class BaseIdentityV3AdminTest(BaseIdentityV3Test):
174
Andrea Frittoli (andreaf)41601412015-05-12 16:39:03 +0100175 credentials = ['primary', 'admin']
Chris Hoge4f6117a2015-03-20 12:39:33 -0500176
Andrea Frittoli00882b62016-12-19 23:22:44 +0000177 # NOTE(andreaf) Identity tests work with credentials, so it is safer
178 # for them to always use disposable credentials. Forcing dynamic creds
179 # on regular identity tests would be however to restrictive, since it
180 # would prevent any identity test from being executed against clouds where
181 # admin credentials are not available.
182 # Since All admin tests require admin credentials to be
183 # executed, so this will not impact the ability to execute tests.
184 force_tenant_isolation = True
185
Chris Hoge4f6117a2015-03-20 12:39:33 -0500186 @classmethod
187 def setup_clients(cls):
Rohan Kanadeb645e172015-02-05 17:38:59 +0530188 super(BaseIdentityV3AdminTest, cls).setup_clients()
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000189 cls.client = cls.os_adm.identity_v3_client
Daniel Mellado91a26b62016-02-11 11:13:04 +0000190 cls.domains_client = cls.os_adm.domains_client
Daniel Mellado7aea5342016-02-09 09:10:12 +0000191 cls.users_client = cls.os_adm.users_v3_client
Daniel Mellado76405392016-02-11 12:47:12 +0000192 cls.trusts_client = cls.os_adm.trusts_client
Arx Cruz24bcb882016-02-10 15:20:16 +0100193 cls.roles_client = cls.os_adm.roles_v3_client
ghanshyamad55eb82016-09-06 13:58:29 +0900194 cls.inherited_roles_client = cls.os_adm.inherited_roles_client
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000195 cls.token = cls.os_adm.token_v3_client
Yaroslav Lobankovf6906e12016-02-26 19:44:53 -0600196 cls.endpoints_client = cls.os_adm.endpoints_v3_client
Yaroslav Lobankov757d1a22015-12-18 11:43:02 +0300197 cls.regions_client = cls.os_adm.regions_client
Yaroslav Lobankovf6906e12016-02-26 19:44:53 -0600198 cls.services_client = cls.os_adm.identity_services_v3_client
Yaroslav Lobankoved4d15c2015-12-18 11:30:10 +0300199 cls.policies_client = cls.os_adm.policies_client
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000200 cls.creds_client = cls.os_adm.credentials_client
Yaroslav Lobankov997a1452015-11-19 17:11:37 +0300201 cls.groups_client = cls.os_adm.groups_client
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -0600202 cls.projects_client = cls.os_adm.projects_client
Rodrigo Duarte12f8d4a2016-07-08 11:53:53 -0300203 cls.role_assignments = cls.os_admin.role_assignments_client
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100204 if CONF.identity.admin_domain_scope:
205 # NOTE(andreaf) When keystone policy requires it, the identity
206 # admin clients for these tests shall use 'domain' scoped tokens.
207 # As the client manager is already created by the base class,
208 # we set the scope for the inner auth provider.
209 cls.os_adm.auth_provider.scope = 'domain'
Yaroslav Lobankov997a1452015-11-19 17:11:37 +0300210
Yaroslav Lobankov2c2f0362016-01-13 18:07:22 +0300211 @classmethod
Tom Cocozzello5544c172016-02-23 17:50:28 -0600212 def disable_user(cls, user_name, domain_id=None):
213 user = cls.get_user_by_name(user_name, domain_id)
ghanshyam7f817db2016-08-01 18:37:13 +0900214 cls.users_client.update_user(user['id'], name=user_name, enabled=False)
BinBin Congc6e8ef52015-11-20 02:08:46 -0500215
Castulo J. Martinez19b81b22016-07-15 08:58:25 -0700216 @classmethod
217 def create_domain(cls):
218 """Create a domain."""
219 domain = cls.domains_client.create_domain(
220 name=data_utils.rand_name('test_domain'),
221 description=data_utils.rand_name('desc'))['domain']
222 return domain
223
Martin Pavlasek4c3f2ab2014-04-15 17:15:15 +0200224 def delete_domain(self, domain_id):
225 # NOTE(mpavlase) It is necessary to disable the domain before deleting
226 # otherwise it raises Forbidden exception
Daniel Mellado91a26b62016-02-11 11:13:04 +0000227 self.domains_client.update_domain(domain_id, enabled=False)
228 self.domains_client.delete_domain(domain_id)
Martin Pavlasek4c3f2ab2014-04-15 17:15:15 +0200229
Castulo J. Martinez19b81b22016-07-15 08:58:25 -0700230 def setup_test_user(self, password=None):
231 """Set up a test user."""
232 project = self.setup_test_project()
233 username = data_utils.rand_name('test_user')
234 email = username + '@testmail.tm'
ghanshyam7f817db2016-08-01 18:37:13 +0900235 user = self._create_test_user(name=username, email=email,
Castulo J. Martinez19b81b22016-07-15 08:58:25 -0700236 project_id=project['id'],
237 password=password)
238 return user
239
240 def setup_test_project(self):
241 """Set up a test project."""
242 project = self.projects_client.create_project(
243 name=data_utils.rand_name('test_project'),
244 description=data_utils.rand_name('desc'))['project']
245 # Delete the project at the end of the test
246 self.addCleanup(self.projects_client.delete_project, project['id'])
247 return project
248
249 def setup_test_domain(self):
250 """Set up a test domain."""
251 domain = self.create_domain()
252 # Delete the domain at the end of the test
253 self.addCleanup(self.delete_domain, domain['id'])
254 return domain