forked from p15670423/monkey
Improved IslandConfigOptions readability by removing defaults and added a unit test for constructor
This commit is contained in:
parent
0f49579148
commit
2afdfa297b
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue