Skip to content

Commit 30530f5

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Remove write_to_file."
2 parents c710a3f + f8919c9 commit 30530f5

File tree

4 files changed

+40
-57
lines changed

4 files changed

+40
-57
lines changed

nova/tests/unit/virt/libvirt/test_driver.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

nova/tests/unit/virt/libvirt/test_utils.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,6 @@ def test_copy_image(self):
248248
finally:
249249
os.unlink(dst_path)
250250

251-
def test_write_to_file(self):
252-
dst_fd, dst_path = tempfile.mkstemp()
253-
try:
254-
os.close(dst_fd)
255-
256-
libvirt_utils.write_to_file(dst_path, 'hello')
257-
with open(dst_path, 'r') as fp:
258-
self.assertEqual(fp.read(), 'hello')
259-
finally:
260-
os.unlink(dst_path)
261-
262251
@mock.patch.object(compute_utils, 'disk_ops_semaphore')
263252
@mock.patch('nova.privsep.utils.supports_direct_io', return_value=False)
264253
@mock.patch('oslo_concurrency.processutils.execute')
@@ -330,8 +319,8 @@ def test_load_file(self):
330319
try:
331320
os.close(dst_fd)
332321

333-
# We have a test for write_to_file. If that is sound, this suffices
334-
libvirt_utils.write_to_file(dst_path, 'hello')
322+
with open(dst_path, 'w') as f:
323+
f.write('hello')
335324
self.assertEqual(libvirt_utils.load_file(dst_path), 'hello')
336325
finally:
337326
os.unlink(dst_path)
@@ -341,8 +330,8 @@ def test_file_open(self):
341330
try:
342331
os.close(dst_fd)
343332

344-
# We have a test for write_to_file. If that is sound, this suffices
345-
libvirt_utils.write_to_file(dst_path, 'hello')
333+
with open(dst_path, 'w') as f:
334+
f.write('hello')
346335
with libvirt_utils.file_open(dst_path, 'r') as fp:
347336
self.assertEqual(fp.read(), 'hello')
348337
finally:

nova/virt/libvirt/driver.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3589,7 +3589,8 @@ def rescue(self, context, instance, network_info, image_meta,
35893589
instance_dir = libvirt_utils.get_instance_path(instance)
35903590
unrescue_xml = self._get_existing_domain_xml(instance, network_info)
35913591
unrescue_xml_path = os.path.join(instance_dir, 'unrescue.xml')
3592-
libvirt_utils.write_to_file(unrescue_xml_path, unrescue_xml)
3592+
with open(unrescue_xml_path, 'w') as f:
3593+
f.write(unrescue_xml)
35933594

35943595
rescue_image_id = None
35953596
rescue_image_meta = None
@@ -9706,8 +9707,8 @@ def pre_live_migration(self, context, instance, block_device_info,
97069707

97079708
image_disk_info_path = os.path.join(instance_dir,
97089709
'disk.info')
9709-
libvirt_utils.write_to_file(image_disk_info_path,
9710-
jsonutils.dumps(image_disk_info))
9710+
with open(image_disk_info_path, 'w') as f:
9711+
f.write(jsonutils.dumps(image_disk_info))
97119712

97129713
if not is_shared_block_storage:
97139714
# Ensure images and backing files are present.

nova/virt/libvirt/utils.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,6 @@ def copy_image(
310310
compression=compression)
311311

312312

313-
# TODO(stephenfin): This is dumb; remove it.
314-
def write_to_file(path: str, contents: str) -> None:
315-
"""Write the given contents to a file
316-
317-
:param path: Destination file
318-
:param contents: Desired contents of the file
319-
"""
320-
with open(path, 'w') as f:
321-
f.write(contents)
322-
323-
324313
def chown_for_id_maps(
325314
path: str, id_maps: ty.List[vconfig.LibvirtConfigGuestIDMap],
326315
) -> None:

0 commit comments

Comments
 (0)