def testReportToAFile(self): reportfileFullPath = "reportfile.txt" fh = open('aconfig','w') fh.write('analyticsnotification = '+ reportfileFullPath +'\n') fh.write('analyticsgaptime = 0.1\n') fh.close() properties = Property('aconfig') properties.parse_properties() self.assertTrue(properties.is_key('analyticsnotification')) log = Log('out.log') arrayLog = [log] resume = reporting.Resume(arrayLog) resume.setAnalyticsGapNotification(0.1) resume.notification_type(reportfileFullPath) fh = open('out.log') lines = fh.readlines() fh.close() logcolors = LogColors() msg = Message(logcolors) time.sleep(0.1) for line in lines: msg.parse(line, log) resume.update(msg, log) fh = open(reportfileFullPath) reportlength = len(fh.readlines()) fh.close() os.remove(reportfileFullPath) self.assertEquals(22, reportlength) os.remove('aconfig')
def testShouldExecuteIfTargetMessage(self): logcolor = LogColors() logfile = 'anylog' log = Log(logfile) fh = open(CONFIG, 'w') fh.write("executor = echo ' %s %s '\n") fh.close() trace = "this is an info log trace" trigger = ['echo', trace, logfile] properties = Property(CONFIG) properties.parse_properties() message = Message(logcolor, target = 'trace') executor_mock = self.mocker.mock() executor_mock._build_trigger(trace, logfile) self.mocker.result(trigger) executor_mock.started self.mocker.result(True) landing_mock = self.mocker.mock() landing_mock.landing(trigger) self.mocker.result(True) executor_mock.trigger_executor self.mocker.result(landing_mock) self.mocker.replay() message.parse(trace, log) self.assertTrue(message.isATarget()) notifications.Executor.notify.im_func(executor_mock, message, log)
def testMarkedTARGETOverMarkableLevel(self): configfile = CONFIG logfile = "/any/path/out.log" trace = "this is a FATAL targeted log trace" fh = open(configfile, 'w') fh.write("targets "+logfile+"=targeted\n") fh.close() properties = Property(configfile) properties.parse_properties() sys.stdout = Writer() logcolors = LogColors() termcolors = TermColorCodes() notifier = notifications.CornerMark(0.02) anylog = Log(logfile, properties) message = Message(logcolors, properties = properties) termcols = os.popen("tput cols") ttcols = termcols.readline() termcols.close() ttcols = int(ttcols) padding = ttcols - len(notifier.MARK) output = padding * " " + termcolors.oncyanemph + notifier.MARK +\ termcolors.reset message.parse(trace, anylog) notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testShouldRaiseIfExecutorNotProvided(self): fh = open(CONFIG, 'w') fh.write('anything = ls -l\n') fh.close() properties = Property(CONFIG) properties.parse_properties() self.assertRaises(Exception, notifications.Executor, properties)
def testgetOverridePauseModeLevels(self): pauseMode = modes.PauseMode() properties = Property(self.configfile) properties.parse_properties() pauseMode.parse_config(properties) for key,value in self.overridenLevelPauses.iteritems(): key = key.split('pause')[1] self.assertEqual(value,pauseMode.getPause(key))
def testshouldReturnFalseMailNotSetup(self): self.__setupAConfig() properties = Property('aconfig') properties.parse_properties() defaults = getDefaults() defaults['properties'] = properties logtailer = LogTailer(defaults) self.assertEqual(False,logtailer.mailIsSetup())
def testShouldProvideNotifyMethod(self): fh = open(CONFIG, 'w') fh.write('executor = ls -l\n') fh.close() properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) self.assertTrue(hasattr(executor, 'notify'))
def testShouldGetInactivityNotificationTypeifInConfigFile(self): fh = open('config.txt','w') fh.write('inactivitynotification = mail\n') fh.close() property = Property('config.txt') property.parse_properties() notifier = notifications.Inactivity(1,property) self.assertEqual('mail',notifier.getNotificationType()) os.remove('config.txt')
def testFullTriggerFalseBasedOnConfig(self): fh = open(CONFIG, 'w') fh.write('executor = ls -l\n') fh.close() properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) self.assertEqual(False, executor.full_trigger_active) executor.stop()
def testShouldHaveUsernameandAtLeastOneHostnameSetUp(self): self.__setUpConfigFile() properties = Property(self.configfile) properties.parse_properties() logging.debug(properties.get_keys()) defaults = self.__getDefaults() defaults['properties'] = properties logtailer = SSHLogTailer(defaults) self.assertTrue(logtailer.sanityCheck())
def testShouldReadExecutorFromConfigFile(self): fh = open(CONFIG, 'w') fh.write('executor = ls -l\n') fh.close() properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) self.assertEquals(['ls', '-l'], executor.executable) executor.stop()
def testIfParametersNotProvidedShouldExit(self): fh = open('wrongconfigfile','w') fh.write('anything = anything\n') fh.close() properties = Property('wrongconfigfile') properties.parse_properties() defaults = self.__getDefaults() defaults['properties'] = properties logtailer = SSHLogTailer(defaults) self.assertFalse(logtailer.sanityCheck())
def testItShouldhaveBuildADictWithAllParamsIfAllParametersOk(self): self.__setUpConfigFile() properties = Property(self.configfile) properties.parse_properties() logging.debug(properties.get_keys()) defaults = self.__getDefaults() defaults['properties'] = properties logtailer = SSHLogTailer(defaults) logtailer.sanityCheck() self.assertEquals(3,len(logtailer.hostnames.keys())) self.assertEquals('username',logtailer.hostnames['hostname0']['username'])
def __createAConfigWithProperties(self): fh = open('config.txt','w') regexes = "log$, ^2009-10-12 got something here$ | red, yellow" allregex = re.compile('|'.join(regexes.split(','))) targetsline = "targets /var/log/messages = "+regexes+"\n" fh.write(targetsline) fh.write("/var/log/messages = green\n") fh.close() property = Property('config.txt') property.parse_properties() return property
def testcontainsOwnTargetLog(self): self.configfh = open('anotherconfig.txt','w') key = "targets /var/log/messages" value = "$2009-08-09 anything, ^regex2" targetline = key+"="+value+"\n" self.configfh.write(targetline) self.configfh.close() property = Property('anotherconfig.txt') property.parse_properties() self.assertEqual(value,property.get_value(key)) os.remove('anotherconfig.txt')
def testshouldHaveItsOwnColor(self): fh = open('config','w') fh.write(self.logname+'='+'green\n') fh.close() properties = Property('config') properties.parse_properties() log = Log(self.logname,properties) log.openLog() self.assertTrue(log.ownOutputColor) log.closeLog() os.remove(self.config)
def testshouldBuildCommandTailBasedOnHostnamesDict(self): self.__setUpConfigFile() properties = Property(self.configfile) properties.parse_properties() logging.debug(properties.get_keys()) defaults = self.__getDefaults() defaults['properties'] = properties logtailer = SSHLogTailer(defaults) logtailer.sanityCheck() command = "tail -F /var/log/anylog555 /var/log/anylog1" logtailer.createCommands() self.assertEquals(command,logtailer.hostnames['hostname0']['command'])
def testIfMailNotificationTypeAlreadyAvailablebyMailShouldSetItUp(self): mailMocker = mox.Mox() mail = mailMocker.CreateMock(notifications.Mail) fh = open('config.txt','w') fh.write('inactivitynotification = mail\n') fh.close() property = Property('config.txt') property.parse_properties() notifier = notifications.Inactivity(1,property) if notifier.getNotificationType() == 'mail': notifier.setMailNotification(mail) else: self.fail('should be notifier with mail Notification') os.remove('config.txt')
def testTargetsNoColors(self): fh = open('config.txt','w') regexes = "log$; ^2009-10-12 got something here$" targetsline = "targets /var/log/messages = "+regexes+"\n" fh.write(targetsline) fh.close() property = Property('config.txt') property.parse_properties() self.assertEqual(regexes, property.get_value('targets /var/log/messages')) log = Log('/var/log/messages', property) color = log.logTargetColor.get(re.compile('log$')) self.assertFalse(color) os.remove('config.txt')
def testShouldContinueIfExecutorFails(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') fh = open(CONFIG, 'w') fh.write('executor = anycommand\n') fh.close() trace = "this is a critical log trace" properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) message.parse(trace, log) executor.notify(message, log) time.sleep(0.0002) executor.stop()
def testResumeBuilderWithAnalyticsFile(self): sys.stdout = Writer() reportfile = 'reportfile.txt' configfile = 'aconfig' fh = open(configfile, 'w') fh.write('analyticsnotification = ' + reportfile + '\n') fh.close() properties = Property(configfile) properties.parse_properties() defaults = getDefaults() defaults['properties'] = properties logtailer = LogTailer(defaults) resumeObj = logtailer.resumeBuilder() self.assertTrue(isinstance(resumeObj, reporting.Resume)) self.assertEquals('file', resumeObj.getNotificationType()) self.assertEquals(reportfile, resumeObj.report_file)
def testshouldHaveitsOwnTargetSchemesIfProvidedInConfigFile(self): fh = open('config.txt','w') regexesColors = ("log$ : yellow, on_cyan; ^2009-10-12 got something " "here$ : black, on_cyan") targetsline = "targets /var/log/messages = "+regexesColors+"\n" fh.write(targetsline) fh.close() property = Property('config.txt') property.parse_properties() self.assertEqual(regexesColors, property.get_value('targets /var/log/messages')) log = Log('/var/log/messages', property) logTargetsColors = {re.compile('log$'): 'yellow, on_cyan', re.compile("^2009-10-12 got something here$") : 'black, on_cyan'} self.assertEqual(logTargetsColors, log.logTargetColor) os.remove('config.txt')
def testShouldNotExecuteIfLevelNotInPullers(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') fh = open(CONFIG, 'w') fh.write('executor = anything %s %s\n') fh.close() trace = "this is an info log trace" properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) message.parse(trace, log) executor.notify(message, log) time.sleep(0.0002) executor.stop() self.assertFalse(sys.stdout.captured)
def testShouldNotifyWithNoFullTrigger(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') logpath = log.path fh = open(CONFIG, 'w') fh.write('executor = echo\n') fh.close() trace = "this is a fatal log trace" properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) message.parse(trace, log) trigger = executor._build_trigger(trace, logpath) self.assertEqual(['echo'], trigger) executor.notify(message, log) executor.stop()
def testShouldContinueTailingIfExecutableTakesLongTime(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') fh = open(CONFIG, 'w') fh.write('executor = ' + EXECUTABLE +'\n') fh.close() trace = "this is an error log trace" properties = Property(CONFIG) properties.parse_properties() executor = notifications.Executor(properties) message.parse(trace, log) start = time.time() executor.notify(message, log) finished = time.time() ellapsed = start - finished time.sleep(0.0002) executor.stop() # executable.py sleeps for three seconds self.assertTrue(ellapsed < 0.1)
def testShouldNotifyWithFullTrigger(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') fh = open(CONFIG, 'w') fh.write('executor = ls -l %s %s\n') fh.close() trace = "this is a FATAL log trace" trigger = ['ls', '-l', trace, log.path ] properties = Property(CONFIG) properties.parse_properties() os_mock = self.mocker.replace('subprocess') os_mock.call(' '.join(trigger), shell = True) self.mocker.result(True) self.mocker.replay() # we just verify the trigger gets # called in the tearDown executor = notifications.Executor(properties) message.parse(trace, log) executor.notify(message, log) time.sleep(0.0002) executor.stop()
def testShouldReportaLogOwnTarget(self): logfile = "/any/path/outtarget.log" configfile = "aconfig.txt" logcolors = LogColors() fh = open(configfile, 'w') fh.write("targets "+logfile+" = should\n") fh.close() properties = Property(configfile) properties.parse_properties() mylog = Log(logfile, properties) optional_params = (None, True, logfile) self.assertEqual(optional_params, (mylog.ownOutputColor, mylog.patTarget, mylog.path)) arraylogs = [mylog] resume = reporting.Resume(arraylogs) message = Message(logcolors, properties = properties) logtrace = "log trace info target should be reported" message.parse(logtrace, mylog) resume.update(message, mylog) outLogReport = resume.logsReport[mylog.path] numofTargets = 1 gotnumTargets = outLogReport['TARGET'] self.assertEquals(numofTargets, gotnumTargets)
def testShouldSetupMailNotificationIfAnalyticsNotificationIsSetup(self): fh = open('aconfig','w') fh.write('analyticsnotification = mail\n') fh.write('analyticsgaptime = 3600\n') fh.close() properties = Property('aconfig') properties.parse_properties() self.assertTrue(properties.is_key('analyticsnotification')) arrayLog = [Log('out.log')] resume = reporting.Resume(arrayLog) mailactionmocker = mox.Mox() mailaction = mailactionmocker.CreateMock(notifications.Mail) if properties.get_value('analyticsnotification') == 'mail': resume.setMailNotification(mailaction) self.assertEquals('mail',resume.getNotificationType()) gaptime = properties.get_value('analyticsgaptime') if gaptime: resume.setAnalyticsGapNotification(gaptime) self.assertEquals(3600,int(resume.getGapNotificationTime())) os.remove('aconfig')
def testparse_properties(self): property = Property(self.configfile) property.parse_properties() configPropertyKeys = property.get_keys().sort() # my colorconfigs keys are already in lowercase self.assertEqual(self.configKeys,configPropertyKeys)
def testshouldReturnNoneifKeyNotFound(self): property = Property(self.configfile) property.parse_properties() key = 'hi' self.assertFalse(property.get_value(key))