|
| 1 | +# Copyright 2020 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 | + |
| 15 | +import pasta |
| 16 | +import pytest |
| 17 | + |
| 18 | +from sagemaker.cli.compatibility.v2.modifiers import image_uris |
| 19 | +from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call, ast_import |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def methods(): |
| 24 | + return ( |
| 25 | + "get_image_uri('us-west-2', 'sagemaker-xgboost')", |
| 26 | + "sagemaker.get_image_uri(repo_region='us-west-2', repo_name='sagemaker-xgboost')", |
| 27 | + "sagemaker.amazon_estimator.get_image_uri('us-west-2', repo_name='sagemaker-xgboost')", |
| 28 | + "sagemaker.amazon.amazon_estimator.get_image_uri('us-west-2', 'sagemaker-xgboost', repo_version='1')", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def import_statements(): |
| 34 | + return ( |
| 35 | + "from sagemaker import get_image_uri", |
| 36 | + "from sagemaker.amazon_estimator import get_image_uri", |
| 37 | + "from sagemaker.amazon.amazon_estimator import get_image_uri", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_method_node_should_be_modified(methods): |
| 42 | + modifier = image_uris.ImageURIRetrieveRefactor() |
| 43 | + for method in methods: |
| 44 | + node = ast_call(method) |
| 45 | + assert modifier.node_should_be_modified(node) |
| 46 | + |
| 47 | + |
| 48 | +def test_methodnode_should_be_modified_random_call(): |
| 49 | + modifier = image_uris.ImageURIRetrieveRefactor() |
| 50 | + node = ast_call("create_image_uri()") |
| 51 | + assert not modifier.node_should_be_modified(node) |
| 52 | + |
| 53 | + |
| 54 | +def test_method_modify_node(methods, caplog): |
| 55 | + modifier = image_uris.ImageURIRetrieveRefactor() |
| 56 | + |
| 57 | + method = "get_image_uri('us-west-2', 'xgboost')" |
| 58 | + node = ast_call(method) |
| 59 | + modifier.modify_node(node) |
| 60 | + assert "image_uris.retrieve('xgboost', 'us-west-2')" == pasta.dump(node) |
| 61 | + |
| 62 | + method = "amazon_estimator.get_image_uri('us-west-2', 'xgboost')" |
| 63 | + node = ast_call(method) |
| 64 | + modifier.modify_node(node) |
| 65 | + assert "image_uris.retrieve('xgboost', 'us-west-2')" == pasta.dump(node) |
| 66 | + |
| 67 | + method = "sagemaker.get_image_uri(repo_region='us-west-2', repo_name='xgboost')" |
| 68 | + node = ast_call(method) |
| 69 | + modifier.modify_node(node) |
| 70 | + assert "sagemaker.image_uris.retrieve('xgboost', 'us-west-2')" == pasta.dump(node) |
| 71 | + |
| 72 | + method = "sagemaker.amazon_estimator.get_image_uri('us-west-2', repo_name='xgboost')" |
| 73 | + node = ast_call(method) |
| 74 | + modifier.modify_node(node) |
| 75 | + assert "sagemaker.image_uris.retrieve('xgboost', 'us-west-2')" == pasta.dump(node) |
| 76 | + |
| 77 | + method = ( |
| 78 | + "sagemaker.amazon.amazon_estimator.get_image_uri('us-west-2', 'xgboost', repo_version='1')" |
| 79 | + ) |
| 80 | + node = ast_call(method) |
| 81 | + modifier.modify_node(node) |
| 82 | + assert "sagemaker.image_uris.retrieve('xgboost', 'us-west-2', '1')" == pasta.dump(node) |
| 83 | + |
| 84 | + |
| 85 | +def test_import_from_node_should_be_modified_image_uris_input(import_statements): |
| 86 | + modifier = image_uris.ImageURIRetrieveImportFromRenamer() |
| 87 | + |
| 88 | + statement = "from sagemaker import get_image_uri" |
| 89 | + node = ast_import(statement) |
| 90 | + assert modifier.node_should_be_modified(node) |
| 91 | + |
| 92 | + statement = "from sagemaker.amazon_estimator import get_image_uri" |
| 93 | + node = ast_import(statement) |
| 94 | + assert modifier.node_should_be_modified(node) |
| 95 | + |
| 96 | + statement = "from sagemaker.amazon.amazon_estimator import get_image_uri" |
| 97 | + node = ast_import(statement) |
| 98 | + assert modifier.node_should_be_modified(node) |
| 99 | + |
| 100 | + |
| 101 | +def test_import_from_node_should_be_modified_random_import(): |
| 102 | + modifier = image_uris.ImageURIRetrieveImportFromRenamer() |
| 103 | + node = ast_import("from sagemaker.amazon_estimator import registry") |
| 104 | + assert not modifier.node_should_be_modified(node) |
| 105 | + |
| 106 | + |
| 107 | +def test_import_from_modify_node(import_statements): |
| 108 | + modifier = image_uris.ImageURIRetrieveImportFromRenamer() |
| 109 | + expected_result = "from sagemaker import image_uris" |
| 110 | + |
| 111 | + for import_statement in import_statements: |
| 112 | + node = ast_import(import_statement) |
| 113 | + modifier.modify_node(node) |
| 114 | + assert expected_result == pasta.dump(node) |
0 commit comments