15
15
# License for the specific language governing permissions and limitations
16
16
# under the License.
19
Tests for database migrations. This test case reads the configuration
20
file /tests/unit/test_migrations.conf for database connection settings
21
to use in the tests. For each connection found in the config file,
22
the test case runs a series of test cases to ensure that migrations work
23
properly both upgrading and downgrading, and that no data loss occurs
33
from migrate.versioning.repository import Repository
34
from sqlalchemy import *
35
from sqlalchemy.pool import NullPool
37
from glance.common import exception
21
38
import glance.registry.db.migration as migration_api
22
import glance.registry.db.api as api
23
import glance.common.config as config
39
from tests.unit.test_misc import execute
26
42
class TestMigrations(unittest.TestCase):
27
44
"""Test sqlalchemy-migrate migrations"""
47
CONFIG_FILE_PATH = os.path.join('tests', 'unit',
48
'test_migrations.conf')
49
REPOSITORY_PATH = os.path.join('glance', 'registry', 'db', 'migrate_repo')
50
REPOSITORY = Repository(REPOSITORY_PATH)
52
def __init__(self, *args, **kwargs):
53
super(TestMigrations, self).__init__(*args, **kwargs)
30
self.db_path = "glance_test_migration.sqlite"
31
sql_connection = os.environ.get('GLANCE_SQL_CONNECTION',
32
"sqlite:///%s" % self.db_path)
34
self.options = dict(sql_connection=sql_connection,
36
config.setup_logging(self.options, {})
56
# Load test databases from the config file. Only do this
57
# once. No need to re-run this on each test...
58
if not TestMigrations.TEST_DATABASES:
59
if os.path.exists(TestMigrations.CONFIG_FILE_PATH):
60
cp = ConfigParser.RawConfigParser()
62
cp.read(TestMigrations.CONFIG_FILE_PATH)
63
defaults = cp.defaults()
64
for key, value in defaults.items():
65
TestMigrations.TEST_DATABASES[key] = value
66
except ConfigParser.ParsingError, e:
67
print ("Failed to read test_migrations.conf config file. "
71
for key, value in TestMigrations.TEST_DATABASES.items():
72
self.engines[key] = create_engine(value, poolclass=NullPool)
74
# We start each test case with a completely blank slate.
75
self._reset_databases()
38
77
def tearDown(self):
39
api.configure_db(self.options)
40
api.unregister_models()
42
def test_db_sync_downgrade_then_upgrade(self):
43
migration_api.db_sync(self.options)
45
latest = migration_api.db_version(self.options)
47
migration_api.downgrade(self.options, latest - 1)
48
cur_version = migration_api.db_version(self.options)
49
self.assertEqual(cur_version, latest - 1)
51
migration_api.upgrade(self.options, cur_version + 1)
52
cur_version = migration_api.db_version(self.options)
53
self.assertEqual(cur_version, latest)
78
# We destroy the test data store between each test case,
79
# and recreate it, which ensures that we have no side-effects
81
self._reset_databases()
83
def _reset_databases(self):
84
for key, engine in self.engines.items():
85
conn_string = TestMigrations.TEST_DATABASES[key]
86
conn_pieces = urlparse.urlparse(conn_string)
87
if conn_string.startswith('sqlite'):
88
# We can just delete the SQLite database, which is
89
# the easiest and cleanest solution
90
db_path = conn_pieces.path.strip('/')
91
if os.path.exists(db_path):
93
# No need to recreate the SQLite DB. SQLite will
94
# create it for us if it's not there...
95
elif conn_string.startswith('mysql'):
96
# We can execute the MySQL client to destroy and re-create
97
# the MYSQL database, which is easier and less error-prone
98
# than using SQLAlchemy to do this via MetaData...trust me.
99
database = conn_pieces.path.strip('/')
100
loc_pieces = conn_pieces.netloc.split('@')
102
auth_pieces = loc_pieces[0].split(':')
103
user = auth_pieces[0]
105
if len(auth_pieces) > 1:
106
if auth_pieces[1].strip():
107
password = "-p%s" % auth_pieces[1]
108
sql = ("drop database if exists %(database)s; "
109
"create database %(database)s;") % locals()
110
cmd = ("mysql -u%(user)s %(password)s -h%(host)s "
111
"-e\"%(sql)s\"") % locals()
112
exitcode, out, err = execute(cmd)
113
self.assertEqual(0, exitcode)
115
def test_walk_versions(self):
117
Walks all version scripts for each tested database, ensuring
118
that there are no errors in the version scripts for each engine
120
for key, engine in self.engines.items():
121
options = {'sql_connection': TestMigrations.TEST_DATABASES[key]}
122
self._walk_versions(options)
124
def _walk_versions(self, options):
125
# Determine latest version script from the repo, then
126
# upgrade from 1 through to the latest, with no data
127
# in the databases. This just checks that the schema itself
128
# upgrades successfully.
130
# Assert we are not under version control...
131
self.assertRaises(exception.DatabaseMigrationError,
132
migration_api.db_version,
134
# Place the database under version control
135
migration_api.version_control(options)
137
latest = migration_api.db_version(options)
138
self.assertEqual(0, latest)
140
for version in xrange(1, TestMigrations.REPOSITORY.latest):
141
migration_api.upgrade(options, version)
142
cur_version = migration_api.db_version(options)
143
self.assertEqual(cur_version, version)
145
# Now walk it back down to 0 from the latest, testing
146
# the downgrade paths.
147
for version in xrange(TestMigrations.REPOSITORY.latest - 1, 0):
148
migration_api.downgrade(options, version)
149
cur_version = migration_api.db_version(options)
150
self.assertEqual(cur_version, version)
152
def test_no_data_loss_2_to_3_to_2(self):
154
Here, we test that in the case when we moved a column "type" from the
155
base images table to be records in the image_properties table, that
156
we don't lose any data during the migration. Similarly, we test that
157
on downgrade, we don't lose any data, as the records are moved from
158
the image_properties table back into the base image table.
160
for key, engine in self.engines.items():
161
options = {'sql_connection': TestMigrations.TEST_DATABASES[key]}
162
self._no_data_loss_2_to_3_to_2(engine, options)
164
def _no_data_loss_2_to_3_to_2(self, engine, options):
165
migration_api.version_control(options)
166
migration_api.upgrade(options, 2)
168
cur_version = migration_api.db_version(options)
169
self.assertEquals(2, cur_version)
171
# We are now on version 2. Check that the images table does
172
# not contain the type column...
174
images_table = Table('images', MetaData(), autoload=True,
175
autoload_with=engine)
177
image_properties_table = Table('image_properties', MetaData(),
179
autoload_with=engine)
181
self.assertTrue('type' in images_table.c,
182
"'type' column found in images table columns! "
183
"images table columns: %s"
184
% images_table.c.keys())
186
conn = engine.connect()
187
sel = select([func.count("*")], from_obj=[images_table])
188
orig_num_images = conn.execute(sel).scalar()
189
sel = select([func.count("*")], from_obj=[image_properties_table])
190
orig_num_image_properties = conn.execute(sel).scalar()
192
now = datetime.datetime.now()
193
inserter = images_table.insert()
194
conn.execute(inserter, [
195
{'deleted': False, 'created_at': now,
196
'updated_at': now, 'type': 'kernel',
197
'status': 'active', 'is_public': True},
198
{'deleted': False, 'created_at': now,
199
'updated_at': now, 'type': 'ramdisk',
200
'status': 'active', 'is_public': True}])
202
sel = select([func.count("*")], from_obj=[images_table])
203
num_images = conn.execute(sel).scalar()
204
self.assertEqual(orig_num_images + 2, num_images)
207
# Now let's upgrade to 3. This should move the type column
208
# to the image_properties table as type properties.
210
migration_api.upgrade(options, 3)
212
cur_version = migration_api.db_version(options)
213
self.assertEquals(3, cur_version)
215
images_table = Table('images', MetaData(), autoload=True,
216
autoload_with=engine)
218
self.assertTrue('type' not in images_table.c,
219
"'type' column not found in images table columns! "
220
"images table columns reported by metadata: %s\n"
221
% images_table.c.keys())
223
image_properties_table = Table('image_properties', MetaData(),
225
autoload_with=engine)
227
conn = engine.connect()
228
sel = select([func.count("*")], from_obj=[image_properties_table])
229
num_image_properties = conn.execute(sel).scalar()
230
self.assertEqual(orig_num_image_properties + 2, num_image_properties)
233
# Downgrade to 2 and check that the type properties were moved
234
# to the main image table
236
migration_api.downgrade(options, 2)
238
images_table = Table('images', MetaData(), autoload=True,
239
autoload_with=engine)
241
self.assertTrue('type' in images_table.c,
242
"'type' column found in images table columns! "
243
"images table columns: %s"
244
% images_table.c.keys())
246
image_properties_table = Table('image_properties', MetaData(),
248
autoload_with=engine)
250
conn = engine.connect()
251
sel = select([func.count("*")], from_obj=[image_properties_table])
252
last_num_image_properties = conn.execute(sel).scalar()
254
self.assertEqual(num_image_properties - 2, last_num_image_properties)