diff mbox series

[05/67] iotests.py: Add ImagePaths

Message ID 20191001194715.2796-6-mreitz@redhat.com
State New
Headers show
Series iotests: Honor $IMGOPTS in Python tests | expand

Commit Message

Max Reitz Oct. 1, 2019, 7:46 p.m. UTC
create_test_image() must be paired with an ImagePath so that the image
is properly cleaned up with remove_test_image() instead of just
os.remove().

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 tests/qemu-iotests/iotests.py | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index fce1ab04c9..5be6ca674c 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -440,8 +440,8 @@  class FilePaths(object):
     Use this context manager to generate filenames and ensure that the file
     gets deleted::
 
-        with FilePaths(['test.img']) as img_path:
-            qemu_img('create', img_path, '1G')
+        with FilePaths(['migration.sock']) as migration_sock_path:
+            # Set up and use UNIX socket on migration.sock
         # migration_sock_path is automatically deleted
     """
     def __init__(self, names):
@@ -460,6 +460,23 @@  class FilePaths(object):
             pass
         return False
 
+class ImagePaths(FilePaths):
+    """
+    Same as FilePaths, except it calls remove_test_image() to clean up
+    (which ensures that external data files are cleaned up, too).
+
+    Use this class for test images in the default format
+    (iotests.imgfmt):
+
+    with ImagePaths(['test.img']) as img_path:
+        create_test_image(img_path, '1G')
+    # The test image is automatically cleaned up
+    """
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        for path in self.paths:
+            remove_test_image(path)
+        return False
+
 class FilePath(FilePaths):
     """
     FilePath is a specialization of FilePaths that takes a single filename.
@@ -470,6 +487,17 @@  class FilePath(FilePaths):
     def __enter__(self):
         return self.paths[0]
 
+class ImagePath(ImagePaths):
+    """
+    ImagePath is a specialization of ImagePaths that takes a single
+    filename.
+    """
+    def __init__(self, name):
+        super(ImagePath, self).__init__([name])
+
+    def __enter__(self):
+        return self.paths[0]
+
 def file_path_remover():
     for path in reversed(file_path_remover.paths):
         try: