DVR: Use IPDevice class consistently

create_rtr_2_fip_link() only creates IPDevice class instances
after it determines the VETH pair does not exist.  Change to
always create them as instead of using int_dev[0].  This is
also setup for a follow-on patch.

Partial-Bug: #1566383
Change-Id: I58e242f05ffb9a33bda5aeb93344861769845d2e
(cherry picked from commit b9bec4be6b)
Conflicts:
	neutron/agent/l3/dvr_fip_ns.py
This commit is contained in:
Brian Haley 2016-04-22 15:43:24 -04:00 committed by Swaminathan Vasudevan
parent 94b49f1409
commit 5d57c7c89e
2 changed files with 27 additions and 40 deletions

View File

@ -245,13 +245,13 @@ class FipNamespace(namespaces.Namespace):
if ri.rtr_fip_subnet is None:
ri.rtr_fip_subnet = self.local_subnets.allocate(ri.router_id)
rtr_2_fip, fip_2_rtr = ri.rtr_fip_subnet.get_pair()
ip_wrapper = ip_lib.IPWrapper(namespace=ri.ns_name)
device_exists = ip_lib.device_exists(rtr_2_fip_name,
namespace=ri.ns_name)
if not device_exists:
int_dev = ip_wrapper.add_veth(rtr_2_fip_name,
fip_2_rtr_name,
fip_ns_name)
rtr_2_fip_dev = ip_lib.IPDevice(rtr_2_fip_name, namespace=ri.ns_name)
if not rtr_2_fip_dev.exists():
ip_wrapper = ip_lib.IPWrapper(namespace=ri.ns_name)
rtr_2_fip_dev, fip_2_rtr_dev = ip_wrapper.add_veth(rtr_2_fip_name,
fip_2_rtr_name,
fip_ns_name)
self._internal_ns_interface_added(str(rtr_2_fip),
rtr_2_fip_name,
ri.ns_name)
@ -261,14 +261,13 @@ class FipNamespace(namespaces.Namespace):
mtu = (self.agent_conf.network_device_mtu or
ri.get_ex_gw_port().get('mtu'))
if mtu:
int_dev[0].link.set_mtu(mtu)
int_dev[1].link.set_mtu(mtu)
int_dev[0].link.set_up()
int_dev[1].link.set_up()
rtr_2_fip_dev.link.set_mtu(mtu)
fip_2_rtr_dev.link.set_mtu(mtu)
rtr_2_fip_dev.link.set_up()
fip_2_rtr_dev.link.set_up()
# add default route for the link local interface
device = ip_lib.IPDevice(rtr_2_fip_name, namespace=ri.ns_name)
device.route.add_gateway(str(fip_2_rtr.ip), table=FIP_RT_TBL)
rtr_2_fip_dev.route.add_gateway(str(fip_2_rtr.ip), table=FIP_RT_TBL)
#setup the NAT rules and chains
ri._handle_fip_nat_rules(rtr_2_fip_name)

View File

@ -218,8 +218,7 @@ class TestDvrFipNs(base.BaseTestCase):
@mock.patch.object(ip_lib, 'IPWrapper')
@mock.patch.object(ip_lib, 'IPDevice')
@mock.patch.object(ip_lib, 'device_exists')
def test_create_rtr_2_fip_link(self, device_exists, IPDevice, IPWrapper):
def _test_create_rtr_2_fip_link(self, dev_exists, IPDevice, IPWrapper):
ri = mock.Mock()
ri.router_id = _uuid()
ri.rtr_fip_subnet = None
@ -232,42 +231,31 @@ class TestDvrFipNs(base.BaseTestCase):
self.fip_ns.local_subnets = allocator = mock.Mock()
pair = lla.LinkLocalAddressPair('169.254.31.28/31')
allocator.allocate.return_value = pair
device_exists.return_value = False
ip_wrapper = IPWrapper()
self.conf.network_device_mtu = 2000
ip_wrapper.add_veth.return_value = (IPDevice(), IPDevice())
device = IPDevice()
device.exists.return_value = dev_exists
self.fip_ns.create_rtr_2_fip_link(ri)
ip_wrapper.add_veth.assert_called_with(rtr_2_fip_name,
fip_2_rtr_name,
fip_ns_name)
if not dev_exists:
ip_wrapper.add_veth.assert_called_with(rtr_2_fip_name,
fip_2_rtr_name,
fip_ns_name)
device.link.set_mtu.assert_called_with(2000)
self.assertEqual(2, device.link.set_mtu.call_count)
self.assertEqual(2, device.link.set_up.call_count)
device = IPDevice()
device.link.set_mtu.assert_called_with(2000)
self.assertEqual(2, device.link.set_mtu.call_count)
device.route.add_gateway.assert_called_once_with(
'169.254.31.29', table=16)
@mock.patch.object(ip_lib, 'IPWrapper')
@mock.patch.object(ip_lib, 'IPDevice')
@mock.patch.object(ip_lib, 'device_exists')
def test_create_rtr_2_fip_link_already_exists(self,
device_exists,
IPDevice,
IPWrapper):
ri = mock.Mock()
ri.router_id = _uuid()
ri.rtr_fip_subnet = None
device_exists.return_value = True
def test_create_rtr_2_fip_link(self):
self._test_create_rtr_2_fip_link(False)
self.fip_ns.local_subnets = allocator = mock.Mock()
pair = lla.LinkLocalAddressPair('169.254.31.28/31')
allocator.allocate.return_value = pair
self.fip_ns.create_rtr_2_fip_link(ri)
ip_wrapper = IPWrapper()
self.assertFalse(ip_wrapper.add_veth.called)
def test_create_rtr_2_fip_link_already_exists(self):
self._test_create_rtr_2_fip_link(True)
@mock.patch.object(ip_lib, 'IPDevice')
def _test_scan_fip_ports(self, ri, ip_list, IPDevice):