island: Add tests for default server config values

This commit is contained in:
Mike Salvatore 2021-05-11 13:25:22 -04:00
parent 990244c3ac
commit de7865aa21
3 changed files with 32 additions and 9 deletions

View File

@ -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"])

View File

@ -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

View File

@ -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