]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
remove netifaces dependency of ryu-agent
authorYoshihiro Kaneko <ykaneko0929@gmail.com>
Mon, 22 Jul 2013 09:01:30 +0000 (09:01 +0000)
committerYoshihiro Kaneko <ykaneko0929@gmail.com>
Wed, 24 Jul 2013 11:18:23 +0000 (20:18 +0900)
Fixes bug #1203663

This patch removes the netifaces dependency.

Change-Id: I0970ca7d8c92fcd1c3dfaded4a9ce6e82b8c033f

neutron/plugins/ryu/agent/ryu_neutron_agent.py
neutron/tests/unit/ryu/test_ryu_agent.py
test-requirements.txt

index 85884f921c418f3665502831f722738842dbd076..7534a5b92236739f7497b559097c7f411751f406 100755 (executable)
@@ -26,12 +26,12 @@ import sys
 import time
 
 import eventlet
-import netifaces
 from oslo.config import cfg
 from ryu.app import client
 from ryu.app import conf_switch_key
 from ryu.app import rest_nw_id
 
+from neutron.agent.linux import ip_lib
 from neutron.agent.linux import ovs_lib
 from neutron.agent.linux.ovs_lib import VifPort
 from neutron.agent import rpc as agent_rpc
@@ -66,6 +66,15 @@ def _get_my_ip():
     return addr
 
 
+def _get_ip_from_nic(nic):
+    ip_wrapper = ip_lib.IPWrapper()
+    dev = ip_wrapper.device(nic)
+    addrs = dev.addr.list(scope='global')
+    for addr in addrs:
+        if addr['ip_version'] == 4:
+            return addr['cidr'].split('/')[0]
+
+
 def _get_ip(cfg_ip_str, cfg_interface_str):
     ip = None
     try:
@@ -81,8 +90,11 @@ def _get_ip(cfg_ip_str, cfg_interface_str):
     except (cfg.NoSuchOptError, cfg.NoSuchGroupError):
         pass
     if iface:
-        iface = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]
-        return iface['addr']
+        ip = _get_ip_from_nic(iface)
+        if ip:
+            return ip
+        LOG.warning(_('Could not get IPv4 address from %(nic)s: %(cfg)s'),
+                    {'nic': iface, 'cfg': cfg_interface_str})
 
     return _get_my_ip()
 
index f5b9a28e93bb251db91e450a4704a009e7b9600e..9896389347f3ba1d10a6fc1387cff5c4e8d09259 100644 (file)
@@ -445,7 +445,7 @@ class TestOVSBridge(RyuAgentTestCase):
         ])
         self.assertEqual(mock_ofport.call_count, 0)
         self.assertEqual(mock_vif.call_count, 0)
-        self.assertEqual(vifport, None)
+        self.assertIsNone(vifport)
 
     def test_get_external_port_tunnel(self):
         with nested(
@@ -464,7 +464,7 @@ class TestOVSBridge(RyuAgentTestCase):
         ])
         self.assertEqual(mock_ofport.call_count, 0)
         self.assertEqual(mock_vif.call_count, 0)
-        self.assertEqual(vifport, None)
+        self.assertIsNone(vifport)
 
     def test_get_external_ports(self):
         with nested(
@@ -488,37 +488,59 @@ class TestRyuNeutronAgent(RyuAgentTestCase):
 
         self.assertEqual(addr, '1.2.3.4')
 
+    def test_get_ip_from_nic(self):
+        mock_device = mock.Mock()
+        mock_device.addr.list = mock.Mock(
+            return_value=[{'ip_version': 6, 'cidr': '::ffff:1.2.3.4'},
+                          {'ip_version': 4, 'cidr': '1.2.3.4/8'}])
+        mock_ip_wrapper = mock.Mock()
+        mock_ip_wrapper.device = mock.Mock(return_value=mock_device)
+        with mock.patch(self._AGENT_NAME + '.ip_lib.IPWrapper',
+                        return_value=mock_ip_wrapper):
+            addr = self.mod_agent._get_ip_from_nic('eth0')
+
+        self.assertEqual(addr, '1.2.3.4')
+
+    def test_get_ip_from_nic_empty(self):
+        mock_device = mock.Mock()
+        mock_device.addr.list = mock.Mock(return_value=[])
+        mock_ip_wrapper = mock.Mock()
+        mock_ip_wrapper.device = mock.Mock(return_value=mock_device)
+        with mock.patch(self._AGENT_NAME + '.ip_lib.IPWrapper',
+                        return_value=mock_ip_wrapper):
+            addr = self.mod_agent._get_ip_from_nic('eth0')
+
+        self.assertIsNone(addr)
+
     def test_get_ip_ip(self):
         cfg_attrs = {'CONF.OVS.cfg_ip': '1.2.3.4',
                      'CONF.OVS.cfg_iface': 'eth0'}
-        netifs_attrs = {'AF_INET': 0,
-                        'ifaddresses.return_value': [[{'addr': '10.0.0.1'}]]}
         with nested(
             mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs),
-            mock.patch(self._AGENT_NAME + '.netifaces', **netifs_attrs),
+            mock.patch(self._AGENT_NAME + '._get_ip_from_nic',
+                       return_value='10.0.0.1'),
             mock.patch(self._AGENT_NAME + '._get_my_ip',
                        return_value='172.16.0.1')
-        ) as (_cfg, mock_netifs, mock_myip):
+        ) as (_cfg, mock_nicip, mock_myip):
             ip = self.mod_agent._get_ip('cfg_ip', 'cfg_iface')
 
