test_api_base: use file name in _calculate_chksum
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 1 Nov 2013 13:17:57 +0000 (15:17 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 1 Nov 2013 15:41:01 +0000 (17:41 +0200)
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@intel.com>
tests/test_api_base.py

index 016454234302273c78f502d18ce95a09f3eecd7b..18ba1a3c1c2f0f651a81a029f1403216a2ff614b 100644 (file)
@@ -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