From 583115c419148c90e80200c5b11cade6fc5793e1 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 2 Jun 2021 08:38:57 -0400 Subject: [PATCH] island: Reset logger handlers after each test The root logger does not get reset for each test that is run. Add an autouse fixture that resets the logger handlers after each test run so that handlers do not accumulate in the root logger. --- .../cc/server_utils/island_logger.py | 7 ++++++ .../cc/server_utils/test_island_logger.py | 25 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/monkey/monkey_island/cc/server_utils/island_logger.py b/monkey/monkey_island/cc/server_utils/island_logger.py index beb6df94f..9a8929e3a 100644 --- a/monkey/monkey_island/cc/server_utils/island_logger.py +++ b/monkey/monkey_island/cc/server_utils/island_logger.py @@ -48,3 +48,10 @@ def _add_console_handler(logger, formatter): ch.setFormatter(formatter) logger.addHandler(ch) + + +def reset_logger(): + logger = logging.getLogger() + + for handler in logger.handlers: + logger.removeHandler(handler) diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_island_logger.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_island_logger.py index 9f4e59af8..6061109d1 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_island_logger.py +++ b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_island_logger.py @@ -1,16 +1,25 @@ import logging import os -from monkey_island.cc.server_utils.island_logger import ISLAND_LOG_FILENAME, setup_logging +import pytest + +import monkey_island.cc.server_utils.island_logger as island_logger + + +@pytest.fixture(autouse=True) +def reset_logger(): + yield + + island_logger.reset_logger() def test_setup_logging_file_log_level_debug(tmpdir): DATA_DIR = tmpdir - LOG_FILE = os.path.join(DATA_DIR, ISLAND_LOG_FILENAME) + LOG_FILE = os.path.join(DATA_DIR, island_logger.ISLAND_LOG_FILENAME) LOG_LEVEL = "DEBUG" TEST_STRING = "Hello, Monkey! (File; Log level: debug)" - setup_logging(DATA_DIR, LOG_LEVEL) + island_logger.setup_logging(DATA_DIR, LOG_LEVEL) logger = logging.getLogger("TestLogger") logger.debug(TEST_STRING) @@ -23,11 +32,11 @@ def test_setup_logging_file_log_level_debug(tmpdir): def test_setup_logging_file_log_level_info(tmpdir): DATA_DIR = tmpdir - LOG_FILE = os.path.join(DATA_DIR, ISLAND_LOG_FILENAME) + LOG_FILE = os.path.join(DATA_DIR, island_logger.ISLAND_LOG_FILENAME) LOG_LEVEL = "INFO" TEST_STRING = "Hello, Monkey! (File; Log level: info)" - setup_logging(DATA_DIR, LOG_LEVEL) + island_logger.setup_logging(DATA_DIR, LOG_LEVEL) logger = logging.getLogger("TestLogger") logger.debug(TEST_STRING) @@ -43,7 +52,7 @@ def test_setup_logging_console_log_level_debug(capsys, tmpdir): LOG_LEVEL = "DEBUG" TEST_STRING = "Hello, Monkey! (Console; Log level: debug)" - setup_logging(DATA_DIR, LOG_LEVEL) + island_logger.setup_logging(DATA_DIR, LOG_LEVEL) logger = logging.getLogger("TestLogger") logger.debug(TEST_STRING) @@ -57,7 +66,7 @@ def test_setup_logging_console_log_level_info(capsys, tmpdir): LOG_LEVEL = "INFO" TEST_STRING = "Hello, Monkey! (Console; Log level: info)" - setup_logging(DATA_DIR, LOG_LEVEL) + island_logger.setup_logging(DATA_DIR, LOG_LEVEL) logger = logging.getLogger("TestLogger") logger.debug(TEST_STRING) @@ -71,7 +80,7 @@ def test_setup_logging_console_log_level_lower_case(capsys, tmpdir): LOG_LEVEL = "debug" TEST_STRING = "Hello, Monkey! (Console; Log level: debug)" - setup_logging(DATA_DIR, LOG_LEVEL) + island_logger.setup_logging(DATA_DIR, LOG_LEVEL) logger = logging.getLogger("TestLogger") logger.debug(TEST_STRING)