Added unit tests for db migration and fixed lint

This commit is contained in:
Liam Young
2014-09-29 14:58:29 +01:00
parent 033a17f6c3
commit 2132e668c7
4 changed files with 74 additions and 21 deletions

View File

@@ -275,6 +275,7 @@ def postgresql_nova_db_changed():
for rid in relation_ids('cloud-compute')]
conditional_neutron_migration()
@hooks.hook('pgsql-neutron-db-relation-changed')
@service_guard(guard_map(), CONFIGS,
active=config('service-guard'))

View File

@@ -507,7 +507,7 @@ def _do_openstack_upgrade(new_src):
# create issues with db upgrades
if relation_ids('neutron-api'):
log('Not running neutron database migration as neutron-api service'
'is present.')
'is present.')
else:
neutron_db_manage(['stamp', cur_os_rel])
migrate_neutron_database()
@@ -561,7 +561,7 @@ def migrate_nova_database():
def migrate_neutron_database():
'''Runs neutron-db-manage to initialize a new database or migrate existing'''
'''Runs neutron-db-manage to init a new database or migrate existing'''
log('Migrating the neutron database.', level=INFO)
neutron_db_manage(['upgrade', 'head'])

View File

@@ -46,8 +46,10 @@ TO_PATCH = [
'ssh_known_hosts_lines',
'ssh_authorized_keys_lines',
'save_script_rc',
'service_restart',
'service_running',
'service_stop',
'services',
'execd_preinstall',
'network_manager',
'volume_service',
@@ -55,7 +57,9 @@ TO_PATCH = [
'eligible_leader',
'keystone_ca_cert_b64',
'neutron_plugin',
'migrate_database',
'migrate_nova_database',
'migrate_neutron_database',
'uuid',
]
@@ -99,11 +103,17 @@ class NovaCCHooksTests(CharmTestCase):
hooks.config_changed()
self.assertTrue(self.save_script_rc.called)
@patch.object(hooks, 'identity_joined')
@patch.object(hooks, 'neutron_api_relation_joined')
@patch.object(hooks, 'configure_https')
def test_config_changed_with_upgrade(self, conf_https):
def test_config_changed_with_upgrade(self, conf_https, neutron_api_joined,
identity_joined):
self.openstack_upgrade_available.return_value = True
self.relation_ids.return_value = ['generic_rid']
hooks.config_changed()
self.assertTrue(self.do_openstack_upgrade.called)
self.assertTrue(neutron_api_joined.called)
self.assertTrue(identity_joined.called)
self.assertTrue(self.save_script_rc.called)
def test_compute_changed_ssh_migration(self):
@@ -322,11 +332,13 @@ class NovaCCHooksTests(CharmTestCase):
configs.write = MagicMock()
hooks.postgresql_nova_db_changed()
@patch.object(hooks, 'conditional_neutron_migration')
@patch.object(hooks, 'CONFIGS')
def test_db_changed(self, configs):
def test_db_changed(self, configs, cond_neutron_mig):
self._shared_db_test(configs)
self.assertTrue(configs.write_all.called)
self.migrate_database.assert_called_with()
self.migrate_nova_database.assert_called_with()
cond_neutron_mig.assert_called_with()
@patch.object(hooks, 'CONFIGS')
def test_db_changed_allowed(self, configs):
@@ -337,7 +349,7 @@ class NovaCCHooksTests(CharmTestCase):
self.local_unit.return_value = 'nova-cloud-controller/3'
self._shared_db_test(configs)
self.assertTrue(configs.write_all.called)
self.migrate_database.assert_called_with()
self.migrate_nova_database.assert_called_with()
@patch.object(hooks, 'CONFIGS')
def test_db_changed_not_allowed(self, configs):
@@ -348,13 +360,13 @@ class NovaCCHooksTests(CharmTestCase):
self.local_unit.return_value = 'nova-cloud-controller/1'
self._shared_db_test(configs)
self.assertTrue(configs.write_all.called)
self.assertFalse(self.migrate_database.called)
self.assertFalse(self.migrate_nova_database.called)
@patch.object(hooks, 'CONFIGS')
def test_postgresql_db_changed(self, configs):
self._postgresql_db_test(configs)
self.assertTrue(configs.write_all.called)
self.migrate_database.assert_called_with()
self.migrate_nova_database.assert_called_with()
@patch.object(os, 'rename')
@patch.object(os.path, 'isfile')
@@ -367,13 +379,15 @@ class NovaCCHooksTests(CharmTestCase):
_identity_joined = self.patch('identity_joined')
self.relation_ids.side_effect = ['relid']
self.canonical_url.return_value = 'http://novaurl'
self.uuid.uuid4.return_value = 'bob'
with patch_open() as (_open, _file):
hooks.neutron_api_relation_joined()
hooks.neutron_api_relation_joined(remote_restart=True)
self.service_stop.assert_called_with('neutron-server')
rename.assert_called_with(neutron_conf, neutron_conf + '_unused')
self.assertTrue(_identity_joined.called)
self.relation_set.assert_called_with(relation_id=None,
nova_url=nova_url)
nova_url=nova_url,
restart_trigger='bob')
@patch.object(hooks, 'CONFIGS')
def test_neutron_api_relation_changed(self, configs):
@@ -484,3 +498,18 @@ class NovaCCHooksTests(CharmTestCase):
'console_keymap': 'en-us'
}
self.assertEqual(_con_sets, console_settings)
def test_conditional_neutron_migration_api_rel(self):
self.relation_ids.return_value = ['neutron-api/0']
hooks.conditional_neutron_migration()
self.log.assert_called_with(
'Not running neutron database migration as neutron-api service'
'is present.'
)
def test_conditional_neutron_migration_noapi_rel(self):
self.relation_ids.return_value = []
hooks.conditional_neutron_migration()
self.services.return_value = ['neutron-server']
self.migrate_neutron_database.assert_called()
self.service_restart.assert_called()

