diff --git a/nova/tests/test_test_utils.py b/nova/tests/test_test_utils.py index ef3a6fb6897e..2184d39af834 100644 --- a/nova/tests/test_test_utils.py +++ b/nova/tests/test_test_utils.py @@ -14,6 +14,11 @@ # License for the specific language governing permissions and limitations # under the License. +import errno +import socket + +import fixtures + from nova import db from nova import test from nova.tests import utils as test_utils @@ -39,3 +44,28 @@ class TestUtilsTestCase(test.TestCase): # The challenge here is to define what exactly such a structure # must look like. pass + + def test_ipv6_supported(self): + self.assertIn(test_utils.is_ipv6_supported(), (False, True)) + + def fake_open(path): + raise IOError + + def fake_socket_fail(x, y): + e = socket.error() + e.errno = errno.EAFNOSUPPORT + raise e + + def fake_socket_ok(x, y): + return + + with fixtures.MonkeyPatch('socket.socket', fake_socket_fail): + self.assertFalse(test_utils.is_ipv6_supported()) + + with fixtures.MonkeyPatch('socket.socket', fake_socket_ok): + with fixtures.MonkeyPatch('sys.platform', 'windows'): + self.assertTrue(test_utils.is_ipv6_supported()) + + with fixtures.MonkeyPatch('sys.platform', 'linux2'): + with fixtures.MonkeyPatch('__builtin__.open', fake_open): + self.assertFalse(test_utils.is_ipv6_supported()) diff --git a/nova/tests/utils.py b/nova/tests/utils.py index 748a85094682..20a924c94b7c 100644 --- a/nova/tests/utils.py +++ b/nova/tests/utils.py @@ -17,6 +17,7 @@ import errno import platform import socket +import sys from oslo.config import cfg @@ -177,7 +178,7 @@ def killer_xml_body(): def is_ipv6_supported(): - has_ipv6_support = True + has_ipv6_support = socket.has_ipv6 try: s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) except socket.error as e: @@ -185,4 +186,14 @@ def is_ipv6_supported(): has_ipv6_support = False else: raise + + # check if there is at least one interface with ipv6 + if has_ipv6_support and sys.platform.startswith('linux'): + try: + with open('/proc/net/if_inet6') as f: + if not f.read(): + has_ipv6_support = False + except IOError: + has_ipv6_support = False + return has_ipv6_support