[bmap-tools] [PATCH 07/16] test_api_base: use file name in _calculate_chksum

Artem Bityutskiy dedekind1 at gmail.com
Mon Nov 4 03:05:27 EST 2013


From: Artem Bityutskiy <artem.bityutskiy at intel.com>

This is a minor clean-up which changes the '_calculate_chksum()' so that it
expects the image to be a path and does not allow for file-like objects. This
is cleaner than seeking the file-like object in this function, which changes
the object state.

This change is rather mechanical, and this is actually a preparation for the
upcoming changes, where I am going to make '_copy_image()' and some other
function generic, and then use them in a new test which I am going to add.

Change-Id: Iec472bd5f02eb9c2a1162f43fb41defe87ff3ed6
Signed-off-by: Artem Bityutskiy <artem.bityutskiy at intel.com>
---
 tests/test_api_base.py | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/tests/test_api_base.py b/tests/test_api_base.py
index 0164542..18ba1a3 100644
--- a/tests/test_api_base.py
+++ b/tests/test_api_base.py
@@ -154,12 +154,10 @@ def _generate_compressed_files(file_obj, delete=True):
     yield tbz2_file_obj.name
     tmp_file_obj.close()
 
-def _calculate_chksum(file_obj):
-    """
-    Calculates checksum for the contents of file object 'file_obj'.
-    """
+def _calculate_chksum(file_path):
+    """Calculates checksum for the contents of file 'file_path'."""
 
-    file_obj.seek(0)
+    file_obj = TransRead.TransRead(file_path)
     hash_obj = hashlib.new("sha256")
 
     chunk_size = 1024*1024
@@ -170,6 +168,7 @@ def _calculate_chksum(file_obj):
             break
         hash_obj.update(chunk)
 
+    file_obj.close()
     return hash_obj.hexdigest()
 
 def _copy_image(image, f_dest, f_bmap, image_chksum, image_size):
@@ -191,7 +190,7 @@ def _copy_image(image, f_dest, f_bmap, image_chksum, image_size):
 
     # Compare the original file and the copy are identical
     f_dest.seek(0)
-    assert _calculate_chksum(f_dest) == image_chksum
+    assert _calculate_chksum(f_dest.name) == image_chksum
 
     f_image.close()
 
@@ -226,7 +225,7 @@ def _do_test(f_image, image_size, delete=True):
                                           delete=delete, dir=directory,
                                           suffix=".bmap2")
 
-    image_chksum = _calculate_chksum(f_image)
+    image_chksum = _calculate_chksum(f_image.name)
 
     #
     # Pass 1: generate the bmap, copy and compare
-- 
1.8.1.4




More information about the Bmap-tools mailing list