From 2ce535e34cb9f4917c76e8683df6c242646ebefe Mon Sep 17 00:00:00 2001 From: Kit Rady <119824796+kitrady@users.noreply.github.com> Date: Sun, 28 Jul 2024 10:46:45 -0500 Subject: [PATCH] Improving analyzers' report format (#72) * improving report format of format analyzer * improving report format of startup header analyzer --- src/alogamous/format_analyzer.py | 7 +++++-- src/alogamous/startup_header_analyzer.py | 2 +- tests/format_analyzer_test.py | 24 +++++++++++++++++++++++- tests/startup_header_analyzer_test.py | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/alogamous/format_analyzer.py b/src/alogamous/format_analyzer.py index 1b70b66..ed99a8b 100644 --- a/src/alogamous/format_analyzer.py +++ b/src/alogamous/format_analyzer.py @@ -18,5 +18,8 @@ def read_log_line(self, line): self.startup_block = False def report(self, out_stream): - out_stream.write("\nLines that do not conform to log format:\n- ") - out_stream.write("\n- ".join(self.un_formated_lines)) + if self.un_formated_lines: + out_stream.write("\nLines that do not conform to log format:\n- ") + out_stream.write("\n- ".join(self.un_formated_lines)) + else: + out_stream.write("All lines conform to log line format") diff --git a/src/alogamous/startup_header_analyzer.py b/src/alogamous/startup_header_analyzer.py index 7036124..bef9a4b 100644 --- a/src/alogamous/startup_header_analyzer.py +++ b/src/alogamous/startup_header_analyzer.py @@ -18,5 +18,5 @@ def read_log_line(self, line): self.startup_block = False def report(self, out_stream): - out_stream.write("\nLines that are part of the startup header:\n- ") + out_stream.write("\nLines that are part of the startup header(s):\n- ") out_stream.write("\n- ".join(self.startup_lines)) diff --git a/tests/format_analyzer_test.py b/tests/format_analyzer_test.py index ceb9f40..85d9e66 100644 --- a/tests/format_analyzer_test.py +++ b/tests/format_analyzer_test.py @@ -3,7 +3,7 @@ from alogamous import format_analyzer, log_line_parser -def test_format_analyzer(): +def test_format_analyzer_with_bad_lines(): parser = log_line_parser.LogLineParser( ["datetime", "source", "level", "message"], " - ", "====================================================" ) @@ -31,3 +31,25 @@ def test_format_analyzer(): - Hello I am a bad log line - Hello I am a bad log line""" ) + + +def test_format_analyzer_with_good_lines(): + parser = log_line_parser.LogLineParser( + ["datetime", "source", "level", "message"], " - ", "====================================================" + ) + format_checker = format_analyzer.FormatAnalyzer(parser) + in_stream = """==================================================== + STARTING Tracking service + Start time: 2024-06-20 09:00:00.001550+00:00 + Version: 2729a + Command line: ['.venv/bin/python3', '-m', 'app.tracking_service', '--market', 'US', '--version', '2729a'] + ==================================================== + 2024-06-20 11:00:17,983 - root - INFO - Adding subscription for pid None + 2024-06-20 11:00:18,115 - root - INFO - Initialized Influx DB Client to host + 2024-06-20 11:00:18,115 - root - INFO - Scheduling Error Handler in 150.0 seconds + 2024-06-20 11:00:18,116 - root - INFO - prometheus client http server running""" + out_stream = StringIO() + for line in in_stream.splitlines(): + format_checker.read_log_line(line) + format_checker.report(out_stream) + assert out_stream.getvalue() == "All lines conform to log line format" diff --git a/tests/startup_header_analyzer_test.py b/tests/startup_header_analyzer_test.py index f2468f0..a3782c8 100644 --- a/tests/startup_header_analyzer_test.py +++ b/tests/startup_header_analyzer_test.py @@ -24,7 +24,7 @@ def test_report(): assert ( out_stream.getvalue() == """ -Lines that are part of the startup header: +Lines that are part of the startup header(s): - STARTING Tracking service - Start time: 2024-06-20 09:00:00.001550+00:00 - Version: 2729a