@@ -14363,19 +14363,21 @@ def test_pre_live_migration_recreate_disk_info(self):
14363
14363
disk_info_path = os.path.join(instance_path, 'disk.info')
14364
14364
14365
14365
with test.nested(
14366
+ mock.patch('builtins.open', new_callable=mock.mock_open),
14366
14367
mock.patch.object(os, 'mkdir'),
14367
- mock.patch('nova.virt.libvirt.utils.write_to_file'),
14368
14368
mock.patch.object(drvr, '_create_images_and_backing')
14369
14369
) as (
14370
- mkdir, write_to_file , create_images_and_backing
14370
+ mock_open, mkdir , create_images_and_backing
14371
14371
):
14372
14372
drvr.pre_live_migration(self.context, instance,
14373
14373
block_device_info=None,
14374
14374
network_info=[],
14375
14375
disk_info=jsonutils.dumps(disk_info),
14376
14376
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))
14379
14381
14380
14382
@mock.patch('nova.virt.libvirt.utils.file_open',
14381
14383
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,
16292
16294
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_images_and_backing')
16293
16295
@mock.patch('nova.virt.libvirt.LibvirtDriver.'
16294
16296
'_get_instance_disk_info_from_config')
16295
- @mock.patch('nova.virt.libvirt.utils.write_to_file')
16296
16297
@mock.patch('nova.virt.libvirt.utils.get_instance_path')
16297
16298
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_guest_config')
16298
16299
@mock.patch('nova.virt.libvirt.blockinfo.get_disk_info')
16299
16300
@mock.patch('nova.virt.libvirt.LibvirtDriver._destroy')
16300
16301
@mock.patch('nova.virt.libvirt.LibvirtDriver.'
16301
16302
'_get_all_assigned_mediated_devices')
16303
+ @mock.patch('builtins.open', new=mock.mock_open())
16302
16304
def test_hard_reboot_does_not_call_glance_show(self,
16303
16305
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,
16305
16307
mock_get_instance_disk_info, mock_create_images_and_backing,
16306
16308
mock_create_domand_and_network, mock_prepare_pci_devices_for_use,
16307
16309
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,
22373
22375
@mock.patch.object(libvirt_driver.LibvirtDriver,
22374
22376
'_create_guest_with_network')
22375
22377
@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')
22378
22378
# NOTE(mdbooth): The following 4 mocks are required to execute
22379
22379
# get_guest_xml().
22380
22380
@mock.patch.object(libvirt_driver.LibvirtDriver, '_set_host_enabled')
22381
22381
@mock.patch.object(libvirt_driver.LibvirtDriver, '_build_device_metadata')
22382
22382
@mock.patch('nova.privsep.utils.supports_direct_io')
22383
22383
@mock.patch('nova.api.metadata.base.InstanceMetadata')
22384
+ @mock.patch('builtins.open', new=mock.mock_open())
22384
22385
def _test_finish_migration(self, mock_instance_metadata,
22385
22386
mock_supports_direct_io,
22386
22387
mock_build_device_metadata,
22387
- mock_set_host_enabled, mock_write_to_file,
22388
+ mock_set_host_enabled,
22388
22389
mock_raw_to_qcow2,
22389
22390
mock_create_guest_with_network,
22390
22391
mock_get_info, mock_inject_data,
@@ -23759,18 +23760,19 @@ def test_detach_interface_device_with_same_mac_address(
23759
23760
@mock.patch('nova.objects.image_meta.ImageMeta.from_image_ref')
23760
23761
@mock.patch('nova.virt.libvirt.LibvirtDriver.'
23761
23762
'_get_all_assigned_mediated_devices')
23762
- @mock.patch('nova.virt.libvirt.utils.write_to_file')
23763
23763
# NOTE(mdbooth): The following 4 mocks are required to execute
23764
23764
# get_guest_xml().
23765
23765
@mock.patch.object(libvirt_driver.LibvirtDriver, '_set_host_enabled')
23766
23766
@mock.patch.object(libvirt_driver.LibvirtDriver, '_build_device_metadata')
23767
23767
@mock.patch('nova.privsep.utils.supports_direct_io')
23768
23768
@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
+ ):
23774
23776
23775
23777
self.flags(instances_path=self.useFixture(fixtures.TempDir()).path)
23776
23778
mock_build_device_metadata.return_value = None
@@ -23927,9 +23929,8 @@ def test_rescue_config_drive(self, mock_mkisofs):
23927
23929
self.assertEqual(expected_kernel_ramdisk_paths,
23928
23930
kernel_ramdisk_paths)
23929
23931
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):
23933
23934
network_info = _fake_network_info(self, 1)
23934
23935
instance = self._create_instance({'config_drive': str(True)})
23935
23936
rescue_image_meta_dict = {'id': uuids.rescue_image_id,
@@ -24085,15 +24086,16 @@ def test_rescue_stable_device_bfv_without_instance_image_ref(self):
24085
24086
mock.patch.object(drvr, '_get_guest_xml'),
24086
24087
mock.patch.object(drvr, '_create_image'),
24087
24088
mock.patch.object(drvr, '_get_existing_domain_xml'),
24088
- mock.patch.object(libvirt_utils, 'write_to_file'),
24089
24089
mock.patch.object(libvirt_utils, 'get_instance_path'),
24090
24090
mock.patch('nova.virt.libvirt.blockinfo.get_disk_info'),
24091
24091
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
+ ):
24097
24099
self.flags(virt_type='kvm', group='libvirt')
24098
24100
mock_image_get.return_value = mock.sentinel.bdm_image_meta_dict
24099
24101
mock_from_dict.return_value = mock.sentinel.bdm_image_meta
@@ -24183,8 +24185,11 @@ def test_rescue_stable_device_bfv(self):
24183
24185
@mock.patch.object(libvirt_utils, 'get_instance_path')
24184
24186
@mock.patch.object(libvirt_utils, 'load_file')
24185
24187
@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
+ ):
24188
24193
dummyxml = ("<domain type='kvm'><name>instance-0000000a</name>"
24189
24194
"<devices>"
24190
24195
"<disk type='block' device='disk'>"
@@ -24208,7 +24213,6 @@ def isdir_sideeffect(*args, **kwargs):
24208
24213
24209
24214
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
24210
24215
with test.nested(
24211
- mock.patch.object(libvirt_utils, 'write_to_file'),
24212
24216
mock.patch.object(drvr, '_destroy'),
24213
24217
mock.patch.object(drvr, '_create_guest'),
24214
24218
mock.patch.object(os, 'unlink'),
@@ -24220,7 +24224,7 @@ def isdir_sideeffect(*args, **kwargs):
24220
24224
mock.patch.object(lvm, 'remove_volumes'),
24221
24225
mock.patch.object(glob, 'iglob',
24222
24226
return_value=[rescue_file, rescue_dir])
24223
- ) as (mock_write, mock_destroy, mock_create, mock_del,
24227
+ ) as (mock_destroy, mock_create, mock_del,
24224
24228
mock_rmtree, mock_isdir, mock_lvm_disks,
24225
24229
mock_remove_volumes, mock_glob):
24226
24230
drvr.unrescue(self.context, instance)
0 commit comments