|
| 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 sys |
| 16 | + |
| 17 | +import pasta |
| 18 | +import pytest |
| 19 | + |
| 20 | +from sagemaker.cli.compatibility.v2.modifiers import py_version |
| 21 | +from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def skip_if_py2(): |
| 26 | + # Remove once https://github.com/aws/sagemaker-python-sdk/issues/1461 is addressed. |
| 27 | + if sys.version_info.major < 3: |
| 28 | + pytest.skip("v2 migration script doesn't support Python 2.") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def constructor_framework_templates(): |
| 33 | + return ( |
| 34 | + "TensorFlow({})", |
| 35 | + "sagemaker.tensorflow.TensorFlow({})", |
| 36 | + "sagemaker.tensorflow.estimator.TensorFlow({})", |
| 37 | + "MXNet({})", |
| 38 | + "sagemaker.mxnet.MXNet({})", |
| 39 | + "sagemaker.mxnet.estimator.MXNet({})", |
| 40 | + "Chainer({})", |
| 41 | + "sagemaker.chainer.Chainer({})", |
| 42 | + "sagemaker.chainer.estimator.Chainer({})", |
| 43 | + "PyTorch({})", |
| 44 | + "sagemaker.pytorch.PyTorch({})", |
| 45 | + "sagemaker.pytorch.estimator.PyTorch({})", |
| 46 | + "SKLearn({})", |
| 47 | + "sagemaker.sklearn.SKLearn({})", |
| 48 | + "sagemaker.sklearn.estimator.SKLearn({})", |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def constructor_model_templates(): |
| 54 | + return ( |
| 55 | + "MXNetModel({})", |
| 56 | + "sagemaker.mxnet.MXNetModel({})", |
| 57 | + "sagemaker.mxnet.model.MXNetModel({})", |
| 58 | + "ChainerModel({})", |
| 59 | + "sagemaker.chainer.ChainerModel({})", |
| 60 | + "sagemaker.chainer.model.ChainerModel({})", |
| 61 | + "PyTorchModel({})", |
| 62 | + "sagemaker.pytorch.PyTorchModel({})", |
| 63 | + "sagemaker.pytorch.model.PyTorchModel({})", |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def constructor_templates(constructor_framework_templates, constructor_model_templates): |
| 69 | + return tuple(list(constructor_framework_templates) + list(constructor_model_templates)) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def constructors_no_version(constructor_templates): |
| 74 | + return (ctr.format("") for ctr in constructor_templates) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def constructors_with_version(constructor_templates): |
| 79 | + return (ctr.format("py_version='py3'") for ctr in constructor_templates) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def constructors_with_image_name(constructor_framework_templates): |
| 84 | + return (ctr.format("image_name='my:image'") for ctr in constructor_framework_templates) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.fixture |
| 88 | +def constructors_with_image(constructor_model_templates): |
| 89 | + return (ctr.format("image='my:image'") for ctr in constructor_model_templates) |
| 90 | + |
| 91 | + |
| 92 | +@pytest.fixture |
| 93 | +def constructors_version_not_needed(): |
| 94 | + return ( |
| 95 | + "TensorFlowModel()", |
| 96 | + "sagemaker.tensorflow.TensorFlowModel()", |
| 97 | + "sagemaker.tensorflow.model.TensorFlowModel()", |
| 98 | + "SKLearnModel()", |
| 99 | + "sagemaker.sklearn.SKLearnModel()", |
| 100 | + "sagemaker.sklearn.model.SKLearnModel()", |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def _test_modified(constructors, should_be): |
| 105 | + modifier = py_version.PyVersionEnforcer() |
| 106 | + for constructor in constructors: |
| 107 | + node = ast_call(constructor) |
| 108 | + if should_be: |
| 109 | + assert modifier.node_should_be_modified(node) |
| 110 | + else: |
| 111 | + assert not modifier.node_should_be_modified(node) |
| 112 | + |
| 113 | + |
| 114 | +def test_node_should_be_modified_fw_constructor_no_version(constructors_no_version): |
| 115 | + _test_modified(constructors_no_version, should_be=True) |
| 116 | + |
| 117 | + |
| 118 | +def test_node_should_be_modified_fw_constructor_with_version(constructors_with_version): |
| 119 | + _test_modified(constructors_with_version, should_be=False) |
| 120 | + |
| 121 | + |
| 122 | +def test_node_should_be_modified_fw_constructor_with_image_name(constructors_with_image_name): |
| 123 | + _test_modified(constructors_with_image_name, should_be=False) |
| 124 | + |
| 125 | + |
| 126 | +def test_node_should_be_modified_fw_constructor_with_image(constructors_with_image): |
| 127 | + _test_modified(constructors_with_image, should_be=False) |
| 128 | + |
| 129 | + |
| 130 | +def test_node_should_be_modified_fw_constructor_not_needed(constructors_version_not_needed): |
| 131 | + _test_modified(constructors_version_not_needed, should_be=False) |
| 132 | + |
| 133 | + |
| 134 | +def test_node_should_be_modified_random_function_call(): |
| 135 | + _test_modified(["sagemaker.session.Session()"], should_be=False) |
| 136 | + |
| 137 | + |
| 138 | +def test_modify_node(constructor_templates): |
| 139 | + modifier = py_version.PyVersionEnforcer() |
| 140 | + for template in constructor_templates: |
| 141 | + no_version, with_version = template.format(""), template.format("py_version='py3'") |
| 142 | + node = ast_call(no_version) |
| 143 | + modifier.modify_node(node) |
| 144 | + |
| 145 | + assert with_version == pasta.dump(node) |
0 commit comments