From c67d8b2d2ddbf62640443c29b2879019411e7564 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 30 Jun 2015 16:06:28 +0200 Subject: [PATCH] Port image/glance.py to Python 3 * Fix usage of six.reraise(): new_exc is the exception value, not the exception type * Replace __builtin__ with six.moves.builtins. * test_extracting_v2_boot_properties(): set config.glance_num_retries to fix the test on Python 3 (comparison between mock and int now raises a TypeError on Python 3). * TestGlanceSerializer: use dictionaries with only one key to have a reliable output even if the hash is randomized (hash randomization is now enabled by default in Python 3). * tox.ini: add cinder.tests.unit.image.test_glance to Python 3.4. Blueprint cinder-python3 Change-Id: I33cd02e1b0666d7b5999b2fdaf469dc59fff1866 --- cinder/image/glance.py | 4 ++-- cinder/tests/unit/image/test_glance.py | 26 +++++++++++--------------- tox.ini | 1 + 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/cinder/image/glance.py b/cinder/image/glance.py index be32766b6..961e7cce6 100644 --- a/cinder/image/glance.py +++ b/cinder/image/glance.py @@ -482,14 +482,14 @@ def _reraise_translated_image_exception(image_id): """Transform the exception for the image but keep its traceback intact.""" _exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_image_exception(image_id, exc_value) - six.reraise(new_exc, None, exc_trace) + six.reraise(type(new_exc), new_exc, exc_trace) def _reraise_translated_exception(): """Transform the exception but keep its traceback intact.""" _exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_plain_exception(exc_value) - six.reraise(new_exc, None, exc_trace) + six.reraise(type(new_exc), new_exc, exc_trace) def _translate_image_exception(image_id, exc_value): diff --git a/cinder/tests/unit/image/test_glance.py b/cinder/tests/unit/image/test_glance.py index a8b61caea..e1defc409 100644 --- a/cinder/tests/unit/image/test_glance.py +++ b/cinder/tests/unit/image/test_glance.py @@ -45,15 +45,11 @@ class TestGlanceSerializer(test.TestCase): 'properties': { 'prop1': 'propvalue1', 'mappings': [ - {'virtual': 'aaa', - 'device': 'bbb'}, - {'virtual': 'xxx', - 'device': 'yyy'}], + {'device': 'bbb'}, + {'device': 'yyy'}], 'block_device_mapping': [ - {'virtual_device': 'fake', - 'device_name': '/dev/fake'}, - {'virtual_device': 'ephemeral0', - 'device_name': '/dev/fake0'}]}} + {'device_name': '/dev/fake'}, + {'device_name': '/dev/fake0'}]}} converted_expected = { 'name': 'image1', @@ -62,12 +58,11 @@ class TestGlanceSerializer(test.TestCase): 'properties': { 'prop1': 'propvalue1', 'mappings': - '[{"device": "bbb", "virtual": "aaa"}, ' - '{"device": "yyy", "virtual": "xxx"}]', + '[{"device": "bbb"}, ' + '{"device": "yyy"}]', 'block_device_mapping': - '[{"virtual_device": "fake", "device_name": "/dev/fake"}, ' - '{"virtual_device": "ephemeral0", ' - '"device_name": "/dev/fake0"}]'}} + '[{"device_name": "/dev/fake"}, ' + '{"device_name": "/dev/fake0"}]'}} converted = glance._convert_to_string(metadata) self.assertEqual(converted, converted_expected) self.assertEqual(glance._convert_from_string(converted), metadata) @@ -518,7 +513,7 @@ class TestGlanceImageService(test.TestCase): self.assertRaises(exception.ImageNotFound, service.download, self.context, image_id, writer) - @mock.patch('__builtin__.open') + @mock.patch('six.moves.builtins.open') @mock.patch('shutil.copyfileobj') def test_download_from_direct_file(self, mock_copyfileobj, mock_open): fixture = self._make_fixture(name='test image', @@ -530,7 +525,7 @@ class TestGlanceImageService(test.TestCase): self.service.download(self.context, image_id, writer) mock_copyfileobj.assert_called_once_with(mock.ANY, writer) - @mock.patch('__builtin__.open') + @mock.patch('six.moves.builtins.open') @mock.patch('shutil.copyfileobj') def test_download_from_direct_file_non_file(self, mock_copyfileobj, mock_open): @@ -613,6 +608,7 @@ class TestGlanceImageService(test.TestCase): def test_extracting_v2_boot_properties(self, config): config.glance_api_version = 2 + config.glance_num_retries = 0 attributes = ['size', 'disk_format', 'owner', 'container_format', 'checksum', 'id', 'name', 'created_at', 'updated_at', diff --git a/tox.ini b/tox.ini index 1ff355b83..0c92b462e 100644 --- a/tox.ini +++ b/tox.ini @@ -29,6 +29,7 @@ downloadcache = ~/cache/pip [testenv:py34] commands = python -m testtools.run \ + cinder.tests.unit.image.test_glance \ cinder.tests.unit.targets.test_base_iscsi_driver \ cinder.tests.unit.targets.test_cxt_driver \ cinder.tests.unit.targets.test_iser_driver \ -- 2.45.2