]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Move get_inteface_by_ip from LinuxBridge class to ip_lib
authorMiguel Angel Ajo <mangelajo@redhat.com>
Tue, 16 Jun 2015 11:48:26 +0000 (13:48 +0200)
committerMiguel Angel Ajo <mangelajo@redhat.com>
Wed, 17 Jun 2015 12:39:32 +0000 (14:39 +0200)
get_interface_by_ip is moved fro LinuxBridgeManager to ip_lib
as a more generic get_device_by_ip.

System-faking unit tests have been switched for functional
testing that also performs a negative test.

This can be reused in the openvswitch-agent code to validate
local_ip reusing the LinuxBridge logic.

Change-Id: I9237871a6e24dd99556c71844624be510e20d289
Related-Bug: #1464178
Related-Bug: #1408603

neutron/agent/linux/ip_lib.py
neutron/plugins/linuxbridge/agent/linuxbridge_neutron_agent.py
neutron/tests/functional/agent/linux/test_ip_lib.py
neutron/tests/unit/plugins/linuxbridge/agent/test_linuxbridge_neutron_agent.py

index 32fe1f9ac84b8bcb590517471c6a08b1a17e6efc..03cc5bd66e40db6787c2b8c7805a3ec59273b15a 100644 (file)
@@ -120,6 +120,12 @@ class IPWrapper(SubProcessBase):
 
         return retval
 
+    def get_device_by_ip(self, ip):
+        """Get the IPDevice from system which has ip configured."""
+        for device in self.get_devices():
+            if device.addr.list(to=ip):
+                return device
+
     def add_tuntap(self, name, mode='tap'):
         self._as_root([], 'tuntap', ('add', name, 'mode', mode))
         return IPDevice(name, namespace=self.namespace)
index 2cf116619e2bfb81a106275db02971a72a90b2b1..13d1c49d0893b38a7dad6f1b7b5f9e19c270e076 100644 (file)
@@ -78,10 +78,12 @@ class LinuxBridgeManager(object):
         self.local_ip = cfg.CONF.VXLAN.local_ip
         self.vxlan_mode = lconst.VXLAN_NONE
         if cfg.CONF.VXLAN.enable_vxlan:
-            self.local_int = self.get_interface_by_ip(self.local_ip)
-            if self.local_int:
+            device = self.ip.get_device_by_ip(self.local_ip)
+            if device:
+                self.local_int = device.name
                 self.check_vxlan_support()
             else:
+                self.local_int = None
                 LOG.warning(_LW('VXLAN is enabled, a valid local_ip '
                                 'must be provided'))
         # Store network mapping to segments
@@ -148,11 +150,6 @@ class LinuxBridgeManager(object):
             except OSError:
                 return 0
 
-    def get_interface_by_ip(self, ip):
-        for device in self.ip.get_devices():
-            if device.addr.list(to=ip):
-                return device.name
-
     def get_bridge_for_tap_device(self, tap_device_name):
         bridges = self.get_all_neutron_bridges()
         for bridge in bridges:
index 19de94bd39c7e8193d01ba1628776dbcb98ebea6..68cecc19d8f752b41b0ba5dfc7c31acd860d1d5d 100644 (file)
@@ -31,6 +31,9 @@ LOG = logging.getLogger(__name__)
 Device = collections.namedtuple('Device',
                                 'name ip_cidrs mac_address namespace')
 
+WRONG_IP = '0.0.0.0'
+TEST_IP = '240.0.0.1'
+
 
 class IpLibTestFramework(functional_base.BaseSudoTestCase):
     def setUp(self):
@@ -50,7 +53,7 @@ class IpLibTestFramework(functional_base.BaseSudoTestCase):
     def generate_device_details(self, name=None, ip_cidrs=None,
                                 mac_address=None, namespace=None):
         return Device(name or base.get_rand_name(),
-                      ip_cidrs or ['240.0.0.1/24'],
+                      ip_cidrs or ["%s/24" % TEST_IP],
                       mac_address or
                       utils.get_random_mac('fa:16:3e:00:00:00'.split(':')),
                       namespace or base.get_rand_name())
@@ -98,6 +101,13 @@ class IpLibTestCase(IpLibTestFramework):
         self.assertFalse(
             ip_lib.device_exists(attr.name, namespace=attr.namespace))
 
