Skip to content

fix: update sagemaker.serverless integration test #2533

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 24 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fab0196
Rename `delete_endpoint` as `delete_predictor`
bveeramani Jul 21, 2021
bc5a586
Update test_serverless.py
bveeramani Jul 21, 2021
5f1131c
Update test_serverless.py
bveeramani Jul 22, 2021
837b45e
Merge branch 'master' into fix-serverless-integ-test
bveeramani Jul 22, 2021
2689fd6
Appease lint
bveeramani Jul 22, 2021
98a8556
Merge branch 'fix-serverless-integ-test' of github.com:bveeramani/sag…
bveeramani Jul 22, 2021
62b6bf7
Merge branch 'master' into fix-serverless-integ-test
shreyapandit Jul 28, 2021
ba91b52
Update serverless test
bveeramani Jul 28, 2021
34cb7cc
Merge branch 'master' into fix-serverless-integ-test
bveeramani Jul 28, 2021
9ef49e3
Appease lint
bveeramani Jul 29, 2021
bcfe280
Merge branch 'fix-serverless-integ-test' of github.com:bveeramani/sag…
bveeramani Jul 29, 2021
8b017e8
Update test_serverless.py
bveeramani Jul 29, 2021
9dff637
Merge branch 'master' into fix-serverless-integ-test
bveeramani Jul 29, 2021
9075a44
Merge branch 'master' into fix-serverless-integ-test
shreyapandit Aug 2, 2021
b522f53
Merge branch 'master' into fix-serverless-integ-test
bveeramani Aug 3, 2021
8e93bde
Appease lint
bveeramani Aug 3, 2021
31daaea
Merge branch 'fix-serverless-integ-test' of github.com:bveeramani/sag…
bveeramani Aug 3, 2021
f9f07bb
Fix test
bveeramani Aug 4, 2021
e35ad7e
Update test_serverless.py
bveeramani Aug 4, 2021
e8c59e5
Update test_serverless.py
bveeramani Aug 4, 2021
42fdd2b
Update test_serverless.py
bveeramani Aug 4, 2021
b2156fd
Update test_serverless.py
bveeramani Aug 4, 2021
c161878
Fix tests
bveeramani Aug 4, 2021
318b1d7
Merge branch 'master' into fix-serverless-integ-test
bveeramani Aug 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ def boto_session(request):
return boto3.Session(region_name=DEFAULT_REGION)


@pytest.fixture(scope="session")
def account(boto_session):
return boto_session.client("sts").get_caller_identity()["Account"]


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to create these two fixtures?

Can't we just use boto_session to access account and region ?

Any benefit for creating this one?

Copy link
Contributor Author

@bveeramani bveeramani Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For account, it's to reduce code duplication. While it's true we could use boto_session.client("sts").get_caller_identity()["Account"] every time we want to access the account ID, IMO setting up a fixture for the account is a cleaner implementation (the account fixture is used more than once).

With respect to the region fixture, there's less of a case for that. I subjectively feel it simplifies the implementation, but I also see the argument for why it may be unnecessary. If you feel that we should get rid of it, I'd be okay with doing so.

@pytest.fixture(scope="session")
def region(boto_session):
return boto_session.region_name


@pytest.fixture(scope="session")
def sagemaker_session(sagemaker_client_config, sagemaker_runtime_config, boto_session):
sagemaker_client_config.setdefault("config", Config(retries=dict(max_attempts=10)))
Expand Down
50 changes: 36 additions & 14 deletions tests/integ/test_serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,47 @@
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import os

import pytest

from sagemaker.serverless import LambdaModel
from sagemaker.utils import unique_name_from_base

# See tests/data/serverless for the image source code.
IMAGE_URI = "142577830533.dkr.ecr.us-west-2.amazonaws.com/serverless-integ-test:latest"
ROLE = "arn:aws:iam::142577830533:role/lambda_basic_execution"
URL = "https://c.files.bbci.co.uk/12A9B/production/_111434467_gettyimages-1143489763.jpg"
URL = "https://sagemaker-integ-tests-data.s3.us-east-1.amazonaws.com/cat.jpeg"

REPOSITORY_NAME = "serverless-integ-test"
ROLE_NAME = "LambdaExecutionRole"


@pytest.fixture(name="image_uri", scope="module")
def fixture_image_uri(account, region):
return f"{account}.dkr.ecr.{region}.amazonaws.com/{REPOSITORY_NAME}:latest"


@pytest.fixture(name="role", scope="module")
def fixture_role(account):
return f"arn:aws:iam::{account}:role/{ROLE_NAME}"


@pytest.fixture(name="client", scope="module")
def fixture_client(boto_session):
return boto_session.client("lambda")


@pytest.fixture(name="repository_exists", scope="module")
def fixture_repository_exists(boto_session):
client = boto_session.client("ecr")
try:
client.describe_repositories(repositoryNames=[REPOSITORY_NAME])
return True
except client.exceptions.RepositoryNotFoundException:
return False


def test_lambda(image_uri, role, client, repository_exists):
if not repository_exists:
pytest.skip("The container image required to run this test does not exist.")

@pytest.mark.skipif(
"CODEBUILD_BUILD_ID" not in os.environ,
reason="The container image is private to the CI account.",
)
def test_lambda():
model = LambdaModel(image_uri=IMAGE_URI, role=ROLE)
model = LambdaModel(image_uri=image_uri, role=role, client=client)

predictor = model.deploy(
unique_name_from_base("my-lambda-function"), timeout=60, memory_size=4092
Expand All @@ -39,5 +61,5 @@ def test_lambda():

assert prediction == {"class": "tabby"}

model.destroy()
predictor.destroy()
model.delete_model()
predictor.delete_predictor()