test_fiemap: implement a test for part of the file
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 28 Nov 2012 13:01:43 +0000 (15:01 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 28 Nov 2012 15:15:20 +0000 (17:15 +0200)
Test no only the entire file, but also parts of the file.

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

index 201143c..493b441 100644 (file)
@@ -5,6 +5,7 @@ files and make sure FIEMAP returns correct information about the holes. """
 #   *  Too many public methods - R0904
 # pylint: disable=R0904
 
+import random
 import unittest
 import itertools
 
@@ -31,9 +32,30 @@ def _check_ranges(f_image, fiemap, first_block, blocks_cnt,
     else:
         raise Error("incorrect list type")
 
-    iterator = itertools.izip_longest(ranges, fiemap_iterator)
+    last_block = first_block + blocks_cnt - 1
 
+    # The 'ranges' list contains all ranges, from block zero to the last
+    # block. However, we are conducting a test for 'blocks_cnt' of blocks
+    # starting from block 'first_block'. Create an iterator which filters
+    # those block ranges from the 'ranges' list, that are out of the
+    # 'first_block'/'blocks_cnt' file region.
+    filter_func = lambda x: x[1] >= first_block and x[0] <= last_block
+    ranges_iterator = filter(filter_func, ranges)
+
+    iterator = itertools.izip_longest(ranges_iterator, fiemap_iterator)
+
+    # Iterate over both - the (filtered) 'ranges' list which contains correct
+    # ranges and the Fiemap generator, and verify the mapped/unmapped ranges
+    # returned by the Fiemap module.
     for correct, check in iterator:
+
+        # The first and the last range of the filtered 'ranges' list may still
+        # be out of the limit - correct them in this case
+        if correct[0] < first_block:
+            correct = (first_block, correct[1])
+        if correct[1] > last_block:
+            correct = (correct[0], last_block)
+
         if check[0] > check[1] or check != correct:
             raise Error("bad or unmatching %s range for file '%s': correct " \
                         "is %d-%d, get_%s_ranges(%d, %d) returned %d-%d" \
@@ -50,13 +72,24 @@ def _do_test(f_image, mapped, unmapped, buf_size = Fiemap.DEFAULT_BUFFER_SIZE):
     # Make sure that Fiemap's get_mapped_ranges() returns the same ranges as
     # we have in the 'mapped' list.
     fiemap = Fiemap.Fiemap(f_image, buf_size)
+
+    # Check both 'get_mapped_ranges()' and 'get_unmapped_ranges()' for the
+    # entire file.
     first_block = 0
     blocks_cnt = fiemap.blocks_cnt
-
     _check_ranges(f_image, fiemap, first_block, blocks_cnt, mapped, "mapped")
     _check_ranges(f_image, fiemap, first_block, blocks_cnt, unmapped,
                   "unmapped")
 
+    # Select a random area in the file and repeat the test few times
+    for _ in xrange(0, 10):
+        first_block = random.randint(0, fiemap.blocks_cnt - 1)
+        blocks_cnt = random.randint(1, fiemap.blocks_cnt - first_block)
+        _check_ranges(f_image, fiemap, first_block, blocks_cnt, mapped,
+                      "mapped")
+        _check_ranges(f_image, fiemap, first_block, blocks_cnt, unmapped,
+                      "unmapped")
+
 class TestCreateCopy(unittest.TestCase):
     """ The test class for this unit tests. Basically executes the '_do_test()'
     function for different sparse files. """