From 36a337e8ff505538995da7fe7e9aa4d2893fc57b Mon Sep 17 00:00:00 2001 From: Anai-Guo Date: Sat, 12 Sep 2026 09:11:14 -0700 Subject: [PATCH] test(tensorboard): drop three duplicated copies of the presigned-URL test block `sagemaker-core/tests/unit/interactive_apps/test_tensorboard.py` contains the same four-test block four times over, plus an older superseded copy of three of them. Because a later `def` silently rebinds an earlier one at module scope, only the last definition of each name is collected, so ~420 lines of the file are dead: * `test_tb_presigned_url_invalid_params` - 4 byte-identical definitions * `test_tb_presigned_url_failure` - 1 superseded + 4 byte-identical * `test_tb_invalid_presigned_kwargs` - 1 superseded + 4 byte-identical * `test_tb_valid_presigned_kwargs` - 1 superseded + 3 byte-identical + 1 truncated The three superseded definitions are the pre-rewrite originals: the newer ones patch `BaseInteractiveApp.__init__` and build the `tb_app` fixture explicitly. The old `test_tb_valid_presigned_kwargs` is in fact broken - its trailing web-browser block dereferences a `tb_app` name it never binds, which is exactly the defect the rewrite fixed - so nothing is lost by removing it. One behavioural detail matters: the *last* copy of `test_tb_valid_presigned_kwargs` (the only one that was actually collected) is a truncated version that drops the final assertions covering `open_in_default_web_browser=True`. Keeping the first complete copy instead restores that coverage. This change keeps exactly one definition of each test and deletes the rest. Verified with `pytest tests/unit/interactive_apps/test_tensorboard.py`: 11 tests collected both before and after, and the same 10 pass / 1 pre-existing environment failure (`test_tb_init_with_default_region`, which needs a default AWS region) in both runs - with the restored web-browser assertions now executing. Signed-off-by: Anai-Guo Co-Authored-By: Claude Opus 5 (1M context) --- .../unit/interactive_apps/test_tensorboard.py | 421 ------------------ 1 file changed, 421 deletions(-) diff --git a/sagemaker-core/tests/unit/interactive_apps/test_tensorboard.py b/sagemaker-core/tests/unit/interactive_apps/test_tensorboard.py index b8a2074e65..b403e4ba22 100644 --- a/sagemaker-core/tests/unit/interactive_apps/test_tensorboard.py +++ b/sagemaker-core/tests/unit/interactive_apps/test_tensorboard.py @@ -274,315 +274,6 @@ def test_tb_presigned_url_not_returned_without_presigned_flag(mock_client): assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) -@patch("boto3.client") -def test_tb_presigned_url_failure(mock_client): - resp = {"ResponseMetadata": {"HTTPStatusCode": 400}} - attrs = {"create_presigned_domain_url.return_value": resp} - mock_client.return_value = Mock(**attrs) - - with pytest.raises(ValueError): - TensorBoardApp(TEST_REGION).get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=False, - ) - - -def test_tb_invalid_presigned_kwargs(): - invalid_kwargs = { - "fake-parameter": True, - "DomainId": TEST_DOMAIN, - "UserProfileName": TEST_USER_PROFILE, - } - - with pytest.raises(botocore.exceptions.ParamValidationError): - TensorBoardApp(TEST_REGION).get_app_url( - optional_create_presigned_url_kwargs=invalid_kwargs, - create_presigned_domain_url=True, - ) - - -@patch("boto3.client") -def test_tb_valid_presigned_kwargs(mock_client): - - rsp = { - "ResponseMetadata": {"HTTPStatusCode": 200}, - "AuthorizedUrl": TEST_PRESIGNED_URL, - } - mock_client = boto3.client("sagemaker") - mock_client.create_presigned_domain_url = Mock(name="create_presigned_domain_url") - mock_client.create_presigned_domain_url.return_value = rsp - - valid_kwargs = {"DomainId": TEST_DOMAIN, "UserProfileName": TEST_USER_PROFILE} - - url = TensorBoardApp(TEST_REGION).get_app_url( - optional_create_presigned_url_kwargs=valid_kwargs, - create_presigned_domain_url=True, - open_in_default_web_browser=False, - ) - - assert url == f"{TEST_PRESIGNED_URL}&redirect=TensorBoard" - mock_client.create_presigned_domain_url.assert_called_once_with(**valid_kwargs) - - # test url when opened in web browser - with patch("webbrowser.open") as mock_web_browser_open: - url = tb_app.get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=True, - ) - mock_web_browser_open.assert_called_with(f"{TEST_PRESIGNED_URL}&redirect=TensorBoard") - assert url == "" - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_presigned_url_invalid_params(mock_init, mock_client): - mock_init.return_value = None - mock_client.return_value = boto3.client("sagemaker") - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - - url = tb_app.get_app_url( - create_presigned_domain_url=False, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - url = tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id="d" * 64, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - url = tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name="u" * 64, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_presigned_url_failure(mock_init, mock_client): - mock_init.return_value = None - resp = {"ResponseMetadata": {"HTTPStatusCode": 400}} - attrs = {"create_presigned_domain_url.return_value": resp} - mock_client.return_value = Mock(**attrs) - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - with pytest.raises(ValueError): - tb_app.get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=False, - ) - - -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_invalid_presigned_kwargs(mock_init): - mock_init.return_value = None - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - with pytest.raises(botocore.exceptions.ParamValidationError): - invalid_kwargs = {"fake-parameter": True} - tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - optional_create_presigned_url_kwargs=invalid_kwargs, - ) - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_valid_presigned_kwargs(mock_init, mock_client): - mock_init.return_value = None - resp = { - "ResponseMetadata": {"HTTPStatusCode": 200}, - "AuthorizedUrl": TEST_PRESIGNED_URL, - } - mock_client = boto3.client("sagemaker") - mock_client.create_presigned_domain_url = Mock(name="create_presigned_domain_url") - mock_client.create_presigned_domain_url.return_value = resp - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - valid_kwargs = {"ExpiresInSeconds": 1500} - tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - optional_create_presigned_url_kwargs=valid_kwargs, - ) - mock_client.create_presigned_domain_url.assert_called_with(**valid_kwargs) - - # test url when opened in web browser - with patch("webbrowser.open") as mock_web_browser_open: - url = tb_app.get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=True, - ) - mock_web_browser_open.assert_called_with(f"{TEST_PRESIGNED_URL}&redirect=TensorBoard") - assert url == "" - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_presigned_url_invalid_params(mock_init, mock_client): - mock_init.return_value = None - mock_client.return_value = boto3.client("sagemaker") - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - - url = tb_app.get_app_url( - create_presigned_domain_url=False, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - url = tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id="d" * 64, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - url = tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name="u" * 64, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_presigned_url_failure(mock_init, mock_client): - mock_init.return_value = None - resp = {"ResponseMetadata": {"HTTPStatusCode": 400}} - attrs = {"create_presigned_domain_url.return_value": resp} - mock_client.return_value = Mock(**attrs) - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - with pytest.raises(ValueError): - tb_app.get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=False, - ) - - -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_invalid_presigned_kwargs(mock_init): - mock_init.return_value = None - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - with pytest.raises(botocore.exceptions.ParamValidationError): - invalid_kwargs = {"fake-parameter": True} - tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - optional_create_presigned_url_kwargs=invalid_kwargs, - ) - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_valid_presigned_kwargs(mock_init, mock_client): - mock_init.return_value = None - resp = { - "ResponseMetadata": {"HTTPStatusCode": 200}, - "AuthorizedUrl": TEST_PRESIGNED_URL, - } - mock_client = boto3.client("sagemaker") - mock_client.create_presigned_domain_url = Mock(name="create_presigned_domain_url") - mock_client.create_presigned_domain_url.return_value = resp - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - valid_kwargs = {"ExpiresInSeconds": 1500} - tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - optional_create_presigned_url_kwargs=valid_kwargs, - ) - mock_client.create_presigned_domain_url.assert_called_with(**valid_kwargs) - - # test url when opened in web browser - with patch("webbrowser.open") as mock_web_browser_open: - url = tb_app.get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=True, - ) - mock_web_browser_open.assert_called_with(f"{TEST_PRESIGNED_URL}&redirect=TensorBoard") - assert url == "" - - @patch("boto3.client") @patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") def test_tb_presigned_url_invalid_params(mock_init, mock_client): @@ -706,118 +397,6 @@ def test_tb_valid_presigned_kwargs(mock_init, mock_client): assert url == "" -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_presigned_url_invalid_params(mock_init, mock_client): - mock_init.return_value = None - mock_client.return_value = boto3.client("sagemaker") - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - - url = tb_app.get_app_url( - create_presigned_domain_url=False, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - url = tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id="d" * 64, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - url = tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name="u" * 64, - open_in_default_web_browser=False, - ) - assert url == BASE_URL_NON_STUDIO_FORMAT.format(region=TEST_REGION) - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_presigned_url_failure(mock_init, mock_client): - mock_init.return_value = None - resp = {"ResponseMetadata": {"HTTPStatusCode": 400}} - attrs = {"create_presigned_domain_url.return_value": resp} - mock_client.return_value = Mock(**attrs) - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - with pytest.raises(ValueError): - tb_app.get_app_url( - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - create_presigned_domain_url=True, - open_in_default_web_browser=False, - ) - - -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_invalid_presigned_kwargs(mock_init): - mock_init.return_value = None - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - with pytest.raises(botocore.exceptions.ParamValidationError): - invalid_kwargs = {"fake-parameter": True} - tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - optional_create_presigned_url_kwargs=invalid_kwargs, - ) - - -@patch("boto3.client") -@patch("sagemaker.core.interactive_apps.base_interactive_app.BaseInteractiveApp.__init__") -def test_tb_valid_presigned_kwargs(mock_init, mock_client): - mock_init.return_value = None - resp = { - "ResponseMetadata": {"HTTPStatusCode": 200}, - "AuthorizedUrl": TEST_PRESIGNED_URL, - } - mock_client = boto3.client("sagemaker") - mock_client.create_presigned_domain_url = Mock(name="create_presigned_domain_url") - mock_client.create_presigned_domain_url.return_value = resp - - tb_app = TensorBoardApp(TEST_REGION) - tb_app.region = TEST_REGION - tb_app._domain_id = None - tb_app._user_profile_name = None - tb_app._in_studio_env = False - tb_app._sagemaker_client = boto3.client("sagemaker", region_name=TEST_REGION) - - valid_kwargs = {"ExpiresInSeconds": 1500} - tb_app.get_app_url( - create_presigned_domain_url=True, - domain_id=TEST_DOMAIN, - user_profile_name=TEST_USER_PROFILE, - open_in_default_web_browser=False, - optional_create_presigned_url_kwargs=valid_kwargs, - ) - mock_client.create_presigned_domain_url.assert_called_with(**valid_kwargs) - - def test_tb_init_with_default_region(): """ Test TensorBoardApp init when user does not provide region.