blob: 13c47eb2039c41ebac96247e8fc3c3a4bf3968b6 [file] [log] [blame]
Steven Hardybf70c5c2013-10-30 21:55:16 +00001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
Steven Hardyc234ada2013-12-10 17:00:41 +000013import datetime
14import re
Matthew Treinish96e9e882014-06-09 18:37:19 -040015
Masayuki Igawabfa07602015-01-20 18:47:17 +090016from tempest_lib import exceptions as lib_exc
17
Steven Hardybf70c5c2013-10-30 21:55:16 +000018from tempest.api.identity import base
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000019from tempest import auth
Steven Hardybf70c5c2013-10-30 21:55:16 +000020from tempest import clients
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090021from tempest.common.utils import data_utils
Matthew Treinishd5021a72014-01-09 18:42:51 +000022from tempest import config
Steven Hardybf70c5c2013-10-30 21:55:16 +000023from tempest import exceptions
Steven Hardyc234ada2013-12-10 17:00:41 +000024from tempest.openstack.common import timeutils
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090025from tempest import test
Steven Hardybf70c5c2013-10-30 21:55:16 +000026
Matthew Treinishd5021a72014-01-09 18:42:51 +000027CONF = config.CONF
28
Steven Hardybf70c5c2013-10-30 21:55:16 +000029
Matthew Treinishdb2c5972014-01-31 22:18:59 +000030class BaseTrustsV3Test(base.BaseIdentityV3AdminTest):
Steven Hardybf70c5c2013-10-30 21:55:16 +000031
32 def setUp(self):
33 super(BaseTrustsV3Test, self).setUp()
34 # Use alt_username as the trustee
Matthew Treinishd5021a72014-01-09 18:42:51 +000035 if not CONF.identity_feature_enabled.trust:
36 raise self.skipException("Trusts aren't enabled")
37
Matthew Treinish5ba84e32014-01-29 16:52:57 +000038 self.trustee_username = CONF.identity.alt_username
Steven Hardybf70c5c2013-10-30 21:55:16 +000039 self.trust_id = None
Steven Hardy776f4572013-12-23 21:42:48 +000040
41 def tearDown(self):
42 if self.trust_id:
43 # Do the delete in tearDown not addCleanup - we want the test to
44 # fail in the event there is a bug which causes undeletable trusts
45 self.delete_trust()
46 super(BaseTrustsV3Test, self).tearDown()
Steven Hardybf70c5c2013-10-30 21:55:16 +000047
48 def create_trustor_and_roles(self):
49 # Get trustor project ID, use the admin project
Matthew Treinishdb2c5972014-01-31 22:18:59 +000050 self.trustor_project_name = self.client.tenant_name
Steven Hardybf70c5c2013-10-30 21:55:16 +000051 self.trustor_project_id = self.get_tenant_by_name(
52 self.trustor_project_name)['id']
53 self.assertIsNotNone(self.trustor_project_id)
54
55 # Create a trustor User
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090056 self.trustor_username = data_utils.rand_name('user-')
Steven Hardybf70c5c2013-10-30 21:55:16 +000057 u_desc = self.trustor_username + 'description'
Steven Hardy68f95282014-01-10 17:40:31 +000058 u_email = self.trustor_username + '@testmail.xx'
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090059 self.trustor_password = data_utils.rand_name('pass-')
David Kranzd8ccb792014-12-29 11:32:05 -050060 user = self.client.create_user(
Steven Hardybf70c5c2013-10-30 21:55:16 +000061 self.trustor_username,
62 description=u_desc,
63 password=self.trustor_password,
64 email=u_email,
65 project_id=self.trustor_project_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000066 self.trustor_user_id = user['id']
67
68 # And two roles, one we'll delegate and one we won't
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090069 self.delegated_role = data_utils.rand_name('DelegatedRole-')
70 self.not_delegated_role = data_utils.rand_name('NotDelegatedRole-')
Steven Hardybf70c5c2013-10-30 21:55:16 +000071
David Kranzd8ccb792014-12-29 11:32:05 -050072 role = self.client.create_role(self.delegated_role)
Steven Hardybf70c5c2013-10-30 21:55:16 +000073 self.delegated_role_id = role['id']
74
David Kranzd8ccb792014-12-29 11:32:05 -050075 role = self.client.create_role(self.not_delegated_role)
Steven Hardybf70c5c2013-10-30 21:55:16 +000076 self.not_delegated_role_id = role['id']
77
78 # Assign roles to trustor
Matthew Treinishdb2c5972014-01-31 22:18:59 +000079 self.client.assign_user_role(self.trustor_project_id,
80 self.trustor_user_id,
81 self.delegated_role_id)
82 self.client.assign_user_role(self.trustor_project_id,
83 self.trustor_user_id,
84 self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000085
86 # Get trustee user ID, use the demo user
Matthew Treinishdb2c5972014-01-31 22:18:59 +000087 trustee_username = self.non_admin_client.user
Steven Hardybf70c5c2013-10-30 21:55:16 +000088 self.trustee_user_id = self.get_user_by_name(trustee_username)['id']
89 self.assertIsNotNone(self.trustee_user_id)
90
91 # Initialize a new client with the trustor credentials
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000092 creds = auth.get_credentials(
93 username=self.trustor_username,
94 password=self.trustor_password,
95 tenant_name=self.trustor_project_name)
96 os = clients.Manager(
97 credentials=creds,
98 interface=self._interface)
Matthew Treinishdb2c5972014-01-31 22:18:59 +000099 self.trustor_client = os.identity_v3_client
Steven Hardybf70c5c2013-10-30 21:55:16 +0000100
Steven Hardy776f4572013-12-23 21:42:48 +0000101 def cleanup_user_and_roles(self):
Steven Hardybf70c5c2013-10-30 21:55:16 +0000102 if self.trustor_user_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000103 self.client.delete_user(self.trustor_user_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000104 if self.delegated_role_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000105 self.client.delete_role(self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000106 if self.not_delegated_role_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000107 self.client.delete_role(self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000108
109 def create_trust(self, impersonate=True, expires=None):
110
David Kranzd8ccb792014-12-29 11:32:05 -0500111 trust_create = self.trustor_client.create_trust(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000112 trustor_user_id=self.trustor_user_id,
113 trustee_user_id=self.trustee_user_id,
114 project_id=self.trustor_project_id,
115 role_names=[self.delegated_role],
116 impersonation=impersonate,
117 expires_at=expires)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000118 self.trust_id = trust_create['id']
119 return trust_create
120
121 def validate_trust(self, trust, impersonate=True, expires=None,
122 summary=False):
123 self.assertIsNotNone(trust['id'])
124 self.assertEqual(impersonate, trust['impersonation'])
Steven Hardyc234ada2013-12-10 17:00:41 +0000125 # FIXME(shardy): ref bug #1246383 we can't check the
126 # microsecond component of the expiry time, because mysql
127 # <5.6.4 doesn't support microseconds.
128 # expected format 2013-12-20T16:08:36.036987Z
129 if expires is not None:
130 expires_nousec = re.sub(r'\.([0-9]){6}Z', '', expires)
131 self.assertTrue(trust['expires_at'].startswith(expires_nousec))
132 else:
133 self.assertIsNone(trust['expires_at'])
Steven Hardybf70c5c2013-10-30 21:55:16 +0000134 self.assertEqual(self.trustor_user_id, trust['trustor_user_id'])
135 self.assertEqual(self.trustee_user_id, trust['trustee_user_id'])
136 self.assertIn('v3/OS-TRUST/trusts', trust['links']['self'])
137 self.assertEqual(self.trustor_project_id, trust['project_id'])
138 if not summary:
139 self.assertEqual(self.delegated_role, trust['roles'][0]['name'])
140 self.assertEqual(1, len(trust['roles']))
141
142 def get_trust(self):
David Kranzd8ccb792014-12-29 11:32:05 -0500143 trust_get = self.trustor_client.get_trust(self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000144 return trust_get
145
146 def validate_role(self, role):
147 self.assertEqual(self.delegated_role_id, role['id'])
148 self.assertEqual(self.delegated_role, role['name'])
149 self.assertIn('v3/roles/%s' % self.delegated_role_id,
150 role['links']['self'])
151 self.assertNotEqual(self.not_delegated_role_id, role['id'])
152 self.assertNotEqual(self.not_delegated_role, role['name'])
153 self.assertNotIn('v3/roles/%s' % self.not_delegated_role_id,
154 role['links']['self'])
155
156 def check_trust_roles(self):
157 # Check we find the delegated role
David Kranzd8ccb792014-12-29 11:32:05 -0500158 roles_get = self.trustor_client.get_trust_roles(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000159 self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000160 self.assertEqual(1, len(roles_get))
161 self.validate_role(roles_get[0])
162
David Kranzd8ccb792014-12-29 11:32:05 -0500163 role_get = self.trustor_client.get_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000164 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000165 self.validate_role(role_get)
166
David Kranzd8ccb792014-12-29 11:32:05 -0500167 role_get = self.trustor_client.check_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000168 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000169
170 # And that we don't find not_delegated_role
Masayuki Igawabfa07602015-01-20 18:47:17 +0900171 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000172 self.trustor_client.get_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000173 self.trust_id,
174 self.not_delegated_role_id)
175
Masayuki Igawabfa07602015-01-20 18:47:17 +0900176 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000177 self.trustor_client.check_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000178 self.trust_id,
179 self.not_delegated_role_id)
180
181 def delete_trust(self):
David Kranze9d2f422014-07-02 13:57:41 -0400182 self.trustor_client.delete_trust(self.trust_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900183 self.assertRaises(lib_exc.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000184 self.trustor_client.get_trust,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000185 self.trust_id)
186 self.trust_id = None
187
188
189class TrustsV3TestJSON(BaseTrustsV3Test):
190 _interface = 'json'
191
192 def setUp(self):
193 super(TrustsV3TestJSON, self).setUp()
194 self.create_trustor_and_roles()
Steven Hardy68f95282014-01-10 17:40:31 +0000195 self.addCleanup(self.cleanup_user_and_roles)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000196
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900197 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000198 def test_trust_impersonate(self):
199 # Test case to check we can create, get and delete a trust
200 # updates are not supported for trusts
201 trust = self.create_trust()
202 self.validate_trust(trust)
203
204 trust_get = self.get_trust()
205 self.validate_trust(trust_get)
206
207 self.check_trust_roles()
208
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900209 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000210 def test_trust_noimpersonate(self):
211 # Test case to check we can create, get and delete a trust
212 # with impersonation=False
213 trust = self.create_trust(impersonate=False)
214 self.validate_trust(trust, impersonate=False)
215
216 trust_get = self.get_trust()
217 self.validate_trust(trust_get, impersonate=False)
218
219 self.check_trust_roles()
220
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900221 @test.attr(type='smoke')
Steven Hardyc234ada2013-12-10 17:00:41 +0000222 def test_trust_expire(self):
223 # Test case to check we can create, get and delete a trust
224 # with an expiry specified
225 expires_at = timeutils.utcnow() + datetime.timedelta(hours=1)
226 expires_str = timeutils.isotime(at=expires_at, subsecond=True)
227
228 trust = self.create_trust(expires=expires_str)
229 self.validate_trust(trust, expires=expires_str)
230
231 trust_get = self.get_trust()
232
233 self.validate_trust(trust_get, expires=expires_str)
234
235 self.check_trust_roles()
236
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900237 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000238 def test_trust_expire_invalid(self):
239 # Test case to check we can check an invlaid expiry time
240 # is rejected with the correct error
241 # with an expiry specified
242 expires_str = 'bad.123Z'
243 self.assertRaises(exceptions.BadRequest,
244 self.create_trust,
245 expires=expires_str)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000246
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900247 @test.attr(type='smoke')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000248 def test_get_trusts_query(self):
249 self.create_trust()
David Kranzd8ccb792014-12-29 11:32:05 -0500250 trusts_get = self.trustor_client.get_trusts(
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000251 trustor_user_id=self.trustor_user_id)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000252 self.assertEqual(1, len(trusts_get))
253 self.validate_trust(trusts_get[0], summary=True)
254
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900255 @test.attr(type='smoke')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000256 def test_get_trusts_all(self):
257 self.create_trust()
David Kranzd8ccb792014-12-29 11:32:05 -0500258 trusts_get = self.client.get_trusts()
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000259 trusts = [t for t in trusts_get
260 if t['id'] == self.trust_id]
261 self.assertEqual(1, len(trusts))
262 self.validate_trust(trusts[0], summary=True)