@@ -14363,19 +14363,21 @@ def test_pre_live_migration_recreate_disk_info(self):
1436314363 disk_info_path = os.path.join(instance_path, 'disk.info')
1436414364
1436514365 with test.nested(
14366+ mock.patch('builtins.open', new_callable=mock.mock_open),
1436614367 mock.patch.object(os, 'mkdir'),
14367- mock.patch('nova.virt.libvirt.utils.write_to_file'),
1436814368 mock.patch.object(drvr, '_create_images_and_backing')
1436914369 ) as (
14370- mkdir, write_to_file , create_images_and_backing
14370+ mock_open, mkdir , create_images_and_backing
1437114371 ):
1437214372 drvr.pre_live_migration(self.context, instance,
1437314373 block_device_info=None,
1437414374 network_info=[],
1437514375 disk_info=jsonutils.dumps(disk_info),
1437614376 migrate_data=migrate_data)
14377- write_to_file.assert_called_with(disk_info_path,
14378- jsonutils.dumps(image_disk_info))
14377+
14378+ self.assertIn(mock.call(disk_info_path, 'w'), mock_open.mock_calls)
14379+ mock_open.return_value.write.assert_called_with(
14380+ jsonutils.dumps(image_disk_info))
1437914381
1438014382 @mock.patch('nova.virt.libvirt.utils.file_open',
1438114383 side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
@@ -16292,16 +16294,16 @@ def test_hard_reboot(self, mock_get_mdev, mock_destroy, mock_get_disk_info,
1629216294 @mock.patch('nova.virt.libvirt.LibvirtDriver._create_images_and_backing')
1629316295 @mock.patch('nova.virt.libvirt.LibvirtDriver.'
1629416296 '_get_instance_disk_info_from_config')
16295- @mock.patch('nova.virt.libvirt.utils.write_to_file')
1629616297 @mock.patch('nova.virt.libvirt.utils.get_instance_path')
1629716298 @mock.patch('nova.virt.libvirt.LibvirtDriver._get_guest_config')
1629816299 @mock.patch('nova.virt.libvirt.blockinfo.get_disk_info')
1629916300 @mock.patch('nova.virt.libvirt.LibvirtDriver._destroy')
1630016301 @mock.patch('nova.virt.libvirt.LibvirtDriver.'
1630116302 '_get_all_assigned_mediated_devices')
16303+ @mock.patch('builtins.open', new=mock.mock_open())
1630216304 def test_hard_reboot_does_not_call_glance_show(self,
1630316305 mock_get_mdev, mock_destroy, mock_get_disk_info,
16304- mock_get_guest_config, mock_get_instance_path, mock_write_to_file,
16306+ mock_get_guest_config, mock_get_instance_path,
1630516307 mock_get_instance_disk_info, mock_create_images_and_backing,
1630616308 mock_create_domand_and_network, mock_prepare_pci_devices_for_use,
1630716309 mock_get_instance_pci_devs, mock_looping_call, mock_ensure_tree):
@@ -22373,18 +22375,17 @@ def test_disk_raw_to_qcow2_no_directio(self, mock_rename, mock_execute,
2237322375 @mock.patch.object(libvirt_driver.LibvirtDriver,
2237422376 '_create_guest_with_network')
2237522377 @mock.patch.object(libvirt_driver.LibvirtDriver, '_disk_raw_to_qcow2')
22376- # Don't write libvirt xml to disk
22377- @mock.patch.object(libvirt_utils, 'write_to_file')
2237822378 # NOTE(mdbooth): The following 4 mocks are required to execute
2237922379 # get_guest_xml().
2238022380 @mock.patch.object(libvirt_driver.LibvirtDriver, '_set_host_enabled')
2238122381 @mock.patch.object(libvirt_driver.LibvirtDriver, '_build_device_metadata')
2238222382 @mock.patch('nova.privsep.utils.supports_direct_io')
2238322383 @mock.patch('nova.api.metadata.base.InstanceMetadata')
22384+ @mock.patch('builtins.open', new=mock.mock_open())
2238422385 def _test_finish_migration(self, mock_instance_metadata,
2238522386 mock_supports_direct_io,
2238622387 mock_build_device_metadata,
22387- mock_set_host_enabled, mock_write_to_file,
22388+ mock_set_host_enabled,
2238822389 mock_raw_to_qcow2,
2238922390 mock_create_guest_with_network,
2239022391 mock_get_info, mock_inject_data,
@@ -23759,18 +23760,19 @@ def test_detach_interface_device_with_same_mac_address(
2375923760 @mock.patch('nova.objects.image_meta.ImageMeta.from_image_ref')
2376023761 @mock.patch('nova.virt.libvirt.LibvirtDriver.'
2376123762 '_get_all_assigned_mediated_devices')
23762- @mock.patch('nova.virt.libvirt.utils.write_to_file')
2376323763 # NOTE(mdbooth): The following 4 mocks are required to execute
2376423764 # get_guest_xml().
2376523765 @mock.patch.object(libvirt_driver.LibvirtDriver, '_set_host_enabled')
2376623766 @mock.patch.object(libvirt_driver.LibvirtDriver, '_build_device_metadata')
2376723767 @mock.patch('nova.privsep.utils.supports_direct_io')
2376823768 @mock.patch('nova.api.metadata.base.InstanceMetadata')
23769- def _test_rescue(self, instance, mock_instance_metadata,
23770- mock_supports_direct_io, mock_build_device_metadata,
23771- mock_set_host_enabled, mock_write_to_file, mock_get_mdev,
23772- mock_get_image_meta_by_ref, image_meta_dict=None, exists=None,
23773- instance_image_meta_dict=None, block_device_info=None):
23769+ @mock.patch('builtins.open', new=mock.mock_open())
23770+ def _test_rescue(
23771+ self, instance, mock_instance_metadata, mock_supports_direct_io,
23772+ mock_build_device_metadata, mock_set_host_enabled, mock_get_mdev,
23773+ mock_get_image_meta_by_ref, image_meta_dict=None, exists=None,
23774+ instance_image_meta_dict=None, block_device_info=None,
23775+ ):
2377423776
2377523777 self.flags(instances_path=self.useFixture(fixtures.TempDir()).path)
2377623778 mock_build_device_metadata.return_value = None
@@ -23927,9 +23929,8 @@ def test_rescue_config_drive(self, mock_mkisofs):
2392723929 self.assertEqual(expected_kernel_ramdisk_paths,
2392823930 kernel_ramdisk_paths)
2392923931
23930- @mock.patch('nova.virt.libvirt.utils.write_to_file')
23931- def test_rescue_stable_device_unsupported_virt_types(self,
23932- mock_libvirt_write_to_file):
23932+ @mock.patch('builtins.open', new=mock.mock_open())
23933+ def test_rescue_stable_device_unsupported_virt_types(self):
2393323934 network_info = _fake_network_info(self, 1)
2393423935 instance = self._create_instance({'config_drive': str(True)})
2393523936 rescue_image_meta_dict = {'id': uuids.rescue_image_id,
@@ -24085,15 +24086,16 @@ def test_rescue_stable_device_bfv_without_instance_image_ref(self):
2408524086 mock.patch.object(drvr, '_get_guest_xml'),
2408624087 mock.patch.object(drvr, '_create_image'),
2408724088 mock.patch.object(drvr, '_get_existing_domain_xml'),
24088- mock.patch.object(libvirt_utils, 'write_to_file'),
2408924089 mock.patch.object(libvirt_utils, 'get_instance_path'),
2409024090 mock.patch('nova.virt.libvirt.blockinfo.get_disk_info'),
2409124091 mock.patch('nova.image.glance.API.get'),
24092- mock.patch('nova.objects.image_meta.ImageMeta.from_dict')
24093- ) as (mock_create, mock_destroy, mock_get_guest_xml, mock_create_image,
24094- mock_get_existing_xml, mock_write, mock_inst_path,
24095- mock_get_disk_info, mock_image_get, mock_from_dict):
24096-
24092+ mock.patch('nova.objects.image_meta.ImageMeta.from_dict'),
24093+ mock.patch('builtins.open', new_callable=mock.mock_open),
24094+ ) as (
24095+ mock_create, mock_destroy, mock_get_guest_xml, mock_create_image,
24096+ mock_get_existing_xml, mock_inst_path, mock_get_disk_info,
24097+ mock_image_get, mock_from_dict, mock_open,
24098+ ):
2409724099 self.flags(virt_type='kvm', group='libvirt')
2409824100 mock_image_get.return_value = mock.sentinel.bdm_image_meta_dict
2409924101 mock_from_dict.return_value = mock.sentinel.bdm_image_meta
@@ -24183,8 +24185,11 @@ def test_rescue_stable_device_bfv(self):
2418324185 @mock.patch.object(libvirt_utils, 'get_instance_path')
2418424186 @mock.patch.object(libvirt_utils, 'load_file')
2418524187 @mock.patch.object(host.Host, '_get_domain')
24186- def _test_unrescue(self, instance, mock_get_domain, mock_load_file,
24187- mock_get_instance_path):
24188+ @mock.patch('builtins.open', new=mock.mock_open())
24189+ def _test_unrescue(
24190+ self, instance, mock_get_domain, mock_load_file,
24191+ mock_get_instance_path,
24192+ ):
2418824193 dummyxml = ("<domain type='kvm'><name>instance-0000000a</name>"
2418924194 "<devices>"
2419024195 "<disk type='block' device='disk'>"
@@ -24208,7 +24213,6 @@ def isdir_sideeffect(*args, **kwargs):
2420824213
2420924214 drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
2421024215 with test.nested(
24211- mock.patch.object(libvirt_utils, 'write_to_file'),
2421224216 mock.patch.object(drvr, '_destroy'),
2421324217 mock.patch.object(drvr, '_create_guest'),
2421424218 mock.patch.object(os, 'unlink'),
@@ -24220,7 +24224,7 @@ def isdir_sideeffect(*args, **kwargs):
2422024224 mock.patch.object(lvm, 'remove_volumes'),
2422124225 mock.patch.object(glob, 'iglob',
2422224226 return_value=[rescue_file, rescue_dir])
24223- ) as (mock_write, mock_destroy, mock_create, mock_del,
24227+ ) as (mock_destroy, mock_create, mock_del,
2422424228 mock_rmtree, mock_isdir, mock_lvm_disks,
2422524229 mock_remove_volumes, mock_glob):
2422624230 drvr.unrescue(self.context, instance)
0 commit comments