island, tests: Handle exceptions when getting deployment type from file and add related tests

This commit is contained in:
Shreya Malviya 2021-09-14 14:22:45 +05:30
parent 9fd6ea9598
commit 90c6392e16
4 changed files with 48 additions and 4 deletions

View File

@ -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

View File

@ -0,0 +1 @@
develop

View File

@ -0,0 +1,3 @@
{
"tnemyolped": "develop"
}

View File

@ -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"