|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +from sagemaker.interactive_apps.tensorboard import TensorBoardApp |
| 16 | +from unittest.mock import patch, mock_open, PropertyMock |
| 17 | + |
| 18 | +import json |
| 19 | +import pytest |
| 20 | + |
| 21 | +TEST_DOMAIN = "testdomain" |
| 22 | +TEST_USER_PROFILE = "testuser" |
| 23 | +TEST_REGION = "testregion" |
| 24 | +TEST_NOTEBOOK_METADATA = json.dumps({"DomainId": TEST_DOMAIN, "UserProfileName": TEST_USER_PROFILE}) |
| 25 | +TEST_TRAINING_JOB = "testjob" |
| 26 | + |
| 27 | +BASE_URL_STUDIO_FORMAT = "https://{}.studio.{}.sagemaker.aws/tensorboard/default" |
| 28 | +REDIRECT_STUDIO_FORMAT = ( |
| 29 | + "/data/plugin/sagemaker_data_manager/add_folder_or_job?Redirect=True&Name={}" |
| 30 | +) |
| 31 | +BASE_URL_NON_STUDIO_FORMAT = ( |
| 32 | + "https://{region}.console.aws.amazon.com/sagemaker/home?region={region}#/tensor-board-landing" |
| 33 | +) |
| 34 | +REDIRECT_NON_STUDIO_FORMAT = "/{}" |
| 35 | + |
| 36 | + |
| 37 | +@patch("os.path.isfile") |
| 38 | +def test_tb_init_and_url_non_studio_user(mock_file_exists): |
| 39 | + """ |
| 40 | + Test TensorBoardApp for non Studio users. |
| 41 | + """ |
| 42 | + mock_file_exists.return_value = False |
| 43 | + tb_app = TensorBoardApp(TEST_REGION) |
| 44 | + assert tb_app.region == TEST_REGION |
| 45 | + assert tb_app._domain_id is None |
| 46 | + assert tb_app._user_profile_name is None |
| 47 | + assert tb_app._valid_domain_and_user is False |
| 48 | + |
| 49 | + # test url without job redirect |
| 50 | + assert tb_app.get_app_url() == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) |
| 51 | + |
| 52 | + # test url with valid job redirect |
| 53 | + assert tb_app.get_app_url(TEST_TRAINING_JOB) == BASE_URL_NON_STUDIO_FORMAT.format( |
| 54 | + region=TEST_REGION |
| 55 | + ) + REDIRECT_NON_STUDIO_FORMAT.format(TEST_TRAINING_JOB) |
| 56 | + |
| 57 | + # test url with invalid job redirect |
| 58 | + with pytest.raises(ValueError): |
| 59 | + tb_app.get_app_url("invald_job_name!") |
| 60 | + |
| 61 | + |
| 62 | +@patch("os.path.isfile") |
| 63 | +def test_tb_init_and_url_studio_user_valid_medatada(mock_file_exists): |
| 64 | + """ |
| 65 | + Test TensorBoardApp for Studio user when the notebook metadata file provided by Studio is valid. |
| 66 | + """ |
| 67 | + mock_file_exists.return_value = True |
| 68 | + with patch("builtins.open", mock_open(read_data=TEST_NOTEBOOK_METADATA)): |
| 69 | + tb_app = TensorBoardApp(TEST_REGION) |
| 70 | + assert tb_app.region == TEST_REGION |
| 71 | + assert tb_app._domain_id == TEST_DOMAIN |
| 72 | + assert tb_app._user_profile_name == TEST_USER_PROFILE |
| 73 | + assert tb_app._valid_domain_and_user is True |
| 74 | + |
| 75 | + # test url without job redirect |
| 76 | + assert ( |
| 77 | + tb_app.get_app_url() |
| 78 | + == BASE_URL_STUDIO_FORMAT.format(TEST_DOMAIN, TEST_REGION) + "/#sagemaker_data_manager" |
| 79 | + ) |
| 80 | + |
| 81 | + # test url with valid job redirect |
| 82 | + assert tb_app.get_app_url(TEST_TRAINING_JOB) == BASE_URL_STUDIO_FORMAT.format( |
| 83 | + TEST_DOMAIN, TEST_REGION |
| 84 | + ) + REDIRECT_STUDIO_FORMAT.format(TEST_TRAINING_JOB) |
| 85 | + |
| 86 | + # test url with invalid job redirect |
| 87 | + with pytest.raises(ValueError): |
| 88 | + tb_app.get_app_url("invald_job_name!") |
| 89 | + |
| 90 | + |
| 91 | +@patch("os.path.isfile") |
| 92 | +def test_tb_init_and_url_studio_user_invalid_medatada(mock_file_exists): |
| 93 | + """ |
| 94 | + Test TensorBoardApp for Studio user when the notebook metadata file provided by Studio is invalid. |
| 95 | + """ |
| 96 | + mock_file_exists.return_value = True |
| 97 | + |
| 98 | + # test file does not contain domain and user profle |
| 99 | + with patch("builtins.open", mock_open(read_data=json.dumps({"Fake": "Fake"}))): |
| 100 | + assert TensorBoardApp(TEST_REGION).get_app_url() == BASE_URL_NON_STUDIO_FORMAT.format( |
| 101 | + region=TEST_REGION |
| 102 | + ) |
| 103 | + |
| 104 | + # test invalid user profile name |
| 105 | + with patch( |
| 106 | + "builtins.open", |
| 107 | + mock_open(read_data=json.dumps({"DomainId": TEST_DOMAIN, "UserProfileName": "u" * 64})), |
| 108 | + ): |
| 109 | + assert TensorBoardApp(TEST_REGION).get_app_url() == BASE_URL_NON_STUDIO_FORMAT.format( |
| 110 | + region=TEST_REGION |
| 111 | + ) |
| 112 | + |
| 113 | + # test invalid domain id |
| 114 | + with patch( |
| 115 | + "builtins.open", |
| 116 | + mock_open( |
| 117 | + read_data=json.dumps({"DomainId": "d" * 64, "UserProfileName": TEST_USER_PROFILE}) |
| 118 | + ), |
| 119 | + ): |
| 120 | + assert TensorBoardApp(TEST_REGION).get_app_url() == BASE_URL_NON_STUDIO_FORMAT.format( |
| 121 | + region=TEST_REGION |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def test_tb_init_with_default_region(): |
| 126 | + """ |
| 127 | + Test TensorBoardApp init when user does not provide region. |
| 128 | + """ |
| 129 | + # happy case |
| 130 | + with patch("sagemaker.Session.boto_region_name", new_callable=PropertyMock) as region_mock: |
| 131 | + region_mock.return_value = TEST_REGION |
| 132 | + tb_app = TensorBoardApp() |
| 133 | + assert tb_app.region == TEST_REGION |
| 134 | + |
| 135 | + # no default region configured |
| 136 | + with patch("sagemaker.Session.boto_region_name", new_callable=PropertyMock) as region_mock: |
| 137 | + region_mock.side_effect = [ValueError()] |
| 138 | + with pytest.raises(ValueError): |
| 139 | + tb_app = TensorBoardApp() |
0 commit comments