From 2621458b37e7543af7fa81a1c9b084f869c0cfb7 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 21 May 2021 11:49:33 +0300 Subject: [PATCH] Refactored tests to test config file parsing and IslandConfig param extraction --- .../server_configs/server_config_empty.json | 2 ++ ...onfig.json => server_config_init_only.json} | 0 .../cc/setup/test_setup_param_factory.py | 16 ---------------- .../tests/unit_tests/monkey_island/conftest.py | 9 +++++++-- .../monkey_island/test_config_file_parser.py | 18 +++++++++--------- 5 files changed, 18 insertions(+), 27 deletions(-) create mode 100644 monkey/tests/data_for_tests/server_configs/server_config_empty.json rename monkey/tests/data_for_tests/server_configs/{test_server_config.json => server_config_init_only.json} (100%) delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/setup/test_setup_param_factory.py diff --git a/monkey/tests/data_for_tests/server_configs/server_config_empty.json b/monkey/tests/data_for_tests/server_configs/server_config_empty.json new file mode 100644 index 000000000..2c63c0851 --- /dev/null +++ b/monkey/tests/data_for_tests/server_configs/server_config_empty.json @@ -0,0 +1,2 @@ +{ +} diff --git a/monkey/tests/data_for_tests/server_configs/test_server_config.json b/monkey/tests/data_for_tests/server_configs/server_config_init_only.json similarity index 100% rename from monkey/tests/data_for_tests/server_configs/test_server_config.json rename to monkey/tests/data_for_tests/server_configs/server_config_init_only.json diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_setup_param_factory.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_setup_param_factory.py deleted file mode 100644 index d8ea9692e..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_setup_param_factory.py +++ /dev/null @@ -1,16 +0,0 @@ -from monkey_island import config_file_parser -from monkey_island.cc.arg_parser import IslandArgs -from monkey_island.setup.setup_param_factory import SetupParamFactory - -MOCK_ISLAND_CMD_ARGS = IslandArgs(setup_only=True, server_config_path="/temp/test_path") - - -def test_setup_param_factory_build(monkeypatch, test_server_config): - config_contents = config_file_parser.load_server_config_from_file(test_server_config) - - setup_params = SetupParamFactory().build(MOCK_ISLAND_CMD_ARGS, config_contents) - assert setup_params.setup_only - assert setup_params.server_config_path == MOCK_ISLAND_CMD_ARGS.server_config_path - assert setup_params.start_mongodb - assert setup_params.log_level == "NOTICE" - assert setup_params.data_dir == "~/.monkey_island" diff --git a/monkey/tests/unit_tests/monkey_island/conftest.py b/monkey/tests/unit_tests/monkey_island/conftest.py index 4b7149595..4d0d48e1f 100644 --- a/monkey/tests/unit_tests/monkey_island/conftest.py +++ b/monkey/tests/unit_tests/monkey_island/conftest.py @@ -9,5 +9,10 @@ def server_configs_dir(data_for_tests_dir): @pytest.fixture(scope="module") -def test_server_config(server_configs_dir): - return os.path.join(server_configs_dir, "test_server_config.json") +def server_config_init_only(server_configs_dir): + return os.path.join(server_configs_dir, "server_config_init_only.json") + + +@pytest.fixture(scope="module") +def server_config_empty(server_configs_dir): + return os.path.join(server_configs_dir, "server_config_empty.json") diff --git a/monkey/tests/unit_tests/monkey_island/test_config_file_parser.py b/monkey/tests/unit_tests/monkey_island/test_config_file_parser.py index 8a0bb3fff..db377e6e7 100644 --- a/monkey/tests/unit_tests/monkey_island/test_config_file_parser.py +++ b/monkey/tests/unit_tests/monkey_island/test_config_file_parser.py @@ -1,16 +1,16 @@ from monkey_island import config_file_parser +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL -def test_load_server_config_from_file(test_server_config): - config = config_file_parser.load_server_config_from_file(test_server_config) +def test_load_server_config_from_file(server_config_init_only): + config = config_file_parser.load_island_config_from_file(server_config_init_only) - assert config["data_dir"] == "~/.monkey_island" - assert config["log_level"] == "NOTICE" + assert config.data_dir == "~/.monkey_island" + assert config.log_level == "NOTICE" -def test_load_server_config_from_file_default_path(monkeypatch, test_server_config): - monkeypatch.setattr(config_file_parser, "DEFAULT_SERVER_CONFIG_PATH", test_server_config) - config = config_file_parser.load_server_config_from_file("") +def test_load_server_config_from_file_empty_file(monkeypatch, server_config_empty): + config = config_file_parser.load_island_config_from_file(server_config_empty) - assert config["data_dir"] == "~/.monkey_island" - assert config["log_level"] == "NOTICE" + assert config.data_dir == DEFAULT_DATA_DIR + assert config.log_level == DEFAULT_LOG_LEVEL