qemu-iotests: Merge FilePaths and FilePath

FilePath creates now one temporary file:

    with FilePath("a") as a:

Or more:

    with FilePath("a", "b", "c") as (a, b, c):

This is also the behavior of the file_path() helper, used by some of the
tests. Now we have only 2 helpers for creating temporary files instead
of 3.

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20200828232152.205833-5-nsoffer@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Nir Soffer 2020-08-29 02:21:51 +03:00 committed by Max Reitz
parent a242b19e80
commit 3192fad7f7
5 changed files with 15 additions and 18 deletions

View File

@ -26,7 +26,7 @@ iotests.script_initialize(supported_fmts=['qcow2', 'qed', 'raw'],
with iotests.FilePath('source.img') as source_img_path, \ with iotests.FilePath('source.img') as source_img_path, \
iotests.FilePath('dest.img') as dest_img_path, \ iotests.FilePath('dest.img') as dest_img_path, \
iotests.FilePaths('migration.sock', 'nbd.sock', base_dir=iotests.sock_dir) \ iotests.FilePath('migration.sock', 'nbd.sock', base_dir=iotests.sock_dir) \
as (migration_sock_path, nbd_sock_path), \ as (migration_sock_path, nbd_sock_path), \
iotests.VM('source') as source_vm, \ iotests.VM('source') as source_vm, \
iotests.VM('dest') as dest_vm: iotests.VM('dest') as dest_vm:

View File

@ -26,7 +26,7 @@ iotests.script_initialize(supported_fmts=['generic'])
with iotests.FilePath('disk.img') as disk_img_path, \ with iotests.FilePath('disk.img') as disk_img_path, \
iotests.FilePath('disk-snapshot.img') as disk_snapshot_img_path, \ iotests.FilePath('disk-snapshot.img') as disk_snapshot_img_path, \
iotests.FilePath('nbd.sock', iotests.sock_dir) as nbd_sock_path, \ iotests.FilePath('nbd.sock', base_dir=iotests.sock_dir) as nbd_sock_path, \
iotests.VM() as vm: iotests.VM() as vm:
img_size = '10M' img_size = '10M'

View File

@ -49,7 +49,7 @@ remainder = [("0xd5", "0x108000", "32k"), # Right-end of partial-left [1]
with iotests.FilePath('base.img') as base_img_path, \ with iotests.FilePath('base.img') as base_img_path, \
iotests.FilePath('fleece.img') as fleece_img_path, \ iotests.FilePath('fleece.img') as fleece_img_path, \
iotests.FilePath('nbd.sock', iotests.sock_dir) as nbd_sock_path, \ iotests.FilePath('nbd.sock', base_dir=iotests.sock_dir) as nbd_sock_path, \
iotests.VM() as vm: iotests.VM() as vm:
log('--- Setting up images ---') log('--- Setting up images ---')

View File

@ -275,7 +275,7 @@ def test_bitmap_sync(bsync_mode, msync_mode='bitmap', failure=None):
an incomplete backup. Testing limitations prevent an incomplete backup. Testing limitations prevent
testing competing writes. testing competing writes.
""" """
with iotests.FilePaths( with iotests.FilePath(
'img', 'bsync1', 'bsync2', 'fbackup0', 'fbackup1', 'fbackup2') as \ 'img', 'bsync1', 'bsync2', 'fbackup0', 'fbackup1', 'fbackup2') as \
(img_path, bsync1, bsync2, fbackup0, fbackup1, fbackup2), \ (img_path, bsync1, bsync2, fbackup0, fbackup1, fbackup2), \
iotests.VM() as vm: iotests.VM() as vm:
@ -440,7 +440,7 @@ def test_backup_api():
""" """
Test malformed and prohibited invocations of the backup API. Test malformed and prohibited invocations of the backup API.
""" """
with iotests.FilePaths('img', 'bsync1') as (img_path, backup_path), \ with iotests.FilePath('img', 'bsync1') as (img_path, backup_path), \
iotests.VM() as vm: iotests.VM() as vm:
log("\n=== API failure tests ===\n") log("\n=== API failure tests ===\n")

View File

@ -448,14 +448,14 @@ class Timeout:
def file_pattern(name): def file_pattern(name):
return "{0}-{1}".format(os.getpid(), name) return "{0}-{1}".format(os.getpid(), name)
class FilePaths: class FilePath:
""" """
Context manager generating multiple file names. The generated files are Context manager generating multiple file names. The generated files are
removed when exiting the context. removed when exiting the context.
Example usage: Example usage:
with FilePaths('a.img', 'b.img') as (img_a, img_b): with FilePath('a.img', 'b.img') as (img_a, img_b):
# Use img_a and img_b here... # Use img_a and img_b here...
# a.img and b.img are automatically removed here. # a.img and b.img are automatically removed here.
@ -463,7 +463,10 @@ class FilePaths:
By default images are created in iotests.test_dir. To create sockets use By default images are created in iotests.test_dir. To create sockets use
iotests.sock_dir: iotests.sock_dir:
with FilePaths('a.sock', base_dir=iotests.sock_dir) as (sock,): with FilePath('a.sock', base_dir=iotests.sock_dir) as sock:
For convenience, calling with one argument yields a single file instead of
a tuple with one item.
""" """
def __init__(self, *names, base_dir=test_dir): def __init__(self, *names, base_dir=test_dir):
@ -472,7 +475,10 @@ class FilePaths:
self.paths.append(os.path.join(base_dir, file_pattern(name))) self.paths.append(os.path.join(base_dir, file_pattern(name)))
def __enter__(self): def __enter__(self):
return self.paths if len(self.paths) == 1:
return self.paths[0]
else:
return self.paths
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
for path in self.paths: for path in self.paths:
@ -482,15 +488,6 @@ class FilePaths:
pass pass
return False return False
class FilePath(FilePaths):
"""
FilePath is a specialization of FilePaths that takes a single filename.
"""
def __init__(self, name, base_dir=test_dir):
super(FilePath, self).__init__(name, base_dir=base_dir)
def __enter__(self):
return self.paths[0]
def file_path_remover(): def file_path_remover():
for path in reversed(file_path_remover.paths): for path in reversed(file_path_remover.paths):