1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# Copyright 2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
To run these tests against a live database:
1. Modify the file `keystone/tests/unit/config_files/backend_sql.conf` to use
the connection for your live database.
2. Set up a blank, live database.
3. Run the tests using::
tox -e py27 -- keystone.tests.unit.test_sql_migrate_extensions
WARNING::
Your database will be wiped.
Do not do this against a Database with valuable data as
all data will be lost.
"""
import sqlalchemy
import uuid
from oslo_db import exception as db_exception
from oslo_db.sqlalchemy import utils
from keystone.contrib import endpoint_filter
from keystone.contrib import endpoint_policy
from keystone.contrib import example
from keystone.contrib import federation
from keystone.contrib import oauth1
from keystone.contrib import revoke
from keystone.tests.unit import test_sql_upgrade
class SqlUpgradeExampleExtension(test_sql_upgrade.SqlMigrateBase):
def repo_package(self):
return example
def test_upgrade(self):
self.assertTableDoesNotExist('example')
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns('example', ['id', 'type', 'extra'])
def test_downgrade(self):
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns('example', ['id', 'type', 'extra'])
self.downgrade(0, repository=self.repo_path)
self.assertTableDoesNotExist('example')
class SqlUpgradeOAuth1Extension(test_sql_upgrade.SqlMigrateBase):
def repo_package(self):
return oauth1
def upgrade(self, version):
super(SqlUpgradeOAuth1Extension, self).upgrade(
version, repository=self.repo_path)
def downgrade(self, version):
super(SqlUpgradeOAuth1Extension, self).downgrade(
version, repository=self.repo_path)
def _assert_v1_3_tables(self):
self.assertTableColumns('consumer',
['id',
'description',
'secret',
'extra'])
self.assertTableColumns('request_token',
['id',
'request_secret',
'verifier',
'authorizing_user_id',
'requested_project_id',
'requested_roles',
'consumer_id',
'expires_at'])
self.assertTableColumns('access_token',
['id',
'access_secret',
'authorizing_user_id',
'project_id',
'requested_roles',
'consumer_id',
'expires_at'])
def _assert_v4_later_tables(self):
self.assertTableColumns('consumer',
['id',
'description',
'secret',
'extra'])
self.assertTableColumns('request_token',
['id',
'request_secret',
'verifier',
'authorizing_user_id',
'requested_project_id',
'role_ids',
'consumer_id',
'expires_at'])
self.assertTableColumns('access_token',
['id',
'access_secret',
'authorizing_user_id',
'project_id',
'role_ids',
'consumer_id',
'expires_at'])
def test_upgrade(self):
self.assertTableDoesNotExist('consumer')
self.assertTableDoesNotExist('request_token')
self.assertTableDoesNotExist('access_token')
self.upgrade(1)
self._assert_v1_3_tables()
# NOTE(blk-u): Migrations 2-3 don't modify the tables in a way that we
# can easily test for.
self.upgrade(4)
self._assert_v4_later_tables()
self.upgrade(5)
self._assert_v4_later_tables()
def test_downgrade(self):
self.upgrade(5)
self._assert_v4_later_tables()
self.downgrade(3)
self._assert_v1_3_tables()
self.downgrade(1)
self._assert_v1_3_tables()
self.downgrade(0)
self.assertTableDoesNotExist('consumer')
self.assertTableDoesNotExist('request_token')
self.assertTableDoesNotExist('access_token')
class EndpointFilterExtension(test_sql_upgrade.SqlMigrateBase):
def repo_package(self):
return endpoint_filter
def upgrade(self, version):
super(EndpointFilterExtension, self).upgrade(
version, repository=self.repo_path)
def downgrade(self, version):
super(EndpointFilterExtension, self).downgrade(
version, repository=self.repo_path)
def _assert_v1_tables(self):
self.assertTableColumns('project_endpoint',
['endpoint_id', 'project_id'])
self.assertTableDoesNotExist('endpoint_group')
self.assertTableDoesNotExist('project_endpoint_group')
def _assert_v2_tables(self):
self.assertTableColumns('project_endpoint',
['endpoint_id', 'project_id'])
self.assertTableColumns('endpoint_group',
['id', 'name', 'description', 'filters'])
self.assertTableColumns('project_endpoint_group',
['endpoint_group_id', 'project_id'])
def test_upgrade(self):
self.assertTableDoesNotExist('project_endpoint')
self.upgrade(1)
self._assert_v1_tables()
self.assertTableColumns('project_endpoint',
['endpoint_id', 'project_id'])
self.upgrade(2)
self._assert_v2_tables()
def test_downgrade(self):
self.upgrade(2)
self._assert_v2_tables()
self.downgrade(1)
self._assert_v1_tables()
self.downgrade(0)
self.assertTableDoesNotExist('project_endpoint')
class EndpointPolicyExtension(test_sql_upgrade.SqlMigrateBase):
def repo_package(self):
return endpoint_policy
def test_upgrade(self):
self.assertTableDoesNotExist('policy_association')
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns('policy_association',
['id', 'policy_id', 'endpoint_id',
'service_id', 'region_id'])
def test_downgrade(self):
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns('policy_association',
['id', 'policy_id', 'endpoint_id',
'service_id', 'region_id'])
self.downgrade(0, repository=self.repo_path)
self.assertTableDoesNotExist('policy_association')
class FederationExtension(test_sql_upgrade.SqlMigrateBase):
"""Test class for ensuring the Federation SQL."""
def setUp(self):
super(FederationExtension, self).setUp()
self.identity_provider = 'identity_provider'
self.federation_protocol = 'federation_protocol'
self.service_provider = 'service_provider'
self.mapping = 'mapping'
def repo_package(self):
return federation
def insert_dict(self, session, table_name, d):
"""Naively inserts key-value pairs into a table, given a dictionary."""
table = sqlalchemy.Table(table_name, self.metadata, autoload=True)
insert = table.insert().values(**d)
session.execute(insert)
session.commit()
def test_upgrade(self):
self.assertTableDoesNotExist(self.identity_provider)
self.assertTableDoesNotExist(self.federation_protocol)
self.assertTableDoesNotExist(self.mapping)
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns(self.identity_provider,
['id',
'enabled',
'description'])
self.assertTableColumns(self.federation_protocol,
['id',
'idp_id',
'mapping_id'])
self.upgrade(2, repository=self.repo_path)
self.assertTableColumns(self.mapping,
['id', 'rules'])
federation_protocol = utils.get_table(
self.engine,
'federation_protocol')
with self.engine.begin() as conn:
conn.execute(federation_protocol.insert(), id=0, idp_id=1)
self.upgrade(3, repository=self.repo_path)
federation_protocol = utils.get_table(
self.engine,
'federation_protocol')
self.assertFalse(federation_protocol.c.mapping_id.nullable)
def test_downgrade(self):
self.upgrade(3, repository=self.repo_path)
self.assertTableColumns(self.identity_provider,
['id', 'enabled', 'description'])
self.assertTableColumns(self.federation_protocol,
['id', 'idp_id', 'mapping_id'])
self.assertTableColumns(self.mapping,
['id', 'rules'])
self.downgrade(2, repository=self.repo_path)
federation_protocol = utils.get_table(
self.engine,
'federation_protocol')
self.assertTrue(federation_protocol.c.mapping_id.nullable)
self.downgrade(0, repository=self.repo_path)
self.assertTableDoesNotExist(self.identity_provider)
self.assertTableDoesNotExist(self.federation_protocol)
self.assertTableDoesNotExist(self.mapping)
def test_fixup_service_provider_attributes(self):
self.upgrade(6, repository=self.repo_path)
self.assertTableColumns(self.service_provider,
['id', 'description', 'enabled', 'auth_url',
'sp_url'])
session = self.Session()
sp1 = {'id': uuid.uuid4().hex,
'auth_url': None,
'sp_url': uuid.uuid4().hex,
'description': uuid.uuid4().hex,
'enabled': True}
sp2 = {'id': uuid.uuid4().hex,
'auth_url': uuid.uuid4().hex,
'sp_url': None,
'description': uuid.uuid4().hex,
'enabled': True}
sp3 = {'id': uuid.uuid4().hex,
'auth_url': None,
'sp_url': None,
'description': uuid.uuid4().hex,
'enabled': True}
# Insert with 'auth_url' or 'sp_url' set to null must fail
self.assertRaises(db_exception.DBError,
self.insert_dict,
session,
self.service_provider,
sp1)
self.assertRaises(db_exception.DBError,
self.insert_dict,
session,
self.service_provider,
sp2)
self.assertRaises(db_exception.DBError,
self.insert_dict,
session,
self.service_provider,
sp3)
session.close()
self.downgrade(5, repository=self.repo_path)
self.assertTableColumns(self.service_provider,
['id', 'description', 'enabled', 'auth_url',
'sp_url'])
session = self.Session()
self.metadata.clear()
# Before the migration, the table should accept null values
self.insert_dict(session, self.service_provider, sp1)
self.insert_dict(session, self.service_provider, sp2)
self.insert_dict(session, self.service_provider, sp3)
# Check if null values are updated to empty string when migrating
session.close()
self.upgrade(6, repository=self.repo_path)
sp_table = sqlalchemy.Table(self.service_provider,
self.metadata,
autoload=True)
session = self.Session()
self.metadata.clear()
sp = session.query(sp_table).filter(sp_table.c.id == sp1['id'])[0]
self.assertEqual('', sp.auth_url)
sp = session.query(sp_table).filter(sp_table.c.id == sp2['id'])[0]
self.assertEqual('', sp.sp_url)
sp = session.query(sp_table).filter(sp_table.c.id == sp3['id'])[0]
self.assertEqual('', sp.auth_url)
self.assertEqual('', sp.sp_url)
_REVOKE_COLUMN_NAMES = ['id', 'domain_id', 'project_id', 'user_id', 'role_id',
'trust_id', 'consumer_id', 'access_token_id',
'issued_before', 'expires_at', 'revoked_at']
class RevokeExtension(test_sql_upgrade.SqlMigrateBase):
def repo_package(self):
return revoke
def test_upgrade(self):
self.assertTableDoesNotExist('revocation_event')
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns('revocation_event',
_REVOKE_COLUMN_NAMES)
def test_downgrade(self):
self.upgrade(1, repository=self.repo_path)
self.assertTableColumns('revocation_event',
_REVOKE_COLUMN_NAMES)
self.downgrade(0, repository=self.repo_path)
self.assertTableDoesNotExist('revocation_event')
|