tests: add a possibility to avoid tmp files deletion
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 27 Nov 2012 10:06:29 +0000 (12:06 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 27 Nov 2012 11:17:41 +0000 (13:17 +0200)
This is needed for debugging purposes. When there are issues, it is very handy
to be able to leave the temporary files and then investigate them. Do lets add
a simple way to do this. In the future, this may become a test parameter.

Change-Id: I067fc9d5a522057290c5c9ee190cf15ee7fa19f1
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
tests/helpers.py
tests/test_api_base.py

index f2d33d3..bd582c6 100644 (file)
@@ -49,14 +49,16 @@ def create_random_sparse_file(file_obj, size):
 
     return holes
 
-def generate_test_files(max_size = 4 * 1024 * 1024, directory = None):
+def generate_test_files(max_size = 4 * 1024 * 1024, directory = None,
+                        delete = True):
     """ This is an iterator which generates files which other tests use as the
     input for the testing. The iterator tries to generate "interesting" files
     which cover various corner-cases. For example, a large hole file, a file
     with no holes, files of unaligned length, etc.
 
     The 'directory' argument specifies the directory path where the generated
-    test files should be created.
+    test files should be created. The 'delete' argument specifies whether the
+    generated test files have to be automatically deleted.
 
     Returns a tuple consisting of the open file object and a list of unmapped
     block ranges (holes) in the file. """
@@ -67,7 +69,8 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None):
 
     # A block-sized hole
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "4Khole_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     block_size = BmapHelpers.get_block_size(file_obj)
     file_obj.truncate(block_size)
     yield (file_obj, [(0, 0)])
@@ -75,21 +78,24 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None):
 
     # A block size + 1 byte hole
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "4Khole_plus_1_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     file_obj.truncate(block_size + 1)
     yield (file_obj, [(0, 0)])
     file_obj.close()
 
     # A block size - 1 byte hole
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "4Khole_minus_1_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     file_obj.truncate(block_size - 1)
     yield (file_obj, [(0, 0)])
     file_obj.close()
 
     # A 1-byte hole
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "1byte_hole_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     file_obj.truncate(1)
     yield (file_obj, [(0, 0)])
     file_obj.close()
@@ -98,7 +104,8 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None):
     for i in xrange(10):
         size = random.randint(1, max_size)
         file_obj = tempfile.NamedTemporaryFile("wb+", suffix = ".img",
-                                dir = directory, prefix = "rand_hole_%d_" % i)
+                                               delete = delete, dir = directory,
+                                               prefix = "rand_hole_%d_" % i)
         file_obj.truncate(size)
         blocks_cnt = (size + block_size - 1) / block_size
         yield (file_obj, [(0, blocks_cnt - 1)])
@@ -110,21 +117,24 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None):
 
     # The maximum size
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     holes = create_random_sparse_file(file_obj, max_size)
     yield (file_obj, holes)
     file_obj.close()
 
     # The maximum size + 1 byte
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_plus_1_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     holes = create_random_sparse_file(file_obj, max_size + 1)
     yield (file_obj, holes)
     file_obj.close()
 
     # The maximum size - 1 byte
     file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_minus_1_",
-                                           dir = directory, suffix = ".img")
+                                           delete = delete, dir = directory,
+                                           suffix = ".img")
     holes = create_random_sparse_file(file_obj, max_size - 1)
     yield (file_obj, holes)
     file_obj.close()
@@ -133,7 +143,8 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None):
     for i in xrange(10):
         size = random.randint(1, max_size)
         file_obj = tempfile.NamedTemporaryFile("wb+", suffix = ".img",
-                                   dir = directory, prefix = "sparse_%d_" % i)
+                                               delete = delete, dir = directory,
+                                               prefix = "sparse_%d_" % i)
         holes = create_random_sparse_file(file_obj, size)
         yield (file_obj, holes)
         file_obj.close()
index 017c971..5f4b73e 100644 (file)
@@ -39,9 +39,12 @@ def _compare_holes(file1, file2):
             raise Error("mismatch for hole %d-%d, it is %d-%d in file2" \
                         % (range1[0], range1[1], range2[0], range2[1]))
 
