Add IPv6 support to create_websocket()

The current implementation is hardcoded to use AF_INET, which means
IPv4. We need to be able to support IPv6 connections, too, so we use the
socket.getaddrinfo() function as described in [1] in order to provide a
more generic solution.

[1] https://docs.python.org/2/library/socket.html

Change-Id: I2ccbe3b25e79031a3c961871034c0c1b94d7ee80
Closes-Bug: 1719575
Related-Bug: 1656329
diff --git a/tempest/common/compute.py b/tempest/common/compute.py
index df0f5a5..aa50a7c 100644
--- a/tempest/common/compute.py
+++ b/tempest/common/compute.py
@@ -287,13 +287,21 @@
 
 def create_websocket(url):
     url = urlparse.urlparse(url)
-    if url.scheme == 'https':
-        client_socket = ssl.wrap_socket(socket.socket(socket.AF_INET,
-                                                      socket.SOCK_STREAM))
+    for res in socket.getaddrinfo(url.hostname, url.port,
+                                  socket.AF_UNSPEC, socket.SOCK_STREAM):
+        af, socktype, proto, _, sa = res
+        client_socket = socket.socket(af, socktype, proto)
+        if url.scheme == 'https':
+            client_socket = ssl.wrap_socket(client_socket)
+        client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        try:
+            client_socket.connect(sa)
+        except socket.error:
+            client_socket.close()
+            continue
+        break
     else:
-        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-    client_socket.connect((url.hostname, url.port))
+        raise socket.error('WebSocket creation failed')
     # Turn the Socket into a WebSocket to do the communication
     return _WebSocket(client_socket, url)