add unittest to volume_size_accuracy
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 19 Jul 2016 09:50:51 +0000 (11:50 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Nov 2020 14:04:34 +0000 (15:04 +0100)
testing/volume_size_accuracy.py

index 8576942..22ed178 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-""" Check very accurately the splitting of files into volumes; not a unit test
+""" Check very accurately the splitting of files into volumes
 
 Check:
 - behaviour for max_volume_sizes % BLOCKSIZE != 0
@@ -16,6 +16,11 @@ By doing the following:
 - repeat with max_volume_size +1, +2, +10, ...
 - repeat with file sizes -1, -2, -10, ...
 
+There are 2 ways to run these tests:
+- run this file as a script will run run_all_tests, which tests lots of
+  parameter combinations
+- if run as unittest, only a few parameter combinations are run
+
 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)
@@ -65,7 +70,6 @@ vol2: |  ...  |       |       |     |       |       |       |       |       |
 .. codeauthor:: Intra2net AG <info@intra2net>
 """
 
-
 import os
 from os.path import dirname, abspath, join as pjoin
 from stat import S_ISREG
@@ -73,6 +77,7 @@ import sys
 from math import ceil
 from glob import glob
 from tempfile import NamedTemporaryFile, TemporaryDirectory
+from unittest import TestCase
 
 # try to import the tarfile from source, not the globally installed one
 source_base = dirname(dirname(abspath(__file__)))
@@ -366,9 +371,106 @@ def run_all_tests(fast_fail=True, print_everything=False):
                             if fast_fail:
                                 print('stopping after test {} (fast-fail set)'
                                       .format(n_tests))
-
     return n_errs
 
 
+class VolumeSizeAccuracyTest(TestCase):
+    """ unittest that runs only a few test parameter combinations """
+
+    # base variables (test case 5)
+    file_sizes = MAX_VOLUME_BLOCKS-5, 2
+    vol_sizes = MAX_VOLUME_BLOCKS-2, 4
+    expect_offsets = 0, MAX_VOLUME_BLOCKS-4, 2
+    file_size_offsets = 0, 0
+    volume_size_offset = 0
+
+    def do_test(self, file_sizes, vol_sizes, expect_offsets, file_size_offsets,
+                volume_size_offset):
+        """ create temp dir, run :py:func:`do_test` with given params """
+        with TemporaryDirectory(prefix='deltatar_test_') as temp_dir:
+            return do_test(temp_dir, file_sizes, vol_sizes, expect_offsets,
+                           file_size_offsets, volume_size_offset)
+
+    def test_base(self):
+        """ no offsets, easy-peasy """
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                self.file_size_offsets, self.volume_size_offset))
+
+    def test_volume_offset(self):
+        """ only a volume size offset """
+        volume_size_offset = 1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                self.file_size_offsets, volume_size_offset))
+
+        volume_size_offset = BLOCKSIZE-1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                self.file_size_offsets, volume_size_offset))
+
+    def test_file0_offset(self):
+        """ only a offset for size of file0 """
+        file0_offset = 1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                (file0_offset, self.file_size_offsets[1]),
+                self.volume_size_offset))
+
+        file0_offset = BLOCKSIZE-1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                (file0_offset, self.file_size_offsets[1]),
+                self.volume_size_offset))
+
+    def test_file1_offset(self):
+        """ only a offset for size of file1 """
+        file1_offset = 1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                (self.file_size_offsets[0], file1_offset),
+                self.volume_size_offset))
+
+        file1_offset = BLOCKSIZE-1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                (self.file_size_offsets[0], file1_offset),
+                self.volume_size_offset))
+
+    def test_multi_offsets(self):
+        """ test 2 combinations of all offsets """
+        file_size_offsets = (1, 1)
+        volume_size_offset = BLOCKSIZE-1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                file_size_offsets, volume_size_offset))
+
+        file_size_offsets = (BLOCKSIZE-1, BLOCKSIZE-1)
+        volume_size_offset = 1
+        self.assertTrue(self.do_test(\
+                self.file_sizes, self.vol_sizes, self.expect_offsets,
+                file_size_offsets, volume_size_offset))
+
+    def test_single_volume(self):
+        """ smaller files --> only a single volume (test case 1) """
+        file_sizes = MAX_VOLUME_BLOCKS-5, 1
+        vol_sizes = MAX_VOLUME_BLOCKS, 0
+        expect_offsets = 0, MAX_VOLUME_BLOCKS-4, MAX_VOLUME_BLOCKS-2
+        self.assertTrue(self.do_test(\
+                file_sizes, vol_sizes, expect_offsets,
+                self.file_size_offsets, self.volume_size_offset))
+
+    def test_single_volume_offset(self):
+        """ single volume + offsets """
+        file_sizes = MAX_VOLUME_BLOCKS-5, 1
+        vol_sizes = MAX_VOLUME_BLOCKS, 0
+        expect_offsets = 0, MAX_VOLUME_BLOCKS-4, MAX_VOLUME_BLOCKS-2
+        file_size_offsets = (BLOCKSIZE-1, BLOCKSIZE-1)
+        volume_size_offset = 1
+        self.assertTrue(self.do_test(\
+                file_sizes, vol_sizes, expect_offsets,
+                file_size_offsets, volume_size_offset))
+
+
 if __name__ == '__main__':
     sys.exit(run_all_tests())