]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix lb-vip does not get route to default gw
authorAaron Rosen <arosen@nicira.com>
Mon, 1 Apr 2013 22:26:12 +0000 (15:26 -0700)
committerAaron Rosen <arosen@nicira.com>
Tue, 2 Apr 2013 01:24:58 +0000 (18:24 -0700)
Previously when creating a lb-vip it would be created without
a default gw. This patch fixes that and adds unit tests to check
that route add is called if the subnet has a gateway_ip.

Fixes bug 1162626

Change-Id: I155749fa6d9c843fca87a73f3cf85720aac26cfa

quantum/plugins/services/agent_loadbalancer/drivers/haproxy/namespace_driver.py
quantum/tests/unit/services/agent_loadbalancer/driver/haproxy/test_namespace_driver.py

index dc928d0844ecb4a6ad1dfd5c81037db4d294d338..ac89505a43ea084d6e22504774348004ddd8c9be 100644 (file)
@@ -179,6 +179,13 @@ class HaproxyNSDriver(object):
         ]
         self.vif_driver.init_l3(interface_name, cidrs, namespace=namespace)
 
+        gw_ip = port['fixed_ips'][0]['subnet'].get('gateway_ip')
+        if gw_ip:
+            cmd = ['route', 'add', 'default', 'gw', gw_ip]
+            ip_wrapper = ip_lib.IPWrapper(self.root_helper,
+                                          namespace=namespace)
+            ip_wrapper.netns.execute(cmd, check_exit_code=False)
+
     def _unplug(self, namespace, port_id):
         port_stub = {'id': port_id}
         self.vip_plug_callback('unplug', port_stub)
index d550a0325232beb7e095686038318671f9a40cab..861104851b90b349c2abcc2acedce946861dd876 100644 (file)
@@ -186,11 +186,13 @@ class TestHaproxyNSDriver(base.BaseTestCase):
                      'network_id': 'net_id',
                      'mac_address': 'mac_addr',
                      'fixed_ips': [{'ip_address': '10.0.0.2',
-                                    'subnet': {'cidr': 'cidr'}}]}
+                                    'subnet': {'cidr': '10.0.0.0/24',
+                                               'gateway_ip': '10.0.0.1'}}]}
         with contextlib.nested(
                 mock.patch('quantum.agent.linux.ip_lib.device_exists'),
                 mock.patch('netaddr.IPNetwork'),
-        ) as (dev_exists, ip_net):
+                mock.patch('quantum.agent.linux.ip_lib.IPWrapper'),
+        ) as (dev_exists, ip_net, ip_wrap):
             self.vif_driver.get_device_name.return_value = 'test_interface'
             dev_exists.return_value = False
             ip_net.return_value = ip_net
@@ -207,7 +209,44 @@ class TestHaproxyNSDriver(base.BaseTestCase):
                                                             ['10.0.0.2/24'],
                                                             namespace=
                                                             'test_ns')
+            cmd = ['route', 'add', 'default', 'gw', '10.0.0.1']
+            ip_wrap.assert_has_calls([
+                mock.call('sudo', namespace='test_ns'),
+                mock.call().netns.execute(cmd, check_exit_code=False),
+            ])
+
+            dev_exists.return_value = True
+            self.assertRaises(exceptions.PreexistingDeviceFailure,
+                              self.driver._plug, 'test_ns', test_port, False)
 
+    def test_plug_no_gw(self):
+        test_port = {'id': 'port_id',
+                     'network_id': 'net_id',
+                     'mac_address': 'mac_addr',
+                     'fixed_ips': [{'ip_address': '10.0.0.2',
+                                    'subnet': {'cidr': '10.0.0.0/24'}}]}
+        with contextlib.nested(
+                mock.patch('quantum.agent.linux.ip_lib.device_exists'),
+                mock.patch('netaddr.IPNetwork'),
+                mock.patch('quantum.agent.linux.ip_lib.IPWrapper'),
+        ) as (dev_exists, ip_net, ip_wrap):
+            self.vif_driver.get_device_name.return_value = 'test_interface'
+            dev_exists.return_value = False
+            ip_net.return_value = ip_net
+            ip_net.prefixlen = 24
+
+            self.driver._plug('test_ns', test_port)
+            self.vip_plug_callback.assert_called_once_with('plug', test_port)
+            self.assertTrue(dev_exists.called)
+            self.vif_driver.plug.assert_called_once_with('net_id', 'port_id',
+                                                         'test_interface',
+                                                         'mac_addr',
+                                                         namespace='test_ns')
+            self.vif_driver.init_l3.assert_called_once_with('test_interface',
+                                                            ['10.0.0.2/24'],
+                                                            namespace=
+                                                            'test_ns')
+            self.assertFalse(ip_wrap.called)
             dev_exists.return_value = True
             self.assertRaises(exceptions.PreexistingDeviceFailure,
                               self.driver._plug, 'test_ns', test_port, False)