preparations for adding unittests to volume_size_accuracy
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 19 Jul 2016 09:50:35 +0000 (11:50 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Nov 2020 14:04:34 +0000 (15:04 +0100)
- rename main to run_all_tests
- rename test to do_test
- make fast_fail and print_everything params

testing/volume_size_accuracy.py

index 67adade..8576942 100755 (executable)
@@ -16,7 +16,7 @@ By doing the following:
 - repeat with max_volume_size +1, +2, +10, ...
 - repeat with file sizes -1, -2, -10, ...
 
-e.g. for max_volume_size = RECORDSIZE + 1 * BLOCKSIZE:
+Tests use max_volume_size = RECORDSIZE + 1 * BLOCKSIZE:
 File 0 has Info0 and blocks Dat00, Dat01, ... Dat0K, (Dat0L, Dat0M, Dat0N)
 File 1 has Info1 and blocks Dat10, (Dat11)
                                                                 end of   end of
@@ -90,10 +90,6 @@ N_BLOCKS_PER_RECORD = RECORDSIZE // BLOCKSIZE
 #: number of blocks per tar volume file
 MAX_VOLUME_BLOCKS = N_BLOCKS_PER_RECORD + 1
 
-#: always print all test output; will produce ~10 lines of text for each of the
-#: ~48000 tests --> not recommended
-ALWAYS_PRINT_EVERYTHING = False
-
 
 def fill_file(file_handle, data_size):
     """ fill given file handle with nonsense data of given size
@@ -146,8 +142,9 @@ def size_str(size):
     return '{} (= {} BLKs + {})'.format(size, *divmod(size, BLOCKSIZE))
 
 
-def test(temp_dir, file_blocks, volume_blocks_arg, offset_blocks,
-         file_size_offsets=(0, 0), volume_size_offset=0):
+def do_test(temp_dir, file_blocks, volume_blocks_arg, offset_blocks,
+            file_size_offsets=(0, 0), volume_size_offset=0,
+            print_everything=False):
     """ create TarFile with given configuration """
 
     # use delayed + conditional print dprnt, that only prints if necessary
@@ -302,36 +299,37 @@ def test(temp_dir, file_blocks, volume_blocks_arg, offset_blocks,
             dprnt('extracted {} has wrong contents!'.format(file_name))
 
     # print output only if something went wrong
-    if (not everything_ok) or ALWAYS_PRINT_EVERYTHING:
+    if (not everything_ok) or print_everything:
         for line in output:
             print(line)
 
     return everything_ok
 
 
-def main():
-    """ Main function, called when running file as script
+def run_all_tests(fast_fail=True, print_everything=False):
+    """ run test with lots of parameter combinations, will take quite a while
 
     see module doc for more info
     """
 
+    # abbreviations for shorter lists
     N = N_BLOCKS_PER_RECORD
     M = MAX_VOLUME_BLOCKS
     B = BLOCKSIZE
 
-    fast_fail = True
-
     # define tests by numbers of blocks:
     # n_blocks file 0, 1; n_blocks vol0, 1, offset Info0, Info1, 0-blocks
-    tests = (((N-5, 1), (N,   0), (0, N-4, N-2)),
-             ((M-5, 1), (M,   0), (0, M-4, M-2)),
-             ((M-4, 1), (M+1, 0), (0, M-3, M-1)),
-             ((M-3, 1), (M+2, 0), (0, M-2, M)),
-             ((M-2, 1), (M-2, 6), (0, 2,   4)),
-             ((M-5, 2), (M-2, 4), (0, M-4, 2)),
-             ((M-4, 2), (M-1, 4), (0, M-3, 2)))
-    size_offsets = (0, 1, 2, 3, 5, 10, 20, 30, 50, 100) + \
-                   (B-1, B-2, B-3, B-5, B-10, B-20, B-30, B-50, B-100)[::-1]
+    tests = (((N-5, 1), (N,   0), (0, N-4, N-2)),    # test case 0
+             ((M-5, 1), (M,   0), (0, M-4, M-2)),    # test case 1
+             ((M-4, 1), (M+1, 0), (0, M-3, M-1)),    # test case 2
+             ((M-3, 1), (M+2, 0), (0, M-2, M)),      # test case 3
+             ((M-2, 1), (M-2, 6), (0, 2,   4)),      # test case 4
+             ((M-5, 2), (M-2, 4), (0, M-4, 2)),      # test case 5
+             ((M-4, 2), (M-1, 4), (0, M-3, 2)))      # test case 6
+
+    # offsets for file and volume sizes in tests:
+    size_offsets = (0, 1, 2, 5, 10, 22, 46, 100) + \
+                   (B-1, B-2, B-5, B-10, B-22, B-46, B-100)[::-1]
 
     n_errs = 0
     n_tests = 0
@@ -358,10 +356,11 @@ def main():
                             break
                         n_tests += 1
                         test_succeeded = \
-                            test(temp_dir, file_sizes, vol_sizes, offsets,
-                                 file_size_offsets=(file_size_offset0,
-                                                    file_size_offset1),
-                                 volume_size_offset=volume_size_offset)
+                            do_test(temp_dir, file_sizes, vol_sizes, offsets,
+                                    file_size_offsets=(file_size_offset0,
+                                                       file_size_offset1),
+                                    volume_size_offset=volume_size_offset,
+                                    print_everything=print_everything)
                         if not test_succeeded:
                             n_errs += 1
                             if fast_fail:
@@ -372,4 +371,4 @@ def main():
 
 
 if __name__ == '__main__':
-    sys.exit(main())
+    sys.exit(run_all_tests())