From de7865aa219f4da8d7359be073374d70af0264cb Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 11 May 2021 13:25:22 -0400 Subject: [PATCH] island: Add tests for default server config values --- monkey/monkey_island.py | 2 +- monkey/monkey_island/config_loader.py | 20 ++++++++++++------- .../tests/monkey_island/test_config_loader.py | 19 +++++++++++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 51f2e1a11..5363ac5de 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -18,7 +18,7 @@ if "__main__" == __name__: try: server_config_path = os.path.expanduser(island_args.server_config) - config = config_loader.load_server_config(server_config_path) + config = config_loader.load_server_config_from_file(server_config_path) setup_logging(config["data_dir"], config["log_level"]) diff --git a/monkey/monkey_island/config_loader.py b/monkey/monkey_island/config_loader.py index 357cacbb8..5cdb149b7 100644 --- a/monkey/monkey_island/config_loader.py +++ b/monkey/monkey_island/config_loader.py @@ -6,15 +6,21 @@ from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR DEFAULT_LOG_LEVEL = "INFO" -def load_server_config(server_config_path): +def load_server_config_from_file(server_config_path): with open(server_config_path, "r") as f: config_content = f.read() config = json.loads(config_content) - config["data_dir"] = os.path.abspath( - os.path.expanduser( - os.path.expandvars(config["data_dir"] if "data_dir" in config else DEFAULT_DATA_DIR) - ) - ) - config["log_level"] = config["log_level"] if "log_level" in config else DEFAULT_LOG_LEVEL + add_default_values_to_config(config) return config + + +def add_default_values_to_config(config): + config["data_dir"] = os.path.abspath( + os.path.expanduser( + os.path.expandvars(config["data_dir"] if "data_dir" in config else DEFAULT_DATA_DIR) + ) + ) + config["log_level"] = config["log_level"] if "log_level" in config else DEFAULT_LOG_LEVEL + + return config diff --git a/monkey/tests/monkey_island/test_config_loader.py b/monkey/tests/monkey_island/test_config_loader.py index f65bfe197..20c330f6a 100644 --- a/monkey/tests/monkey_island/test_config_loader.py +++ b/monkey/tests/monkey_island/test_config_loader.py @@ -1,10 +1,27 @@ import os from monkey_island import config_loader +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR def test_load_server_config_from_file(test_server_config, mock_home_env): - config = config_loader.load_server_config(test_server_config) + config = config_loader.load_server_config_from_file(test_server_config) assert config["data_dir"] == os.path.join(mock_home_env, ".monkey_island") assert config["log_level"] == "NOTICE" + + +def test_default_log_level(): + test_config = {} + config = config_loader.add_default_values_to_config(test_config) + + assert "log_level" in config + assert config["log_level"] == "INFO" + + +def test_default_data_dir(mock_home_env): + test_config = {} + config = config_loader.add_default_values_to_config(test_config) + + assert "data_dir" in config + assert config["data_dir"] == DEFAULT_DATA_DIR