forked from p15670423/monkey
Merge pull request #1191 from guardicore/test-data-dir-expand-user-env
Test data dir expand user env
This commit is contained in:
commit
4e6bb21942
|
@ -11,8 +11,9 @@ from monkey_island.cc.server_utils.consts import (
|
||||||
|
|
||||||
class IslandConfigOptions:
|
class IslandConfigOptions:
|
||||||
def __init__(self, config_contents: dict):
|
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.log_level = config_contents.get("log_level", DEFAULT_LOG_LEVEL)
|
||||||
|
|
||||||
self.start_mongodb = config_contents.get(
|
self.start_mongodb = config_contents.get(
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from monkey_island.cc.server_utils.consts import (
|
from monkey_island.cc.server_utils.consts import (
|
||||||
DEFAULT_DATA_DIR,
|
DEFAULT_DATA_DIR,
|
||||||
DEFAULT_LOG_LEVEL,
|
DEFAULT_LOG_LEVEL,
|
||||||
|
@ -16,11 +18,41 @@ TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED = {}
|
||||||
TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO = {"mongodb": {}}
|
TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO = {"mongodb": {}}
|
||||||
|
|
||||||
|
|
||||||
def test_island_config_options__data_dir():
|
def test_island_config_options__data_dir_specified():
|
||||||
options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_SPECIFIED)
|
assert_island_config_options_data_dir_equals(TEST_CONFIG_FILE_CONTENTS_SPECIFIED, "/tmp")
|
||||||
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_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": os.path.join("~", 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": os.path.join("$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():
|
def test_island_config_options__log_level():
|
||||||
|
|
Loading…
Reference in New Issue