forked from p15670423/monkey
Monkey island: change the methods in data_dir.py and version_file_setup.py to handle Path rather than str.
This commit is contained in:
parent
e77ed9769b
commit
15949a9ed5
|
@ -22,7 +22,7 @@ def _setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, s
|
|||
# TODO refactor like in https://github.com/guardicore/monkey/pull/1528 because
|
||||
# there's absolutely no reason to be exposed to IslandConfigOptions extraction logic
|
||||
# if you want to modify data directory related code.
|
||||
setup_data_dir(str(config.data_dir))
|
||||
setup_data_dir(config.data_dir)
|
||||
|
||||
return config, server_config_path
|
||||
|
||||
|
@ -34,7 +34,7 @@ def _setup_default_config() -> Tuple[IslandConfigOptions, str]:
|
|||
# TODO refactor like in https://github.com/guardicore/monkey/pull/1528 because
|
||||
# there's absolutely no reason to be exposed to IslandConfigOptions extraction logic
|
||||
# if you want to modify data directory related code.
|
||||
setup_data_dir(str(default_data_dir))
|
||||
setup_data_dir(default_data_dir)
|
||||
|
||||
server_config_path = server_config_handler.create_default_server_config_file(default_data_dir)
|
||||
config = server_config_handler.load_server_config_from_file(server_config_path)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import logging
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from common.version import get_version
|
||||
from monkey_island.cc.server_utils.file_utils import create_secure_directory
|
||||
|
@ -12,15 +13,15 @@ from monkey_island.cc.setup.version_file_setup import (
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_data_dir(data_dir_path: str):
|
||||
def setup_data_dir(data_dir_path: Path):
|
||||
logger.info("Setting up data directory.")
|
||||
_reset_data_dir(data_dir_path)
|
||||
create_secure_directory(data_dir_path)
|
||||
create_secure_directory(str(data_dir_path))
|
||||
write_version(data_dir_path)
|
||||
logger.info("Data directory set up.")
|
||||
|
||||
|
||||
def _reset_data_dir(data_dir_path: str):
|
||||
def _reset_data_dir(data_dir_path: Path):
|
||||
try:
|
||||
data_dir_version = get_version_from_dir(data_dir_path)
|
||||
except FileNotFoundError:
|
||||
|
|
|
@ -7,13 +7,13 @@ from common.version import get_version
|
|||
_version_filename = "VERSION"
|
||||
|
||||
|
||||
def get_version_from_dir(dir_path: str) -> str:
|
||||
version_file_path = Path(dir_path, _version_filename)
|
||||
def get_version_from_dir(dir_path: Path) -> str:
|
||||
version_file_path = dir_path.joinpath(_version_filename)
|
||||
return version_file_path.read_text()
|
||||
|
||||
|
||||
def write_version(dir_path: str):
|
||||
version_file_path = Path(dir_path, _version_filename)
|
||||
def write_version(dir_path: Path):
|
||||
version_file_path = dir_path.joinpath(_version_filename)
|
||||
version_file_path.write_text(get_version())
|
||||
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ def mocked_version_file_path(mocked_data_dir_path: Path) -> Path:
|
|||
|
||||
def test_setup_data_dir(mocked_data_dir_path, mocked_version_file_path):
|
||||
data_dir_path = mocked_data_dir_path
|
||||
setup_data_dir(str(data_dir_path))
|
||||
setup_data_dir(data_dir_path)
|
||||
assert data_dir_path.is_dir()
|
||||
|
||||
version_file_path = mocked_version_file_path
|
||||
|
@ -43,7 +43,7 @@ def test_old_version_present(mocked_data_dir_path, mocked_version_file_path):
|
|||
bogus_file_path.touch()
|
||||
|
||||
# mock version
|
||||
setup_data_dir(str(mocked_data_dir_path))
|
||||
setup_data_dir(mocked_data_dir_path)
|
||||
|
||||
assert mocked_version_file_path.read_text() == current_version
|
||||
assert not bogus_file_path.is_file()
|
||||
|
@ -55,6 +55,6 @@ def test_data_dir_setup_not_needed(mocked_data_dir_path, mocked_version_file_pat
|
|||
bogus_file_path = mocked_data_dir_path.joinpath("test.txt")
|
||||
bogus_file_path.touch()
|
||||
|
||||
setup_data_dir(str(mocked_data_dir_path))
|
||||
setup_data_dir(mocked_data_dir_path)
|
||||
assert mocked_version_file_path.read_text() == current_version
|
||||
assert bogus_file_path.is_file()
|
||||
|
|
Loading…
Reference in New Issue