Fix get_subnet_for_dvr() to return correct gateway mac

Fix filters to get the right gateway port for a subnet.
Wrong filters led to random port's mac being returned which may cause
side effects. See bug for details.

Closes-Bug: #1530179
Change-Id: I8368255f00ab3e9586c8ff28dfe6739541000810
This commit is contained in:
Oleg Bondarev 2015-12-30 20:24:55 +03:00
parent a6547e9cf0
commit 0a00205c2a
2 changed files with 44 additions and 6 deletions

View File

@ -169,11 +169,12 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
else:
# retrieve the gateway port on this subnet
if fixed_ips:
filter = fixed_ips[0]
ip_address = fixed_ips[0]['ip_address']
else:
filter = {'fixed_ips': {'subnet_id': [subnet],
'ip_address':
[subnet_info['gateway_ip']]}}
ip_address = subnet_info['gateway_ip']
filter = {'fixed_ips': {'subnet_id': [subnet],
'ip_address': [ip_address]}}
internal_gateway_ports = self.plugin.get_ports(
context, filters=filter)

View File

@ -19,7 +19,7 @@ from oslo_config import cfg
from neutron import context
from neutron.db import dvr_mac_db
from neutron.extensions import dvr
from neutron.tests.unit import testlib_api
from neutron.tests.unit.plugins.ml2 import test_plugin
class DVRDbMixinImpl(dvr_mac_db.DVRDbMixin):
@ -28,7 +28,7 @@ class DVRDbMixinImpl(dvr_mac_db.DVRDbMixin):
self.notifier = notifier
class DvrDbMixinTestCase(testlib_api.SqlTestCase):
class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
def setUp(self):
super(DvrDbMixinTestCase, self).setUp()
@ -90,3 +90,40 @@ class DvrDbMixinTestCase(testlib_api.SqlTestCase):
with mock.patch.object(self.mixin, '_create_dvr_mac_address') as f:
self.mixin.get_dvr_mac_address_by_host(self.ctx, 'foo_host')
self.assertEqual(1, f.call_count)
def test_get_subnet_for_dvr_returns_correct_mac(self):
with self.subnet() as subnet,\
self.port(subnet=subnet),\
self.port(subnet=subnet):
dvr_subnet = self.mixin.get_subnet_for_dvr(self.ctx,
subnet['subnet']['id'])
# no gateway port should be found so no info should be returned
self.assertEqual({}, dvr_subnet)
with self.port(
subnet=subnet,
fixed_ips=[{'ip_address': subnet['subnet'][
'gateway_ip']}]) as gw_port:
dvr_subnet = self.mixin.get_subnet_for_dvr(
self.ctx, subnet['subnet']['id'])
self.assertEqual(gw_port['port']['mac_address'],
dvr_subnet['gateway_mac'])
def test_get_subnet_for_dvr_returns_correct_mac_fixed_ips_passed(self):
with self.subnet() as subnet,\
self.port(subnet=subnet,
fixed_ips=[{'ip_address': '10.0.0.2'}]),\
self.port(subnet=subnet,
fixed_ips=[{'ip_address': '10.0.0.3'}]):
fixed_ips = [{'subnet_id': subnet['subnet']['id'],
'ip_address': '10.0.0.4'}]
dvr_subnet = self.mixin.get_subnet_for_dvr(
self.ctx, subnet['subnet']['id'], fixed_ips)
# no gateway port should be found so no info should be returned
self.assertEqual({}, dvr_subnet)
with self.port(
subnet=subnet,
fixed_ips=[{'ip_address': '10.0.0.4'}]) as gw_port:
dvr_subnet = self.mixin.get_subnet_for_dvr(
self.ctx, subnet['subnet']['id'], fixed_ips)
self.assertEqual(gw_port['port']['mac_address'],
dvr_subnet['gateway_mac'])