Added unit tests for db migration and fixed lint

This commit is contained in:
Liam Young 2014-09-29 16:12:37 +01:00
parent e600645566
commit 8af0760a06
5 changed files with 86 additions and 19 deletions

View File

@ -187,7 +187,8 @@ def db_changed():
log('shared-db relation incomplete. Peer not ready?')
return
CONFIGS.write_all()
conditional_neutron_migration()
conditional_neutron_migration()
@hooks.hook('pgsql-db-relation-changed')
@restart_on_change(restart_map())
@ -197,6 +198,7 @@ def postgresql_neutron_db_changed():
CONFIGS.write(neutron_plugin_attribute(plugin, 'config'))
conditional_neutron_migration()
@hooks.hook('amqp-relation-broken',
'identity-service-relation-broken',
'shared-db-relation-broken',

View File

@ -201,7 +201,7 @@ def do_openstack_upgrade(configs):
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.')
plugin = config('neutron-plugin')
cmd = ['neutron-db-manage',

View File

@ -1,6 +1,5 @@
from test_utils import CharmTestCase
from test_utils import patch_open
from mock import patch, MagicMock
from mock import patch
import neutron_api_context as context
import charmhelpers
TO_PATCH = [
@ -56,6 +55,7 @@ class HAProxyContextTest(CharmTestCase):
super(HAProxyContextTest, self).setUp(context, TO_PATCH)
self.determine_api_port.return_value = 9686
self.determine_apache_port.return_value = 9686
self.api_port = 9696
def tearDown(self):
super(HAProxyContextTest, self).tearDown()
@ -65,7 +65,8 @@ class HAProxyContextTest(CharmTestCase):
def test_context_No_peers(self, _log, _rids):
_rids.return_value = []
hap_ctxt = context.HAProxyContext()
self.assertTrue('units' not in hap_ctxt())
with patch('__builtin__.__import__'):
self.assertTrue('units' not in hap_ctxt())
@patch.object(charmhelpers.contrib.openstack.context, 'config')
@patch.object(charmhelpers.contrib.openstack.context, 'local_unit')
@ -74,8 +75,10 @@ class HAProxyContextTest(CharmTestCase):
@patch.object(charmhelpers.contrib.openstack.context, 'related_units')
@patch.object(charmhelpers.contrib.openstack.context, 'relation_ids')
@patch.object(charmhelpers.contrib.openstack.context, 'log')
def test_context_peers(self, _log, _rids, _runits, _rget, _uget,
_lunit, _config):
@patch('__builtin__.__import__')
@patch('__builtin__.open')
def test_context_peers(self, _open, _import, _log, _rids, _runits, _rget,
_uget, _lunit, _config):
unit_addresses = {
'neutron-api-0': '10.10.10.10',
'neutron-api-1': '10.10.10.11',
@ -93,11 +96,10 @@ class HAProxyContextTest(CharmTestCase):
'service_ports': service_ports,
'neutron_bind_port': 9686,
}
with patch_open() as (_open, _file):
_file.write = MagicMock()
hap_ctxt = context.HAProxyContext()
self.assertEquals(hap_ctxt(), ctxt_data)
_file.write.assert_called_with('ENABLED=1\n')
_import().api_port.return_value = 9696
hap_ctxt = context.HAProxyContext()
self.assertEquals(hap_ctxt(), ctxt_data)
_open.assert_called_with('/etc/default/haproxy', 'w')
class NeutronAPIContextsTest(CharmTestCase):
@ -119,28 +121,32 @@ class NeutronAPIContextsTest(CharmTestCase):
@patch.object(context.NeutronCCContext, 'network_manager')
@patch.object(context.NeutronCCContext, 'plugin')
def test_neutroncc_context_no_setting(self, plugin, nm):
@patch('__builtin__.__import__')
def test_neutroncc_context_no_setting(self, _import, plugin, nm):
plugin.return_value = None
napi_ctxt = context.NeutronCCContext()
ctxt_data = {
'debug': True,
'external_network': 'bob',
'neutron_bind_port': self.api_port,
'verbose': True,
}
napi_ctxt = context.NeutronCCContext()
with patch.object(napi_ctxt, '_ensure_packages'):
self.assertEquals(ctxt_data, napi_ctxt())
@patch.object(context.NeutronCCContext, 'network_manager')
@patch.object(context.NeutronCCContext, 'plugin')
def test_neutroncc_context_api_rel(self, plugin, nm):
@patch('__builtin__.__import__')
def test_neutroncc_context_api_rel(self, _import, plugin, nm):
nova_url = 'http://127.0.0.10'
plugin.return_value = None
self.related_units.return_value = ['unit1']
self.relation_ids.return_value = ['rid2']
self.test_relation.set({'nova_url': nova_url})
self.test_relation.set({'nova_url': nova_url,
'restart_trigger': 'bob'})
napi_ctxt = context.NeutronCCContext()
self.assertEquals(nova_url, napi_ctxt()['nova_url'])
self.assertEquals('bob', napi_ctxt()['restart_trigger'])
self.assertEquals(self.api_port, napi_ctxt()['neutron_bind_port'])
def test_neutroncc_context_manager(self):

View File

@ -42,6 +42,8 @@ TO_PATCH = [
'unit_get',
'get_iface_for_address',
'get_netmask_for_address',
'migrate_neutron_database',
'service_restart',
]
NEUTRON_CONF_DIR = "/etc/neutron"
@ -154,19 +156,23 @@ class NeutronAPIHooksTests(CharmTestCase):
'Attempting to associate a postgresql database when'
' there is already associated a mysql one')
def test_shared_db_changed(self):
@patch.object(hooks, 'conditional_neutron_migration')
def test_shared_db_changed(self, cond_neutron_mig):
self.CONFIGS.complete_contexts.return_value = ['shared-db']
self._call_hook('shared-db-relation-changed')
self.assertTrue(self.CONFIGS.write_all.called)
cond_neutron_mig.assert_called_with()
def test_shared_db_changed_partial_ctxt(self):
self.CONFIGS.complete_contexts.return_value = []
self._call_hook('shared-db-relation-changed')
self.assertFalse(self.CONFIGS.write_all.called)
def test_pgsql_db_changed(self):
@patch.object(hooks, 'conditional_neutron_migration')
def test_pgsql_db_changed(self, cond_neutron_mig):
self._call_hook('pgsql-db-relation-changed')
self.assertTrue(self.CONFIGS.write.called)
cond_neutron_mig.assert_called_with()
def test_amqp_broken(self):
self._call_hook('amqp-relation-broken')
@ -356,3 +362,44 @@ class NeutronAPIHooksTests(CharmTestCase):
self.check_call.assert_called_with(['a2dissite',
'openstack_https_frontend'])
self.assertTrue(_id_rel_joined.called)
def test_conditional_neutron_migration_no_ncc_rel(self):
self.test_relation.set({
'clustered': 'false',
})
self.relation_ids.return_value = []
hooks.conditional_neutron_migration()
self.log.assert_called_with(
'Not running neutron database migration, no nova-cloud-controller'
'is present.'
)
def test_conditional_neutron_migration_ncc_rel_leader(self):
self.test_relation.set({
'clustered': 'true',
})
self.is_leader.return_value = True
hooks.conditional_neutron_migration()
self.migrate_neutron_database.assert_called_with()
self.service_restart.assert_called_with('neutron-server')
def test_conditional_neutron_migration_ncc_rel_notleader(self):
self.test_relation.set({
'clustered': 'true',
})
self.is_leader.return_value = False
hooks.conditional_neutron_migration()
self.assertFalse(self.migrate_neutron_database.called)
self.assertFalse(self.service_restart.called)
self.log.assert_called_with(
'Not running neutron database migration, not leader'
)
def test_conditional_neutron_migration_not_clustered(self):
self.test_relation.set({
'clustered': 'false',
})
self.relation_ids.return_value = ['nova-cc/o']
hooks.conditional_neutron_migration()
self.migrate_neutron_database.assert_called_with()
self.service_restart.assert_called_with('neutron-server')

View File

@ -5,7 +5,9 @@ import charmhelpers.contrib.openstack.templating as templating
templating.OSConfigRenderer = MagicMock()
import neutron_api_utils as nutils
with patch('charmhelpers.core.hookenv.config') as config:
config.return_value = 'neutron'
import neutron_api_utils as nutils
from test_utils import (
CharmTestCase,
@ -26,6 +28,7 @@ TO_PATCH = [
'log',
'neutron_plugin_attribute',
'os_release',
'subprocess',
]
@ -166,3 +169,12 @@ class TestNeutronAPIUtils(CharmTestCase):
self.configure_installation_source.assert_called_with(
'cloud:precise-havana'
)
def test_migrate_neutron_database(self):
nutils.migrate_neutron_database()
cmd = ['neutron-db-manage',
'--config-file', '/etc/neutron/neutron.conf',
'--config-file', '/etc/neutron/plugins/ml2/ml2_conf.ini',
'upgrade',
'head']
self.subprocess.check_output.assert_called_with(cmd)