Merge "Add share-networks validation"

This commit is contained in:
Jenkins 2015-11-30 22:12:34 +00:00 committed by Gerrit Code Review
commit e68f586867
2 changed files with 59 additions and 5 deletions

View File

@ -196,9 +196,30 @@ class NeutronSingleNetworkPlugin(NeutronNetworkPlugin):
def _update_share_network_net_data(self, context, share_network):
upd = dict()
if share_network.get('nova_net_id') is not None:
raise exception.NetworkBadConfigurationException(
"Share network has nova_net_id set.")
if not share_network.get('neutron_net_id') == self.net:
if share_network.get('neutron_net_id') is not None:
raise exception.NetworkBadConfigurationException(
"Using neutron net id different from None or value "
"specified in the config is forbidden for "
"NeutronSingleNetworkPlugin. Allowed values: (%(net)s, "
"None), received value: %(err)s" % {
"net": self.net,
"err": share_network.get('neutron_net_id')})
upd['neutron_net_id'] = self.net
if not share_network.get('neutron_subnet_id') == self.subnet:
if share_network.get('neutron_subnet_id') is not None:
raise exception.NetworkBadConfigurationException(
"Using neutron subnet id different from None or value "
"specified in the config is forbidden for "
"NeutronSingleNetworkPlugin. Allowed values: (%(snet)s, "
"None), received value: %(err)s" % {
"snet": self.subnet,
"err": share_network.get('neutron_subnet_id')})
upd['neutron_subnet_id'] = self.subnet
if upd:
share_network = self.db.share_network_update(

View File

@ -388,8 +388,26 @@ class NeutronSingleNetworkPluginTest(test.TestCase):
self.assertEqual(share_network, result)
def test___update_share_network_net_data_different_values_empty(self):
instance = self._get_neutron_single_network_plugin_instance()
share_network_input = {
'id': 'fake_share_network_id',
}
share_network_result = {
'neutron_net_id': instance.net,
'neutron_subnet_id': instance.subnet,
}
self.mock_object(
instance.db, 'share_network_update',
mock.Mock(return_value='foo'))
instance._update_share_network_net_data(
self.context, share_network_input)
instance.db.share_network_update.assert_called_once_with(
self.context, share_network_input['id'], share_network_result)
@ddt.data(
{'n': 'foo', 's': 'bar'},
{'n': 'fake_net_id', 's': 'bar'},
{'n': 'foo', 's': 'fake_subnet_id'})
@ddt.unpack
@ -404,12 +422,27 @@ class NeutronSingleNetworkPluginTest(test.TestCase):
instance.db, 'share_network_update',
mock.Mock(return_value=share_network))
result = instance._update_share_network_net_data(
self.assertRaises(
exception.NetworkBadConfigurationException,
instance._update_share_network_net_data,
self.context, share_network)
self.assertFalse(instance.db.share_network_update.called)
self.assertEqual(share_network, result)
instance.db.share_network_update.assert_called_once_with(
self.context, share_network['id'], mock.ANY)
def test___update_share_network_net_data_nova_net_id_present(self):
instance = self._get_neutron_single_network_plugin_instance()
share_network = {
'id': 'fake_share_network_id',
'nova_net_id': 'foo',
}
self.mock_object(
instance.db, 'share_network_update',
mock.Mock(return_value=share_network))
self.assertRaises(
exception.NetworkBadConfigurationException,
instance._update_share_network_net_data,
self.context, share_network)
self.assertFalse(instance.db.share_network_update.called)
@mock.patch.object(
plugin.NeutronNetworkPlugin, "allocate_network", mock.Mock())