From 28e9a940323e4ebe46a875add1a87f113ec6c610 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 2 Oct 2015 17:35:37 +0200 Subject: [PATCH] Port test_netapp to Python 3 * 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 | 35 ++++++++++++++++++++++++-------- tox.ini | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cinder/tests/unit/test_netapp.py b/cinder/tests/unit/test_netapp.py index 39334d793..78552beed 100644 --- a/cinder/tests/unit/test_netapp.py +++ b/cinder/tests/unit/test_netapp.py @@ -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 = """ +RESPONSE_PREFIX_DIRECT_CMODE = b""" """ -RESPONSE_PREFIX_DIRECT_7MODE = """ +RESPONSE_PREFIX_DIRECT_7MODE = b""" """ -RESPONSE_PREFIX_DIRECT = """ +RESPONSE_PREFIX_DIRECT = b""" """ -RESPONSE_SUFFIX_DIRECT = """""" +RESPONSE_SUFFIX_DIRECT = b"""""" 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 d86012f20..61f6f15f0 100644 --- 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 \ -- 2.45.2