import os
import tempfile
-import random
import filecmp
import unittest
import itertools
-from bmaptools import BmapCreate, BmapCopy, BmapHelpers, Fiemap
+
+from tests import test_helpers
+from bmaptools import BmapCreate, BmapCopy, Fiemap
# Size of the image to generate and test
IMAGE_SIZE = 64 * 1024 * 1024
""" A class for exceptions generated by this test. """
pass
-def create_random_sparse_file(file_obj, size):
- """ Create a sparse file with randomly distributed holes. The mapped areas
- are filled with random data. """
-
- block_size = BmapHelpers.get_block_size(file_obj)
- blocks_cnt = (size + block_size - 1) / block_size
-
- def process_block(block):
- """ This is a helper function which processes a block. It randomly
- decides whether the block should be filled with random data or should
- become a hole. Returns 'True' if the block was mapped and 'False'
- otherwise. """
-
- map_the_block = bool(random.randint(0, 1))
-
- if map_the_block:
- file_obj.seek(block * block_size)
- file_obj.write(bytearray(os.urandom(block_size)))
- else:
- file_obj.truncate((block + 1) * block_size)
-
- return map_the_block
-
- holes = []
- iterator = xrange(0, blocks_cnt)
- for was_mapped, group in itertools.groupby(iterator, process_block):
- if not was_mapped:
- # Start of a hole. Find the last element in the group.
- first = group.next()
- last = first
- for last in group:
- pass
-
- holes.append((first, last))
-
- file_obj.flush()
-
- # Make sure the Fiemap module reports holes correctly
- fiemap = Fiemap.Fiemap(file_obj)
- fiemap_iterator = fiemap.get_unmapped_ranges(0, blocks_cnt)
- iterator = itertools.izip_longest(holes, fiemap_iterator)
- for ours, fiemaps in iterator:
- if ours != fiemaps:
- raise Error("mismatch for hole %d-%d, Fiemap returned %d-%d" \
- % (ours[0], ours[1], fiemaps[0], fiemaps[1]))
-
def compare_holes(file1, file2):
""" Make sure that files 'file1' and 'file2' have holes at the same places.
The 'file1' and 'file2' arguments may be full file paths or file
'setUp()' method. """
# Create a sparse file with randomly distributed holes
- create_random_sparse_file(self._f_image, self._image_size)
+ test_helpers.create_random_sparse_file(self._f_image, self._image_size)
#
# Pass 1: generate the bmap, copy and compare
--- /dev/null
+""" This module contains independent functions shared between various
+tests. """
+
+import os
+import random
+import itertools
+from bmaptools import BmapHelpers
+
+def create_random_sparse_file(file_obj, size):
+ """ Create a sparse file with randomly distributed holes. The mapped areas
+ are filled with random data. Returns a list of unmapped block ranges
+ (holes). """
+
+ block_size = BmapHelpers.get_block_size(file_obj)
+ blocks_cnt = (size + block_size - 1) / block_size
+
+ def process_block(block):
+ """ This is a helper function which processes a block. It randomly
+ decides whether the block should be filled with random data or should
+ become a hole. Returns 'True' if the block was mapped and 'False'
+ otherwise. """
+
+ map_the_block = bool(random.randint(0, 1))
+
+ if map_the_block:
+ file_obj.seek(block * block_size)
+ file_obj.write(bytearray(os.urandom(block_size)))
+ else:
+ file_obj.truncate((block + 1) * block_size)
+
+ return map_the_block
+
+ holes = []
+ iterator = xrange(0, blocks_cnt)
+ for was_mapped, group in itertools.groupby(iterator, process_block):
+ if not was_mapped:
+ # Start of a hole. Find the last element in the group.
+ first = group.next()
+ last = first
+ for last in group:
+ pass
+
+ holes.append((first, last))
+
+ file_obj.flush()
+
+ return holes