View File

@@ -592,29 +592,29 @@ class NovaCCUtilsTests(CharmTestCase):
_known_hosts.assert_called_with('bar', None)
@patch('subprocess.check_output')
def test_migrate_database(self, check_output):
def test_migrate_nova_database(self, check_output):
"Migrate database with nova-manage"
self.relation_ids.return_value = []
utils.migrate_database()
utils.migrate_nova_database()
check_output.assert_called_with(['nova-manage', 'db', 'sync'])
self.enable_services.assert_called()
self.cmd_all_services.assert_called_with('start')
@patch('subprocess.check_output')
def test_migrate_database_cluster(self, check_output):
def test_migrate_nova_database_cluster(self, check_output):
"Migrate database with nova-manage in a clustered env"
self.relation_ids.return_value = ['cluster:1']
utils.migrate_database()
utils.migrate_nova_database()
check_output.assert_called_with(['nova-manage', 'db', 'sync'])
self.peer_store.assert_called_with('dbsync_state', 'complete')
self.enable_services.assert_called()
self.cmd_all_services.assert_called_with('start')
@patch.object(utils, 'get_step_upgrade_source')
@patch.object(utils, 'migrate_database')
@patch.object(utils, 'migrate_nova_database')
@patch.object(utils, 'determine_packages')
def test_upgrade_grizzly_icehouse(self, determine_packages,
migrate_database,
migrate_nova_database,
get_step_upgrade_source):
"Simulate a call to do_openstack_upgrade() for grizzly->icehouse"
get_step_upgrade_source.return_value = 'cloud:precise-havana'
@@ -623,6 +623,7 @@ class NovaCCUtilsTests(CharmTestCase):
'havana',
'icehouse']
self.eligible_leader.return_value = True
self.relation_ids.return_value = []
utils.do_openstack_upgrade()
expected = [call(['stamp', 'grizzly']), call(['upgrade', 'head']),
call(['stamp', 'havana']), call(['upgrade', 'head'])]
@@ -634,19 +635,20 @@ class NovaCCUtilsTests(CharmTestCase):
expected = [call(release='havana'), call(release='icehouse')]
self.assertEquals(self.register_configs.call_args_list, expected)
self.assertEquals(self.ml2_migration.call_count, 1)
self.assertTrue(migrate_database.call_count, 2)
self.assertTrue(migrate_nova_database.call_count, 2)
@patch.object(utils, 'get_step_upgrade_source')
@patch.object(utils, 'migrate_database')
@patch.object(utils, 'migrate_nova_database')
@patch.object(utils, 'determine_packages')
def test_upgrade_havana_icehouse(self, determine_packages,
migrate_database,
migrate_nova_database,
get_step_upgrade_source):
"Simulate a call to do_openstack_upgrade() for havana->icehouse"
get_step_upgrade_source.return_value = None
self.os_release.return_value = 'havana'
self.get_os_codename_install_source.return_value = 'icehouse'
self.eligible_leader.return_value = True
self.relation_ids.return_value = []
utils.do_openstack_upgrade()
self.neutron_db_manage.assert_called_with(['upgrade', 'head'])
self.apt_update.assert_called_with(fatal=True)
@@ -655,7 +657,28 @@ class NovaCCUtilsTests(CharmTestCase):
self.apt_install.assert_called_with(determine_packages(), fatal=True)
self.register_configs.assert_called_with(release='icehouse')
self.assertEquals(self.ml2_migration.call_count, 1)
self.assertTrue(migrate_database.call_count, 1)
self.assertTrue(migrate_nova_database.call_count, 1)
@patch.object(utils, 'get_step_upgrade_source')
@patch.object(utils, 'migrate_nova_database')
@patch.object(utils, 'determine_packages')
def test_upgrade_havana_icehouse_apirel(self, determine_packages,
migrate_nova_database,
get_step_upgrade_source):
"Simulate a call to do_openstack_upgrade() for havana->icehouse api"
get_step_upgrade_source.return_value = None
self.os_release.return_value = 'havana'
self.get_os_codename_install_source.return_value = 'icehouse'
self.eligible_leader.return_value = True
self.relation_ids.return_value = ['neutron-api/0']
utils.do_openstack_upgrade()
self.apt_update.assert_called_with(fatal=True)
self.apt_upgrade.assert_called_with(options=DPKG_OPTS, fatal=True,
dist=True)
self.apt_install.assert_called_with(determine_packages(), fatal=True)
self.register_configs.assert_called_with(release='icehouse')
self.assertEquals(self.ml2_migration.call_count, 1)
self.assertTrue(migrate_nova_database.call_count, 1)
@patch.object(utils, '_do_openstack_upgrade')
def test_upgrade_grizzly_icehouse_source(self, _do_openstack_upgrade):