|
| 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 | +"""Utils Tests.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from tests.unit import DATA_DIR |
| 19 | +from sagemaker.modules.utils import ( |
| 20 | + _is_valid_s3_uri, |
| 21 | + _is_valid_path, |
| 22 | + _get_unique_name, |
| 23 | + _get_repo_name_from_image, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "test_case", |
| 29 | + [ |
| 30 | + { |
| 31 | + "path": "s3://bucket/key", |
| 32 | + "path_type": "Any", |
| 33 | + "expected": True, |
| 34 | + }, |
| 35 | + { |
| 36 | + "path": "s3://bucket/key", |
| 37 | + "path_type": "File", |
| 38 | + "expected": True, |
| 39 | + }, |
| 40 | + { |
| 41 | + "path": "s3://bucket/key/", |
| 42 | + "path_type": "Directory", |
| 43 | + "expected": True, |
| 44 | + }, |
| 45 | + { |
| 46 | + "path": "s3://bucket/key/", |
| 47 | + "path_type": "File", |
| 48 | + "expected": False, |
| 49 | + }, |
| 50 | + { |
| 51 | + "path": "s3://bucket/key", |
| 52 | + "path_type": "Directory", |
| 53 | + "expected": False, |
| 54 | + }, |
| 55 | + { |
| 56 | + "path": "/bucket/key", |
| 57 | + "path_type": "Any", |
| 58 | + "expected": False, |
| 59 | + }, |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_is_valid_s3_uri(test_case): |
| 63 | + assert _is_valid_s3_uri(test_case["path"], test_case["path_type"]) == test_case["expected"] |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "test_case", |
| 68 | + [ |
| 69 | + { |
| 70 | + "path": DATA_DIR, |
| 71 | + "path_type": "Any", |
| 72 | + "expected": True, |
| 73 | + }, |
| 74 | + { |
| 75 | + "path": DATA_DIR, |
| 76 | + "path_type": "Directory", |
| 77 | + "expected": True, |
| 78 | + }, |
| 79 | + { |
| 80 | + "path": f"{DATA_DIR}/dummy_input.txt", |
| 81 | + "path_type": "File", |
| 82 | + "expected": True, |
| 83 | + }, |
| 84 | + { |
| 85 | + "path": f"{DATA_DIR}/dummy_input.txt", |
| 86 | + "path_type": "Directory", |
| 87 | + "expected": False, |
| 88 | + }, |
| 89 | + { |
| 90 | + "path": f"{DATA_DIR}/non_existent", |
| 91 | + "path_type": "Any", |
| 92 | + "expected": False, |
| 93 | + }, |
| 94 | + ], |
| 95 | +) |
| 96 | +def test_is_valid_path(test_case): |
| 97 | + assert _is_valid_path(test_case["path"], test_case["path_type"]) == test_case["expected"] |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize( |
| 101 | + "test_case", |
| 102 | + [ |
| 103 | + { |
| 104 | + "base": "test", |
| 105 | + "max_length": 5, |
| 106 | + }, |
| 107 | + { |
| 108 | + "base": "1111111111" * 7, |
| 109 | + "max_length": None, |
| 110 | + }, |
| 111 | + ], |
| 112 | +) |
| 113 | +def test_get_unique_name(test_case): |
| 114 | + assert ( |
| 115 | + len(_get_unique_name(test_case["base"], test_case.get("max_length"))) |
| 116 | + <= test_case["max_length"] |
| 117 | + if test_case.get("max_length") |
| 118 | + else 63 |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.parametrize( |
| 123 | + "test_case", |
| 124 | + [ |
| 125 | + { |
| 126 | + "image": "123456789012.dkr.ecr.us-west-2.amazonaws.com/my-custom-image:latest", |
| 127 | + "expected": "my-custom-image", |
| 128 | + }, |
| 129 | + { |
| 130 | + "image": "my-custom-image:latest", |
| 131 | + "expected": "my-custom-image", |
| 132 | + }, |
| 133 | + { |
| 134 | + "image": "public.ecr.aws/docker/library/my-custom-image:latest", |
| 135 | + "expected": "my-custom-image", |
| 136 | + }, |
| 137 | + ], |
| 138 | +) |
| 139 | +def test_get_repo_name_from_image(test_case): |
| 140 | + assert _get_repo_name_from_image(test_case["image"]) == test_case["expected"] |
0 commit comments