blob: 1561a6e3ce188999900778629184f94070d39bd2 [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
Steven Hardybf70c5c2013-10-30 21:55:16 +000015from tempest.api.identity import base
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000016from tempest import auth
Steven Hardybf70c5c2013-10-30 21:55:16 +000017from tempest import clients
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090018from tempest.common.utils import data_utils
Matthew Treinishd5021a72014-01-09 18:42:51 +000019from tempest import config
Steven Hardybf70c5c2013-10-30 21:55:16 +000020from tempest import exceptions
Steven Hardyc234ada2013-12-10 17:00:41 +000021from tempest.openstack.common import timeutils
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090022from tempest import test
Steven Hardybf70c5c2013-10-30 21:55:16 +000023
Matthew Treinishd5021a72014-01-09 18:42:51 +000024CONF = config.CONF
25
Steven Hardybf70c5c2013-10-30 21:55:16 +000026
Matthew Treinishdb2c5972014-01-31 22:18:59 +000027class BaseTrustsV3Test(base.BaseIdentityV3AdminTest):
Steven Hardybf70c5c2013-10-30 21:55:16 +000028
29 def setUp(self):
30 super(BaseTrustsV3Test, self).setUp()
31 # Use alt_username as the trustee
Matthew Treinishd5021a72014-01-09 18:42:51 +000032 if not CONF.identity_feature_enabled.trust:
33 raise self.skipException("Trusts aren't enabled")
34
Matthew Treinish5ba84e32014-01-29 16:52:57 +000035 self.trustee_username = CONF.identity.alt_username
Steven Hardybf70c5c2013-10-30 21:55:16 +000036 self.trust_id = None
Steven Hardy776f4572013-12-23 21:42:48 +000037
38 def tearDown(self):
39 if self.trust_id:
40 # Do the delete in tearDown not addCleanup - we want the test to
41 # fail in the event there is a bug which causes undeletable trusts
42 self.delete_trust()
43 super(BaseTrustsV3Test, self).tearDown()
Steven Hardybf70c5c2013-10-30 21:55:16 +000044
45 def create_trustor_and_roles(self):
46 # Get trustor project ID, use the admin project
Matthew Treinishdb2c5972014-01-31 22:18:59 +000047 self.trustor_project_name = self.client.tenant_name
Steven Hardybf70c5c2013-10-30 21:55:16 +000048 self.trustor_project_id = self.get_tenant_by_name(
49 self.trustor_project_name)['id']
50 self.assertIsNotNone(self.trustor_project_id)
51
52 # Create a trustor User
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090053 self.trustor_username = data_utils.rand_name('user-')
Steven Hardybf70c5c2013-10-30 21:55:16 +000054 u_desc = self.trustor_username + 'description'
Steven Hardy68f95282014-01-10 17:40:31 +000055 u_email = self.trustor_username + '@testmail.xx'
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090056 self.trustor_password = data_utils.rand_name('pass-')
David Kranze9d2f422014-07-02 13:57:41 -040057 _, user = self.client.create_user(
Steven Hardybf70c5c2013-10-30 21:55:16 +000058 self.trustor_username,
59 description=u_desc,
60 password=self.trustor_password,
61 email=u_email,
62 project_id=self.trustor_project_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000063 self.trustor_user_id = user['id']
64
65 # And two roles, one we'll delegate and one we won't
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090066 self.delegated_role = data_utils.rand_name('DelegatedRole-')
67 self.not_delegated_role = data_utils.rand_name('NotDelegatedRole-')
Steven Hardybf70c5c2013-10-30 21:55:16 +000068
David Kranze9d2f422014-07-02 13:57:41 -040069 _, role = self.client.create_role(self.delegated_role)
Steven Hardybf70c5c2013-10-30 21:55:16 +000070 self.delegated_role_id = role['id']
71
David Kranze9d2f422014-07-02 13:57:41 -040072 _, role = self.client.create_role(self.not_delegated_role)
Steven Hardybf70c5c2013-10-30 21:55:16 +000073 self.not_delegated_role_id = role['id']
74
75 # Assign roles to trustor
Matthew Treinishdb2c5972014-01-31 22:18:59 +000076 self.client.assign_user_role(self.trustor_project_id,
77 self.trustor_user_id,
78 self.delegated_role_id)
79 self.client.assign_user_role(self.trustor_project_id,
80 self.trustor_user_id,
81 self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +000082
83 # Get trustee user ID, use the demo user
Matthew Treinishdb2c5972014-01-31 22:18:59 +000084 trustee_username = self.non_admin_client.user
Steven Hardybf70c5c2013-10-30 21:55:16 +000085 self.trustee_user_id = self.get_user_by_name(trustee_username)['id']
86 self.assertIsNotNone(self.trustee_user_id)
87
88 # Initialize a new client with the trustor credentials
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000089 creds = auth.get_credentials(
90 username=self.trustor_username,
91 password=self.trustor_password,
92 tenant_name=self.trustor_project_name)
93 os = clients.Manager(
94 credentials=creds,
95 interface=self._interface)
Matthew Treinishdb2c5972014-01-31 22:18:59 +000096 self.trustor_client = os.identity_v3_client
Steven Hardybf70c5c2013-10-30 21:55:16 +000097
Steven Hardy776f4572013-12-23 21:42:48 +000098 def cleanup_user_and_roles(self):
Steven Hardybf70c5c2013-10-30 21:55:16 +000099 if self.trustor_user_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000100 self.client.delete_user(self.trustor_user_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000101 if self.delegated_role_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000102 self.client.delete_role(self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000103 if self.not_delegated_role_id:
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000104 self.client.delete_role(self.not_delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000105
106 def create_trust(self, impersonate=True, expires=None):
107
David Kranze9d2f422014-07-02 13:57:41 -0400108 _, trust_create = self.trustor_client.create_trust(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000109 trustor_user_id=self.trustor_user_id,
110 trustee_user_id=self.trustee_user_id,
111 project_id=self.trustor_project_id,
112 role_names=[self.delegated_role],
113 impersonation=impersonate,
114 expires_at=expires)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000115 self.trust_id = trust_create['id']
116 return trust_create
117
118 def validate_trust(self, trust, impersonate=True, expires=None,
119 summary=False):
120 self.assertIsNotNone(trust['id'])
121 self.assertEqual(impersonate, trust['impersonation'])
Steven Hardyc234ada2013-12-10 17:00:41 +0000122 # FIXME(shardy): ref bug #1246383 we can't check the
123 # microsecond component of the expiry time, because mysql
124 # <5.6.4 doesn't support microseconds.
125 # expected format 2013-12-20T16:08:36.036987Z
126 if expires is not None:
127 expires_nousec = re.sub(r'\.([0-9]){6}Z', '', expires)
128 self.assertTrue(trust['expires_at'].startswith(expires_nousec))
129 else:
130 self.assertIsNone(trust['expires_at'])
Steven Hardybf70c5c2013-10-30 21:55:16 +0000131 self.assertEqual(self.trustor_user_id, trust['trustor_user_id'])
132 self.assertEqual(self.trustee_user_id, trust['trustee_user_id'])
133 self.assertIn('v3/OS-TRUST/trusts', trust['links']['self'])
134 self.assertEqual(self.trustor_project_id, trust['project_id'])
135 if not summary:
136 self.assertEqual(self.delegated_role, trust['roles'][0]['name'])
137 self.assertEqual(1, len(trust['roles']))
138
139 def get_trust(self):
David Kranze9d2f422014-07-02 13:57:41 -0400140 _, trust_get = self.trustor_client.get_trust(self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000141 return trust_get
142
143 def validate_role(self, role):
144 self.assertEqual(self.delegated_role_id, role['id'])
145 self.assertEqual(self.delegated_role, role['name'])
146 self.assertIn('v3/roles/%s' % self.delegated_role_id,
147 role['links']['self'])
148 self.assertNotEqual(self.not_delegated_role_id, role['id'])
149 self.assertNotEqual(self.not_delegated_role, role['name'])
150 self.assertNotIn('v3/roles/%s' % self.not_delegated_role_id,
151 role['links']['self'])
152
153 def check_trust_roles(self):
154 # Check we find the delegated role
David Kranze9d2f422014-07-02 13:57:41 -0400155 _, roles_get = self.trustor_client.get_trust_roles(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000156 self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000157 self.assertEqual(1, len(roles_get))
158 self.validate_role(roles_get[0])
159
David Kranze9d2f422014-07-02 13:57:41 -0400160 _, role_get = self.trustor_client.get_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000161 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000162 self.validate_role(role_get)
163
David Kranze9d2f422014-07-02 13:57:41 -0400164 _, role_get = self.trustor_client.check_trust_role(
Steven Hardybf70c5c2013-10-30 21:55:16 +0000165 self.trust_id, self.delegated_role_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000166
167 # And that we don't find not_delegated_role
168 self.assertRaises(exceptions.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000169 self.trustor_client.get_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000170 self.trust_id,
171 self.not_delegated_role_id)
172
173 self.assertRaises(exceptions.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000174 self.trustor_client.check_trust_role,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000175 self.trust_id,
176 self.not_delegated_role_id)
177
178 def delete_trust(self):
David Kranze9d2f422014-07-02 13:57:41 -0400179 self.trustor_client.delete_trust(self.trust_id)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000180 self.assertRaises(exceptions.NotFound,
Matthew Treinishdb2c5972014-01-31 22:18:59 +0000181 self.trustor_client.get_trust,
Steven Hardybf70c5c2013-10-30 21:55:16 +0000182 self.trust_id)
183 self.trust_id = None
184
185
186class TrustsV3TestJSON(BaseTrustsV3Test):
187 _interface = 'json'
188
189 def setUp(self):
190 super(TrustsV3TestJSON, self).setUp()
191 self.create_trustor_and_roles()
Steven Hardy68f95282014-01-10 17:40:31 +0000192 self.addCleanup(self.cleanup_user_and_roles)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000193
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900194 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000195 def test_trust_impersonate(self):
196 # Test case to check we can create, get and delete a trust
197 # updates are not supported for trusts
198 trust = self.create_trust()
199 self.validate_trust(trust)
200
201 trust_get = self.get_trust()
202 self.validate_trust(trust_get)
203
204 self.check_trust_roles()
205
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900206 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000207 def test_trust_noimpersonate(self):
208 # Test case to check we can create, get and delete a trust
209 # with impersonation=False
210 trust = self.create_trust(impersonate=False)
211 self.validate_trust(trust, impersonate=False)
212
213 trust_get = self.get_trust()
214 self.validate_trust(trust_get, impersonate=False)
215
216 self.check_trust_roles()
217
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900218 @test.attr(type='smoke')
Steven Hardyc234ada2013-12-10 17:00:41 +0000219 def test_trust_expire(self):
220 # Test case to check we can create, get and delete a trust
221 # with an expiry specified
222 expires_at = timeutils.utcnow() + datetime.timedelta(hours=1)
223 expires_str = timeutils.isotime(at=expires_at, subsecond=True)
224
225 trust = self.create_trust(expires=expires_str)
226 self.validate_trust(trust, expires=expires_str)
227
228 trust_get = self.get_trust()
229
230 self.validate_trust(trust_get, expires=expires_str)
231
232 self.check_trust_roles()
233
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900234 @test.attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000235 def test_trust_expire_invalid(self):
236 # Test case to check we can check an invlaid expiry time
237 # is rejected with the correct error
238 # with an expiry specified
239 expires_str = 'bad.123Z'
240 self.assertRaises(exceptions.BadRequest,
241 self.create_trust,
242 expires=expires_str)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000243
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900244 @test.attr(type='smoke')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000245 def test_get_trusts_query(self):
246 self.create_trust()
David Kranze9d2f422014-07-02 13:57:41 -0400247 _, trusts_get = self.trustor_client.get_trusts(
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000248 trustor_user_id=self.trustor_user_id)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000249 self.assertEqual(1, len(trusts_get))
250 self.validate_trust(trusts_get[0], summary=True)
251
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900252 @test.attr(type='smoke')
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000253 def test_get_trusts_all(self):
254 self.create_trust()
David Kranze9d2f422014-07-02 13:57:41 -0400255 _, trusts_get = self.client.get_trusts()
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000256 trusts = [t for t in trusts_get
257 if t['id'] == self.trust_id]
258 self.assertEqual(1, len(trusts))
259 self.validate_trust(trusts[0], summary=True)