-        self.assertEqual(mock_netifs.ifaddresses.call_count, 0)
+        self.assertEqual(mock_nicip.call_count, 0)
         self.assertEqual(mock_myip.call_count, 0)
         self.assertEqual(ip, '1.2.3.4')
 
-    def test_get_ip_iface(self):
+    def test_get_ip_nic(self):
         cfg_attrs = {'CONF.OVS.cfg_ip': None,
                      'CONF.OVS.cfg_iface': 'eth0'}
-        netifs_attrs = {'AF_INET': 0,
-                        'ifaddresses.return_value': [[{'addr': '10.0.0.1'}]]}
         with nested(
             mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs),
-            mock.patch(self._AGENT_NAME + '.netifaces', **netifs_attrs),
+            mock.patch(self._AGENT_NAME + '._get_ip_from_nic',
+                       return_value='10.0.0.1'),
             mock.patch(self._AGENT_NAME + '._get_my_ip',
                        return_value='172.16.0.1')
-        ) as (_cfg, mock_netifs, mock_myip):
+        ) as (_cfg, mock_nicip, mock_myip):
             ip = self.mod_agent._get_ip('cfg_ip', 'cfg_iface')
 
-        mock_netifs.ifaddresses.assert_has_calls([
+        mock_nicip.assert_has_calls([
             mock.call('eth0')
         ])
         self.assertEqual(mock_myip.call_count, 0)
@@ -527,17 +549,36 @@ class TestRyuNeutronAgent(RyuAgentTestCase):
     def test_get_ip_myip(self):
         cfg_attrs = {'CONF.OVS.cfg_ip': None,
                      'CONF.OVS.cfg_iface': None}
-        netifs_attrs = {'AF_INET': 0,
-                        'ifaddresses.return_value': [[{'addr': '10.0.0.1'}]]}
         with nested(
             mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs),
-            mock.patch(self._AGENT_NAME + '.netifaces', **netifs_attrs),
+            mock.patch(self._AGENT_NAME + '._get_ip_from_nic',
+                       return_value='10.0.0.1'),
             mock.patch(self._AGENT_NAME + '._get_my_ip',
                        return_value='172.16.0.1')
-        ) as (_cfg, mock_netifs, mock_myip):
+        ) as (_cfg, mock_nicip, mock_myip):
             ip = self.mod_agent._get_ip('cfg_ip', 'cfg_iface')
 
-        self.assertEqual(mock_netifs.ifaddresses.call_count, 0)
+        self.assertEqual(mock_nicip.call_count, 0)
+        mock_myip.assert_has_calls([
+            mock.call()
+        ])
+        self.assertEqual(ip, '172.16.0.1')
+
+    def test_get_ip_nic_myip(self):
+        cfg_attrs = {'CONF.OVS.cfg_ip': None,
+                     'CONF.OVS.cfg_iface': 'eth0'}
+        with nested(
+            mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs),
+            mock.patch(self._AGENT_NAME + '._get_ip_from_nic',
+                       return_value=None),
+            mock.patch(self._AGENT_NAME + '._get_my_ip',
+                       return_value='172.16.0.1')
+        ) as (_cfg, mock_nicip, mock_myip):
+            ip = self.mod_agent._get_ip('cfg_ip', 'cfg_iface')
+
+        mock_nicip.assert_has_calls([
+            mock.call('eth0')
+        ])
         mock_myip.assert_has_calls([
             mock.call()
         ])
index 4d89e7b392400f967f124f63bca44d4547833628..88a1dbabbc7308d8441422ee4c90e4446362b8ae 100644 (file)
@@ -24,8 +24,3 @@ webtest==1.3.3
 ###############################
 configobj
 ###############################
-
-# Packages for the Ryu Plugin
-###############################
-netifaces
-###############################