-
Notifications
You must be signed in to change notification settings - Fork 1.2k
change: Replaced generic ValueError with custom subclass when reporting unexpected resource status #919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
change: Replaced generic ValueError with custom subclass when reporting unexpected resource status #919
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
"""Custom exception classes for Sagemaker SDK""" | ||
from __future__ import absolute_import | ||
|
||
|
||
class UnexpectedStatusException(ValueError): | ||
"""Raised when resource status is not expected and thus not allowed for further execution""" | ||
|
||
def __init__(self, message, allowed_statuses, actual_status): | ||
self.allowed_statuses = allowed_statuses | ||
self.actual_status = actual_status | ||
super(UnexpectedStatusException, self).__init__(message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from __future__ import absolute_import | ||
|
||
import pytest | ||
from mock import Mock, MagicMock | ||
import sagemaker | ||
|
||
EXPANDED_ROLE = "arn:aws:iam::111111111111:role/ExpandedRole" | ||
REGION = "us-west-2" | ||
MODEL_PACKAGE_NAME = "my_model_package" | ||
JOB_NAME = "my_job_name" | ||
ENDPOINT_NAME = "the_point_of_end" | ||
|
||
|
||
def get_sagemaker_session(returns_status): | ||
boto_mock = Mock(name="boto_session", region_name=REGION) | ||
client_mock = Mock() | ||
client_mock.describe_model_package = MagicMock( | ||
return_value={"ModelPackageStatus": returns_status} | ||
) | ||
client_mock.describe_endpoint = MagicMock(return_value={"EndpointStatus": returns_status}) | ||
ims = sagemaker.Session(boto_session=boto_mock, sagemaker_client=client_mock) | ||
ims.expand_role = Mock(return_value=EXPANDED_ROLE) | ||
return ims | ||
|
||
|
||
def test_does_not_raise_when_successfully_created_package(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="Completed") | ||
sagemaker_session.wait_for_model_package(MODEL_PACKAGE_NAME) | ||
except sagemaker.exceptions.UnexpectedStatusException: | ||
pytest.fail("UnexpectedStatusException was thrown while it should not") | ||
|
||
|
||
def test_raise_when_failed_created_package(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="EnRoute") | ||
sagemaker_session.wait_for_model_package(MODEL_PACKAGE_NAME) | ||
assert ( | ||
False | ||
), "sagemaker.exceptions.UnexpectedStatusException should have been raised but was not" | ||
except Exception as e: | ||
assert type(e) == sagemaker.exceptions.UnexpectedStatusException | ||
assert e.actual_status == "EnRoute" | ||
assert "Completed" in e.allowed_statuses | ||
|
||
|
||
def test_does_not_raise_when_correct_job_status(): | ||
try: | ||
job = Mock() | ||
sagemaker_session = get_sagemaker_session(returns_status="Stopped") | ||
sagemaker_session._check_job_status( | ||
job, {"TransformationJobStatus": "Stopped"}, "TransformationJobStatus" | ||
) | ||
except sagemaker.exceptions.UnexpectedStatusException: | ||
pytest.fail("UnexpectedStatusException was thrown while it should not") | ||
|
||
|
||
def test_does_raise_when_incorrect_job_status(): | ||
try: | ||
job = Mock() | ||
sagemaker_session = get_sagemaker_session(returns_status="Failed") | ||
sagemaker_session._check_job_status( | ||
job, {"TransformationJobStatus": "Failed"}, "TransformationJobStatus" | ||
) | ||
assert ( | ||
False | ||
), "sagemaker.exceptions.UnexpectedStatusException should have been raised but was not" | ||
except Exception as e: | ||
assert type(e) == sagemaker.exceptions.UnexpectedStatusException | ||
assert e.actual_status == "Failed" | ||
assert "Completed" in e.allowed_statuses | ||
assert "Stopped" in e.allowed_statuses | ||
|
||
|
||
def test_does_not_raise_when_successfully_deployed_endpoint(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="InService") | ||
sagemaker_session.wait_for_endpoint(ENDPOINT_NAME) | ||
except sagemaker.exceptions.UnexpectedStatusException: | ||
pytest.fail("UnexpectedStatusException was thrown while it should not") | ||
|
||
|
||
def test_raise_when_failed_to_deploy_endpoint(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="Failed") | ||
assert sagemaker_session.wait_for_endpoint(ENDPOINT_NAME) | ||
assert ( | ||
False | ||
), "sagemaker.exceptions.UnexpectedStatusException should have been raised but was not" | ||
except Exception as e: | ||
assert type(e) == sagemaker.exceptions.UnexpectedStatusException | ||
assert e.actual_status == "Failed" | ||
assert "InService" in e.allowed_statuses |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.