def test_hostnameinconfig(self): logfile = 'out.log' fh = open(logfile, 'w') someLogTraces = ['FATAL> something went wrong', 'ERROR> not so wrong', 'WARN> be careful', 'DEBUG> looking behind the scenes', 'INFO> the app is running'] for line in someLogTraces: fh.write(line + '\n') fh.close() logcolors = LogColors() # using default colors termcolors = TermColorCodes() target = None notifier = notifications.Print(PropertiesMock()) message = Message(logcolors, target) log = Log(logfile) log.openLog() sys.stdout = MemoryWriter() hostname = socket.gethostname() for _ in range(len(someLogTraces)): line = log.readLine() line = line.rstrip() level = line.split('>') message.parse(line, log) output = (hostname + ': ' + logcolors.getLevelColor(level[0]) + line + termcolors.reset) notifier.notify(message, log) self.assertTrue(output in sys.stdout.captured) line = log.readLine() self.assertEqual('', line) message.parse(line, log) self.assertFalse(notifier.notify(message, log))
def test_shouldPost_if_alertable(self): logcolors = LogColors() logtrace = 'this is an error log trace' log = Log('anylog') message = Message(logcolors) message.parse(logtrace, log) poster = notifications.Poster(PropertiesStub(), http_conn=HttpConnStub) body = poster.notify(message, log) self.assertTrue(body)
def test_execute_if_targetMessage(self): logcolors = LogColors() logtrace = 'this is a target log trace' log = Log('anylog') message = Message(logcolors, target='trace') message.parse(logtrace, log) poster = notifications.Poster(PropertiesStub(), http_conn=HttpConnStub) poster.registered_logs[log] = {'id': 1, 'logserver': "anything"} body = poster.notify(message, log) self.assertTrue(body)
def test_printandshoot_notalertable(self): log_trace = 'this is an info log trace' log = Log('anylog') logcolors = LogColors() caller = SubProcessStubPopen() printandshoot = notifications.PrintShot(PropertiesStub(), caller=caller) message = Message(logcolors) message.parse(log_trace, log) printandshoot.notify(message, log) self.assertFalse(sys.stdout.contains(SubProcessStubPopen.msg))
def test_execute_if_targetMessage(self): logcolors = LogColors() logtrace = 'this is a target log trace' log = Log('anylog') message = Message(logcolors, target='trace') message.parse(logtrace, log) poster = notifications.Poster(PropertiesStub(), http_conn=HttpConnStub) poster.registered_logs[log] = {'id' : 1, 'logserver' : "anything"} body = poster.notify(message, log) self.assertTrue(body)
def testnoNotification(self): pattern = re.compile(r'hi, this line to be notified') trace = "info this is just a log trace" notifier = notifications.Filter(pattern) sys.stdout = MemoryWriter() logcolors = LogColors() message = Message(logcolors) anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) # assert is empty self.assertFalse(sys.stdout.captured)
def test_trace_level(self): level = 'TRACE' trace = "TRACE level for finer informational log traces than DEBUG" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testShouldNotExecuteIfLevelNotInPullers(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') command = 'anything %s %s' trace = "this is an info log trace" properties = PropertiesStub(command) executor = notifications.Executor(properties, trigger_executor=TriggerExecutorStub) message.parse(trace, log) executor.notify(message, log) len_captured_lines = len(sys.stdout.captured) self.assertEqual(len_captured_lines, 0)
def testshouldNotColorizeifLevelKeyInaWord(self): # Testing boundary regex as for suggestion of # Carlo Bertoldi trace = "this is a logtrace where someinfoword could be found" sys.stdout = MemoryWriter() logcolors = LogColors() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) self.assertEqual(trace, sys.stdout.captured[0]) self.assertEqual('', message.messageLevel)
def test_printandshoot(self): log_trace = 'this is a fatal log trace' log = Log('anylog') logcolors = LogColors() caller = SubProcessStubPopen() printandshoot = notifications.PrintShot(PropertiesStub(), caller=caller) message = Message(logcolors) message.parse(log_trace, log) printandshoot.notify(message, log) colorized_logtrace = '\x1b[31mthis is a fatal log trace\x1b[0m' self.assertEqual(colorized_logtrace, sys.stdout.captured[0]) # in [1] has '\n', and [2] should have the result self.assertEqual(SubProcessStub.msg, sys.stdout.captured[2])
def testshouldFailColorizeWithBackground(self): trace = "FATAL there could be an error in the application" level = 'WARN' sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() logcolors.parse_config(PropertiesBackGround()) message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset notifier.notify(message, anylog) self.assertNotEqual(output, sys.stdout.captured[0])
def test_printandshoot_throws(self): log_trace = 'this is a very fatal log trace' log = Log('anylog') logcolors = LogColors() caller = SubProcessStubRaise() printandshoot = notifications.PrintShot(PropertiesStub(), caller=caller) message = Message(logcolors) message.parse(log_trace, log) try: printandshoot.notify(message, log) self.fail() except Exception, err: self.assertEqual(str(err), SubProcessStubRaise.msg)
def testShouldColorizeWarningLevelAsWell(self): '''test that *warning* keyword gets colorized as well''' level = 'WARNING' trace = "WARNING there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def test_notification(self): pattern = re.compile(r'this line will not be notified') trace = "info hi, but this line will be" level = "INFO" notifier = notifications.IgnoreAction(pattern) sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) output = logcolors.getLevelColor(level)+trace+termcolors.reset self.assertEqual(output, sys.stdout.captured[0])
def testWillMarkForSpecifiedTime(self): trace = "FATAL there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.CornerMark(10) anylog = Log('out.log') message.parse(trace, anylog) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.backgroundemph + notifier.MARK +\ termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testnotify(self): pattern = re.compile(r'hi, this line to be notified') trace = "info hi, this line to be notified" level = "INFO" notifier = notifications.Filter(pattern) sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) output = logcolors.getLevelColor(level) + trace + termcolors.reset self.assertEqual(output, sys.stdout.captured[0])
def testshouldColorizefirstLevelFoundignoringSecondinSameTrace(self): # Test for fix 5 # Should give priority to FATAL in next trace level = 'FATAL' trace = "FATAL there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') message.parse(trace, anylog) output = (logcolors.getLevelColor(level) + trace + termcolors.reset) notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0])
def testShouldExecuteIfTargetMessage(self): logcolor = LogColors() logfile = 'anylog' log = Log(logfile) command = "echo %s %s" trace = "this is an info log trace" trigger = ['echo', trace, logfile] properties = PropertiesStub(command) message = Message(logcolor, target='trace') message.parse(trace, log) executor = notifications.Executor(properties, trigger_executor=TriggerExecutorStub) executor.notify(message, log) self.assertEqual(TriggerExecutorStub.execute_msg, sys.stdout.captured[0])
def testMarkedTARGETOverMarkableLevel(self): logfile = "/any/path/out.log" trace = "this is a FATAL targeted log trace" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() notifier = notifications.CornerMark(0.02) anylog = Log(logfile, PropertiesStub()) message = Message(logcolors, properties=PropertiesStub) padding = self.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 tailer(self): '''Stdout multicolor tailer''' message = Message(self.logcolors, self.target, self.properties) anylog = Log('anylog') for hostname in self.hostnames.keys(): command = self.hostnames[hostname]['command'] self.logger.debug("command [%s] to be executed in host [%s]" % (command, hostname)) self.hostnameChannels[hostname]['channel'].exec_command(command) try: lasthostnameChanged = "" while True: for hostname in self.hostnames.keys(): sshChannel = self.hostnameChannels[hostname]['channel'] rr, _, _ = select.select([sshChannel], [], [], 0.0) if len(rr) > 0: lines = sshChannel.recv(4096).split('\n') if hostname != lasthostnameChanged: self.__hostnameChangedHeader(hostname) for line in lines: message.parse(line, anylog) self.actions.notify(message, anylog) lasthostnameChanged = hostname self._wait_for(1) except: print "\nfinishing ..." for hostname in self.hostnames.keys(): sshChannel = self.hostnameChannels[hostname]['channel'] sshclient = self.hostnameChannels[hostname]['client'] command = self.hostnames[hostname]['command'] procidCommand = 'pgrep -f -x ' + '\"' + command + '\"' self.logger.debug("procid command [%s]" % procidCommand) sshChannel.close() stdin, stdout, stderr = sshclient.exec_command(procidCommand) res = stdout.readlines() if res: procid = res[0].rstrip() self.logger.debug("procid [%s]" % procid) # is process still alive after closing channel? if procid: # kill it killprocid = 'kill -9 ' + procid stdin, stdout, stderr = sshclient.exec_command( killprocid) #sshChannel.shutdown(2) sshclient.close() print "Ended log4tailer, because colors are fun" sys.exit()
def tailer(self): '''Stdout multicolor tailer''' message = Message(self.logcolors,self.target,self.properties) anylog = Log('anylog') for hostname in self.hostnames.keys(): command = self.hostnames[hostname]['command'] self.logger.debug("command [%s] to be executed in host [%s]" % ( command,hostname)) self.hostnameChannels[hostname]['channel'].exec_command(command) try: lasthostnameChanged = "" while True: for hostname in self.hostnames.keys(): sshChannel = self.hostnameChannels[hostname]['channel'] rr, _, _ = select.select([sshChannel],[],[],0.0) if len(rr)>0: lines = sshChannel.recv(4096).split('\n') if hostname != lasthostnameChanged: self.__hostnameChangedHeader(hostname) for line in lines: message.parse(line, anylog) self.actions.notify(message, anylog) lasthostnameChanged = hostname self._wait_for(1) except: print "\nfinishing ..." for hostname in self.hostnames.keys(): sshChannel = self.hostnameChannels[hostname]['channel'] sshclient = self.hostnameChannels[hostname]['client'] command = self.hostnames[hostname]['command'] procidCommand = 'pgrep -f -x '+'\"'+command+'\"' self.logger.debug("procid command [%s]" % procidCommand) sshChannel.close() stdin,stdout,stderr = sshclient.exec_command(procidCommand) res = stdout.readlines() if res: procid = res[0].rstrip() self.logger.debug("procid [%s]" % procid) # is process still alive after closing channel? if procid: # kill it killprocid = 'kill -9 '+procid stdin,stdout,stderr = sshclient.exec_command(killprocid) #sshChannel.shutdown(2) sshclient.close() print "Ended log4tailer, because colors are fun" sys.exit()
def testMarkedFATALMarkedWARNING(self): trace = "FATAL this is a fatal trace" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.CornerMark(0.02) anylog = Log('out.log') message.parse(trace, anylog) notifier.notify(message, anylog) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.onyellowemph + notifier.MARK +\ termcolors.reset trace = "WARN this is just a warn" message.parse(trace, anylog) notifier.notify(message, anylog) self.assertEquals(output, sys.stdout.captured[2])
def testWillMarkforSpecifiedTimeandNotAfterwards(self): trace = "FATAL there could be an error in the application" sys.stdout = MemoryWriter() logcolors = LogColors() termcolors = TermColorCodes() message = Message(logcolors) notifier = notifications.CornerMark(0.01) anylog = Log('out.log') message.parse(trace, anylog) padding = self.ttcols - len(notifier.MARK) output = padding * " " + termcolors.backgroundemph + notifier.MARK +\ termcolors.reset notifier.notify(message, anylog) self.assertEqual(output, sys.stdout.captured[0]) trace = "INFO this is an info trace" sys.stdout.flush() notifier.timer.corner_mark_ellapsed = overgap message.parse(trace, anylog) notifier.notify(message, anylog) self.assertFalse(sys.stdout.captured)
def test_not_execute_if_not_alertable_Level(self): properties = self.mocker.mock() properties.get_value('server_url') self.mocker.result('localhost') properties.get_value('server_port') self.mocker.result(8000) properties.get_value('server_service_uri') self.mocker.result('/') properties.get_value('server_service_register_uri') self.mocker.result('/register') properties.get_value('server_service_unregister_uri') self.mocker.result('/unregister') logcolors = LogColors() logtrace = 'this is an info log trace' log = Log('anylog') message = Message(logcolors, target='anything') message.parse(logtrace, log) self.mocker.replay() poster = notifications.Poster(properties) poster.registered_logs[log] = True body = poster.notify(message, log) self.assertFalse(body)
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) gaptime = 0.1 resume.setAnalyticsGapNotification(gaptime) resume.notification_type(reportfileFullPath) fh = open('out.log') lines = fh.readlines() fh.close() logcolors = LogColors() msg = Message(logcolors) def overgap(): return gaptime + 1 def belowgap(): return gaptime - 0.01 resume.timer.inactivityEllapsed = overgap for line in lines: msg.parse(line, log) resume.update(msg, log) resume.timer.inactivityEllapsed = belowgap fh = open(reportfileFullPath) reportlength = len(fh.readlines()) fh.close() os.remove(reportfileFullPath) self.assertEquals(24, reportlength) os.remove('aconfig')
def test_shouldPost_if_alertable(self): properties = self.mocker.mock() properties.get_value('server_url') self.mocker.result('localhost') properties.get_value('server_port') self.mocker.result(8000) properties.get_value('server_service_uri') self.mocker.result('/') properties.get_value('server_service_register_uri') self.mocker.result('/register') properties.get_value('server_service_unregister_uri') self.mocker.result('/unregister') logcolors = LogColors() logtrace = 'this is an error log trace' log = Log('anylog') message = Message(logcolors) message.parse(logtrace, log) self.mocker.replay() poster = notifications.Poster(properties) require_server() body = poster.notify(message, log) self.assertTrue(body)
def test_not_execute_if_not_alertable_Level(self): properties = self.mocker.mock() properties.get_value('server_url') self.mocker.result('localhost') properties.get_value('server_port') self.mocker.result(8000) properties.get_value('server_service_uri') self.mocker.result('/') properties.get_value('server_service_register_uri') self.mocker.result('/register') properties.get_value('server_service_unregister_uri') self.mocker.result('/unregister') logcolors = LogColors() logtrace = 'this is an info log trace' log = Log('anylog') message = Message(logcolors, target = 'anything') message.parse(logtrace, log) self.mocker.replay() poster = notifications.Poster(properties) poster.registered_logs[log] = True body = poster.notify(message, log) self.assertFalse(body)
def testshouldColorizeMultilineLogTraces(self): trace = 'FATAL> something went wrong\nin here as well' trace0, trace1 = trace.split('\n') level = 'FATAL' termcolors = TermColorCodes() # now assert trace0 and trace1 are in FATAL level sys.stdout = MemoryWriter() logcolors = LogColors() message = Message(logcolors) notifier = notifications.Print() anylog = Log('out.log') expectedLogTrace0 = logcolors.getLevelColor(level) + \ trace0 + termcolors.reset expectedLogTrace1 = logcolors.getLevelColor(level) + \ trace1 + termcolors.reset message.parse(trace0, anylog) notifier.notify(message, anylog) self.assertEqual(expectedLogTrace0, sys.stdout.captured[0]) self.assertEqual('FATAL', message.messageLevel) message.parse(trace1, anylog) notifier.notify(message, anylog) self.assertEqual(expectedLogTrace1, sys.stdout.captured[2]) self.assertEqual('FATAL', message.messageLevel)
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 testMessage(self): logcolors = LogColors() # using default colors termcolors = TermColorCodes() target = None notifier = notifications.Print() message = Message(logcolors, target) log = Log(self.logfile) log.openLog() sys.stdout = MemoryWriter() #testing Colors with default pauseModes for count in range(len(self.someLogTraces)): line = log.readLine() line = line.rstrip() level = line.split('>') message.parse(line, log) output = (logcolors.getLevelColor(level[0]) + line + termcolors.reset) notifier.notify(message, log) self.assertTrue(output in sys.stdout.captured) line = log.readLine() self.assertEqual('', line) message.parse(line, log) self.assertFalse(notifier.notify(message, log))