From a0c34ed99560e28be566a5a6fe00f26c12e81e23 Mon Sep 17 00:00:00 2001
From: Chmouel Boudjnah <chmouel@enovance.com>
Date: Thu, 8 Aug 2013 11:11:39 +0200
Subject: [PATCH] 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
---
 swift/common/utils.py          |  8 +++++++-
 test/unit/common/test_utils.py | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 2 deletions(-)

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 3b3b04adcb..a7fd544e93 100644
--- a/test/unit/common/test_utils.py
+++ b/test/unit/common/test_utils.py
@@ -35,6 +35,7 @@ import time
 import unittest
 import fcntl
 import shutil
+from contextlib import nested
 
 from Queue import Queue, Empty
 from getpass import getuser
@@ -42,7 +43,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,
@@ -644,6 +645,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 = ''