diff --git a/swift/common/utils.py b/swift/common/utils.py index 3c8c3672fb..febeeaab72 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -1124,7 +1124,13 @@ def whataremyips(): if family not in (netifaces.AF_INET, netifaces.AF_INET6): continue 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: pass return addresses diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 612df89ab6..652c7e4d6f 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -36,6 +36,7 @@ import time import unittest import fcntl import shutil +from contextlib import nested from Queue import Queue, Empty from getpass import getuser @@ -43,7 +44,7 @@ from shutil import rmtree from StringIO import StringIO from functools import partial from tempfile import TemporaryFile, NamedTemporaryFile, mkdtemp - +from netifaces import AF_INET6 from mock import MagicMock, patch from swift.common.exceptions import (Timeout, MessageTimeout, @@ -645,6 +646,36 @@ class TestUtils(unittest.TestCase): self.assert_(len(myips) > 1) 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): _prefix = utils.HASH_PATH_PREFIX utils.HASH_PATH_PREFIX = ''