-def _generate_compressed_files(file_obj):
+def _generate_compressed_files(file_obj, delete = True):
     """ This is an iterator which generates compressed versions of a file
-    represented by a file object 'file_obj'. """
+    represented by a file object 'file_obj'.
+
+    The 'delete' argument specifies whether the compressed files that this
+    iterator generates have to be automatically deleted. """
 
     import bz2
     import gzip
@@ -55,7 +58,8 @@ def _generate_compressed_files(file_obj):
 
     # Generate a .bz2 version of the file
     tmp_file_obj = tempfile.NamedTemporaryFile('wb+', prefix = prefix,
-                                               dir = directory, suffix = '.bz2')
+                                               delete = delete, dir = directory,
+                                               suffix = '.bz2')
     bz2_file_obj = bz2.BZ2File(tmp_file_obj.name, 'wb')
     file_obj.seek(0)
     shutil.copyfileobj(file_obj, bz2_file_obj)
@@ -65,7 +69,8 @@ def _generate_compressed_files(file_obj):
 
     # Generate a .gz version of the file
     tmp_file_obj = tempfile.NamedTemporaryFile('wb+', prefix = prefix,
-                                               dir = directory, suffix = '.gz')
+                                               delete = delete, dir = directory,
+                                               suffix = '.gz')
     gzip_file_obj = gzip.GzipFile(tmp_file_obj.name, 'wb')
     file_obj.seek(0)
     shutil.copyfileobj(file_obj, gzip_file_obj)
@@ -90,11 +95,14 @@ def _calculate_sha1(file_obj):
 
     return hash_obj.hexdigest()
 
-def _do_test(f_image):
+def _do_test(f_image, delete = True):
     """" A basic test for the bmap creation and copying functionality. It first
     generates a bmap for file object 'f_image', and then copies the sparse file
     to a different file, and then checks that the original file and the copy
-    are identical. """
+    are identical.
+
+    The 'delete' argument specifies whether the temporary files that this
+    function creates have to be automatically deleted. """
 
     # Make sure the temporary files start with the same name as 'f_image' in
     # order to simplify debugging.
@@ -104,13 +112,16 @@ def _do_test(f_image):
 
     # Create and open a temporary file for a copy of the copy
     f_copy = tempfile.NamedTemporaryFile("wb+", prefix = prefix,
-                                         dir = directory, suffix = ".copy")
+                                        delete = delete, dir = directory,
+                                        suffix = ".copy")
 
     # Create and open 2 temporary files for the bmap
     f_bmap1 = tempfile.NamedTemporaryFile("w+", prefix = prefix,
-                                          dir = directory, suffix = ".bmap1")
+                                          delete = delete, dir = directory,
+                                          suffix = ".bmap1")
     f_bmap2 = tempfile.NamedTemporaryFile("w+", prefix = prefix,
-                                          dir = directory, suffix = ".bmap2")
+                                          delete = delete, dir = directory,
+                                          suffix = ".bmap2")
 
     image_sha1 = _calculate_sha1(f_image)
 
@@ -169,7 +180,7 @@ def _do_test(f_image):
     # Pass 4: test compressed files copying with bmap
     #
 
-    for compressed in _generate_compressed_files(f_image):
+    for compressed in _generate_compressed_files(f_image, delete = delete):
         writer = BmapCopy.BmapCopy(compressed, f_copy, f_bmap1)
         writer.copy()
 
@@ -192,7 +203,7 @@ def _do_test(f_image):
     # Pass 6: test compressed files copying without bmap
     #
 
-    for compressed in _generate_compressed_files(f_image):
+    for compressed in _generate_compressed_files(f_image, delete = delete):
         writer = BmapCopy.BmapCopy(compressed, f_copy)
         writer.copy()
 
@@ -212,5 +223,13 @@ class TestCreateCopy(unittest.TestCase):
         """ The test entry point. Executes the '_do_test()' function for files
         of different sizes, holes distribution and format. """
 
-        for f_image, _ in tests.helpers.generate_test_files():
-            _do_test(f_image)
+        # Delete all the test-related temporary files automatically
+        delete = True
+        # Create all the test-related temporary files in the default directory
+        # (usually /tmp).
+        directory = None
+
+        iterator = tests.helpers.generate_test_files(delete = delete,
+                                                     directory = directory)
+        for f_image, _ in iterator:
+            _do_test(f_image, delete = delete)