raise Error("mismatch for hole %d-%d, it is %d-%d in file2" \
% (range1[0], range1[1], range2[0], range2[1]))
-class TestCreateCopy(unittest.TestCase):
+def _do_test(f_image):
"""" A basic test for the bmap creation and copying functionality. It first
- generates a bmap for a sparse file, and then copies the sparse file to a
- different file, and then checks that all the blocks were copied. The
- original sparse file is generated randomly. The test entry point is the
- 'test()' method. """
+ 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. """
+
+ # Create and open a temporary file for a copy of the copy
+ f_copy = tempfile.NamedTemporaryFile("wb+")
+
+ # Create and open 2 temporary files for the bmap
+ f_bmap1 = tempfile.NamedTemporaryFile("w+")
+ f_bmap2 = tempfile.NamedTemporaryFile("w+")
+
+ #
+ # Pass 1: generate the bmap, copy and compare
+ #
+
+ # Create bmap for the random sparse file
+ creator = BmapCreate.BmapCreate(f_image.name, f_bmap1.name)
+ creator.generate()
+
+ # Copy the random sparse file to a different file using bmap
+ writer = BmapCopy.BmapCopy(f_image.name, f_copy.name, f_bmap1.name)
+ writer.copy(False, True)
+
+ # Compare the original file and the copy are identical
+ assert filecmp.cmp(f_image.name, f_copy.name, False)
+ # Make sure that holes in the copy are identical to holes in the random
+ # sparse file.
+ compare_holes(f_image.name, f_copy.name)
+
+ #
+ # Pass 2: same as pass 1, but use file objects instead of paths
+ #
+
+ creator = BmapCreate.BmapCreate(f_image, f_bmap2)
+ creator.generate()
+
+ writer = BmapCopy.BmapCopy(f_image, f_copy, f_bmap2)
+ writer.copy(False, True)
+
+ assert filecmp.cmp(f_image.name, f_copy.name, False)
+ compare_holes(f_image, f_copy)
+
+ # Make sure the bmap files generated at pass 1 and pass 2 are identical
+ assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False)
+
+ #
+ # Pass 3: repeat pass 2 to make sure the same 'BmapCreate' and
+ # 'BmapCopy' objects can be used more than once.
+ #
+
+ f_bmap2.seek(0)
+ creator.generate()
+ f_bmap2.seek(0)
+ creator.generate()
+ writer.copy(True, False)
+ writer.copy(False, True)
+ writer.sync()
+ assert filecmp.cmp(f_image.name, f_copy.name, False)
+ compare_holes(f_image, f_copy)
+ assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False)
+
+ #
+ # Pass 4: copy the sparse file without bmap and make sure it is
+ # identical to the original file
+ #
+
+ writer = BmapCopy.BmapCopy(f_image, f_copy.name)
+ writer.copy(True, True)
+ assert filecmp.cmp(f_image.name, f_copy.name, False)
+
+ writer = BmapCopy.BmapCopy(f_image, f_copy)
+ writer.copy(False, True)
+ assert filecmp.cmp(f_image.name, f_copy.name, False)
+
+ # Close temporary files, which will also remove them
+ f_copy.close()
+ f_bmap1.close()
+ f_bmap2.close()
+
+class TestCreateCopy(unittest.TestCase):
+ """ The test class for this unit tests. Basically executes the '_do_test()'
+ function for different sparse files. """
@staticmethod
- def _do_test(f_image):
- """ Run the test for the 'f_image' file object. """
-
- # Create and open a temporary file for a copy of the copy
- f_copy = tempfile.NamedTemporaryFile("wb+")
-
- # Create and open 2 temporary files for the bmap
- f_bmap1 = tempfile.NamedTemporaryFile("w+")
- f_bmap2 = tempfile.NamedTemporaryFile("w+")
-
- #
- # Pass 1: generate the bmap, copy and compare
- #
-
- # Create bmap for the random sparse file
- creator = BmapCreate.BmapCreate(f_image.name, f_bmap1.name)
- creator.generate()
-
- # Copy the random sparse file to a different file using bmap
- writer = BmapCopy.BmapCopy(f_image.name, f_copy.name, f_bmap1.name)
- writer.copy(False, True)
-
- # Compare the original file and the copy are identical
- assert filecmp.cmp(f_image.name, f_copy.name, False)
- # Make sure that holes in the copy are identical to holes in the random
- # sparse file.
- compare_holes(f_image.name, f_copy.name)
-
- #
- # Pass 2: same as pass 1, but use file objects instead of paths
- #
-
- creator = BmapCreate.BmapCreate(f_image, f_bmap2)
- creator.generate()
-
- writer = BmapCopy.BmapCopy(f_image, f_copy, f_bmap2)
- writer.copy(False, True)
-
- assert filecmp.cmp(f_image.name, f_copy.name, False)
- compare_holes(f_image, f_copy)
-
- # Make sure the bmap files generated at pass 1 and pass 2 are identical
- assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False)
-
- #
- # Pass 3: repeat pass 2 to make sure the same 'BmapCreate' and
- # 'BmapCopy' objects can be used more than once.
- #
- f_bmap2.seek(0)
- creator.generate()
- f_bmap2.seek(0)
- creator.generate()
- writer.copy(True, False)
- writer.copy(False, True)
- writer.sync()
- assert filecmp.cmp(f_image.name, f_copy.name, False)
- compare_holes(f_image, f_copy)
- assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False)
-
- #
- # Pass 4: copy the sparse file without bmap and make sure it is
- # identical to the original file
- #
- writer = BmapCopy.BmapCopy(f_image, f_copy.name)
- writer.copy(True, True)
- assert filecmp.cmp(f_image.name, f_copy.name, False)
-
- writer = BmapCopy.BmapCopy(f_image, f_copy)
- writer.copy(False, True)
- assert filecmp.cmp(f_image.name, f_copy.name, False)
-
- # Close temporary files, which will also remove them
- f_copy.close()
- f_bmap1.close()
- f_bmap2.close()
-
- def test(self):
- """ The test entry point. Executes the create-copy-verify test for
- files of different sizes, holes distribution and format. """
+ def test():
+ """ 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():
- self._do_test(f_image)
+ _do_test(f_image)