bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)
[python-delta-tar] / testing / test_compression_level.py
1 # Copyright (C) 2014 Intra2net AG
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU Lesser General Public License as published
5 # by the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see
15 # <http://www.gnu.org/licenses/lgpl-3.0.html>
16
17 # Authors: Victor Ramirez de la Corte <virako@wadobo.com>
18 #          Eduardo Robles Elvira <edulix@wadobo.com>
19
20 import os
21 import shutil
22 import sys
23 import hashlib
24 import unittest
25 from . import run_benchmark
26 from . import create_pseudo_random_files
27
28 DEFAULTS_COMPRESS_LEVEL_TEST = (int(42 / 10), 42, 42, 1, 8)
29
30 class CompressLevelTest(unittest.TestCase):
31     """
32     Small integration test that uses create_pseudo_random_files to create a
33     deterministic files and directories tree and then uses delta-tar with the
34     run_benchmark script.
35     """
36
37     BUF_SIZE = 2**12
38
39     def __init__(self, num_dir=None, num_files=None, size=None,
40                  compression_lvl_1=None, compression_lvl_2=None):
41         if isinstance (num_dir, str) and \
42            num_files         is None and \
43            size              is None and \
44            compression_lvl_1 is None and \
45            compression_lvl_2 is None:
46             # invoked as module
47             num_dir           = DEFAULTS_COMPRESS_LEVEL_TEST [0]
48             num_files         = DEFAULTS_COMPRESS_LEVEL_TEST [1]
49             size              = DEFAULTS_COMPRESS_LEVEL_TEST [2]
50             compression_lvl_1 = DEFAULTS_COMPRESS_LEVEL_TEST [3]
51             compression_lvl_2 = DEFAULTS_COMPRESS_LEVEL_TEST [4]
52         elif num_dir           is None or \
53              num_files         is None or \
54              size              is None or \
55              compression_lvl_1 is None or \
56              compression_lvl_2 is None:
57             raise ValueError
58         super(CompressLevelTest, self).__init__('test_compression_level')
59         self.num_dir = num_dir
60         self.num_files = num_files
61         self.size = size
62         self.compression_lvl_1 = compression_lvl_1
63         self.compression_lvl_2 = compression_lvl_2
64
65     def setUp(self):
66         '''
67         Create pseudo random test files
68         '''
69         shutil.rmtree("source_dir", ignore_errors=True)
70         args = ['--nfile', str(self.num_files),
71                 '--ndir', str(self.num_dir),
72                 '--size', str(self.size),
73                 '--path', 'source_dir',
74                 '--seed', '1337']
75         self.seed = create_pseudo_random_files.main(args)
76
77     def tearDown(self):
78         '''
79         Remove pseudo random files created by unit tests
80         '''
81         shutil.rmtree("source_dir", ignore_errors=True)
82
83     def test_compression_level(self):
84         '''
85         Create two test compression with two different compression level.
86         '''
87         args = ['--test', 'delta-tarfile',
88                 '-l', str(self.compression_lvl_1),
89                 '-p', 'source_dir']
90         run_benchmark.main(args)
91         size1 = os.path.getsize('test_tarfile.tar.gz')
92         os.unlink('test_tarfile.tar.gz')
93
94         args = ['--test', 'delta-tarfile',
95                 '-l', str(self.compression_lvl_2),
96                 '-p', 'source_dir']
97         run_benchmark.main(args)
98         size2 = os.path.getsize('test_tarfile.tar.gz')
99         os.unlink('test_tarfile.tar.gz')
100
101         self.assertGreater(size1, size2, msg="""Fail test using lvl %d and
102                 lvl %d, with the seed: %d. """ % (self.compression_lvl_1,
103                     self.compression_lvl_2, self.seed))
104
105
106 def suite():
107     suite = unittest.TestSuite()
108     for size in (10, ):
109         suite.addTest(CompressLevelTest(int(size / 10), size, size, 1, 8))
110     return suite