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):
"""
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()
# pylint: disable=R0904
import os
-import sys
import tempfile
import filecmp
-import hashlib
import itertools
import random
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."""
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
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
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.
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
#
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()
# 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)