From 65e1c6d000b5ae2965142cdca7598a50fa134103 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Mon, 26 Nov 2012 13:39:05 +0200 Subject: [PATCH] test_api_base: re-structure the test some more Kill the 'setUp()' and 'tearDown()' methods and open/close the temporary files in '_do_test()' instead. This works better for me because I will call '_do_test()' many times and it is simpler when it starts with new temporary files every time, rather than teach it cleaning-up the old ones. Change-Id: I135b8423d1e7d95a6c2de8d8bc0d3f8597ca5418 Signed-off-by: Artem Bityutskiy --- tests/test_api_base.py | 68 +++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/tests/test_api_base.py b/tests/test_api_base.py index a3786aa..d8ea72b 100644 --- a/tests/test_api_base.py +++ b/tests/test_api_base.py @@ -44,93 +44,81 @@ class TestCreateCopy(unittest.TestCase): original sparse file is generated randomly. The test entry point is the 'test()' method. """ - # Pylint does not like the 'setUP' and 'tearDown' names - mute it. - # pylint: disable=C0103 - def setUp(self): - """ Initialize the test - called by the unittest framework before the - test starts. """ + def _do_test(self, f_image): + """ Run the test for the 'f_image' file object. """ # Create and open a temporary file for a copy of the copy - self._f_copy = tempfile.NamedTemporaryFile("wb+") + f_copy = tempfile.NamedTemporaryFile("wb+") # Create and open 2 temporary files for the bmap - self._f_bmap1 = tempfile.NamedTemporaryFile("w+") - self._f_bmap2 = tempfile.NamedTemporaryFile("w+") - - def tearDown(self): - """ The clean-up method - called by the unittest framework when the - test finishes. """ - - self._f_copy.close() - self._f_bmap1.close() - self._f_bmap2.close() - - # pylint: enable=C0103 - - def _do_test(self, f_image): - """ Run the test for the 'f_image' file object. """ + f_bmap1 = tempfile.NamedTemporaryFile("w+") + f_bmap2 = tempfile.NamedTemporaryFile("w+") # # Pass 1: generate the bmap, copy and compare # # Create bmap for the random sparse file - creator = BmapCreate.BmapCreate(f_image.name, self._f_bmap1.name) + creator = BmapCreate.BmapCreate(f_image.name, f_bmap1.name) creator.generate() # Copy the random sparse file to a different file using bmap - writer = BmapCopy.BmapCopy(f_image.name, self._f_copy.name, - self._f_bmap1.name) + writer = BmapCopy.BmapCopy(f_image.name, f_copy.name, f_bmap1.name) writer.copy(False, True) # Compare the original file and the copy are identical - assert filecmp.cmp(f_image.name, self._f_copy.name, False) + assert filecmp.cmp(f_image.name, f_copy.name, False) # Make sure that holes in the copy are identical to holes in the random # sparse file. - compare_holes(f_image.name, self._f_copy.name) + compare_holes(f_image.name, f_copy.name) # # Pass 2: same as pass 1, but use file objects instead of paths # - creator = BmapCreate.BmapCreate(f_image, self._f_bmap2) + creator = BmapCreate.BmapCreate(f_image, f_bmap2) creator.generate() - writer = BmapCopy.BmapCopy(f_image, self._f_copy, self._f_bmap2) + writer = BmapCopy.BmapCopy(f_image, f_copy, f_bmap2) writer.copy(False, True) - assert filecmp.cmp(f_image.name, self._f_copy.name, False) - compare_holes(f_image, self._f_copy) + assert filecmp.cmp(f_image.name, f_copy.name, False) + compare_holes(f_image, f_copy) # Make sure the bmap files generated at pass 1 and pass 2 are identical - assert filecmp.cmp(self._f_bmap1.name, self._f_bmap2.name, False) + assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False) # # Pass 3: repeat pass 2 to make sure the same 'BmapCreate' and # 'BmapCopy' objects can be used more than once. # - self._f_bmap2.seek(0) + f_bmap2.seek(0) creator.generate() - self._f_bmap2.seek(0) + f_bmap2.seek(0) creator.generate() writer.copy(True, False) writer.copy(False, True) writer.sync() - assert filecmp.cmp(f_image.name, self._f_copy.name, False) - compare_holes(f_image, self._f_copy) - assert filecmp.cmp(self._f_bmap1.name, self._f_bmap2.name, False) + assert filecmp.cmp(f_image.name, f_copy.name, False) + compare_holes(f_image, f_copy) + assert filecmp.cmp(f_bmap1.name, f_bmap2.name, False) # # Pass 4: copy the sparse file without bmap and make sure it is # identical to the original file # - writer = BmapCopy.BmapCopy(f_image, self._f_copy.name) + writer = BmapCopy.BmapCopy(f_image, f_copy.name) writer.copy(True, True) - assert filecmp.cmp(f_image.name, self._f_copy.name, False) + assert filecmp.cmp(f_image.name, f_copy.name, False) - writer = BmapCopy.BmapCopy(f_image, self._f_copy) + writer = BmapCopy.BmapCopy(f_image, f_copy) writer.copy(False, True) - assert filecmp.cmp(f_image.name, self._f_copy.name, False) + assert filecmp.cmp(f_image.name, f_copy.name, False) + + # Close temporary files, which will also remove them + f_copy.close() + f_bmap1.close() + f_bmap2.close() def test(self): -- 2.7.4