diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index 0eb42dec..75565377 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -63,7 +63,7 @@ class _BaseHTTPClient(object): chunk = body while chunk: chunk = body.read(CHUNKSIZE) - if chunk == '': + if not chunk: break yield chunk diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 2bdca0a3..9f3a1fec 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -360,9 +360,12 @@ def get_data_file(args): return None if not sys.stdin.isatty(): # (2) image data is provided through standard input + image = sys.stdin + if hasattr(sys.stdin, 'buffer'): + image = sys.stdin.buffer if msvcrt: - msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) - return sys.stdin + msvcrt.setmode(image.fileno(), os.O_BINARY) + return image else: # (3) no image data provided return None diff --git a/glanceclient/exc.py b/glanceclient/exc.py index 29189e4d..c8616c3e 100644 --- a/glanceclient/exc.py +++ b/glanceclient/exc.py @@ -16,6 +16,8 @@ import re import sys +import six + class BaseException(Exception): """An error occurred.""" @@ -177,6 +179,8 @@ def from_response(response, body=None): details = ': '.join(details_temp) return cls(details=details) elif body: + if six.PY3: + body = body.decode('utf-8') details = body.replace('\n\n', '\n') return cls(details=details) diff --git a/glanceclient/tests/unit/test_exc.py b/glanceclient/tests/unit/test_exc.py index 575c62b5..9a2d01fd 100644 --- a/glanceclient/tests/unit/test_exc.py +++ b/glanceclient/tests/unit/test_exc.py @@ -68,3 +68,11 @@ class TestHTTPExceptions(testtools.TestCase): self.assertIsInstance(err, exc.HTTPNotFound) self.assertEqual("404 Entity Not Found: Entity could not be found", err.details) + + def test_format_no_content_type(self): + mock_resp = mock.Mock() + mock_resp.status_code = 400 + mock_resp.headers = {'content-type': 'application/octet-stream'} + body = b'Error \n\n' + err = exc.from_response(mock_resp, body) + self.assertEqual('Error \n', err.details) diff --git a/glanceclient/tests/unit/test_http.py b/glanceclient/tests/unit/test_http.py index 28ae8e1a..020e146c 100644 --- a/glanceclient/tests/unit/test_http.py +++ b/glanceclient/tests/unit/test_http.py @@ -239,6 +239,14 @@ class TestClient(testtools.TestCase): test_client = http.HTTPClient(endpoint, token=u'adc123') self.assertEqual(600.0, test_client.timeout) + def test__chunk_body_exact_size_chunk(self): + test_client = http._BaseHTTPClient() + bytestring = b'x' * http.CHUNKSIZE + data = six.BytesIO(bytestring) + chunk = list(test_client._chunk_body(data)) + self.assertEqual(1, len(chunk)) + self.assertEqual([bytestring], chunk) + def test_http_chunked_request(self): text = "Ok" data = six.StringIO(text) diff --git a/glanceclient/tests/unit/v1/test_shell.py b/glanceclient/tests/unit/v1/test_shell.py index 93f3fe6c..95bbd07c 100644 --- a/glanceclient/tests/unit/v1/test_shell.py +++ b/glanceclient/tests/unit/v1/test_shell.py @@ -574,7 +574,7 @@ class ShellStdinHandlingTests(testtools.TestCase): self.assertIn('data', self.collected_args[1]) self.assertIsInstance(self.collected_args[1]['data'], file_type) - self.assertEqual('Some Data', + self.assertEqual(b'Some Data', self.collected_args[1]['data'].read()) finally: @@ -599,7 +599,7 @@ class ShellStdinHandlingTests(testtools.TestCase): self.assertIn('data', self.collected_args[1]) self.assertIsInstance(self.collected_args[1]['data'], file_type) - self.assertEqual('Some Data\n', + self.assertEqual(b'Some Data\n', self.collected_args[1]['data'].read()) finally: