]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port test_netapp to Python 3
authorVictor Stinner <vstinner@redhat.com>
Fri, 2 Oct 2015 15:35:37 +0000 (17:35 +0200)
committerVictor Stinner <vstinner@redhat.com>
Fri, 2 Oct 2015 15:47:39 +0000 (17:47 +0200)
* Add close() method to FakeHttplibSocket,
  FakeDirectCmodeHTTPConnection, FakeDirect7modeHTTPConnection
* Replace StringIO() with BytesIO() for socket buffers
* makefile(): ignore optional arguments after mode
* Fix bytes versus Unicode: use bytes for HTTP body. Encode HTTP body
  to UTF-8 and HTTP headers to Latin1.
* tox.ini: add cinder.tests.unit.test_netapp to Python 3.4

Partial-Implements: blueprint cinder-python3
Change-Id: I3dc8c36a84ffbb47be3e85eed34518c6adad48ec

cinder/tests/unit/test_netapp.py
tox.ini

index 39334d793fc76a43d65142ff778ecb329fa49da3..78552beed8d5ec397f9f3ab870dd88992a84a6ff 100644 (file)
@@ -67,8 +67,8 @@ class FakeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 class FakeHttplibSocket(object):
     """A fake socket implementation for http_client.HTTPResponse."""
     def __init__(self, value):
-        self._rbuffer = six.StringIO(value)
-        self._wbuffer = six.StringIO('')
+        self._rbuffer = six.BytesIO(value)
+        self._wbuffer = six.BytesIO()
         oldclose = self._wbuffer.close
 
         def newclose():
@@ -76,24 +76,27 @@ class FakeHttplibSocket(object):
             oldclose()
         self._wbuffer.close = newclose
 
-    def makefile(self, mode, _other):
+    def makefile(self, mode, *args):
         """Returns the socket's internal buffer"""
         if mode == 'r' or mode == 'rb':
             return self._rbuffer
         if mode == 'w' or mode == 'wb':
             return self._wbuffer
 
+    def close(self):
+        pass
+
 
-RESPONSE_PREFIX_DIRECT_CMODE = """<?xml version='1.0' encoding='UTF-8' ?>
+RESPONSE_PREFIX_DIRECT_CMODE = b"""<?xml version='1.0' encoding='UTF-8' ?>
 <!DOCTYPE netapp SYSTEM 'file:/etc/netapp_gx.dtd'>"""
 
-RESPONSE_PREFIX_DIRECT_7MODE = """<?xml version='1.0' encoding='UTF-8' ?>
+RESPONSE_PREFIX_DIRECT_7MODE = b"""<?xml version='1.0' encoding='UTF-8' ?>
 <!DOCTYPE netapp SYSTEM "/na_admin/netapp_filer.dtd">"""
 
-RESPONSE_PREFIX_DIRECT = """
+RESPONSE_PREFIX_DIRECT = b"""
 <netapp version='1.15' xmlns='http://www.netapp.com/filer/admin'>"""
 
-RESPONSE_SUFFIX_DIRECT = """</netapp>"""
+RESPONSE_SUFFIX_DIRECT = b"""</netapp>"""
 
 
 class FakeDirectCMODEServerHandler(FakeHTTPRequestHandler):
@@ -431,6 +434,8 @@ class FakeDirectCMODEServerHandler(FakeHTTPRequestHandler):
         s.end_headers()
         s.wfile.write(RESPONSE_PREFIX_DIRECT_CMODE)
         s.wfile.write(RESPONSE_PREFIX_DIRECT)
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
         s.wfile.write(body)
         s.wfile.write(RESPONSE_SUFFIX_DIRECT)
 
@@ -466,8 +471,10 @@ class FakeDirectCmodeHTTPConnection(object):
         req_str = '%s %s HTTP/1.1\r\n' % (method, path)
         for key, value in headers.items():
             req_str += "%s: %s\r\n" % (key, value)
+        if isinstance(req_str, six.text_type):
+            req_str = req_str.encode('latin1')
         if data:
-            req_str += '\r\n%s' % data
+            req_str += b'\r\n' + data
 
         # NOTE(vish): normally the http transport normalizes from unicode
         sock = FakeHttplibSocket(req_str.decode("latin-1").encode("utf-8"))
@@ -489,6 +496,9 @@ class FakeDirectCmodeHTTPConnection(object):
     def getresponsebody(self):
         return self.sock.result
 
+    def close(self):
+        pass
+
 
 class NetAppDirectCmodeISCSIDriverTestCase(test.TestCase):
     """Test case for NetAppISCSIDriver"""
@@ -1174,6 +1184,8 @@ class FakeDirect7MODEServerHandler(FakeHTTPRequestHandler):
         s.end_headers()
         s.wfile.write(RESPONSE_PREFIX_DIRECT_7MODE)
         s.wfile.write(RESPONSE_PREFIX_DIRECT)
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
         s.wfile.write(body)
         s.wfile.write(RESPONSE_SUFFIX_DIRECT)
 
@@ -1194,8 +1206,10 @@ class FakeDirect7modeHTTPConnection(object):
         req_str = '%s %s HTTP/1.1\r\n' % (method, path)
         for key, value in headers.items():
             req_str += "%s: %s\r\n" % (key, value)
+        if isinstance(req_str, six.text_type):
+            req_str = req_str.encode('latin1')
         if data:
-            req_str += '\r\n%s' % data
+            req_str += b'\r\n' + data
 
         # NOTE(vish): normally the http transport normailizes from unicode
         sock = FakeHttplibSocket(req_str.decode("latin-1").encode("utf-8"))
@@ -1217,6 +1231,9 @@ class FakeDirect7modeHTTPConnection(object):
     def getresponsebody(self):
         return self.sock.result
 
+    def close(self):
+        pass
+
 
 class NetAppDirect7modeISCSIDriverTestCase_NV(test.TestCase):
     """Test case for NetAppISCSIDriver without vfiler"""
diff --git a/tox.ini b/tox.ini
index d86012f2008a58612f8d1e0d4c680411ec792319..61f6f15f0743b2f204f3505815de91f28de37ff3 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -79,6 +79,7 @@ commands =
     cinder.tests.unit.test_infortrend_cli \
     cinder.tests.unit.test_migrations \
     cinder.tests.unit.test_misc \
+    cinder.tests.unit.test_netapp \
     cinder.tests.unit.test_netapp_nfs \
     cinder.tests.unit.test_nfs \
     cinder.tests.unit.test_nimble \