Skip to content

Commit

Permalink
tst: logs of previous bot runs survived until the next bot run
Browse files Browse the repository at this point in the history
strip nullbytes from first line of bot log
truncate log_stream before initializing bot instance
ignore rare errors when the logs of the previous bot run appears in the logs of the current bot run
  • Loading branch information
wagner-intevation committed May 15, 2023
1 parent c3314f8 commit 44ecc22
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions intelmq/lib/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ def prepare_bot(self, parameters={}, destination_queues=None, prepare_source_que
new=self.mocked_config):
with mock.patch('intelmq.lib.utils.log', self.get_mocked_logger(self.logger)):
with mock.patch('intelmq.lib.utils.get_global_settings', mocked_get_global_settings):
"""
Since Bot.__del__ method calls Bot.stop, log messages of the previous bot run like:
"Processed/Forwarded X messages since last logging."
"Bot stopped"
appear in the log_stream at this point. So we clean the log before calling the bot, so that the tests in run_bot succeed.
"""
self.log_stream.truncate(0)
self.bot = self.bot_reference(self.bot_id)
self.bot._Bot__stats_cache = None

Expand Down Expand Up @@ -359,10 +366,10 @@ def run_bot(self, iterations: int = 1, error_on_pipeline: bool = False,
try:
self.assertLoglineMatches(0, BOT_INIT_REGEX.format(self.bot_name,
self.bot_id), "INFO")
except AssertionError:
if version_info <= (3, 8):
# In Python 3.7, the logging of the previous bot run can end up in the logging of the next run, resulting in line 0 being:
# "Processed 1 messages since last logging." (written at shutdown of that bot)
except AssertionError as exc:
# In some obscure but rate instances the logging of the previous bot run can end up in the logging of the next run, resulting in line 0 being:
# "Processed/Forwarded 1 messages since last logging." (written at shutdown of that previous bot run)
if 'since last logging' in exc.args[0]:
pass
else:
raise
Expand Down Expand Up @@ -497,7 +504,7 @@ def assertLoglineMatches(self, line_no: int, pattern: str, levelname: str = "ERR

self.assertIsNotNone(self.loglines)
logline = self.loglines[line_no]
fields = utils.parse_logline(logline)
fields = utils.parse_logline(logline.strip('\x00'))

self.assertEqual(self.bot_id, fields["bot_id"],
"bot_id {!r} didn't match {!r}."
Expand Down

0 comments on commit 44ecc22

Please sign in to comment.