From: Artem Bityutskiy Date: Mon, 17 Dec 2012 12:30:04 +0000 (+0200) Subject: test_api_base: rework the test to match the changed API X-Git-Tag: v2.0~46 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ba4cb4d42f22921e95ac0b273940b8c2f9ccc011;p=tools%2Fbmap-tools.git test_api_base: rework the test to match the changed API BmapCopy does not accept paths anymore - amend the tests. Change-Id: Ib78dcceb959bce673c08bfb1dca0aa86b985c7d4 Signed-off-by: Artem Bityutskiy --- diff --git a/tests/test_api_base.py b/tests/test_api_base.py index 80648a6..22ff908 100644 --- a/tests/test_api_base.py +++ b/tests/test_api_base.py @@ -4,9 +4,8 @@ to a different file using the bmap. Then it compares the original random sparse file and the copy and verifies that they are identical. """ # Disable the following pylint recommendations: -# * Too many instance attributes - R0902 # * Too many public methods - R0904 -# pylint: disable=R0902,R0904 +# pylint: disable=R0904 import os import sys @@ -15,9 +14,10 @@ import filecmp import hashlib import unittest import itertools +import random import tests.helpers -from bmaptools import BmapCreate, BmapCopy, Fiemap +from bmaptools import BmapCreate, BmapCopy, Fiemap, TransRead class Error(Exception): """ A class for exceptions generated by this test. """ @@ -117,6 +117,33 @@ def _calculate_sha1(file_obj): return hash_obj.hexdigest() +def _copy_image(image, f_dest, f_bmap, image_sha1, image_size): + """ Copy image 'image' using bmap 'f_bmap' to the destination file + 'f_dest'. """ + + if hasattr(image, "read"): + f_image = image + image.seek(0) + else: + f_image = TransRead.TransRead(image) + + f_dest.seek(0) + if f_bmap: + f_bmap.seek(0) + + writer = BmapCopy.BmapCopy(f_image, f_dest, f_bmap, image_size) + # Randomly decide whether we want the progress bar or not + if bool(random.getrandbits(1)): + writer.set_progress_indicator(sys.stderr, None) + writer.copy(bool(random.getrandbits(1)), bool(random.getrandbits(1))) + + # Compare the original file and the copy are identical + f_dest.seek(0) + assert _calculate_sha1(f_dest) == image_sha1 + + if not hasattr(image, "read"): + f_image.close() + def _do_test(f_image, image_size, delete = True): """" A basic test for the bmap creation and copying functionality. It first generates a bmap for file object 'f_image', and then copies the sparse file @@ -156,93 +183,50 @@ def _do_test(f_image, image_size, delete = True): 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 _calculate_sha1(f_copy) == image_sha1 + _copy_image(f_image, f_copy, f_bmap1, image_sha1, image_size) # Make sure that holes in the copy are identical to holes in the random # sparse file. _compare_holes(f_image.name, f_copy.name) - # And do another copy, but this time use 'set_image_size()' - writer = BmapCopy.BmapCopy(f_image.name, f_copy.name, f_bmap1.name) - writer.set_image_size(image_size) - writer.copy(True, False) - assert _calculate_sha1(f_copy) == image_sha1 - # # Pass 2: same as pass 1, but use file objects instead of paths # creator = BmapCreate.BmapCreate(f_image, f_bmap2) creator.generate() - - f_bmap2.seek(0) - writer = BmapCopy.BmapCopy(f_image, f_copy, f_bmap2) - writer.copy(False, True) - - assert _calculate_sha1(f_copy) == image_sha1 + _copy_image(f_image, f_copy, f_bmap2, image_sha1, image_size) _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) - # And do another time, but involve 'set_image_size()' - f_bmap2.seek(0) - writer = BmapCopy.BmapCopy(f_image, f_copy, f_bmap2) - writer.set_image_size(image_size) - writer.copy(False, False) - assert _calculate_sha1(f_copy) == image_sha1 - # # Pass 3: test compressed files copying with bmap # for compressed in _generate_compressed_files(f_image, delete = delete): - # Test without setting the size - f_bmap1.seek(0) - writer = BmapCopy.BmapCopy(compressed, f_copy, f_bmap1) - writer.copy() - assert _calculate_sha1(f_copy) == image_sha1 + _copy_image(compressed, f_copy, f_bmap1, image_sha1, image_size) - # Test with setting the size and the progress bar - writer = BmapCopy.BmapCopy(compressed, f_copy.name, f_bmap1.name) - writer.set_progress_indicator(sys.stderr, None) - writer.set_image_size(image_size) - writer.copy() - assert _calculate_sha1(f_copy) == image_sha1 + # Test without setting the size + _copy_image(compressed, f_copy, f_bmap1, image_sha1, None) # # Pass 5: copy 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 _calculate_sha1(f_copy) == image_sha1 - - # Another time with setting image size - writer = BmapCopy.BmapCopy(f_image, f_copy) - writer.set_image_size(image_size) - writer.copy(False, True) - assert _calculate_sha1(f_copy) == image_sha1 + _copy_image(f_image, f_copy, None, image_sha1, image_size) + _copy_image(f_image, f_copy, None, image_sha1, None) # # Pass 6: test compressed files copying without bmap # for compressed in _generate_compressed_files(f_image, delete = delete): - writer = BmapCopy.BmapCopy(compressed, f_copy) - writer.copy() - assert _calculate_sha1(f_copy) == image_sha1 + _copy_image(compressed, f_copy, f_bmap1, image_sha1, image_size) - # Another time with setting image size - writer = BmapCopy.BmapCopy(compressed, f_copy) - writer.set_image_size(image_size) - writer.copy() - assert _calculate_sha1(f_copy) == image_sha1 + # Test without setting the size + _copy_image(compressed, f_copy, f_bmap1, image_sha1, None) # Close temporary files, which will also remove them f_copy.close() @@ -258,10 +242,6 @@ class TestCreateCopy(unittest.TestCase): """ The test entry point. Executes the '_do_test()' function for files of different sizes, holes distribution and format. """ - # Temoprary disable the test - it fails and will be fixed in the - # next commit - return - # Delete all the test-related temporary files automatically delete = True # Create all the test-related temporary files in the default directory @@ -270,5 +250,5 @@ class TestCreateCopy(unittest.TestCase): iterator = tests.helpers.generate_test_files(delete = delete, directory = directory) - for f_image, size, _, _ in iterator: - _do_test(f_image, size, delete = delete) + for f_image, image_size, _, _ in iterator: + _do_test(f_image, image_size, delete = delete)