Repository navigation
Expand file tree
/
Copy pathtest_user_agent.py
More file actions
63 lines (46 loc) · 2.51 KB
/
Copy pathtest_user_agent.py
File metadata and controls
63 lines (46 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Unit tests for sagemaker.core.user_agent SDK version resolution."""
from __future__ import absolute_import
from unittest.mock import patch
import importlib_metadata
from sagemaker.core import user_agent
def test_resolve_prefers_meta_sagemaker():
"""The meta ``sagemaker`` distribution version is preferred and tried first."""
with patch.object(
user_agent.importlib_metadata, "version", return_value="3.27.0"
) as mock_version:
assert user_agent._resolve_sdk_version() == "3.27.0"
mock_version.assert_called_with("sagemaker")
def test_resolve_falls_back_to_core_when_meta_absent():
"""When the meta package is not installed, fall back to ``sagemaker-core``."""
def fake_version(distribution):
if distribution == "sagemaker":
raise importlib_metadata.PackageNotFoundError(distribution)
return "2.27.0"
with patch.object(user_agent.importlib_metadata, "version", side_effect=fake_version):
assert user_agent._resolve_sdk_version() == "2.27.0"
def test_resolve_static_fallback_when_none_installed():
"""When neither distribution resolves, return the static fallback (never raise)."""
def raise_not_found(distribution):
raise importlib_metadata.PackageNotFoundError(distribution)
with patch.object(user_agent.importlib_metadata, "version", side_effect=raise_not_found):
assert user_agent._resolve_sdk_version() == "3.0"
def test_version_is_dynamic_not_hardcoded():
"""Regression guard for the hardcoded "3.0": the version must echo installed metadata."""
with patch.object(user_agent.importlib_metadata, "version", return_value="9.9.9"):
assert user_agent._resolve_sdk_version() == "9.9.9"
def test_user_agent_suffix_uses_resolved_version():
"""The user-agent suffix carries the resolved SDK version."""
with patch.object(user_agent, "SDK_VERSION", "9.9.9"):
assert user_agent.get_user_agent_extra_suffix() == f"lib/{user_agent.SDK_PREFIX}#9.9.9"