Skip to content

Commit

Permalink
Throw proper Exception instead of generic one
Browse files Browse the repository at this point in the history
This to solve check flake8 B017

Signed-off-by: Luca Carrogu <carrogu@amazon.com>
  • Loading branch information
lukeseawalker committed Jan 16, 2023
1 parent 554c96f commit ff67b7b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ def __init__(self, config):
self._compute_fleet_data = {}
self.set_config(config)

class ClusterStatusUpdateEventError(Exception):
"""Raised when there is a failure in updating the status due to an error on event handler execution."""

pass

def set_config(self, config): # noqa: D102
if self._config != config:
log.info("Applying new clusterstatusmgtd config: %s", config)
Expand Down Expand Up @@ -366,7 +371,7 @@ def _call_update_event(self):
_write_json_to_file(self._config.computefleet_status_path, compute_fleet_data)
except Exception as e:
log.error("Update event handler failed during fleet status translation: %s", e)
raise
raise ClusterStatusManager.ClusterStatusUpdateEventError

cinc_log_file = "/var/log/chef-client.log"
log.info("Calling update event handler, log can be found at %s", cinc_log_file)
Expand All @@ -386,7 +391,7 @@ def _call_update_event(self):
_run_command(cmd, self._config.update_event_timeout_minutes)
except Exception:
log.error("Update event handler failed. Check log file %s", cinc_log_file)
raise
raise ClusterStatusManager.ClusterStatusUpdateEventError

def _update_status(self, request_status, in_progress_status, final_status):
if self._compute_fleet_status == request_status:
Expand Down
4 changes: 2 additions & 2 deletions test/unit/clusterstatusmgtd/test_clusterstatusmgtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_get_status(self, compute_fleet_status_manager, get_item_response, expec
"""Test get_status method."""
if get_item_response is Exception:
compute_fleet_status_manager._table.get_item.side_effect = get_item_response
with pytest.raises(Exception):
with pytest.raises(ClusterStatusManager.ClusterStatusUpdateEventError):
compute_fleet_status_manager.get_status()
else:
compute_fleet_status_manager._table.get_item.return_value = get_item_response
Expand Down Expand Up @@ -398,7 +398,7 @@ def test_call_update_event(self, mocker, status, translated_status, exception):
run_command_mock = mocker.patch("clusterstatusmgtd._run_command")
if isinstance(exception, Exception):
run_command_mock.side_effect = exception
with pytest.raises(Exception):
with pytest.raises(ClusterStatusManager.ClusterStatusUpdateEventError):
clusterstatus_manager._call_update_event()
else:
file_writer_mock = mocker.mock_open()
Expand Down
4 changes: 2 additions & 2 deletions test/unit/slurm/test_fleet_config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import pytest
from assertpy import assert_that
from slurm.pcluster_fleet_config_generator import generate_fleet_config_file
from slurm.pcluster_fleet_config_generator import generate_fleet_config_file, CriticalError


@pytest.mark.parametrize(
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_generate_fleet_config_file_error_cases(mocker, tmpdir, cluster_config,
output_file = f"{tmpdir}/fleet-config.json"

if expected_message:
with pytest.raises(Exception, match=expected_message):
with pytest.raises(CriticalError, match=expected_message):
generate_fleet_config_file(output_file, input_file="fake")
else:
generate_fleet_config_file(output_file, input_file="fake")
Expand Down

0 comments on commit ff67b7b

Please sign in to comment.