remove netifaces dependency of ryu-agent

Fixes bug #1203663

This patch removes the netifaces dependency.

Change-Id: I0970ca7d8c92fcd1c3dfaded4a9ce6e82b8c033f
This commit is contained in:
Yoshihiro Kaneko 2013-07-22 09:01:30 +00:00
parent b5d1c993de
commit 85dc410ef8
3 changed files with 74 additions and 26 deletions

View File

@ -26,12 +26,12 @@ import sys
import time import time
import eventlet import eventlet
import netifaces
from oslo.config import cfg from oslo.config import cfg
from ryu.app import client from ryu.app import client
from ryu.app import conf_switch_key from ryu.app import conf_switch_key
from ryu.app import rest_nw_id 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 import ovs_lib
from neutron.agent.linux.ovs_lib import VifPort from neutron.agent.linux.ovs_lib import VifPort
from neutron.agent import rpc as agent_rpc from neutron.agent import rpc as agent_rpc
@ -66,6 +66,15 @@ def _get_my_ip():
return addr 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): def _get_ip(cfg_ip_str, cfg_interface_str):
ip = None ip = None
try: try:
@ -81,8 +90,11 @@ def _get_ip(cfg_ip_str, cfg_interface_str):
except (cfg.NoSuchOptError, cfg.NoSuchGroupError): except (cfg.NoSuchOptError, cfg.NoSuchGroupError):
pass pass
if iface: if iface:
iface = netifaces.ifaddresses(iface)[netifaces.AF_INET][0] ip = _get_ip_from_nic(iface)
return iface['addr'] 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() return _get_my_ip()

View File

@ -445,7 +445,7 @@ class TestOVSBridge(RyuAgentTestCase):
]) ])
self.assertEqual(mock_ofport.call_count, 0) self.assertEqual(mock_ofport.call_count, 0)
self.assertEqual(mock_vif.call_count, 0) self.assertEqual(mock_vif.call_count, 0)
self.assertEqual(vifport, None) self.assertIsNone(vifport)
def test_get_external_port_tunnel(self): def test_get_external_port_tunnel(self):
with nested( with nested(
@ -464,7 +464,7 @@ class TestOVSBridge(RyuAgentTestCase):
]) ])
self.assertEqual(mock_ofport.call_count, 0) self.assertEqual(mock_ofport.call_count, 0)
self.assertEqual(mock_vif.call_count, 0) self.assertEqual(mock_vif.call_count, 0)
self.assertEqual(vifport, None) self.assertIsNone(vifport)
def test_get_external_ports(self): def test_get_external_ports(self):
with nested( with nested(
@ -488,37 +488,59 @@ class TestRyuNeutronAgent(RyuAgentTestCase):
self.assertEqual(addr, '1.2.3.4') 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): def test_get_ip_ip(self):
cfg_attrs = {'CONF.OVS.cfg_ip': '1.2.3.4', cfg_attrs = {'CONF.OVS.cfg_ip': '1.2.3.4',
'CONF.OVS.cfg_iface': 'eth0'} 'CONF.OVS.cfg_iface': 'eth0'}
netifs_attrs = {'AF_INET': 0,
'ifaddresses.return_value': [[{'addr': '10.0.0.1'}]]}
with nested( with nested(
mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs), 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', mock.patch(self._AGENT_NAME + '._get_my_ip',
return_value='172.16.0.1') 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') 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(mock_myip.call_count, 0)
self.assertEqual(ip, '1.2.3.4') 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, cfg_attrs = {'CONF.OVS.cfg_ip': None,
'CONF.OVS.cfg_iface': 'eth0'} 'CONF.OVS.cfg_iface': 'eth0'}
netifs_attrs = {'AF_INET': 0,
'ifaddresses.return_value': [[{'addr': '10.0.0.1'}]]}
with nested( with nested(
mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs), 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', mock.patch(self._AGENT_NAME + '._get_my_ip',
return_value='172.16.0.1') 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') ip = self.mod_agent._get_ip('cfg_ip', 'cfg_iface')
mock_netifs.ifaddresses.assert_has_calls([ mock_nicip.assert_has_calls([
mock.call('eth0') mock.call('eth0')
]) ])
self.assertEqual(mock_myip.call_count, 0) self.assertEqual(mock_myip.call_count, 0)
@ -527,17 +549,36 @@ class TestRyuNeutronAgent(RyuAgentTestCase):
def test_get_ip_myip(self): def test_get_ip_myip(self):
cfg_attrs = {'CONF.OVS.cfg_ip': None, cfg_attrs = {'CONF.OVS.cfg_ip': None,
'CONF.OVS.cfg_iface': None} 'CONF.OVS.cfg_iface': None}
netifs_attrs = {'AF_INET': 0,
'ifaddresses.return_value': [[{'addr': '10.0.0.1'}]]}
with nested( with nested(
mock.patch(self._AGENT_NAME + '.cfg', **cfg_attrs), 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', mock.patch(self._AGENT_NAME + '._get_my_ip',
return_value='172.16.0.1') 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') 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_myip.assert_has_calls([
mock.call() mock.call()
]) ])

View File

@ -24,8 +24,3 @@ webtest==1.3.3
############################### ###############################
configobj configobj
############################### ###############################
# Packages for the Ryu Plugin
###############################
netifaces
###############################