tests: create a separate module for helper functions
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 26 Nov 2012 07:52:57 +0000 (09:52 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 26 Nov 2012 12:00:00 +0000 (14:00 +0200)
I am going to add a test for the Fiemap module, and it will need the
'create_random_sparse_file()' function from the base API test. Therefor, move
this function to a separate 'test_helpers.py' module which will be shared
across tests.

Additionally, remove the holes verification from 'create_random_sparse_file()'
- the test which I will add soon verifies holes separately.

Also change the egging to exclude the tests.

Change-Id: I2c81251c5e1884a4f644e89b417c8a18050aeada
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
setup.py
tests/__init__.py [new file with mode: 0644]
tests/test_api_base.py
tests/test_helpers.py [new file with mode: 0644]

index a4c4ba1..a2e2d83 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ setup(
     author_email = "artem.bityutskiy@linux.intel.com",
     version = "0.6",
     scripts = ['bmaptool'],
-    packages = find_packages(),
+    packages = find_packages(exclude = [ "test*" ]),
     license='GPLv2',
     long_description="Tools to generate block map (AKA bmap) and copy " \
                      "images using bmap",
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
index 7314a37..8401f99 100644 (file)
@@ -10,11 +10,12 @@ file and the copy and verifies that they are identical. """
 
 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
@@ -23,52 +24,6 @@ class Error(Exception):
     """ 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
@@ -137,7 +92,7 @@ class TestCreateCopy(unittest.TestCase):
         '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
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
new file mode 100644 (file)
index 0000000..c1fa94f
--- /dev/null
@@ -0,0 +1,47 @@
+""" 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