diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 5bf773c6b..52dd70c9b 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -75,8 +75,13 @@ class VersionUpdateService: def get_deployment_from_file(file_path: str) -> str: deployment = "unknown" - with open(file_path, "r") as deployment_info_file: - deployment_info = json.load(deployment_info_file) - deployment = deployment_info["deployment"] + try: + with open(file_path, "r") as deployment_info_file: + deployment_info = json.load(deployment_info_file) + deployment = deployment_info["deployment"] + except Exception as ex: + logger.debug( + f"Couldn't get deployment info from {str(file_path)}. Exception: {str(ex)}." + ) return deployment diff --git a/monkey/tests/data_for_tests/deployment_flawed b/monkey/tests/data_for_tests/deployment_flawed new file mode 100644 index 000000000..6563189c5 --- /dev/null +++ b/monkey/tests/data_for_tests/deployment_flawed @@ -0,0 +1 @@ +develop diff --git a/monkey/tests/data_for_tests/deployment_key_error.json b/monkey/tests/data_for_tests/deployment_key_error.json new file mode 100644 index 000000000..acba72b2d --- /dev/null +++ b/monkey/tests/data_for_tests/deployment_key_error.json @@ -0,0 +1,3 @@ +{ + "tnemyolped": "develop" +} diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py index 84cb45535..7b0067c0b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py @@ -10,7 +10,42 @@ def deployment_info_file_path(data_for_tests_dir): return os.path.join(data_for_tests_dir, "deployment.json") -def test_get_deployment_field(deployment_info_file_path, monkeypatch): +@pytest.fixture +def ghost_file(): + return "ghost_file" + + +@pytest.fixture +def key_error_deployment_info_file_path(data_for_tests_dir): + return os.path.join(data_for_tests_dir, "deployment_key_error.json") + + +@pytest.fixture +def flawed_deployment_info_file_path(data_for_tests_dir): + return os.path.join(data_for_tests_dir, "deployment_flawed") + + +def test_get_deployment_field_from_file(deployment_info_file_path, monkeypatch): monkeypatch.setattr(os.path, "join", lambda *args: deployment_info_file_path) deployment = VersionUpdateService().get_deployment_from_file(deployment_info_file_path) assert deployment == "develop" + + +def test_get_deployment_field_from_nonexistent_file(ghost_file, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: ghost_file) + deployment = VersionUpdateService().get_deployment_from_file(ghost_file) + assert deployment == "unknown" + + +def test_get_deployment_field_key_error(key_error_deployment_info_file_path, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: key_error_deployment_info_file_path) + deployment = VersionUpdateService().get_deployment_from_file( + key_error_deployment_info_file_path + ) + assert deployment == "unknown" + + +def test_get_deployment_field_from_flawed_json_file(flawed_deployment_info_file_path, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: flawed_deployment_info_file_path) + deployment = VersionUpdateService().get_deployment_from_file(flawed_deployment_info_file_path) + assert deployment == "unknown"