|
| 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 | +import pytest |
| 15 | +from unittest.mock import MagicMock |
| 16 | +from sagemaker.core.utils.utils import Unassigned |
| 17 | +from sagemaker.core.telemetry.resource_creation import _RESOURCE_ARN_ATTRIBUTES, get_resource_arn |
| 18 | + |
| 19 | + |
| 20 | +# Each entry: (class_name, arn_attr, arn_value) |
| 21 | +_RESOURCE_TEST_CASES = [ |
| 22 | + ( |
| 23 | + "TrainingJob", |
| 24 | + "training_job_arn", |
| 25 | + "arn:aws:sagemaker:us-west-2:123456789012:training-job/my-job", |
| 26 | + ), |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def test_get_resource_arn_none_response(): |
| 31 | + assert get_resource_arn(None) is None |
| 32 | + |
| 33 | + |
| 34 | +def test_get_resource_arn_unknown_type(): |
| 35 | + assert get_resource_arn("some string") is None |
| 36 | + assert get_resource_arn(42) is None |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("class_name,arn_attr,arn_value", _RESOURCE_TEST_CASES) |
| 40 | +def test_get_resource_arn_with_valid_arn(class_name, arn_attr, arn_value): |
| 41 | + mock_resource = MagicMock() |
| 42 | + mock_resource.__class__.__name__ = class_name |
| 43 | + setattr(mock_resource, arn_attr, arn_value) |
| 44 | + assert get_resource_arn(mock_resource) == arn_value |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("class_name,arn_attr,arn_value", _RESOURCE_TEST_CASES) |
| 48 | +def test_get_resource_arn_with_unassigned(class_name, arn_attr, arn_value): |
| 49 | + mock_resource = MagicMock() |
| 50 | + mock_resource.__class__.__name__ = class_name |
| 51 | + setattr(mock_resource, arn_attr, Unassigned()) |
| 52 | + assert get_resource_arn(mock_resource) is None |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("class_name,arn_attr,arn_value", _RESOURCE_TEST_CASES) |
| 56 | +def test_get_resource_arn_with_none_arn(class_name, arn_attr, arn_value): |
| 57 | + mock_resource = MagicMock() |
| 58 | + mock_resource.__class__.__name__ = class_name |
| 59 | + setattr(mock_resource, arn_attr, None) |
| 60 | + assert get_resource_arn(mock_resource) is None |
| 61 | + |
| 62 | + |
| 63 | +# Verify string keys in _RESOURCE_ARN_ATTRIBUTES match actual class names |
| 64 | +@pytest.mark.parametrize("class_name,arn_attr,arn_value", _RESOURCE_TEST_CASES) |
| 65 | +def test_resource_class_name_matches_dict_key(class_name, arn_attr, arn_value): |
| 66 | + from sagemaker.core.resources import TrainingJob |
| 67 | + |
| 68 | + _CLASS_MAP = { |
| 69 | + "TrainingJob": TrainingJob, |
| 70 | + } |
| 71 | + cls = _CLASS_MAP.get(class_name) |
| 72 | + assert cls is not None, f"No class found for key '{class_name}'" |
| 73 | + assert cls.__name__ == class_name |
| 74 | + assert class_name in _RESOURCE_ARN_ATTRIBUTES |
0 commit comments