From 05c2ba2ebc6041c2e3d3481c1898a90517d01e5b Mon Sep 17 00:00:00 2001 From: Vipin Balachandran Date: Fri, 4 Sep 2015 13:47:34 +0530 Subject: [PATCH] VMware: Remove global patching of open Commit 212aff327a36f925be3be69b9b54b946dbdd5c2a replaced 'cinder.openstack.common.fileutils.file_open' with 'open' from builtins. It also updated unit tests to patch 'six. moves.builtins.open' instead of 'cinder.openstack.common. fileutils.file_open'. This resulted in intermittent failure of test 'test_create_backing_from_stream_optimized_file' in VMwareVcVmdkDriverTestCase. The failure happens because the test patches 'open' from builtins and tests code which throws an exception in a 'with open(...)' block. This exception is handled in excutils.save_and_reraise_exception which uses 'with open(...)' and the exception handling fails due to the global patching of 'open'. This patch replaces the global patching of builtins 'open' with a patch decorator which binds 'open' to the namespace of the code under testing. Closes-bug: #1488690 Change-Id: Ib0f47b8fcbc54a9111c1bdff6d4b6fa7568518ca --- cinder/tests/unit/test_vmware_vmdk.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/cinder/tests/unit/test_vmware_vmdk.py b/cinder/tests/unit/test_vmware_vmdk.py index f27031fed..4babcbe50 100644 --- a/cinder/tests/unit/test_vmware_vmdk.py +++ b/cinder/tests/unit/test_vmware_vmdk.py @@ -1428,7 +1428,7 @@ class VMwareEsxVmdkDriverTestCase(test.TestCase): fake_name, fake_size) @mock.patch.object(image_transfer, 'copy_stream_optimized_disk') - @mock.patch('six.moves.builtins.open') + @mock.patch('cinder.volume.drivers.vmware.vmdk.open', create=True) @mock.patch.object(VMDK_DRIVER, '_temporary_file') @mock.patch('oslo_utils.uuidutils.generate_uuid') @mock.patch.object(VMDK_DRIVER, '_create_backing') @@ -1484,7 +1484,7 @@ class VMwareEsxVmdkDriverTestCase(test.TestCase): @mock.patch.object(VMDK_DRIVER, 'extend_volume') @mock.patch.object(VMDK_DRIVER, '_restore_backing') - @mock.patch('six.moves.builtins.open') + @mock.patch('cinder.volume.drivers.vmware.vmdk.open', create=True) @mock.patch.object(VMDK_DRIVER, '_temporary_file') @mock.patch('oslo_utils.uuidutils.generate_uuid') @mock.patch.object(VMDK_DRIVER, 'volumeops') @@ -1650,13 +1650,12 @@ class VMwareEsxVmdkDriverTestCase(test.TestCase): @mock.patch.object(VMDK_DRIVER, '_delete_temp_backing') @mock.patch.object(image_transfer, 'download_stream_optimized_data') - @mock.patch('cinder.volume.drivers.vmware.vmdk.open') + @mock.patch('cinder.volume.drivers.vmware.vmdk.open', create=True) @mock.patch.object(VMDK_DRIVER, 'volumeops') @mock.patch.object(VMDK_DRIVER, '_get_disk_type') @mock.patch.object(VMDK_DRIVER, '_get_storage_profile_id') @mock.patch.object(VMDK_DRIVER, 'session') @mock.patch.object(VMDK_DRIVER, '_select_ds_for_volume') - @test.testtools.skip("SKIP until this test is removed or fixed") def test_create_backing_from_stream_optimized_file( self, select_ds, session, get_storage_profile_id, get_disk_type, vops, file_open, download_data, delete_temp_backing): @@ -2515,7 +2514,7 @@ class VMwareVcVmdkDriverTestCase(VMwareEsxVmdkDriverTestCase): _select_ds_for_volume) @mock.patch.object(image_transfer, 'copy_stream_optimized_disk') - @mock.patch('six.moves.builtins.open') + @mock.patch('cinder.volume.drivers.vmware.vmdk.open', create=True) @mock.patch.object(VMDK_DRIVER, '_temporary_file') @mock.patch('oslo_utils.uuidutils.generate_uuid') @mock.patch.object(VMDK_DRIVER, '_create_backing') @@ -2528,7 +2527,7 @@ class VMwareVcVmdkDriverTestCase(VMwareEsxVmdkDriverTestCase): @mock.patch.object(VMDK_DRIVER, 'extend_volume') @mock.patch.object(VMDK_DRIVER, '_restore_backing') - @mock.patch('six.moves.builtins.open') + @mock.patch('cinder.volume.drivers.vmware.vmdk.open', create=True) @mock.patch.object(VMDK_DRIVER, '_temporary_file') @mock.patch('oslo_utils.uuidutils.generate_uuid') @mock.patch.object(VMDK_DRIVER, 'volumeops') @@ -2554,14 +2553,13 @@ class VMwareVcVmdkDriverTestCase(VMwareEsxVmdkDriverTestCase): @mock.patch.object(VMDK_DRIVER, '_delete_temp_backing') @mock.patch.object(image_transfer, 'download_stream_optimized_data') - @mock.patch('six.moves.builtins.open') + @mock.patch('cinder.volume.drivers.vmware.vmdk.open', create=True) @mock.patch.object(VMDK_DRIVER, 'volumeops') @mock.patch( 'cinder.volume.drivers.vmware.vmdk.VMwareEsxVmdkDriver._get_disk_type') @mock.patch.object(VMDK_DRIVER, '_get_storage_profile_id') @mock.patch.object(VMDK_DRIVER, 'session') @mock.patch.object(VMDK_DRIVER, '_select_ds_for_volume') - @test.testtools.skip("SKIP until this test is removed or fixed") def test_create_backing_from_stream_optimized_file( self, select_ds, session, get_storage_profile_id, get_disk_type, vops, file_open, download_data, delete_temp_backing): -- 2.45.2