def testStoredFiles(self): Environment.cleanupTestFolder() targetFolder = Environment.targetFolder() # Create some files in target folder if not os.path.exists(targetFolder): os.makedirs(targetFolder) filename = os.path.join(targetFolder, 'myName_20180304_1200.zip') filename2 = os.path.join(targetFolder, 'otherName_20180304_1200.zip') with open(filename, 'w') as f: f.write('Simple test file for BRIT') with open(filename2, 'w') as f: f.write('Simple test file for BRIT') newConfig = Configuration() newConfig.definitions.append( Definition('Name1', 'dir', Environment.examplesFolder(), './C/temp1')) newTask = Task('myName', ['Name1'], targetFolder=Environment.targetFolder()) newConfig.addTask(newTask) fileNames = [] for fName in newTask.storedFiles(): fileNames.append(fName) self.assert_(len(fileNames) == 1, "Only one file should be found") self.assert_(fileNames[0] == filename, "Wrong file found")
def test_prepareTargetFolder(self): Environment.cleanupTestFolder() targetFolder = os.path.join(Environment.targetFolder(), 'sub') newTask = Task('myName', ['Name1'], targetFolder=targetFolder) newTask._prepareTargetFolder() self.assert_(os.path.exists(targetFolder), 'Targtefolder not created') Environment.cleanupTestFolder()
def testStrategy(self): # Let's prepare a configuration newConfig = Configuration() newConfig.retainStrategies.append(RetainStrategy('myStrategy')) newTask = Task('myName', [], 'myStrategy') newConfig.tasks.append(newTask) newTask.configuration = newConfig strat = newTask.strategy() self.assert_(strat <> None, 'No Strategy returned') self.assert_(strat.name == 'myStrategy', 'Wrong Strategy returned')
def test_getTargetFolder(self): newTask = Task('myName', [], 'myStrategy') self.assert_(newTask._getTargetFolder() == 'c:/temp/Backup', 'fall back folder not OK') newConfig = Configuration() newConfig.backupDirectory = 'c:/temp/Backup2' newConfig.addTask(newTask) self.assert_(newTask._getTargetFolder() == 'c:/temp/Backup2', 'folder from configuration not taken') newConfig.backupDirectory = 'c:/temp/Backup3' self.assert_(newTask._getTargetFolder() == 'c:/temp/Backup3', 'folder from task not taken')
def testCopy(self): oldTask = Task('myName', ['config1', 'config2'], 'strategy', 'C:/Backup', True) newTask = oldTask.copy() self.assert_(newTask <> None, 'No task create') self.assert_(newTask.name == 'myName', 'Name not OK') self.assert_(newTask.definitionNames[0] == 'config1', 'Config 1 not OK') self.assert_(newTask.definitionNames[1] == 'config2', 'Config 2 not OK') self.assert_(newTask.strategyName == 'strategy', 'Strategy not OK') self.assert_(newTask.targetFolder == 'C:/Backup', 'targetFolder not OK') self.assert_(newTask.isActive, 'isActive not OK')
def testAddTask(self): newConfig = Configuration() newTask = Task('TestTest1', ['config1', 'config2']) newConfig.addTask(newTask) self.assert_(newTask in newConfig.tasks, 'Task not added to list') self.assert_(newTask.configuration == newConfig, 'Configuration not set')
def testToJson(self): newTask = Task('myName', ['config1', 'config2'], 'strategy', isActive=True) json = newTask.toJson() self.assert_(json <> None, 'No json created') self.assert_(json['class_name'] == 'Task', 'class_name not OK') self.assert_(json['name'] == 'myName', 'name not OK') self.assert_(newTask.definitionNames[0] == 'config1', 'Config 1 not OK') self.assert_(newTask.definitionNames[1] == 'config2', 'Config 2 not OK') self.assert_(not ('configuration' in json), 'Configuration not removes from json') self.assert_(json['strategyName'] == 'strategy', 'Strategy not OK') self.assert_(json['isActive'], 'isActive not OK')
def testWriteToReadFrom(self): newConfig = Configuration() newConfig.addTask(Task('TestTest1', ['config1', 'config2'])) newConfig.addTask(Task('TestTest2', ['config1', 'config2'])) newConfig.definitions.append( Definition('name1', 'dir', 'C:/temp1', './C/temp1')) newConfig.definitions.append( Definition('name2', 'dir', 'C:/temp2', './C/temp2')) newConfig.definitions.append( Definition('name3', 'dir', 'C:/temp3', './C/temp3')) newConfig.retainStrategies.append(RetainStrategy('reatinDefault')) newConfig.backupDirectory = 'c:/temp/backup' Environment.cleanupTestFolder() Environment.setupExamplesDir() testFile = os.path.join(Environment.examplesFolder(), 'test_config.brit') newConfig.writeTo(testFile) self.assert_(os.path.isfile(testFile), 'No config file written') newConfig2 = Configuration.readFrom(testFile) self.assert_(newConfig2 <> None, 'No configuration has been read from Json') self.assert_( len(newConfig2.tasks) == 2, 'Length of list of tasks not OK') self.assert_(newConfig2.tasks[0].name == 'TestTest1', 'Name of first task not OK') self.assert_(newConfig2.tasks[0].configuration == newConfig2, 'Configuration of task not set') self.assert_( len(newConfig2.definitions) == 3, 'Length of list of Definitions not OK') self.assert_( len(newConfig2.retainStrategies) == 1, 'Length of list of RetainStrategies not OK') self.assert_(newConfig2.backupDirectory == 'c:/temp/backup', 'backupDirectory not OK') Environment.cleanupTestFolder()
def testCreateWithoutStrategy(self): newTask = Task('myName', ['config1', 'config2']) self.assert_(newTask <> None, 'No task create') self.assert_(newTask.name == 'myName', 'Name not OK') self.assert_(newTask.definitionNames[0] == 'config1', 'Config 1 not OK') self.assert_(newTask.definitionNames[1] == 'config2', 'Config 2 not OK') self.assert_(newTask.strategyName == '', 'Strategy not OK') self.assert_(newTask.targetFolder == '', 'targetFolder not OK') self.assert_(not newTask.isActive, 'isActive not OK')
def testRun(self): Environment.cleanupTestFolder() Environment.setupExamplesDir() newConfig = Configuration() newConfig.definitions.append( Definition('Name1', 'dir', Environment.examplesFolder(), './C/temp1')) newTask = Task('myName', ['Name1'], targetFolder=Environment.targetFolder()) newConfig.addTask(newTask) filename = newTask.run() self.assert_(os.path.exists(Environment.targetFolder()), 'Target folder not created') self.assert_(filename <> None and filename <> '', 'No file name returned on run') Environment.cleanupTestFolder()
def testDefinitions(self): # Let's prepare a configuration newConfig = Configuration() newConfig.definitions.append( Definition('name1', 'dir', 'C:/temp1', './C/temp1')) newConfig.definitions.append( Definition('name2', 'dir', 'C:/temp2', './C/temp2')) newConfig.definitions.append( Definition('name3', 'dir', 'C:/temp3', './C/temp3')) # And now the task newTask = Task('myName', ['name1', 'name3', 'badConf']) newConfig.tasks.append(newTask) newTask.configuration = newConfig # Now the actual test defs = newTask.defintions() self.assert_(defs <> None, 'No defs returned') self.assert_(len(defs) == 2, 'Nb of found defs not OK') self.assert_(defs[0].name == 'name1', 'Name of first def not OK') self.assert_(defs[1].name == 'name3', 'Name of second def not OK')
def testFromJson(self): newTask = Task.fromJson({ "class_name": "Task", "name": "myName", "strategyName": "strategy", "definitionNames": ["config1", "config2"], "targetFolder": "C:/Backup", "isActive": True }) self.assert_(newTask <> None, 'No task created') self.assert_(newTask.name == 'myName', 'Name not OK') self.assert_(newTask.definitionNames[0] == 'config1', 'Config 1 not OK') self.assert_(newTask.definitionNames[1] == 'config2', 'Config 2 not OK') self.assert_(newTask.strategyName == 'strategy', 'Strategy not OK') self.assert_(newTask.targetFolder == 'C:/Backup', 'targetFolder not OK') self.assert_(newTask.isActive, 'isActive not OK')
def test_doArchiveFileFail(self): filename = Environment.setupExamplesDir() zipTargetFile = 'dir/example' config = Configuration() config.backupDirectory = Environment.targetFolder() definition = Definition('name1', 'file', filename, zipTargetFile) config.definitions.append(definition) task = Task('myName', ['name1']) config.addTask(task) task._prepareTargetFolder() targetfile = task._getTargetFilename() archiveFile = task._prepareArchive(targetfile) # Now the actual test # I open and lock the example file to provoke an I/O error file_ = open(filename, 'a') if os.name == 'nt': msvcrt.locking(file_.fileno(), msvcrt.LK_LOCK, 0x7fffffff) with LogCapture(level=logging.WARNING) as l: task._doArchiveFile(archiveFile, filename, zipTargetFile) l.check(( 'root', 'WARNING', 'File could not be read: C:\\ProgramData\\brit_test\\examples\\example.txt Reason: I/O error(13): Permission denied' )) archiveFile.close() # Unlock and close the file if os.name == 'nt': msvcrt.locking(file_.fileno(), msvcrt.LK_UNLCK, 0x7fffffff) file_.close() self.assert_(not self.fileIsZipped(zipTargetFile, targetfile), 'File placed in zip file even though it was locked') Environment.cleanupTestFolder()
def test_doArchiveFile(self): filename = Environment.setupExamplesDir() zipTargetFile = 'dir/example' config = Configuration() config.backupDirectory = Environment.targetFolder() definition = Definition('name1', 'file', filename, zipTargetFile) config.definitions.append(definition) task = Task('myName', ['name1']) config.addTask(task) task._prepareTargetFolder() targetfile = task._getTargetFilename() archiveFile = task._prepareArchive(targetfile) # Now the actual test task._doArchiveFile(archiveFile, filename, zipTargetFile) archiveFile.close() self.assert_(self.fileIsZipped(zipTargetFile, targetfile), 'File not placed in zip file') Environment.cleanupTestFolder()