Skip to content

change: add integ test for tagging #735

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 8 commits into from
Apr 5, 2019
Merged
Changes from all commits
Commits
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
34 changes: 30 additions & 4 deletions tests/integ/test_tf_script_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import boto3
from sagemaker.tensorflow import TensorFlow
from six.moves.urllib.parse import urlparse
from sagemaker.utils import unique_name_from_base
import tests.integ as integ
from tests.integ import kms_utils
import tests.integ.timeout as timeout
Expand All @@ -31,6 +32,7 @@
SCRIPT = os.path.join(RESOURCE_PATH, 'mnist.py')
PARAMETER_SERVER_DISTRIBUTION = {'parameter_server': {'enabled': True}}
MPI_DISTRIBUTION = {'mpi': {'enabled': True}}
TAGS = [{'Key': 'some-key', 'Value': 'some-value'}]


@pytest.fixture(scope='session', params=['ml.c5.xlarge', 'ml.p2.xlarge'])
Expand All @@ -48,7 +50,7 @@ def test_mnist(sagemaker_session, instance_type):
py_version='py3',
framework_version=TensorFlow.LATEST_VERSION,
metric_definitions=[{'Name': 'train:global_steps', 'Regex': r'global_step\/sec:\s(.*)'}],
base_job_name='test-tf-sm-mnist')
base_job_name=unique_name_from_base('test-tf-sm-mnist'))
Copy link
Contributor

Choose a reason for hiding this comment

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

this won't work... need to remove base_job_name, and use unique name as job_name arg to fit function. otherwise the base_job_name may be truncated before standard timestamp is added, and you end up with duplicate names again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops. i missed this comments. I will make another pr to fix this.

inputs = estimator.sagemaker_session.upload_data(
path=os.path.join(RESOURCE_PATH, 'data'),
key_prefix='scriptmode/mnist')
Expand Down Expand Up @@ -76,7 +78,7 @@ def test_server_side_encryption(sagemaker_session):
sagemaker_session=sagemaker_session,
py_version='py3',
framework_version='1.11',
base_job_name='test-server-side-encryption',
base_job_name=unique_name_from_base('test-server-side-encryption'),
code_location=output_path,
output_path=output_path,
model_dir='/opt/ml/model',
Expand All @@ -103,7 +105,7 @@ def test_mnist_distributed(sagemaker_session, instance_type):
script_mode=True,
framework_version=TensorFlow.LATEST_VERSION,
distributions=PARAMETER_SERVER_DISTRIBUTION,
base_job_name='test-tf-sm-mnist')
base_job_name=unique_name_from_base('test-tf-sm-mnist'))
inputs = estimator.sagemaker_session.upload_data(
path=os.path.join(RESOURCE_PATH, 'data'),
key_prefix='scriptmode/distributed_mnist')
Expand All @@ -122,21 +124,25 @@ def test_mnist_async(sagemaker_session):
sagemaker_session=sagemaker_session,
py_version='py3',
framework_version=TensorFlow.LATEST_VERSION,
base_job_name='test-tf-sm-mnist')
base_job_name=unique_name_from_base('test-tf-sm-mnist'),
tags=TAGS)
inputs = estimator.sagemaker_session.upload_data(
path=os.path.join(RESOURCE_PATH, 'data'),
key_prefix='scriptmode/mnist')
estimator.fit(inputs, wait=False)
training_job_name = estimator.latest_training_job.name
time.sleep(20)
endpoint_name = training_job_name
_assert_training_job_tags_match(sagemaker_session.sagemaker_client, estimator.latest_training_job.name, TAGS)
with timeout.timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session):
estimator = TensorFlow.attach(training_job_name=training_job_name, sagemaker_session=sagemaker_session)
predictor = estimator.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge',
endpoint_name=endpoint_name)

result = predictor.predict(np.zeros(784))
print('predict result: {}'.format(result))
_assert_endpoint_tags_match(sagemaker_session.sagemaker_client, predictor.endpoint, TAGS)
_assert_model_tags_match(sagemaker_session.sagemaker_client, estimator.latest_training_job.name, TAGS)


def _assert_s3_files_exist(s3_url, files):
Expand All @@ -147,3 +153,23 @@ def _assert_s3_files_exist(s3_url, files):
found = [x['Key'] for x in contents if x['Key'].endswith(f)]
if not found:
raise ValueError('File {} is not found under {}'.format(f, s3_url))


def _assert_tags_match(sagemaker_client, resource_arn, tags):
actual_tags = sagemaker_client.list_tags(ResourceArn=resource_arn)['Tags']
assert actual_tags == tags


def _assert_model_tags_match(sagemaker_client, model_name, tags):
model_description = sagemaker_client.describe_model(ModelName=model_name)
_assert_tags_match(sagemaker_client, model_description['ModelArn'], tags)


def _assert_endpoint_tags_match(sagemaker_client, endpoint_name, tags):
endpoint_description = sagemaker_client.describe_endpoint(EndpointName=endpoint_name)
_assert_tags_match(sagemaker_client, endpoint_description['EndpointArn'], tags)


def _assert_training_job_tags_match(sagemaker_client, training_job_name, tags):
training_job_description = sagemaker_client.describe_training_job(TrainingJobName=training_job_name)
_assert_tags_match(sagemaker_client, training_job_description['TrainingJobArn'], tags)