#!/usr/bin/python3.4 # -*- coding: utf-8 -*- # Copyright © 2015 Carl Chenet # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # 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 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 . import argparse import bz2 import gzip import logging import os import os.path import stat import sys import tarfile import unittest import zipfile import brebis.applogger import brebis.archiveinfomsg import brebis.checkbackups.checkarchive import brebis.checkbackups.checkbackups import brebis.checkbackups.checkbzip2 import brebis.checkbackups.checkgzip import brebis.checkbackups.checklzma import brebis.checkhashes import brebis.checkbackups.checktar import brebis.checkbackups.checktree import brebis.checkbackups.checkzip import brebis.cliparse import brebis.configurations from brebis.expectedvalues import ExpectedValues import brebis.generatelist.generatelistfortar import brebis.generatelist.generatelistforbzip2 import brebis.listtype import brebis.main # !! logging module uses a single logger for the whole file TESTLOG = 'tests/testlog' DEFAULTDELIMITER = '|' # mock the object produced by argparse, useful for lots of tests below class Options: '''Mock the object produced by argparse, useful for lots of unittests''' def __init__(self): self.delimiter = DEFAULTDELIMITER class MyDict(dict): '''mock object''' pass class TestApp(unittest.TestCase): def test_applogger(self): '''Test the AppLoggerclass''' brebis.applogger.AppLogger(TESTLOG) self.assertEqual(True, str(logging.getLogger('applogger')).startswith('> 16) self.assertEqual('f', __result) def test_zip_translate_type_directory(self): '''test the __translate_type private method from checkzip.CheckZip - expecting directory''' __myobj = brebis.checkbackups.checkzip.CheckZip({'path': 'tests/checkzip_private_methods/myzip.zip', 'files_list': 'tests/checkzip_private_methods/myzip-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checkzip_private_methods/myzip.zip' __myz = zipfile.ZipFile(__file,'r') __myinfo = __myz.infolist() __result = __myobj._CheckZip__translate_type(__myinfo[0].external_attr >> 16) self.assertEqual('d', __result) ############################################################## # # Testing the private/protected methods from checktar.CheckTar # ############################################################## def test_tar_extract_stored_file(self): '''test the _extract_stored_file protected method from checktar.CheckTar''' __myobj = brebis.checkbackups.checktar.CheckTar({'path': 'tests/checktar_private_methods/mytargz/mytargz.tar.gz', 'files_list': 'tests/checktar_private_methods/mytargz/mytargz-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checktar_private_methods/mytargz/mytargz.tar.gz' self._tar = tarfile.open(__file) __result = __myobj._extract_stored_file('mytargz/hello') self.assertEqual(type(__result), type(self._tar.extractfile('mytargz/hello'))) def test_tar_translate_type_file(self): '''test the __translate_type private method from checktar.CheckTar - expecting file''' __myobj = brebis.checkbackups.checktar.CheckTar({'path': 'tests/checktar_private_methods/mytargz/mytargz.tar.gz', 'files_list': 'tests/checktar_private_methods/mytargz/mytargz-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checktar_private_methods/mytargz/mytargz.tar.gz' self._tar = tarfile.open(__file) __result = __myobj._CheckTar__translate_type(self._tar.getmembers()[2].type) self.assertEqual('f', __result) def test_tar_translate_type_directory(self): '''test the __translate_type private method from checktar.CheckTar - expecting directory''' __myobj = brebis.checkbackups.checktar.CheckTar({'path': 'tests/checktar_private_methods/mytargz/mytargz.tar.gz', 'files_list': 'tests/checktar_private_methods/mytargz/mytargz-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checktar_private_methods/mytargz/mytargz.tar.gz' self._tar = tarfile.open(__file) __result = __myobj._CheckTar__translate_type(self._tar.getmembers()[0].type) self.assertEqual('d', __result) def test_tar_translate_type_symbolic_link(self): '''test the __translate_type private method from checktar.CheckTar - expecting symbolic link''' __myobj = brebis.checkbackups.checktar.CheckTar({'path': 'tests/checktar_private_methods/mytargz/mytargz.tar.gz', 'files_list': 'tests/checktar_private_methods/mytargz/mytargz-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checktar_private_methods/mytargz/mytargz.tar.gz' self._tar = tarfile.open(__file) __result = __myobj._CheckTar__translate_type(self._tar.getmembers()[1].type) self.assertEqual('s', __result) ################################################################ # # Testing the private/protected methods from checktree.CheckTree # ################################################################ def test_tree_extract_stored_file(self): '''test the _extract_stored_file protected method from checktree.CheckTree''' __myobj = brebis.checkbackups.checktree.CheckTree({'path': 'tests/checktree_private_methods/mytree', 'files_list': 'tests/checktree_private_methods/mytree-list', 'type': 'tree', 'delimiter': ''}, Options()) __file = 'tests/checktree_private_methods/mytree' __result = __myobj._extract_stored_file('hello') with open(os.path.join(__file, 'hello'), 'rb') as self.__desc: self.assertEqual(type(__result), type(self.__desc)) __result.close() def test_tree_translate_type_file(self): '''test the __translate_type private method from checktree.CheckTree - expecting file''' __myobj = brebis.checkbackups.checktree.CheckTree({'path': 'tests/checktree_private_methods/mytree', 'files_list': 'tests/checktree_private_methods/mytree-list', 'type': 'tree', 'delimiter': ''}, Options()) __file = 'tests/checktree_private_methods/mytree/hello' __result = __myobj._CheckTree__translate_type(os.lstat(__file).st_mode) self.assertEqual('f', __result) def test_tree_translate_type_directory(self): '''test the __translate_type private method from checktree.CheckTree - expecting directory''' __myobj = brebis.checkbackups.checktree.CheckTree({'path': 'tests/checktree_private_methods/mytree', 'files_list': 'tests/checktree_private_methods/mytree-list', 'type': 'tree', 'delimiter': ''}, Options()) __file = 'tests/checktree_private_methods/mytree' __result = __myobj._CheckTree__translate_type(os.lstat(__file).st_mode) self.assertEqual('d', __result) def test_tree_translate_type_symbolic_link(self): '''test the __translate_type private method from checktree.CheckTree - expecting symbolic link''' __myobj = brebis.checkbackups.checktree.CheckTree({'path': 'tests/checktree_private_methods/mytree', 'files_list': 'tests/checktree_private_methods/mytree-list', 'type': 'tree', 'delimiter': ''}, Options()) __file = 'tests/checktree_private_methods/mytree/riri' __result = __myobj._CheckTree__translate_type(os.lstat(__file).st_mode) self.assertEqual('s', __result) ################################################################ # # Testing the private/protected methods from checkgzip.CheckGzip # ################################################################ def test_gzip_extract_stored_file(self): '''test the _extract_stored_file protected method from checkgzip.CheckGzip''' __myobj = brebis.checkbackups.checkgzip.CheckGzip({'path': 'tests/checkgzip_private_methods/mygzip.gz', 'files_list': 'tests/checkgzip_private_methods/mygzip-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checkgzip_private_methods/mygzip.gz' __result = __myobj._extract_stored_file('mygzip') with gzip.open(__file, 'rb') as self.__desc: self.assertEqual(type(__result), type(self.__desc)) __result.close() def test_extract_size_from_gzip_archive(self): '''test the extraction of a gzip uncompressed file in the gzip archive''' __arcpath = 'tests/checkgzip_private_methods/mygzip.gz' __myobj = brebis.checkbackups.checkgzip.CheckGzip({'path': __arcpath, 'files_list': 'tests/checkgzip_private_methods/mygzip-list', 'type': 'archive', 'delimiter': ''}, Options()) with open(__arcpath, 'rb') as __myf: self.assertEqual(23, __myobj._CheckGzip__extract_size(__myf)) def test_extract_initial_filename_from_gzip_archive(self): '''test the extraction of the initial name of an uncompressed file''' __arcpath = 'tests/checkgzip_private_methods/mygzip.gz' __myobj = brebis.checkbackups.checkgzip.CheckGzip({'path': __arcpath, 'files_list': 'tests/checkgzip_private_methods/mygzip-list', 'type': 'archive', 'delimiter': ''}, Options()) with open(__arcpath, 'rb') as __myf: self.assertEqual('mygzip', __myobj._CheckGzip__extract_initial_filename(__myf, 'mygzip')) ################################################################## # # Testing the private/protected methods from checkbzip2.CheckBzip2 # ################################################################## def test_bzip2_extract_stored_file(self): '''test the _extract_stored_file protected method from checkbzip2.CheckBzip2''' __myobj = brebis.checkbackups.checkbzip2.CheckBzip2({'path': 'tests/checkbzip2_private_methods/mybz2.bz2', 'files_list': 'tests/checkbzip2_private_methods/mybzip2-list', 'type': 'archive', 'delimiter': ''}, Options()) __file = 'tests/checkbzip2_private_methods/mybz2.bz2' __result = __myobj._extract_stored_file('mygzip') with bz2.BZ2File(__file, 'r') as self.__desc: self.assertEqual(type(__result), type(self.__desc)) __result.close() ###################################################################################### # # Testing the private/protected methods from generatelistforbzip2.GenerateListForBzip2 # ###################################################################################### def test__listconfinfo(self): '''test the GenerateListForBzip2 class''' __myobj = brebis.generatelist.generatelistforbzip2.GenerateListForBzip2({ 'arcpath': 'tests/checkbzip2_private_methods/mybz2.bz2', 'delimiter': '|', 'hashtype': '', 'parsingexceptions' : '', 'getallhashes': True, 'genfull': True, 'confoutput':'','listoutput':'','fulloutput':''}) self.assertEqual(__myobj._GenerateListForBzip2__lci, {'arclistpath': 'tests/checkbzip2_private_methods/mybz2.list', 'listoffiles': ['[files]\n', 'mybz2| type|f md5|f5488b7ce878d89b59ef2752f260354f\n']}) self.assertEqual(__myobj._GenerateListForBzip2__ci, {'arcconfpath': 'tests/checkbzip2_private_methods/mybz2.conf', 'arclistpath': 'tests/checkbzip2_private_methods/mybz2.list', 'arcname': 'mybz2', 'arcpath': 'tests/checkbzip2_private_methods/mybz2.bz2', 'arctype': 'archive', 'sha512': 'b45fa678a2208bfbf457f602b2d3de0c83e82ad9ba042b029b8d37f23bfe2774d1cd25576da880d20bb7f6ffcfc2ecb4bcc80112e880415de82fe63d81f81cb7'}) ############################################################################################### # # Testing the private/protected methods from generatelist.generatelist.GenerateList # ############################################################################################### def test_generatelist_generate_list(self): '''test the _generate_list protected method from GenerateList''' __file = 'tests/generatelist_private_methods/mytar.list' __myobj = brebis.generatelist.generatelist.GenerateList() __content = ['[files]\n', 'foo:\n'] __myobj._generate_list({'arclistpath': __file, 'listoffiles': __content}) with open(__file, 'r') as __f: self.assertEqual(__f.readlines(), __content) ############################################################################################### # # Testing the private/protected methods from generatelist.generatelistfortar.GenerateListForTar # ############################################################################################### def test_listfortar_translate_type_file(self): '''test the __translate_type private method from GenerateListForTar - expecting file''' __file = 'tests/generatelistfortar_private_methods/mytar.tar.gz' __myobj = brebis.generatelist.generatelistfortar.GenerateListForTar({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) self.__tar = tarfile.open(__file) __result = __myobj._GenerateListForTar__translate_type(self.__tar.getmembers()[3].type) self.assertEqual('f', __result) def test_listfortar_translate_type_directory(self): '''test the __translate_type private method from GenerateListForTar - expecting directory''' __file = 'tests/generatelistfortar_private_methods/mytar.tar.gz' __myobj = brebis.generatelist.generatelistfortar.GenerateListForTar({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) self.__tar = tarfile.open(__file) __result = __myobj._GenerateListForTar__translate_type(self.__tar.getmembers()[1].type) self.assertEqual('d', __result) def test_listfortar_translate_type_symbolic_link(self): '''test the __translate_type private method from GenerateListForTar - expecting symbolic link''' __file = 'tests/generatelistfortar_private_methods/mytar.tar.gz' __myobj = brebis.generatelist.generatelistfortar.GenerateListForTar({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) self.__tar = tarfile.open(__file) __result = __myobj._GenerateListForTar__translate_type(self.__tar.getmembers()[2].type) self.assertEqual('s', __result) ############################################################################################### # # Testing the private/protected methods from generatelist.generatelistforzip.GenerateListForZip # ############################################################################################### def test_listforzip_translate_type_file(self): '''test the __translate_type private method from GenerateListForZip - expecting file''' __file = 'tests/generatelistforzip_private_methods/myzip.zip' __myobj = brebis.generatelist.generatelistforzip.GenerateListForZip({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) __myz = zipfile.ZipFile(__file,'r') __myinfo = __myz.infolist() __result = __myobj._GenerateListForZip__translate_type(__myinfo[-1].external_attr >> 16) self.assertEqual('f', __result) def test_listforzip_translate_type_directory(self): '''test the __translate_type private method from GenerateListForZip - expecting directory''' __file = 'tests/generatelistforzip_private_methods/myzip.zip' __myobj = brebis.generatelist.generatelistforzip.GenerateListForZip({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) __myz = zipfile.ZipFile(__file,'r') __myinfo = __myz.infolist() __result = __myobj._GenerateListForZip__translate_type(__myinfo[0].external_attr >> 16) self.assertEqual('d', __result) def test_listforzip_extract_uid_gid(self): '''test the __extract_uid_gid private method from GenerateListForZip''' __file = 'tests/generatelistforzip_private_methods/myzip.zip' __myobj = brebis.generatelist.generatelistforzip.GenerateListForZip({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) __myz = zipfile.ZipFile(__file,'r') __myinfo = __myz.infolist() __result = __myobj._GenerateListForZip__extract_uid_gid(__myinfo[-1]) self.assertEqual((1000,1000), __result) ################################################################################################# # # Testing the private/protected methods from generatelist.generatelistfortree.GenerateListForTree # ################################################################################################# def test_listfortree_translate_type_file(self): '''test the __translate_type private method from GenerateListForTree - expecting file''' __dir = 'tests/generatelistfortree_private_methods/mydir' __file = os.path.join(__dir, 'foo') __myobj = brebis.generatelist.generatelistfortree.GenerateListForTree({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) __myobj._GenerateListForTree__fileinfo = os.lstat(__file) __result = __myobj._GenerateListForTree__translate_type(os.lstat(__file).st_mode) self.assertEqual('f', __result) def test_listfortree_translate_type_directory(self): '''test the __translate_type private method from GenerateListForTree - expecting directory''' __dir = 'tests/generatelistfortree_private_methods/mydir' __file = os.path.join(__dir, 'bar') __myobj = brebis.generatelist.generatelistfortree.GenerateListForTree({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) __result = __myobj._GenerateListForTree__translate_type(os.lstat(__file).st_mode) self.assertEqual('d', __result) def test_listfortree_translate_type_symbolic_link(self): '''test the __translate_type private method from GenerateListForTree - expecting symbolic link''' __dir = 'tests/generatelistfortree_private_methods/mydir' __file = os.path.join(__dir, 'oof') __myobj = brebis.generatelist.generatelistfortree.GenerateListForTree({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) __result = __myobj._GenerateListForTree__translate_type(os.lstat(__file).st_mode) self.assertEqual('s', __result) ################################################################################################# # # Testing the private/protected methods from generatelist.generatelistfortree.GenerateListForTree # ################################################################################################# def test_listforgzip_extract_size_from_gzip_archive(self): '''test the extraction of a gzip uncompressed file in the gzip archive''' __file = 'tests/generatelistforgzip_private_methods/mygzip.gz' __myobj = brebis.generatelist.generatelistforgzip.GenerateListForGzip({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) with open(__file, 'rb') as __myf: self.assertEqual(15, __myobj._GenerateListForGzip__extract_size(__myf)) def test_listforgzip_extract_initial_filename_from_gzip_archive(self): __file = 'tests/generatelistforgzip_private_methods/mygzip.gz' __myobj = brebis.generatelist.generatelistforgzip.GenerateListForGzip({'arcpath':__file, 'delimiter':DEFAULTDELIMITER, 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull':False, 'confoutput':'','listoutput':'','fulloutput':''}) with open(__file, 'rb') as __myf: self.assertEqual('mygzip', __myobj._GenerateListForGzip__extract_initial_filename(__myf, 'mygzip')) ############################################################## # # Testing the private/protected methods from cliparse.CliParse # ############################################################## def test_cliparse_cliparse(self): '''Test the cliparse.CliParse class''' __archivepath = '/tmp/archive.conf' __logpath = '/tmp/a.out' with open(__archivepath, 'w') as __desc: __desc.write('') sys.argv.append('-c') sys.argv.append(__archivepath) sys.argv.append('-l') sys.argv.append(__logpath) __myobj = brebis.cliparse.CliParse() self.assertEqual(vars(__myobj.options), {'archives': [], 'confpath': __archivepath, 'delimiter': '|', 'hashtype': '', 'parsingexceptions': '', 'getallhashes': False, 'genfull': False, 'genlist': False, 'logfile': __logpath, 'fulloutput':'', 'confoutput':'', 'listoutput':''}) os.remove(__archivepath) ####################################################################################### # # Testing the consistent given version of the software both in setup.py and cliparse.py # ####################################################################################### def test_version_consistency_in_setup_py_and_cliparse_py(self): '''test the consistency of the version of the software in both setup.py and cliparse.py ''' with open('setup.py') as __setuppy: __setuppycode = __setuppy.readlines() with open('brebis/cliparse.py') as __cliparsepy: __cliparsepycode = __cliparsepy.readlines() for line in __setuppycode: if 'version = ' in line: setuppyversion = line.split("'")[1] break for line in __cliparsepycode: if 'version=' in line: cliparsepyversion = line.split('s ')[1].split("'")[0] break self.assertEqual(setuppyversion, cliparsepyversion) ####################################################################################### # # Testing the brebis/archiveinfomsg.py # ####################################################################################### def test_archiveinfomsg_main(self): '''test the archivemsginfo.ArchiveMsgInfo__main method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = ['testarchiveinfomsgmain1'] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testarchiveinfomsgmain.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__main(__mydict, {'path': 'testarchiveinfomsgmain.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) with open(_logfile) as _res: self.assertIn('WARNING:root:1 file missing in testarchiveinfomsgmain.tar.gz: \nWARNING:root:testarchiveinfomsgmain1\n', _res.read()) def test_archiveinfomsg_missing_files(self): '''test the __missing_files method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testmissingfiles.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__missing_files(['titi'], 'testmissingfiles.tar.gz') with open(_logfile) as _res: self.assertIn('WARNING:root:1 file missing in testmissingfiles.tar.gz: \nWARNING:root:titi\n', _res.read()) def test_archiveinfomsg_unexpected_files(self): '''test the archiveinfomsg.ArchiveInfoMsg__unexpected_files method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testunexpectedfiles.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__unexpected_files(['titi'], 'testunexpectedfiles.tar.gz') with open(_logfile) as _res: self.assertIn('WARNING:root:1 unexpected file checking testunexpectedfiles.tar.gz: \nWARNING:root:titi\n', _res.read()) def test_archiveinfomsg_classify_differences(self): '''test the archiveinfomsg.ArchiveInfoMsg__classify_differences method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [{'path':'classifydifferences1', 'size':'12','expected':'3'}] __mydict.missing_smaller_than = [{'path':'classifydifferences2', 'size':'12','expected':'3'}] __mydict.missing_bigger_than = [{'path':'classifydifferences3', 'size':'12','expected':'3'}] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testclassifydifferences.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__classify_differences(__mydict, 'testclassifydifferences.tar.gz') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences1 size is 12. Should have been 3.', __testresult) self.assertIn('WARNING:root:classifydifferences2 size is 12. Should have been smaller than 3.', __testresult) self.assertIn('WARNING:root:classifydifferences3 size is 12. Should have been bigger than 3.', __testresult) def test_archiveinfomsg_log_differences(self): '''test the archiveinfomsg.ArchiveInfoMsg__log_differences method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [{'path':'classifydifferences4', 'size':'12','expected':'3'}] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testlogdifferences.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__log_differences(__mydict.missing_equality, 'testlogdifferences.tar.gz', '{} {} with unexpected size while checking {}: ') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences4 size is 12. Should have been 3.', __testresult) def test_archiveinfomsg_uid_gid_mismatches(self): '''test the archiveinfomsg.ArchiveInfoMsg__uid_gid_mismatches method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [{'path':'classifydifferences5', 'expecteduid':'1010','uid':'1000'}] __mydict.mismatched_gids = [{'path':'classifydifferences6', 'expectedgid':'1010','gid':'1000'}] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testuidgidmismatches.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__uid_gid_mismatches(__mydict, 'testuidgidmismatches.tar.gz') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences5 uid is 1000. Should have been 1010.', __testresult) self.assertIn('WARNING:root:classifydifferences6 gid is 1000. Should have been 1010.', __testresult) def test_archiveinfomsg_mode_mismatches(self): '''test the archiveinfomsg.ArchiveInfoMsg__mode_mismatches method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [{'path':'classifydifferences7', 'expectedmode':'644','mode':'777'}] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testmodemismatches.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__mode_mismatches(__mydict, 'testmodemismatches.tar.gz') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences7 mode is 777. Should have been 644.', __testresult) def test_archiveinfomsg_target_mismatches(self): '''test the archiveinfomsg.ArchiveInfoMsg__target_mismatches method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [{'path':'classifydifferences8', 'expectedtarget':'../target1','target':'../target2'}] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testtargetmismatches.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__target_mismatches(__mydict, 'testtargetmismatches.tar.gz') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences8 target is ../target2. Should have been ../target1.', __testresult) def test_archiveinfomsg_type_mismatches(self): '''test the archiveinfomsg.ArchiveInfoMsg__type_mismatches method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [{'path':'classifydifferences9', 'expectedtype':'s','type':'f'}] __mydict.mismatched_hashes = [] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testtypemismatches.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__target_mismatches(__mydict, 'testtypemismatches.tar.gz') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences9 is a regular file. Should have been a symbolic link.', __testresult) def test_archiveinfomsg_hash_mismatches(self): '''test the archiveinfomsg.ArchiveInfoMsg__hash_mismatches method''' _logfile = TESTLOG brebis.applogger.AppLogger(TESTLOG) __mydict = MyDict() __mydict.missing_files = [] __mydict.unexpected_files = [] __mydict.missing_equality = [] __mydict.missing_smaller_than = [] __mydict.missing_bigger_than = [] __mydict.mismatched_uids = [] __mydict.mismatched_gids = [] __mydict.mismatched_modes = [] __mydict.mismatched_types = [] __mydict.mismatched_hashes = [{'path':'classifydifferences10', 'expectedhash':'azeraezr','hash':'qdslfmjaze'}] __mydict.mismatched_targets = [] __mydict.mismatched_mtimes = [] __myobj = brebis.archiveinfomsg.ArchiveInfoMsg(__mydict, {'path': 'testhashmismatches.tar.gz', 'sha512': None, 'files_list': '', 'type': 'archive', 'delimiter': None}) __myobj._ArchiveInfoMsg__hash_mismatches(__mydict, 'testhashmismatches.tar.gz') with open(_logfile) as _res: __testresult = _res.read() self.assertIn('WARNING:root:classifydifferences10 hash is qdslfmjaze. Should have been azeraezr.', __testresult) ####################################################################################### # # Testing the brebis.listtype.ListType # ####################################################################################### def test_listtype_tar(self): '''test the listtype class with a tar archive''' #__mydict = brebis.listtype.ListType({'arcpath': 'test.tar.gz', 'delimiter': None, 'genfull': True}) __mydict = MyDict() __mydict.archives = ['tests/listtype/mytargz.tar.gz'] __mydict.delimiter = None __mydict.hashtype = '' __mydict.genfull = True __mydict.getallhashes = True __mydict.fulloutput = '' __mydict.confoutput = '' __mydict.listoutput = '' __mydict = brebis.listtype.ListType(__mydict) self.assertEqual(str(type(__mydict._ListType__bck)), "") def test_listtype_tree(self): '''test the listtype class with a tree archive''' __mydict = MyDict() __mydict.archives = ['tests/listtype/mytree'] __mydict.delimiter = None __mydict.hashtype = '' __mydict.genfull = True __mydict.getallhashes = True __mydict.fulloutput = '' __mydict.confoutput = '' __mydict.listoutput = '' __mydict = brebis.listtype.ListType(__mydict) self.assertEqual(str(type(__mydict._ListType__bck)), "") def test_listtype_zip(self): '''test the listtype class with a zip archive''' __mydict = MyDict() __mydict.archives = ['tests/listtype/myzip.zip'] __mydict.delimiter = None __mydict.hashtype = '' __mydict.genfull = True __mydict.getallhashes = True __mydict.fulloutput = '' __mydict.confoutput = '' __mydict.listoutput = '' __mydict = brebis.listtype.ListType(__mydict) self.assertEqual(str(type(__mydict._ListType__bck)), "") def test_listtype_gz(self): '''test the listtype class with a gzip archive''' __mydict = MyDict() __mydict.archives = ['tests/listtype/mygzip.gz'] __mydict.delimiter = None __mydict.hashtype = '' __mydict.genfull = True __mydict.getallhashes = True __mydict.fulloutput = '' __mydict.confoutput = '' __mydict.listoutput = '' __mydict = brebis.listtype.ListType(__mydict) self.assertEqual(str(type(__mydict._ListType__bck)), "") def test_listtype_bz2(self): '''test the listtype class with a bzip2 archive''' __mydict = MyDict() __mydict.archives = ['tests/listtype/mybz2.bz2'] __mydict.delimiter = None __mydict.hashtype = '' __mydict.genfull = True __mydict.getallhashes = True __mydict.fulloutput = '' __mydict.confoutput = '' __mydict.listoutput = '' __mydict = brebis.listtype.ListType(__mydict) self.assertEqual(str(type(__mydict._ListType__bck)), "") def test_listtype_xz(self): '''test the listtype class with a lzma archive''' __mydict = MyDict() __mydict.archives = ['tests/listtype/mylzma.xz'] __mydict.delimiter = None __mydict.hashtype = '' __mydict.genfull = True __mydict.getallhashes = True __mydict.fulloutput = '' __mydict.confoutput = '' __mydict.listoutput = '' __mydict = brebis.listtype.ListType(__mydict) self.assertEqual(str(type(__mydict._ListType__bck)), "") ################################################################ # # End of the unit tests # ################################################################ @classmethod def tearDownClass(TestApp): '''clean after the tests''' _logfile = TESTLOG os.remove(_logfile) if __name__ == '__main__': unittest.main()