Island: Switch get_deployment_from_file() to use Paths

This commit is contained in:
Mike Salvatore 2021-09-14 10:19:14 -04:00
parent 38011f20b5
commit 412aefab3e
2 changed files with 15 additions and 25 deletions

View File

@ -1,6 +1,6 @@
import json import json
import logging import logging
import os from pathlib import Path
import requests import requests
@ -8,6 +8,8 @@ from common.utils.exceptions import VersionServerConnectionError
from common.version import get_version from common.version import get_version
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
DEPLOYMENT_FILE_PATH = Path(MONKEY_ISLAND_ABS_PATH) / "cc" / "deployment.json"
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -41,10 +43,9 @@ class VersionUpdateService:
Checks if newer monkey version is available Checks if newer monkey version is available
:return: False if not, version in string format ('1.6.2') otherwise :return: False if not, version in string format ('1.6.2') otherwise
""" """
deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json")
url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % (
VersionUpdateService.get_deployment_from_file(deployment_info_file_path), VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH),
get_version(), get_version(),
) )
@ -64,15 +65,13 @@ class VersionUpdateService:
@staticmethod @staticmethod
def get_download_link(): def get_download_link():
deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json")
return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % (
VersionUpdateService.get_deployment_from_file(deployment_info_file_path), VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH),
get_version(), get_version(),
) )
@staticmethod @staticmethod
def get_deployment_from_file(file_path: str) -> str: def get_deployment_from_file(file_path: Path) -> str:
deployment = "unknown" deployment = "unknown"
try: try:

View File

@ -1,4 +1,4 @@
import os from pathlib import Path
import pytest import pytest
@ -6,30 +6,20 @@ from monkey_island.cc.services.version_update import VersionUpdateService
@pytest.fixture @pytest.fixture
def deployment_info_file_path(monkeypatch, data_for_tests_dir): def deployment_info_file_path(data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment.json") path = data_for_tests_dir / "deployment.json"
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path return path
@pytest.fixture @pytest.fixture
def ghost_file(monkeypatch): def key_error_deployment_info_file_path(data_for_tests_dir):
path = "ghost_file" path = data_for_tests_dir / "deployment_key_error.json"
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path return path
@pytest.fixture @pytest.fixture
def key_error_deployment_info_file_path(monkeypatch, data_for_tests_dir): def flawed_deployment_info_file_path(data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment_key_error.json") path = data_for_tests_dir / "deployment_flawed"
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path
@pytest.fixture
def flawed_deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment_flawed")
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path return path
@ -38,7 +28,8 @@ def test_get_deployment_field_from_file(deployment_info_file_path):
assert deployment == "develop" assert deployment == "develop"
def test_get_deployment_field_from_nonexistent_file(ghost_file): def test_get_deployment_field_from_nonexistent_file():
ghost_file = Path("ghost_file")
deployment = VersionUpdateService().get_deployment_from_file(ghost_file) deployment = VersionUpdateService().get_deployment_from_file(ghost_file)
assert deployment == "unknown" assert deployment == "unknown"