qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 04/67] iotests.py: create_test_image, remove_test_image


From: Max Reitz
Subject: [PATCH 04/67] iotests.py: create_test_image, remove_test_image
Date: Tue, 1 Oct 2019 21:46:12 +0200

Python tests should use these two new functions instead of
qemu_img('create', ...) + os.remove(), so that user-supplied image
options are interpreted and handled correctly.

Signed-off-by: Max Reitz <address@hidden>
---
 tests/qemu-iotests/iotests.py | 56 +++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index b5ea424de4..fce1ab04c9 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -122,6 +122,62 @@ def qemu_img_create(*args):
 
     return qemu_img(*args)
 
+def create_test_image(filename, size=None, fmt=imgfmt, opts=[],
+                      backing_file=None, backing_fmt=None,
+                      objects=[], unsafe=False):
+    if fmt == imgfmt:
+        # Only use imgopts for the default format
+        opts = imgopts + opts
+
+    for i, opt in enumerate(opts):
+        if '$TEST_IMG' in opt:
+            opts[i] = opt.replace('$TEST_IMG', filename)
+
+    # default luks support
+    if fmt == 'luks':
+        if not any('key-secret' in opt for opt in opts):
+            opts.append(luks_default_key_secret_opt)
+        objects.append(luks_default_secret_object)
+
+    args = ['create', '-f', fmt]
+
+    if len(opts) > 0:
+        args += ['-o', ','.join(opts)]
+
+    if backing_file is not None:
+        args += ['-b', backing_file]
+
+    if backing_fmt is not None:
+        args += ['-F', backing_fmt]
+
+    if len(objects) > 0:
+        # Generate a [['--object', $obj], [...], ...] list and flatten it
+        args += [arg for objarg in (['--object', obj] for obj in objects) \
+                     for arg in objarg]
+
+    if unsafe:
+        args.append('-u')
+
+    args.append(filename)
+    if size is not None:
+        args.append(str(size))
+
+    return qemu_img(*args)
+
+# Use this to remove images create with create_test_image in the
+# default image format (iotests.imgfmt)
+def remove_test_image(filename):
+    try:
+        os.remove(filename)
+
+        data_file = next(opt.replace('data_file=', '') \
+                            .replace('$TEST_IMG', filename) \
+                         for opt in imgopts if opt.startswith('data_file='))
+
+        os.remove(data_file)
+    except:
+        pass
+
 def qemu_img_verbose(*args):
     '''Run qemu-img without suppressing its output and return the exit code'''
     exitcode = subprocess.call(qemu_img_args + list(args))
-- 
2.21.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]