From 97642f45dc3d460c1c1de0cd2feeec0eeaa519ff Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 25 Oct 2021 11:13:06 +0300 Subject: [PATCH] Island: if the data directory is empty no need to consider backing it up --- monkey/monkey_island/cc/setup/data_dir.py | 11 ++++++++++- .../monkey_island/cc/setup/test_data_dir.py | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/setup/data_dir.py b/monkey/monkey_island/cc/setup/data_dir.py index c728dca04..e2697d5d5 100644 --- a/monkey/monkey_island/cc/setup/data_dir.py +++ b/monkey/monkey_island/cc/setup/data_dir.py @@ -1,4 +1,5 @@ import logging +import os import shutil from pathlib import Path @@ -15,7 +16,7 @@ class IncompatibleDataDirectory(Exception): def setup_data_dir(data_dir_path: Path) -> None: logger.info(f"Setting up data directory at {data_dir_path}.") - if data_dir_path.exists() and _data_dir_version_mismatch_exists(data_dir_path): + if _is_data_dir_old(data_dir_path): logger.info("Version in data directory does not match the Island's version.") _handle_old_data_directory(data_dir_path) create_secure_directory(str(data_dir_path)) @@ -23,6 +24,14 @@ def setup_data_dir(data_dir_path: Path) -> None: logger.info("Data directory set up.") +def _is_data_dir_old(data_dir_path: Path) -> bool: + dir_exists = data_dir_path.exists() + if not dir_exists or not os.listdir(data_dir_path): + return False + + return _data_dir_version_mismatch_exists(data_dir_path) + + def _handle_old_data_directory(data_dir_path: Path) -> None: should_delete_data_directory = _prompt_user_to_delete_data_directory(data_dir_path) if should_delete_data_directory: diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_data_dir.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_data_dir.py index fe60d227d..34912ef50 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_data_dir.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_data_dir.py @@ -77,3 +77,10 @@ def test_data_dir_setup_not_needed(temp_data_dir_path, temp_version_file_path): setup_data_dir(temp_data_dir_path) assert temp_version_file_path.read_text() == current_version assert bogus_file_path.is_file() + + +def test_empty_data_dir(temp_data_dir_path, temp_version_file_path): + temp_data_dir_path.mkdir() + + setup_data_dir(temp_data_dir_path) + assert temp_version_file_path.read_text() == current_version