return map_the_block
mapped = []
- holes = []
+ unmapped = []
iterator = xrange(0, blocks_cnt)
for was_mapped, group in itertools.groupby(iterator, process_block):
# Start of a mapped region or a hole. Find the last element in the
if was_mapped:
mapped.append((first, last))
else:
- holes.append((first, last))
+ unmapped.append((first, last))
file_obj.truncate(size)
file_obj.flush()
- return (mapped, holes)
+ return (mapped, unmapped)
def _create_random_file(file_obj, size):
""" Fill the 'file_obj' file object with semi-random data up to the size
file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_",
delete = delete, dir = directory,
suffix = ".img")
- mapped, holes = _create_random_sparse_file(file_obj, max_size)
- yield (file_obj, mapped, holes)
+ mapped, unmapped = _create_random_sparse_file(file_obj, max_size)
+ yield (file_obj, mapped, unmapped)
file_obj.close()
# The maximum size + 1 byte
file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_plus_1_",
delete = delete, dir = directory,
suffix = ".img")
- mapped, holes = _create_random_sparse_file(file_obj, max_size + 1)
- yield (file_obj, mapped, holes)
+ mapped, unmapped = _create_random_sparse_file(file_obj, max_size + 1)
+ yield (file_obj, mapped, unmapped)
file_obj.close()
# The maximum size - 1 byte
file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_minus_1_",
delete = delete, dir = directory,
suffix = ".img")
- mapped, holes = _create_random_sparse_file(file_obj, max_size - 1)
- yield (file_obj, mapped, holes)
+ mapped, unmapped = _create_random_sparse_file(file_obj, max_size - 1)
+ yield (file_obj, mapped, unmapped)
file_obj.close()
# And 10 files of random size
file_obj = tempfile.NamedTemporaryFile("wb+", suffix = ".img",
delete = delete, dir = directory,
prefix = "sparse_%d_" % i)
- mapped, holes = _create_random_sparse_file(file_obj, size)
- yield (file_obj, mapped, holes)
+ mapped, unmapped = _create_random_sparse_file(file_obj, size)
+ yield (file_obj, mapped, unmapped)
file_obj.close()
#
""" A class for exceptions generated by this test. """
pass
-def _do_test(f_image, mapped, holes, buf_size = Fiemap.DEFAULT_BUFFER_SIZE):
- """ 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. The 'buf_size' argument specifies the Fiemap class internal
- buffer size. """
+def _check_ranges(f_image, fiemap, first_block, blocks_cnt,
+ ranges, ranges_type):
+ """ This is a helper function for '_do_test()' which compares the correct
+ 'ranges' list of mapped or unmapped blocks ranges for file object 'f_image'
+ with what the Fiemap module reports. The 'ranges_type' argument defines
+ whether the 'ranges' list is a list of mapped or unmapped blocks. The
+ 'first_block' and 'blocks_cnt' define the subset of blocks in 'f_image'
+ that should be verified by this function. """
- # 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)
- fiemap_iterator = fiemap.get_mapped_ranges(0, fiemap.blocks_cnt)
- iterator = itertools.izip_longest(fiemap_iterator, mapped)
+ if ranges_type is "mapped":
+ fiemap_iterator = fiemap.get_mapped_ranges(first_block, blocks_cnt)
+ elif ranges_type is "unmapped":
+ fiemap_iterator = fiemap.get_unmapped_ranges(first_block, blocks_cnt)
+ else:
+ raise Error("incorrect list type")
- for range1, range2 in iterator:
- if range1[0] > range1[1]:
- raise Error("bad mapped area range %d-%d" % (range1[0], range1[1]))
+ iterator = itertools.izip_longest(ranges, fiemap_iterator)
- 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))
+ for correct, check in iterator:
+ 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" \
+ % (ranges_type, f_image.name, correct[0], correct[1],
+ ranges_type, first_block, blocks_cnt,
+ check[0], check[1]))
- # 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)
+def _do_test(f_image, mapped, unmapped, buf_size = Fiemap.DEFAULT_BUFFER_SIZE):
+ """ Verifiy that Fiemap reports the correct mapped and unmapped areas for
+ the 'f_image' file object. The 'mapped' and 'unmapped' lists contain the
+ correct ranges. The 'buf_size' argument specifies the internal buffer size
+ of the 'Fiemap' class. """
- for range1, range2 in iterator:
- if range1[0] > range1[1]:
- raise Error("bad hole range %d-%d" % (range1[0], range1[1]))
+ # 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)
+ first_block = 0
+ blocks_cnt = fiemap.blocks_cnt
- 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))
+ _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()'
iterator = tests.helpers.generate_test_files(max_size, directory,
delete)
- for f_image, mapped, holes in iterator:
- _do_test(f_image, mapped, holes)
- _do_test(f_image, mapped, holes, Fiemap.MIN_BUFFER_SIZE)
- _do_test(f_image, mapped, holes, Fiemap.MIN_BUFFER_SIZE * 2)
- _do_test(f_image, mapped, holes, Fiemap.DEFAULT_BUFFER_SIZE / 2)
- _do_test(f_image, mapped, holes, Fiemap.DEFAULT_BUFFER_SIZE * 2)
+ for f_image, mapped, unmapped in iterator:
+ _do_test(f_image, mapped, unmapped)
+ _do_test(f_image, mapped, unmapped, Fiemap.MIN_BUFFER_SIZE)
+ _do_test(f_image, mapped, unmapped, Fiemap.MIN_BUFFER_SIZE * 2)
+ _do_test(f_image, mapped, unmapped, Fiemap.DEFAULT_BUFFER_SIZE / 2)
+ _do_test(f_image, mapped, unmapped, Fiemap.DEFAULT_BUFFER_SIZE * 2)