forked from p15670423/monkey
Modify/add unit tests (test_island_logger.py)
This commit is contained in:
parent
e8c1c81edf
commit
c5ba48db53
|
@ -1,7 +1,8 @@
|
|||
import logging.config
|
||||
import os
|
||||
from copy import deepcopy
|
||||
from typing import Dict
|
||||
|
||||
ISLAND_LOG_FILENAME = "monkey_island.log"
|
||||
|
||||
LOGGER_CONFIG_DICT = {
|
||||
"version": 1,
|
||||
|
@ -13,15 +14,13 @@ LOGGER_CONFIG_DICT = {
|
|||
}
|
||||
},
|
||||
"handlers": {
|
||||
"console": {
|
||||
"console_handler": {
|
||||
"class": "logging.StreamHandler",
|
||||
"level": "DEBUG",
|
||||
"formatter": "simple",
|
||||
"stream": "ext://sys.stdout",
|
||||
},
|
||||
"info_file_handler": {
|
||||
"file_handler": {
|
||||
"class": "logging.handlers.RotatingFileHandler",
|
||||
"level": "INFO",
|
||||
"formatter": "simple",
|
||||
"filename": None, # set in setup_logging()
|
||||
"maxBytes": 10485760,
|
||||
|
@ -29,7 +28,10 @@ LOGGER_CONFIG_DICT = {
|
|||
"encoding": "utf8",
|
||||
},
|
||||
},
|
||||
"root": {"level": None, "handlers": ["console", "info_file_handler"]}, # set in setup_logging()
|
||||
"root": {
|
||||
"level": None, # set in setup_logging()
|
||||
"handlers": ["console_handler", "file_handler"],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,18 +45,9 @@ def setup_logging(data_dir_path, log_level):
|
|||
|
||||
logger_configuration = deepcopy(LOGGER_CONFIG_DICT)
|
||||
|
||||
logger_configuration["handlers"]["info_file_handler"]["filename"] = os.path.join(
|
||||
data_dir_path, "monkey_island.log"
|
||||
logger_configuration["handlers"]["file_handler"]["filename"] = os.path.join(
|
||||
data_dir_path, ISLAND_LOG_FILENAME
|
||||
)
|
||||
logger_configuration["root"]["level"] = log_level
|
||||
_expanduser_log_file_paths(logger_configuration)
|
||||
|
||||
logging.config.dictConfig(logger_configuration)
|
||||
|
||||
|
||||
def _expanduser_log_file_paths(config: Dict):
|
||||
handlers = config.get("handlers", {})
|
||||
|
||||
for handler_settings in handlers.values():
|
||||
if "filename" in handler_settings:
|
||||
handler_settings["filename"] = os.path.expanduser(handler_settings["filename"])
|
||||
|
|
|
@ -1,29 +1,38 @@
|
|||
import logging
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from monkey_island.cc.server_utils.island_logger import setup_logging
|
||||
from monkey_island.cc.server_utils.island_logger import ISLAND_LOG_FILENAME, setup_logging
|
||||
|
||||
|
||||
# TODO move into monkey/monkey_island/cc/test_common/fixtures after rebase/backmerge
|
||||
@pytest.fixture
|
||||
def mock_home_env(monkeypatch, tmpdir):
|
||||
monkeypatch.setenv("HOME", str(tmpdir))
|
||||
|
||||
|
||||
def test_expanduser_filename(mock_home_env, tmpdir):
|
||||
def test_setup_logging_log_level_debug(tmpdir):
|
||||
DATA_DIR = tmpdir
|
||||
INFO_LOG = os.path.join(DATA_DIR, "monkey_island.log")
|
||||
LOG_FILE = os.path.join(DATA_DIR, ISLAND_LOG_FILENAME)
|
||||
LOG_LEVEL = "DEBUG"
|
||||
TEST_STRING = "Hello, Monkey!"
|
||||
TEST_STRING = "Hello, Monkey! (Log level: debug)"
|
||||
|
||||
setup_logging(DATA_DIR, LOG_LEVEL)
|
||||
|
||||
logger = logging.getLogger("TestLogger")
|
||||
logger.info(TEST_STRING)
|
||||
logger.debug(TEST_STRING)
|
||||
|
||||
assert os.path.isfile(INFO_LOG)
|
||||
with open(INFO_LOG, "r") as f:
|
||||
assert os.path.isfile(LOG_FILE)
|
||||
with open(LOG_FILE, "r") as f:
|
||||
line = f.readline()
|
||||
assert TEST_STRING in line
|
||||
|
||||
|
||||
def test_setup_logging_log_level_info(tmpdir):
|
||||
DATA_DIR = tmpdir
|
||||
LOG_FILE = os.path.join(DATA_DIR, ISLAND_LOG_FILENAME)
|
||||
LOG_LEVEL = "INFO"
|
||||
TEST_STRING = "Hello, Monkey! (Log level: info)"
|
||||
|
||||
setup_logging(DATA_DIR, LOG_LEVEL)
|
||||
|
||||
logger = logging.getLogger("TestLogger")
|
||||
logger.debug(TEST_STRING)
|
||||
|
||||
assert os.path.isfile(LOG_FILE)
|
||||
with open(LOG_FILE, "r") as f:
|
||||
line = f.readline()
|
||||
assert TEST_STRING not in line
|
||||
|
|
Loading…
Reference in New Issue