|
| 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 | +"""Tests for lazy initialization of DEFAULT_JUMPSTART_SAGEMAKER_SESSION (GH #4468).""" |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | + |
| 17 | +import copy |
| 18 | +from types import SimpleNamespace |
| 19 | + |
| 20 | +import pytest |
| 21 | +from mock import MagicMock, patch |
| 22 | + |
| 23 | +from sagemaker.jumpstart import constants |
| 24 | +from sagemaker.jumpstart.constants import _LazyJumpStartSagemakerSession |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(autouse=True) |
| 28 | +def reset_lazy_session_cache(): |
| 29 | + """Ensure each test starts and ends with an unresolved proxy cache.""" |
| 30 | + _LazyJumpStartSagemakerSession._resolved = False |
| 31 | + _LazyJumpStartSagemakerSession._session = None |
| 32 | + yield |
| 33 | + _LazyJumpStartSagemakerSession._resolved = False |
| 34 | + _LazyJumpStartSagemakerSession._session = None |
| 35 | + |
| 36 | + |
| 37 | +def test_default_session_is_a_lazy_proxy(): |
| 38 | + assert isinstance(constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION, _LazyJumpStartSagemakerSession) |
| 39 | + |
| 40 | + |
| 41 | +def test_truthiness_does_not_build_a_session(): |
| 42 | + """``session or DEFAULT_...`` / ``if session:`` must stay lazy (no boto clients).""" |
| 43 | + with patch.object(constants, "Session") as session_cls: |
| 44 | + assert bool(constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION) is True |
| 45 | + assert _LazyJumpStartSagemakerSession._resolved is False |
| 46 | + session_cls.assert_not_called() |
| 47 | + |
| 48 | + |
| 49 | +def test_first_attribute_access_builds_session_once(): |
| 50 | + fake = MagicMock() |
| 51 | + fake.boto_region_name = "us-west-2" |
| 52 | + with patch.object(constants, "Session", return_value=fake) as session_cls: |
| 53 | + assert constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION.boto_region_name == "us-west-2" |
| 54 | + assert _LazyJumpStartSagemakerSession._resolved is True |
| 55 | + _ = constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION.boto_region_name |
| 56 | + session_cls.assert_called_once() |
| 57 | + |
| 58 | + |
| 59 | +def test_setattr_is_forwarded_to_real_session(): |
| 60 | + fake = MagicMock() |
| 61 | + with patch.object(constants, "Session", return_value=fake): |
| 62 | + constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION.sagemaker_client = "client" |
| 63 | + assert fake.sagemaker_client == "client" |
| 64 | + |
| 65 | + |
| 66 | +def test_copy_returns_the_real_session(): |
| 67 | + """utils.get_default_jumpstart_session_with_user_agent_suffix copies then mutates, |
| 68 | + so copy.copy(proxy) must yield a real (copyable, mutable) session, not the proxy.""" |
| 69 | + fake = SimpleNamespace(boto_session="orig", sagemaker_client="orig") |
| 70 | + with patch.object(constants, "Session", return_value=fake): |
| 71 | + result = copy.copy(constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION) |
| 72 | + assert not isinstance(result, _LazyJumpStartSagemakerSession) |
| 73 | + assert isinstance(result, SimpleNamespace) |
| 74 | + assert result is not fake |
| 75 | + result.boto_session = "new" |
| 76 | + assert fake.boto_session == "orig" |
| 77 | + |
| 78 | + |
| 79 | +def test_failed_build_degrades_to_none_contract(): |
| 80 | + """If Session construction raises, resolution yields None and logs a warning; |
| 81 | + attribute access then behaves exactly as it would on ``None``.""" |
| 82 | + with patch.object(constants, "Session", side_effect=RuntimeError("boom")): |
| 83 | + assert bool(constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION) is True |
| 84 | + assert _LazyJumpStartSagemakerSession._resolve() is None |
| 85 | + with pytest.raises(AttributeError): |
| 86 | + _ = constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION.boto_region_name |
0 commit comments