From 0ab20d558bc6d8f1681c0b22f7193cef997677fc Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 1 Jun 2021 03:33:34 -0400 Subject: [PATCH 1/4] island: Add unit test to verify '~' expanded in data_dir --- .../setup/test_island_config_options.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 index 9175bc274..240def907 100644 --- 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 @@ -1,3 +1,5 @@ +import os + from monkey_island.cc.server_utils.consts import ( DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL, @@ -37,3 +39,16 @@ def test_island_config_options__mongodb(): assert options.start_mongodb == DEFAULT_START_MONGO_DB options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO) assert options.start_mongodb == DEFAULT_START_MONGO_DB + + +def set_home_env(monkeypatch, tmpdir): + monkeypatch.setenv("HOME", str(tmpdir)) + + +def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + DATA_DIR_NAME = "test_data_dir" + + options = IslandConfigOptions({"data_dir": f"~/{DATA_DIR_NAME}"}) + + assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME) From c761dba030b38e8a92ecab529512ef6b90a34899 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 1 Jun 2021 03:35:33 -0400 Subject: [PATCH 2/4] island: Expand environment variables in data_dir --- monkey/monkey_island/setup/island_config_options.py | 5 +++-- .../monkey_island/setup/test_island_config_options.py | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/monkey/monkey_island/setup/island_config_options.py b/monkey/monkey_island/setup/island_config_options.py index 938c840a0..5ce62ba2e 100644 --- a/monkey/monkey_island/setup/island_config_options.py +++ b/monkey/monkey_island/setup/island_config_options.py @@ -11,8 +11,9 @@ from monkey_island.cc.server_utils.consts import ( class IslandConfigOptions: def __init__(self, config_contents: dict): - self.data_dir = os.path.expanduser(config_contents.get("data_dir", DEFAULT_DATA_DIR)) - + self.data_dir = os.path.expandvars( + os.path.expanduser(config_contents.get("data_dir", DEFAULT_DATA_DIR)) + ) self.log_level = config_contents.get("log_level", DEFAULT_LOG_LEVEL) self.start_mongodb = config_contents.get( 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 index 240def907..fb2a1eb8c 100644 --- 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 @@ -52,3 +52,12 @@ def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir): options = IslandConfigOptions({"data_dir": f"~/{DATA_DIR_NAME}"}) assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME) + + +def test_island_config_options__data_dir_expandvars(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + DATA_DIR_NAME = "test_data_dir" + + options = IslandConfigOptions({"data_dir": f"$HOME/{DATA_DIR_NAME}"}) + + assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME) From 2cd665ff112c7dc5c03dbdfef4d15bf70e53a492 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 1 Jun 2021 03:46:28 -0400 Subject: [PATCH 3/4] island: Refactor duplicate code in IslandConfigOptions data_dir tests --- .../setup/test_island_config_options.py | 62 +++++++++++-------- 1 file changed, 35 insertions(+), 27 deletions(-) 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 index fb2a1eb8c..e6d480611 100644 --- 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 @@ -18,11 +18,41 @@ TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED = {} TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO = {"mongodb": {}} -def test_island_config_options__data_dir(): - options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_SPECIFIED) - assert options.data_dir == "/tmp" - options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED) - assert options.data_dir == DEFAULT_DATA_DIR +def test_island_config_options__data_dir_specified(): + assert_island_config_options_data_dir_equals(TEST_CONFIG_FILE_CONTENTS_SPECIFIED, "/tmp") + + +def test_island_config_options__data_dir_uses_default(): + assert_island_config_options_data_dir_equals( + TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED, DEFAULT_DATA_DIR + ) + + +def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + DATA_DIR_NAME = "test_data_dir" + + assert_island_config_options_data_dir_equals( + {"data_dir": f"~/{DATA_DIR_NAME}"}, os.path.join(tmpdir, DATA_DIR_NAME) + ) + + +def test_island_config_options__data_dir_expandvars(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + DATA_DIR_NAME = "test_data_dir" + + assert_island_config_options_data_dir_equals( + {"data_dir": f"$HOME/{DATA_DIR_NAME}"}, os.path.join(tmpdir, DATA_DIR_NAME) + ) + + +def set_home_env(monkeypatch, tmpdir): + monkeypatch.setenv("HOME", str(tmpdir)) + + +def assert_island_config_options_data_dir_equals(config_file_contents, expected_data_dir): + options = IslandConfigOptions(config_file_contents) + assert options.data_dir == expected_data_dir def test_island_config_options__log_level(): @@ -39,25 +69,3 @@ def test_island_config_options__mongodb(): assert options.start_mongodb == DEFAULT_START_MONGO_DB options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO) assert options.start_mongodb == DEFAULT_START_MONGO_DB - - -def set_home_env(monkeypatch, tmpdir): - monkeypatch.setenv("HOME", str(tmpdir)) - - -def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) - DATA_DIR_NAME = "test_data_dir" - - options = IslandConfigOptions({"data_dir": f"~/{DATA_DIR_NAME}"}) - - assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME) - - -def test_island_config_options__data_dir_expandvars(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) - DATA_DIR_NAME = "test_data_dir" - - options = IslandConfigOptions({"data_dir": f"$HOME/{DATA_DIR_NAME}"}) - - assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME) From 09e7deb936f626bf1855119e40648dcf436aff5b Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 1 Jun 2021 03:59:07 -0400 Subject: [PATCH 4/4] island: Use os.path.join when creating test config contents dict --- .../monkey_island/setup/test_island_config_options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index e6d480611..35126a278 100644 --- 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 @@ -33,7 +33,7 @@ def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir): DATA_DIR_NAME = "test_data_dir" assert_island_config_options_data_dir_equals( - {"data_dir": f"~/{DATA_DIR_NAME}"}, os.path.join(tmpdir, DATA_DIR_NAME) + {"data_dir": os.path.join("~", DATA_DIR_NAME)}, os.path.join(tmpdir, DATA_DIR_NAME) ) @@ -42,7 +42,7 @@ def test_island_config_options__data_dir_expandvars(monkeypatch, tmpdir): DATA_DIR_NAME = "test_data_dir" assert_island_config_options_data_dir_equals( - {"data_dir": f"$HOME/{DATA_DIR_NAME}"}, os.path.join(tmpdir, DATA_DIR_NAME) + {"data_dir": os.path.join("$HOME", DATA_DIR_NAME)}, os.path.join(tmpdir, DATA_DIR_NAME) )