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_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 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 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 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_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 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 testShouldReportOtherNotifications(self): inactivity_mock = self.mocker.mock() inactivity_mock.alerting_msg self.mocker.count(1, None) self.mocker.result('Inactivity action detected') inactivity_mock.alerted self.mocker.count(1, None) self.mocker.result(True) def populate_log(): fh = open('out2.log', 'w') someLogTracesForLog = [ 'something here', 'something there', 'something somewhere' ] for line in someLogTracesForLog: fh.write(line + '\n') fh.close() populate_log() log = Log('out2.log') arrayLogs = [log] logcolors = LogColors() message = Message(logcolors) resume = reporting.Resume(arrayLogs) resume.add_notifier(inactivity_mock) self.mocker.replay() for anylog in arrayLogs: self.readAndUpdateLines(anylog, message, resume) outlogReport = resume.logsReport[log.path] expectedOthersReport = 'Inactivity action detected' gotLogTrace = outlogReport['OTHERS'][0].split('=>> ')[1] self.assertEquals(expectedOthersReport, gotLogTrace)
def test_flush_report_aftergaptime_daemonized(self): log = Log('out.log') arrayLogs = [log] logcolors = LogColors() message = Message(logcolors) resume = reporting.Resume(arrayLogs) resume.is_daemonized = True resume.gapTime = 0.05 for anylog in arrayLogs: self.readAndUpdateLines(anylog, message, resume) def overgap(): return resume.gapTime + 10 resume.timer.inactivityEllapsed = overgap resume.update(message, log) expected = { 'TARGET': 0, 'TRACE': 0, 'DEBUG': 0, 'INFO': 0, 'WARN': 0, 'OTHERS': [], 'ERROR': [], 'FATAL': [], 'CRITICAL': [] } outlogReport = resume.logsReport[log.path] self.assertEquals(expected, outlogReport)
def test_unregister_method_for_shutdown(self): logcolors = LogColors() log = Log('anylog') poster = notifications.Poster(PropertiesStub(), http_conn=HttpConnStub) poster.registered_logs[log] = {'id': 5, 'logserver': 'anyserver'} body = poster.unregister(log) self.assertTrue(body)
def testNotMarkMarkedNotMark(self): trace = "INFO this is an info trace" 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.assertFalse(sys.stdout.captured) def belowgap(): return 0 notifier.timer.corner_mark_ellapsed = belowgap trace = "FATAL there could be an error in the application" message.parse(trace, anylog) notifier.notify(message, anylog) self.assertTrue(sys.stdout.captured) self.assertEquals(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_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 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 testShouldNotifyWithNoFullTrigger(self): logcolor = LogColors() message = Message(logcolor) log = Log('anylog') logpath = log.path command = 'echo' properties = PropertiesStub(command) trace = "this is a fatal log trace" executor = notifications.Executor(properties, trigger_executor=TriggerExecutorStub) trigger = executor._build_trigger(trace, logpath) self.assertEqual(['echo'], trigger)
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 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 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 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 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 testReportResumeForTwoDifferentLogs(self): log = Log('out.log') log2 = Log('out2.log') arrayLogs = [log, log2] fh = log.openLog() logcolors = LogColors() message = Message(logcolors) resume = reporting.Resume(arrayLogs) for anylog in arrayLogs: self.readAndUpdateLines(anylog, message, resume) outlogReport = resume.logsReport[log.path] expectedOutLogErrorReport = 'error> not so wrong' gotLogTrace = outlogReport['ERROR'][0].split('=>> ')[1] self.assertEquals(expectedOutLogErrorReport, gotLogTrace)
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 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 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))
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 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 test_register_to_server_first_time(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() log = Log('anylog') self.mocker.replay() require_server() poster = notifications.Poster(properties) body = poster.register(log) self.assertFalse(body)
def test_unregister_method_for_shutdown(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() log = Log('anylog') self.mocker.replay() require_server() poster = notifications.Poster(properties) poster.registered_logs[log] = {'id': 5, 'logserver': 'anyserver'} body = poster.unregister(log) self.assertTrue(body)
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 testLogColorsParseConfig(self): logcolors = LogColors() logcolors.parse_config(PropertiesMock()) self.assertFalse(hasattr(logcolors, 'one')) self.assertFalse(hasattr(logcolors, 'two'))