From 85dc410ef8f79366931bc754440039d265f8a6b4 Mon Sep 17 00:00:00 2001 From: Yoshihiro Kaneko Date: Mon, 22 Jul 2013 09:01:30 +0000 Subject: [PATCH] remove netifaces dependency of ryu-agent Fixes bug #1203663 This patch removes the netifaces dependency. Change-Id: I0970ca7d8c92fcd1c3dfaded4a9ce6e82b8c033f --- .../plugins/ryu/agent/ryu_neutron_agent.py | 18 ++++- neutron/tests/unit/ryu/test_ryu_agent.py | 77 ++++++++++++++----- test-requirements.txt | 5 -- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/neutron/plugins/ryu/agent/ryu_neutron_agent.py b/neutron/plugins/ryu/agent/ryu_neutron_agent.py index 85884f921..7534a5b92 100755 --- a/neutron/plugins/ryu/agent/ryu_neutron_agent.py +++ b/neutron/plugins/ryu/agent/ryu_neutron_agent.py @@ -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() diff --git a/neutron/tests/unit/ryu/test_ryu_agent.py b/neutron/tests/unit/ryu/test_ryu_agent.py index f5b9a28e9..989638934 100644 --- a/neutron/tests/unit/ryu/test_ryu_agent.py +++ b/neutron/tests/unit/ryu/test_ryu_agent.py @@ -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() ]) diff --git a/test-requirements.txt b/test-requirements.txt index 4d89e7b39..88a1dbabb 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -24,8 +24,3 @@ webtest==1.3.3 ############################### configobj ############################### - -# Packages for the Ryu Plugin -############################### -netifaces -############################### -- 2.45.2