diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 59ac8301a..d723a6923 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -2,7 +2,6 @@ from gevent import monkey as gevent_monkey from monkey_island.cc.arg_parser import parse_cli_args from monkey_island.config_file_parser import load_island_config_from_file -from monkey_island.setup.island_config_options import IslandConfigOptions gevent_monkey.patch_all() @@ -12,7 +11,6 @@ from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E if "__main__" == __name__: island_args = parse_cli_args() - config_options = IslandConfigOptions() try: # This is here in order to catch EVERYTHING, some functions are being called on diff --git a/monkey/monkey_island/config_file_parser.py b/monkey/monkey_island/config_file_parser.py index c7aa4f146..830ae6720 100644 --- a/monkey/monkey_island/config_file_parser.py +++ b/monkey/monkey_island/config_file_parser.py @@ -7,7 +7,7 @@ from monkey_island.setup.island_config_options import IslandConfigOptions def load_island_config_from_file(server_config_path: str) -> IslandConfigOptions: config_contents = read_config_file(server_config_path) - return IslandConfigOptions.build_from_config_file_contents(config_contents) + return IslandConfigOptions(config_contents) def read_config_file(server_config_path: str) -> dict: diff --git a/monkey/monkey_island/setup/island_config_options.py b/monkey/monkey_island/setup/island_config_options.py index 134630178..bf1c06e1b 100644 --- a/monkey/monkey_island/setup/island_config_options.py +++ b/monkey/monkey_island/setup/island_config_options.py @@ -1,7 +1,5 @@ from __future__ import annotations -from dataclasses import dataclass - from monkey_island.cc.server_utils.consts import ( DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL, @@ -9,22 +7,12 @@ from monkey_island.cc.server_utils.consts import ( ) -@dataclass class IslandConfigOptions: - log_level: str = DEFAULT_LOG_LEVEL - data_dir: str = DEFAULT_DATA_DIR - start_mongodb: bool = DEFAULT_START_MONGO_DB + def __init__(self, config_contents: dict): + self.data_dir = config_contents.get("data_dir", DEFAULT_DATA_DIR) - @staticmethod - def build_from_config_file_contents(config_contents: dict) -> IslandConfigOptions: - config = IslandConfigOptions() - if "data_dir" in config_contents: - config.data_dir = config_contents["data_dir"] + self.log_level = config_contents.get("log_level", DEFAULT_LOG_LEVEL) - if "log_level" in config_contents: - config.log_level = config_contents["log_level"] - - if "mongodb" in config_contents and "start_mongodb" in config_contents["mongodb"]: - config.start_mongodb = config_contents["mongodb"]["start_mongodb"] - - return config + self.start_mongodb = config_contents.get( + "mongodb", {"start_mongodb": DEFAULT_START_MONGO_DB} + ).get("start_mongodb", DEFAULT_START_MONGO_DB) diff --git a/monkey/tests/unit_tests/monkey_island/setup/test_island_config_options.py b/monkey/tests/unit_tests/monkey_island/setup/test_island_config_options.py new file mode 100644 index 000000000..564df30a0 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/setup/test_island_config_options.py @@ -0,0 +1,20 @@ +from monkey_island.cc.server_utils.consts import DEFAULT_LOG_LEVEL, DEFAULT_START_MONGO_DB +from monkey_island.setup.island_config_options import IslandConfigOptions + +MOCKED_CONFIG_FILE_CONTENTS_STANDARD = {"data_dir": "/tmp", "mongodb": {"start_mongodb": False}} + +MOCKED_CONFIG_FILE_CONTENTS_NO_STARTMONGO = {"data_dir": "/tmp", "mongodb": {}} + + +def test_island_config_options__standard(): + options = IslandConfigOptions(MOCKED_CONFIG_FILE_CONTENTS_STANDARD) + assert not options.start_mongodb + assert options.data_dir == "/tmp" + assert options.log_level == DEFAULT_LOG_LEVEL + + +def test_island_config_options__no_starmongo(): + options = IslandConfigOptions(MOCKED_CONFIG_FILE_CONTENTS_NO_STARTMONGO) + assert options.start_mongodb == DEFAULT_START_MONGO_DB + assert options.data_dir == "/tmp" + assert options.log_level == DEFAULT_LOG_LEVEL