test_fiemap: add Fiemap module unit test
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 27 Nov 2012 13:11:21 +0000 (15:11 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 27 Nov 2012 14:13:21 +0000 (16:13 +0200)
Change-Id: I0da8bb80f8b15fd27695381324bb213726cfeca2
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
tests/test_fiemap.py [new file with mode: 0644]

diff --git a/tests/test_fiemap.py b/tests/test_fiemap.py
new file mode 100644 (file)
index 0000000..a6dff22
--- /dev/null
@@ -0,0 +1,68 @@
+""" This test verifies Fiemap module functionality. It generates random sparse
+files and make sure FIEMAP returns correct information about the holes. """
+
+import unittest
+import itertools
+
+import tests.helpers
+from bmaptools import Fiemap
+
+class Error(Exception):
+    """ A class for exceptions generated by this test. """
+    pass
+
+def _do_test(f_image, mapped, holes):
+    """ Verifiy that Fiemap reports the correct mapped areas and holes ranges
+    fro the 'f_image' file object. The 'mapped' and 'holes' lists contains the
+    correct ranges. """
+
+    # Make sure that Fiemap's get_mapped_ranges() returns the same ranges as
+    # we have in the 'mapped' list.
+    fiemap = Fiemap.Fiemap(f_image)
+    fiemap_iterator = fiemap.get_mapped_ranges(0, fiemap.blocks_cnt)
+    iterator = itertools.izip_longest(fiemap_iterator, mapped)
+
+    for range1, range2 in iterator:
+        if range1[0] > range1[1]:
+            raise Error("bad mapped area range %d-%d" % (range1[0], range1[1]))
+
+        if range1 != range2:
+            raise Error("mapped areas mismatch: %d-%d as per Fiemap module, " \
+                        "%d-%d in the image file '%s'" \
+                        % (range1[0], range1[1], range2[0], range2[1],
+                           f_image.name))
+
+    # Make sure that Fiemap's get_unmapped_ranges() returns the same ranges as
+    # we have in the 'holes' list.
+    fiemap_iterator = fiemap.get_unmapped_ranges(0, fiemap.blocks_cnt)
+    iterator = itertools.izip_longest(fiemap_iterator, holes)
+
+    for range1, range2 in iterator:
+        if range1[0] > range1[1]:
+            raise Error("bad hole range %d-%d" % (range1[0], range1[1]))
+
+        if range1 != range2:
+            raise Error("holes mismatch: %d-%d as per Fiemap module, %d-%d " \
+                        "in the image file '%s'" \
+                        % (range1[0], range1[1], range2[0], range2[1],
+                           f_image.name))
+
+class TestCreateCopy(unittest.TestCase):
+    """ The test class for this unit tests. Basically executes the '_do_test()'
+    function for different sparse files. """
+
+    @staticmethod
+    def test():
+        """ The test entry point. Executes the '_do_test()' function for files
+        of different sizes, holes distribution and format. """
+
+        # 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, mapped, holes in iterator:
+            _do_test(f_image, mapped, holes)