From: Artem Bityutskiy Date: Wed, 28 Nov 2012 12:28:57 +0000 (+0200) Subject: test_fiemap: code re-structuring X-Git-Tag: v1.0~17 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e7c7e5fcceb273cde92e403f24b7e4c46ea48ecb;p=tools%2Fbmap-tools.git test_fiemap: code re-structuring This patch re-structures the _do_test() function of the 'test_fiemap' unit test. The re-structuring is needed because I am going to extend the test and verify the 'get_(un)mapped_ranges()' finctions fro parts of the file, not only for the entire file. Without the re-structuring there will be a lot of code duplication. So, this patch introduces a '_check_ranges()' helper function which verifies the 'get_(un)mapped_ranges()' function for a given range of blocks. Additinally, to make the code look uniform, this patch renames all the 'holes' list variables into 'unmapped', to match the existing 'mapped' list. Change-Id: Ie1a30941eea9004e742621e87bf325a7f464ab27 Signed-off-by: Artem Bityutskiy --- diff --git a/tests/helpers.py b/tests/helpers.py index 247ab53..2b2f4f0 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -42,7 +42,7 @@ def _create_random_sparse_file(file_obj, size): 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 @@ -55,12 +55,12 @@ def _create_random_sparse_file(file_obj, size): 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 @@ -151,24 +151,24 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None, 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 @@ -177,8 +177,8 @@ def generate_test_files(max_size = 4 * 1024 * 1024, directory = None, 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() # diff --git a/tests/test_fiemap.py b/tests/test_fiemap.py index f183d37..201143c 100644 --- a/tests/test_fiemap.py +++ b/tests/test_fiemap.py @@ -15,42 +15,47 @@ class Error(Exception): """ 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()' @@ -71,9 +76,9 @@ class TestCreateCopy(unittest.TestCase): 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)