From 8480362c931eda863aa48c3f61ff3f130cd06e7a Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Fri, 1 Nov 2013 17:35:04 +0200 Subject: [PATCH] test_api_base: move a couple of functions to helpers.py Move '_copy_and_verify_image()' and '_calculate_chksum()' to the helpers.py file since I am going to use them in the new unit test which I am about to add. Remove the leading underscore since these functions become usable from outside. Move some necessary module imports to helpers.py too. Change-Id: Ifca399497f2d7b7bf046f916c1e744a4422cc7cc Signed-off-by: Artem Bityutskiy --- tests/helpers.py | 48 ++++++++++++++++++++++- tests/test_api_base.py | 101 ++++++++++++++----------------------------------- 2 files changed, 75 insertions(+), 74 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index 81c24e3..c0d1607 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -23,7 +23,9 @@ tests. import tempfile import random import itertools -from bmaptools import BmapHelpers +import hashlib +import sys +from bmaptools import BmapHelpers, BmapCopy, TransRead def _create_random_sparse_file(file_obj, size): """ @@ -248,3 +250,47 @@ def generate_test_files(max_size=4*1024*1024, directory=None, delete=True): blocks_cnt = (size + block_size - 1) / block_size yield (file_obj, size, [(0, blocks_cnt - 1)], []) file_obj.close() + +def calculate_chksum(file_path): + """Calculates checksum for the contents of file 'file_path'.""" + + file_obj = TransRead.TransRead(file_path) + hash_obj = hashlib.new("sha256") + + chunk_size = 1024*1024 + + while True: + chunk = file_obj.read(chunk_size) + if not chunk: + break + hash_obj.update(chunk) + + file_obj.close() + return hash_obj.hexdigest() + +def copy_and_verify_image(image, dest, bmap, image_chksum, image_size): + """ + Copy image 'image' using bmap file 'bmap' to the destination file 'dest' + and verify the resulting image checksum. + """ + + f_image = TransRead.TransRead(image) + f_dest = open(dest, "w+") + if (bmap): + f_bmap = open(bmap, "r") + else: + f_bmap = None + + 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.stdout, None) + writer.copy(bool(random.getrandbits(1)), bool(random.getrandbits(1))) + + # Compare the original file and the copy are identical + assert calculate_chksum(dest) == image_chksum + + if f_bmap: + f_bmap.close() + f_dest.close() + f_image.close() diff --git a/tests/test_api_base.py b/tests/test_api_base.py index 2a9ce10..1379e12 100644 --- a/tests/test_api_base.py +++ b/tests/test_api_base.py @@ -23,10 +23,8 @@ file and the copy and verifies that they are identical. # pylint: disable=R0904 import os -import sys import tempfile import filecmp -import hashlib import itertools import random @@ -36,8 +34,8 @@ try: except ImportError: import unittest -import tests.helpers -from bmaptools import BmapCreate, BmapCopy, Fiemap, TransRead +from tests import helpers +from bmaptools import BmapCreate, Fiemap, TransRead class Error(Exception): """A class for exceptions generated by this test.""" @@ -157,50 +155,6 @@ def _generate_compressed_files(file_path, delete=True): file_obj.close() -def _calculate_chksum(file_path): - """Calculates checksum for the contents of file 'file_path'.""" - - file_obj = TransRead.TransRead(file_path) - hash_obj = hashlib.new("sha256") - - chunk_size = 1024*1024 - - while True: - chunk = file_obj.read(chunk_size) - if not chunk: - break - hash_obj.update(chunk) - - file_obj.close() - return hash_obj.hexdigest() - -def _copy_and_verify_image(image, dest, bmap, image_chksum, image_size): - """ - Copy image 'image' using bmap file 'bmap' to the destination file 'dest' - and verify the resulting image checksum. - """ - - f_image = TransRead.TransRead(image) - f_dest = open(dest, "w+") - if (bmap): - f_bmap = open(bmap, "r") - else: - f_bmap = None - - 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.stdout, None) - writer.copy(bool(random.getrandbits(1)), bool(random.getrandbits(1))) - - # Compare the original file and the copy are identical - assert _calculate_chksum(dest) == image_chksum - - if f_bmap: - f_bmap.close() - f_dest.close() - f_image.close() - def _do_test(image, image_size, delete=True): """ A basic test for the bmap creation and copying functionality. It first @@ -232,7 +186,7 @@ def _do_test(image, image_size, delete=True): delete=delete, dir=directory, suffix=".bmap2") - image_chksum = _calculate_chksum(image) + image_chksum = helpers.calculate_chksum(image) # # Pass 1: generate the bmap, copy and compare @@ -242,8 +196,8 @@ def _do_test(image, image_size, delete=True): creator = BmapCreate.BmapCreate(image, f_bmap1.name) creator.generate() - _copy_and_verify_image(image, f_copy.name, f_bmap1.name, image_chksum, - image_size) + helpers.copy_and_verify_image(image, f_copy.name, f_bmap1.name, + image_chksum, image_size) # Make sure that holes in the copy are identical to holes in the random # sparse file. @@ -255,8 +209,8 @@ def _do_test(image, image_size, delete=True): creator = BmapCreate.BmapCreate(image, f_bmap2) creator.generate() - _copy_and_verify_image(image, f_copy.name, f_bmap2.name, image_chksum, - image_size) + helpers.copy_and_verify_image(image, f_copy.name, f_bmap2.name, + image_chksum, image_size) _compare_holes(image, f_copy.name) # Make sure the bmap files generated at pass 1 and pass 2 are identical @@ -267,44 +221,45 @@ def _do_test(image, image_size, delete=True): # for compressed in _generate_compressed_files(image, delete=delete): - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, image_size) + helpers.copy_and_verify_image(compressed, f_copy.name, + f_bmap1.name, image_chksum, image_size) # Test without setting the size - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, None) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, None) # Append a "file:" prefixe to make BmapCopy use urllib compressed = "file:" + compressed - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, image_size) - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, None) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, image_size) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, None) # # Pass 5: copy without bmap and make sure it is identical to the original # file. - _copy_and_verify_image(image, f_copy.name, None, image_chksum, image_size) - _copy_and_verify_image(image, f_copy.name, None, image_chksum, None) + helpers.copy_and_verify_image(image, f_copy.name, None, image_chksum, + image_size) + helpers.copy_and_verify_image(image, f_copy.name, None, image_chksum, None) # # Pass 6: test compressed files copying without bmap # for compressed in _generate_compressed_files(image, delete=delete): - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, image_size) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, image_size) # Test without setting the size - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, None) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, None) # Append a "file:" prefix to make BmapCopy use urllib - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, image_size) - _copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, - image_chksum, None) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, image_size) + helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, + image_chksum, None) # Close temporary files, which will also remove them f_copy.close() @@ -330,8 +285,8 @@ class TestCreateCopy(unittest.TestCase): # FIEMAP). directory = '.' - iterator = tests.helpers.generate_test_files(delete=delete, - directory=directory) + iterator = helpers.generate_test_files(delete=delete, + directory=directory) for f_image, image_size, _, _ in iterator: assert image_size == os.path.getsize(f_image.name) _do_test(f_image.name, image_size, delete=delete) -- 2.7.4