|
| 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 resolve_base_model_fields utility function.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import pytest |
| 17 | +from unittest.mock import patch, MagicMock |
| 18 | + |
| 19 | +from sagemaker.core.utils.utils import Unassigned |
| 20 | +from sagemaker.serve.model_builder_utils import resolve_base_model_fields |
| 21 | + |
| 22 | + |
| 23 | +class FakeBaseModel: |
| 24 | + """Fake BaseModel for testing.""" |
| 25 | + |
| 26 | + def __init__(self, hub_content_name=None, hub_content_version=None, recipe_name=None): |
| 27 | + self.hub_content_name = hub_content_name |
| 28 | + self.hub_content_version = hub_content_version |
| 29 | + self.recipe_name = recipe_name |
| 30 | + |
| 31 | + |
| 32 | +class FakeHubContent: |
| 33 | + """Fake HubContent response.""" |
| 34 | + |
| 35 | + def __init__(self, hub_content_version=None): |
| 36 | + self.hub_content_version = hub_content_version |
| 37 | + |
| 38 | + |
| 39 | +class TestResolveBaseModelFields: |
| 40 | + """Tests for resolve_base_model_fields.""" |
| 41 | + |
| 42 | + def test_resolve_with_none_base_model(self): |
| 43 | + """Test that None base_model is returned unchanged.""" |
| 44 | + result = resolve_base_model_fields(None) |
| 45 | + assert result is None |
| 46 | + |
| 47 | + def test_resolve_with_no_hub_content_name_returns_unchanged(self): |
| 48 | + """Test that base_model without hub_content_name is returned unchanged.""" |
| 49 | + base_model = FakeBaseModel( |
| 50 | + hub_content_name=Unassigned(), |
| 51 | + hub_content_version=Unassigned(), |
| 52 | + recipe_name=Unassigned(), |
| 53 | + ) |
| 54 | + result = resolve_base_model_fields(base_model) |
| 55 | + assert isinstance(result.hub_content_version, Unassigned) |
| 56 | + assert isinstance(result.recipe_name, Unassigned) |
| 57 | + |
| 58 | + def test_resolve_with_none_hub_content_name_returns_unchanged(self): |
| 59 | + """Test that base_model with None hub_content_name is returned unchanged.""" |
| 60 | + base_model = FakeBaseModel( |
| 61 | + hub_content_name=None, |
| 62 | + hub_content_version=Unassigned(), |
| 63 | + recipe_name=Unassigned(), |
| 64 | + ) |
| 65 | + result = resolve_base_model_fields(base_model) |
| 66 | + assert isinstance(result.hub_content_version, Unassigned) |
| 67 | + |
| 68 | + def test_resolve_with_empty_hub_content_name_returns_unchanged(self): |
| 69 | + """Test that base_model with empty hub_content_name is returned unchanged.""" |
| 70 | + base_model = FakeBaseModel( |
| 71 | + hub_content_name="", |
| 72 | + hub_content_version=Unassigned(), |
| 73 | + recipe_name=Unassigned(), |
| 74 | + ) |
| 75 | + result = resolve_base_model_fields(base_model) |
| 76 | + assert isinstance(result.hub_content_version, Unassigned) |
| 77 | + |
| 78 | + def test_resolve_with_all_fields_present_no_api_call(self): |
| 79 | + """Test that no API call is made when all fields are already present.""" |
| 80 | + base_model = FakeBaseModel( |
| 81 | + hub_content_name="huggingface-model-abc", |
| 82 | + hub_content_version="1.0.0", |
| 83 | + recipe_name="my-recipe", |
| 84 | + ) |
| 85 | + with patch("sagemaker.serve.model_builder_utils.HubContent", autospec=True) as mock_hc: |
| 86 | + # HubContent should NOT be imported/called |
| 87 | + result = resolve_base_model_fields(base_model) |
| 88 | + assert result.hub_content_version == "1.0.0" |
| 89 | + assert result.recipe_name == "my-recipe" |
| 90 | + |
| 91 | + @patch("sagemaker.core.resources.HubContent") |
| 92 | + def test_resolve_missing_hub_content_version_resolves_from_hub(self, mock_hub_content_cls): |
| 93 | + """Test that missing hub_content_version is resolved from SageMakerPublicHub.""" |
| 94 | + fake_hc = FakeHubContent(hub_content_version="2.5.0") |
| 95 | + mock_hub_content_cls.get.return_value = fake_hc |
| 96 | + |
| 97 | + base_model = FakeBaseModel( |
| 98 | + hub_content_name="huggingface-reasoning-qwen3-32b", |
| 99 | + hub_content_version=Unassigned(), |
| 100 | + recipe_name="some-recipe", |
| 101 | + ) |
| 102 | + |
| 103 | + with patch( |
| 104 | + "sagemaker.serve.model_builder_utils.HubContent", mock_hub_content_cls |
| 105 | + ): |
| 106 | + result = resolve_base_model_fields(base_model) |
| 107 | + |
| 108 | + assert result.hub_content_version == "2.5.0" |
| 109 | + mock_hub_content_cls.get.assert_called_once_with( |
| 110 | + hub_content_type="Model", |
| 111 | + hub_name="SageMakerPublicHub", |
| 112 | + hub_content_name="huggingface-reasoning-qwen3-32b", |
| 113 | + ) |
| 114 | + |
| 115 | + @patch("sagemaker.core.resources.HubContent") |
| 116 | + def test_resolve_missing_recipe_name_logs_warning(self, mock_hub_content_cls): |
| 117 | + """Test that missing recipe_name logs a warning but does not crash.""" |
| 118 | + base_model = FakeBaseModel( |
| 119 | + hub_content_name="huggingface-reasoning-qwen3-32b", |
| 120 | + hub_content_version="1.0.0", |
| 121 | + recipe_name=Unassigned(), |
| 122 | + ) |
| 123 | + |
| 124 | + result = resolve_base_model_fields(base_model) |
| 125 | + # recipe_name should still be Unassigned (not resolved automatically) |
| 126 | + assert isinstance(result.recipe_name, Unassigned) |
| 127 | + # But the function should not crash |
| 128 | + assert result.hub_content_version == "1.0.0" |
| 129 | + |
| 130 | + @patch("sagemaker.core.resources.HubContent") |
| 131 | + def test_resolve_hub_content_not_found_does_not_crash(self, mock_hub_content_cls): |
| 132 | + """Test that HubContent.get() failure is handled gracefully.""" |
| 133 | + mock_hub_content_cls.get.side_effect = Exception("HubContent not found") |
| 134 | + |
| 135 | + base_model = FakeBaseModel( |
| 136 | + hub_content_name="nonexistent-model", |
| 137 | + hub_content_version=Unassigned(), |
| 138 | + recipe_name="some-recipe", |
| 139 | + ) |
| 140 | + |
| 141 | + with patch( |
| 142 | + "sagemaker.serve.model_builder_utils.HubContent", mock_hub_content_cls |
| 143 | + ): |
| 144 | + # Should not raise, just log a warning |
| 145 | + result = resolve_base_model_fields(base_model) |
| 146 | + |
| 147 | + # hub_content_version should still be Unassigned since resolution failed |
| 148 | + assert isinstance(result.hub_content_version, Unassigned) |
| 149 | + |
| 150 | + @patch("sagemaker.core.resources.HubContent") |
| 151 | + def test_resolve_both_version_and_recipe_missing(self, mock_hub_content_cls): |
| 152 | + """Test resolution when both hub_content_version and recipe_name are missing.""" |
| 153 | + fake_hc = FakeHubContent(hub_content_version="3.0.0") |
| 154 | + mock_hub_content_cls.get.return_value = fake_hc |
| 155 | + |
| 156 | + base_model = FakeBaseModel( |
| 157 | + hub_content_name="huggingface-reasoning-qwen3-32b", |
| 158 | + hub_content_version=Unassigned(), |
| 159 | + recipe_name=Unassigned(), |
| 160 | + ) |
| 161 | + |
| 162 | + with patch( |
| 163 | + "sagemaker.serve.model_builder_utils.HubContent", mock_hub_content_cls |
| 164 | + ): |
| 165 | + result = resolve_base_model_fields(base_model) |
| 166 | + |
| 167 | + # Version should be resolved |
| 168 | + assert result.hub_content_version == "3.0.0" |
| 169 | + # Recipe should still be Unassigned (with warning logged) |
| 170 | + assert isinstance(result.recipe_name, Unassigned) |
| 171 | + |
| 172 | + @patch("sagemaker.core.resources.HubContent") |
| 173 | + def test_resolve_with_none_version_resolves(self, mock_hub_content_cls): |
| 174 | + """Test that None hub_content_version (not just Unassigned) is also resolved.""" |
| 175 | + fake_hc = FakeHubContent(hub_content_version="1.2.3") |
| 176 | + mock_hub_content_cls.get.return_value = fake_hc |
| 177 | + |
| 178 | + base_model = FakeBaseModel( |
| 179 | + hub_content_name="huggingface-model-xyz", |
| 180 | + hub_content_version=None, |
| 181 | + recipe_name="my-recipe", |
| 182 | + ) |
| 183 | + |
| 184 | + with patch( |
| 185 | + "sagemaker.serve.model_builder_utils.HubContent", mock_hub_content_cls |
| 186 | + ): |
| 187 | + result = resolve_base_model_fields(base_model) |
| 188 | + |
| 189 | + assert result.hub_content_version == "1.2.3" |
| 190 | + |
| 191 | + @patch("sagemaker.core.resources.HubContent") |
| 192 | + def test_resolve_with_empty_string_version_resolves(self, mock_hub_content_cls): |
| 193 | + """Test that empty string hub_content_version is also resolved.""" |
| 194 | + fake_hc = FakeHubContent(hub_content_version="4.0.0") |
| 195 | + mock_hub_content_cls.get.return_value = fake_hc |
| 196 | + |
| 197 | + base_model = FakeBaseModel( |
| 198 | + hub_content_name="huggingface-model-xyz", |
| 199 | + hub_content_version="", |
| 200 | + recipe_name="my-recipe", |
| 201 | + ) |
| 202 | + |
| 203 | + with patch( |
| 204 | + "sagemaker.serve.model_builder_utils.HubContent", mock_hub_content_cls |
| 205 | + ): |
| 206 | + result = resolve_base_model_fields(base_model) |
| 207 | + |
| 208 | + assert result.hub_content_version == "4.0.0" |
0 commit comments