Don't append %interface with ipv6 ips.

- Since netifaces is appending a %interface (i.e: %eth0) at the end and
  does not make a correct ipv6 address we are removing it.
- Improve the tests of whataremyips along the way.

Closes-Bug: 1209443
Change-Id: I585e795083783009961b429607ca3f66b8d7ec30
This commit is contained in:
Chmouel Boudjnah 2013-08-08 11:11:39 +02:00
parent 35246ce347
commit a0c34ed995
2 changed files with 39 additions and 2 deletions
swift/common
test/unit/common

@ -1124,7 +1124,13 @@ def whataremyips():
if family not in (netifaces.AF_INET, netifaces.AF_INET6): if family not in (netifaces.AF_INET, netifaces.AF_INET6):
continue continue
for address in iface_data[family]: for address in iface_data[family]:
addresses.append(address['addr']) addr = address['addr']
# If we have an ipv6 address remove the
# %ether_interface at the end
if family == netifaces.AF_INET6:
addr = addr.split('%')[0]
addresses.append(addr)
except ValueError: except ValueError:
pass pass
return addresses return addresses

@ -35,6 +35,7 @@ import time
import unittest import unittest
import fcntl import fcntl
import shutil import shutil
from contextlib import nested
from Queue import Queue, Empty from Queue import Queue, Empty
from getpass import getuser from getpass import getuser
@ -42,7 +43,7 @@ from shutil import rmtree
from StringIO import StringIO from StringIO import StringIO
from functools import partial from functools import partial
from tempfile import TemporaryFile, NamedTemporaryFile, mkdtemp from tempfile import TemporaryFile, NamedTemporaryFile, mkdtemp
from netifaces import AF_INET6
from mock import MagicMock, patch from mock import MagicMock, patch
from swift.common.exceptions import (Timeout, MessageTimeout, from swift.common.exceptions import (Timeout, MessageTimeout,
@ -644,6 +645,36 @@ class TestUtils(unittest.TestCase):
self.assert_(len(myips) > 1) self.assert_(len(myips) > 1)
self.assert_('127.0.0.1' in myips) self.assert_('127.0.0.1' in myips)
def test_whataremyips_error(self):
def my_interfaces():
return ['eth0']
def my_ifaddress_error(interface):
raise ValueError
with nested(
patch('netifaces.interfaces', my_interfaces),
patch('netifaces.ifaddresses', my_ifaddress_error)):
self.assertEquals(utils.whataremyips(), [])
def test_whataremyips_ipv6(self):
test_ipv6_address = '2001:6b0:dead:beef:2::32'
test_interface = 'eth0'
def my_ipv6_interfaces():
return ['eth0']
def my_ipv6_ifaddresses(interface):
return {AF_INET6:
[{'netmask': 'ffff:ffff:ffff:ffff::',
'addr': '%s%%%s' % (test_ipv6_address, test_interface)}]}
with nested(
patch('netifaces.interfaces', my_ipv6_interfaces),
patch('netifaces.ifaddresses', my_ipv6_ifaddresses)):
myips = utils.whataremyips()
self.assertEquals(len(myips), 1)
self.assertEquals(myips[0], test_ipv6_address)
def test_hash_path(self): def test_hash_path(self):
_prefix = utils.HASH_PATH_PREFIX _prefix = utils.HASH_PATH_PREFIX
utils.HASH_PATH_PREFIX = '' utils.HASH_PATH_PREFIX = ''