+    def test_ipwrapper_get_device_by_ip(self):
+        attr = self.generate_device_details()
+        self.manage_device(attr)
+        ip_wrapper = ip_lib.IPWrapper(namespace=attr.namespace)
+        self.assertEqual(attr.name, ip_wrapper.get_device_by_ip(TEST_IP).name)
+        self.assertIsNone(ip_wrapper.get_device_by_ip(WRONG_IP))
+
     def test_device_exists_with_ips_and_mac(self):
         attr = self.generate_device_details()
         device = self.manage_device(attr)
index e1ae8315f3a86249163fe2302f76e296b0dc136a..991ff43ea82e345708199121021972e8bf384563 100644 (file)
@@ -46,8 +46,8 @@ class TestLinuxBridge(base.BaseTestCase):
         super(TestLinuxBridge, self).setUp()
         interface_mappings = {'physnet1': 'eth1'}
 
-        with mock.patch.object(linuxbridge_neutron_agent.LinuxBridgeManager,
-                               'get_interface_by_ip', return_value=None):
+        with mock.patch.object(ip_lib.IPWrapper,
+                               'get_device_by_ip', return_value=None):
             self.linux_bridge = linuxbridge_neutron_agent.LinuxBridgeManager(
                 interface_mappings)
 
@@ -98,8 +98,8 @@ class TestLinuxBridgeAgent(base.BaseTestCase):
                                     'get_interface_mac')
         self.get_mac = self.get_mac_p.start()
         self.get_mac.return_value = '00:00:00:00:00:01'
-        with mock.patch.object(linuxbridge_neutron_agent.LinuxBridgeManager,
-                               'get_interface_by_ip', return_value=None):
+        with mock.patch.object(ip_lib.IPWrapper,
+                               'get_device_by_ip', return_value=None):
             self.agent = linuxbridge_neutron_agent.LinuxBridgeNeutronAgentRPC(
                 {}, 0, cfg.CONF.AGENT.quitting_rpc_timeout)
             with mock.patch.object(self.agent, "daemon_loop"):
@@ -351,8 +351,8 @@ class TestLinuxBridgeManager(base.BaseTestCase):
         super(TestLinuxBridgeManager, self).setUp()
         self.interface_mappings = {'physnet1': 'eth1'}
 
-        with mock.patch.object(linuxbridge_neutron_agent.LinuxBridgeManager,
-                               'get_interface_by_ip', return_value=None):
+        with mock.patch.object(ip_lib.IPWrapper,
+                               'get_device_by_ip', return_value=None):
             self.lbm = linuxbridge_neutron_agent.LinuxBridgeManager(
                 self.interface_mappings)
 
@@ -421,16 +421,6 @@ class TestLinuxBridgeManager(base.BaseTestCase):
             listdir_fn.side_effect = OSError()
             self.assertEqual(self.lbm.get_tap_devices_count('br0'), 0)
 
-    def test_get_interface_by_ip(self):
-        with mock.patch.object(ip_lib.IPWrapper, 'get_devices') as get_dev_fn,\
-                mock.patch.object(ip_lib.IpAddrCommand, 'list') as ip_list_fn:
-            device = mock.Mock()
-            device.name = 'dev_name'
-            get_dev_fn.return_value = [device]
-            ip_list_fn.returnvalue = mock.Mock()
-            self.assertEqual(self.lbm.get_interface_by_ip(LOCAL_IP),
-                             'dev_name')
-
     def test_get_bridge_for_tap_device(self):
         with mock.patch.object(self.lbm,
                                "get_all_neutron_bridges") as get_all_qbr_fn,\
@@ -768,8 +758,8 @@ class TestLinuxBridgeManager(base.BaseTestCase):
 
     def test_delete_vxlan_bridge_no_int_mappings(self):
         interface_mappings = {}
-        with mock.patch.object(linuxbridge_neutron_agent.LinuxBridgeManager,
-                               'get_interface_by_ip', return_value=None):
+        with mock.patch.object(ip_lib.IPWrapper,
+                               'get_device_by_ip', return_value=None):
             lbm = linuxbridge_neutron_agent.LinuxBridgeManager(
                 interface_mappings)
 
@@ -930,8 +920,8 @@ class TestLinuxBridgeRpcCallbacks(base.BaseTestCase):
             def __init__(self):
                 self.agent_id = 1
                 with mock.patch.object(
-                        linuxbridge_neutron_agent.LinuxBridgeManager,
-                        'get_interface_by_ip', return_value=None):
+                        ip_lib.IPWrapper,
+                        'get_device_by_ip', return_value=None):
                     self.br_mgr = (linuxbridge_neutron_agent.
                                    LinuxBridgeManager({'physnet1': 'eth1'}))