# Copyright (C) 2014 Intra2net AG # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see # # Authors: Victor Ramirez de la Corte # Eduardo Robles Elvira import os import shutil import sys import hashlib import unittest from . import run_benchmark from . import create_pseudo_random_files DEFAULTS_COMPRESS_LEVEL_TEST = (int(42 / 10), 42, 42, 1, 8) class CompressLevelTest(unittest.TestCase): """ Small integration test that uses create_pseudo_random_files to create a deterministic files and directories tree and then uses delta-tar with the run_benchmark script. """ BUF_SIZE = 2**12 def __init__(self, num_dir=None, num_files=None, size=None, compression_lvl_1=None, compression_lvl_2=None): if isinstance (num_dir, str) and \ num_files is None and \ size is None and \ compression_lvl_1 is None and \ compression_lvl_2 is None: # invoked as module num_dir = DEFAULTS_COMPRESS_LEVEL_TEST [0] num_files = DEFAULTS_COMPRESS_LEVEL_TEST [1] size = DEFAULTS_COMPRESS_LEVEL_TEST [2] compression_lvl_1 = DEFAULTS_COMPRESS_LEVEL_TEST [3] compression_lvl_2 = DEFAULTS_COMPRESS_LEVEL_TEST [4] elif num_dir is None or \ num_files is None or \ size is None or \ compression_lvl_1 is None or \ compression_lvl_2 is None: raise ValueError super(CompressLevelTest, self).__init__('test_compression_level') self.num_dir = num_dir self.num_files = num_files self.size = size self.compression_lvl_1 = compression_lvl_1 self.compression_lvl_2 = compression_lvl_2 def setUp(self): ''' Create pseudo random test files ''' shutil.rmtree("source_dir", ignore_errors=True) args = ['--nfile', str(self.num_files), '--ndir', str(self.num_dir), '--size', str(self.size), '--path', 'source_dir', '--seed', '1337'] self.seed = create_pseudo_random_files.main(args) def tearDown(self): ''' Remove pseudo random files created by unit tests ''' shutil.rmtree("source_dir", ignore_errors=True) def test_compression_level(self): ''' Create two test compression with two different compression level. ''' args = ['--test', 'delta-tarfile', '-l', str(self.compression_lvl_1), '-p', 'source_dir'] run_benchmark.main(args) size1 = os.path.getsize('test_tarfile.tar.gz') os.unlink('test_tarfile.tar.gz') args = ['--test', 'delta-tarfile', '-l', str(self.compression_lvl_2), '-p', 'source_dir'] run_benchmark.main(args) size2 = os.path.getsize('test_tarfile.tar.gz') os.unlink('test_tarfile.tar.gz') self.assertGreater(size1, size2, msg="""Fail test using lvl %d and lvl %d, with the seed: %d. """ % (self.compression_lvl_1, self.compression_lvl_2, self.seed)) def suite(): suite = unittest.TestSuite() for size in (10, ): suite.addTest(CompressLevelTest(int(size / 10), size, size, 1, 8)) return suite