def test(self): """The test entry point.""" test_data_dir = os.path.join(os.path.dirname(__file__), _TEST_DATA_SUBDIR) image_path = os.path.join(test_data_dir, _IMAGE_NAME) # Construct the list of bmap files to test self._bmap_paths = [] for dentry in os.listdir(test_data_dir): dentry_path = os.path.join(test_data_dir, dentry) if os.path.isfile(dentry_path) and dentry.startswith(_BMAP_TEMPL): self._bmap_paths.append(dentry_path) # Create and open a temporary file for uncompressed image and its copy self._f_image = tempfile.NamedTemporaryFile("wb+", prefix=_IMAGE_NAME, suffix=".image") self._f_copy = tempfile.NamedTemporaryFile("wb+", prefix=_IMAGE_NAME, suffix=".copy") # Uncompress the test image into 'self._f_image' f_tmp_img = TransRead.TransRead(image_path) shutil.copyfileobj(f_tmp_img, self._f_image) f_tmp_img.close() self._f_image.flush() image_chksum = helpers.calculate_chksum(self._f_image.name) image_size = os.path.getsize(self._f_image.name) # Test the current version of BmapCopy for bmap_path in self._bmap_paths: helpers.copy_and_verify_image(image_path, self._f_copy.name, bmap_path, image_chksum, image_size) # Test the older versions of BmapCopy self._test_older_bmapcopy() self._f_copy.close() self._f_image.close()
def _do_test(image, image_size, delete=True): """ A basic test for the bmap creation and copying functionality. It first generates a bmap for file 'image', and then copies the sparse file to a different file, and then checks that the original file and the copy are identical. The 'image_size' argument is size of the image in bytes. The 'delete' argument specifies whether the temporary files that this function creates have to be automatically deleted. """ try: Filemap.filemap(image) except Filemap.ErrorNotSupp as e: sys.stderr.write('%s\n' % e) return # Make sure the temporary files start with the same name as 'image' in # order to simplify debugging. prefix = os.path.splitext(os.path.basename(image))[0] + '.' # Put the temporary files in the directory with the image directory = os.path.dirname(image) # Create and open a temporary file for a copy of the image f_copy = tempfile.NamedTemporaryFile("wb+", prefix=prefix, delete=delete, dir=directory, suffix=".copy") # Create and open 2 temporary files for the bmap f_bmap1 = tempfile.NamedTemporaryFile("w+", prefix=prefix, delete=delete, dir=directory, suffix=".bmap1") f_bmap2 = tempfile.NamedTemporaryFile("w+", prefix=prefix, delete=delete, dir=directory, suffix=".bmap2") image_chksum = helpers.calculate_chksum(image) # # Pass 1: generate the bmap, copy and compare # # Create bmap for the random sparse file creator = BmapCreate.BmapCreate(image, f_bmap1.name) creator.generate() 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. _compare_holes(image, f_copy.name) # # Pass 2: same as pass 1, but use file objects instead of paths # creator = BmapCreate.BmapCreate(image, f_bmap2) creator.generate() 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 assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False) # # Pass 3: test compressed files copying with bmap # for compressed in _generate_compressed_files(image, delete=delete): helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, image_chksum, image_size) # Test without setting the size 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 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. 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): helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, image_chksum, image_size) # Test without setting the size helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, image_chksum, None) # Append a "file:" prefix to make BmapCopy use urllib 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() f_bmap1.close() f_bmap2.close()
def _do_test(image, image_size, delete=True): """ A basic test for the bmap creation and copying functionality. It first generates a bmap for file 'image', and then copies the sparse file to a different file, and then checks that the original file and the copy are identical. The 'image_size' argument is size of the image in bytes. The 'delete' argument specifies whether the temporary files that this function creates have to be automatically deleted. """ # Make sure the temporary files start with the same name as 'image' in # order to simplify debugging. prefix = os.path.splitext(os.path.basename(image))[0] + '.' # Put the temporary files in the directory with the image directory = os.path.dirname(image) # Create and open a temporary file for a copy of the image f_copy = tempfile.NamedTemporaryFile("wb+", prefix=prefix, delete=delete, dir=directory, suffix=".copy") # Create and open 2 temporary files for the bmap f_bmap1 = tempfile.NamedTemporaryFile("w+", prefix=prefix, delete=delete, dir=directory, suffix=".bmap1") f_bmap2 = tempfile.NamedTemporaryFile("w+", prefix=prefix, delete=delete, dir=directory, suffix=".bmap2") image_chksum = helpers.calculate_chksum(image) # # Pass 1: generate the bmap, copy and compare # # Create bmap for the random sparse file creator = BmapCreate.BmapCreate(image, f_bmap1.name) creator.generate() 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. _compare_holes(image, f_copy.name) # # Pass 2: same as pass 1, but use file objects instead of paths # creator = BmapCreate.BmapCreate(image, f_bmap2) creator.generate() 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 assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False) # # Pass 3: test compressed files copying with bmap # for compressed in _generate_compressed_files(image, delete=delete): helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, image_chksum, image_size) # Test without setting the size 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 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. 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): helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, image_chksum, image_size) # Test without setting the size helpers.copy_and_verify_image(compressed, f_copy.name, f_bmap1.name, image_chksum, None) # Append a "file:" prefix to make BmapCopy use urllib 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() f_bmap1.close() f_bmap2.close()