|
| 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 json |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from mock import Mock |
| 20 | + |
| 21 | +from sagemaker.workflow.parameters import ParameterInteger, ParameterString |
| 22 | +from sagemaker.workflow.pipeline import Pipeline |
| 23 | +from sagemaker.workflow.callback_step import CallbackStep, CallbackOutput, CallbackOutputTypeEnum |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def sagemaker_session_mock(): |
| 28 | + return Mock() |
| 29 | + |
| 30 | + |
| 31 | +def test_callback_step(): |
| 32 | + param = ParameterInteger(name="MyInt") |
| 33 | + outputParam1 = CallbackOutput(output_name="output1", output_type=CallbackOutputTypeEnum.String) |
| 34 | + outputParam2 = CallbackOutput(output_name="output2", output_type=CallbackOutputTypeEnum.Boolean) |
| 35 | + cb_step = CallbackStep( |
| 36 | + name="MyCallbackStep", |
| 37 | + depends_on=["TestStep"], |
| 38 | + sqs_queue_url="https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 39 | + inputs={"arg1": "foo", "arg2": 5, "arg3": param}, |
| 40 | + outputs=[outputParam1, outputParam2], |
| 41 | + ) |
| 42 | + cb_step.add_depends_on(["SecondTestStep"]) |
| 43 | + assert cb_step.to_request() == { |
| 44 | + "Name": "MyCallbackStep", |
| 45 | + "Type": "Callback", |
| 46 | + "DependsOn": ["TestStep", "SecondTestStep"], |
| 47 | + "SqsQueueUrl": "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 48 | + "OutputParameters": [ |
| 49 | + {"OutputName": "output1", "OutputType": "String"}, |
| 50 | + {"OutputName": "output2", "OutputType": "Boolean"}, |
| 51 | + ], |
| 52 | + "Arguments": {"arg1": "foo", "arg2": 5, "arg3": param}, |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def test_callback_step_output_expr(): |
| 57 | + param = ParameterInteger(name="MyInt") |
| 58 | + outputParam1 = CallbackOutput(output_name="output1", output_type=CallbackOutputTypeEnum.String) |
| 59 | + outputParam2 = CallbackOutput(output_name="output2", output_type=CallbackOutputTypeEnum.Boolean) |
| 60 | + cb_step = CallbackStep( |
| 61 | + name="MyCallbackStep", |
| 62 | + depends_on=["TestStep"], |
| 63 | + sqs_queue_url="https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 64 | + inputs={"arg1": "foo", "arg2": 5, "arg3": param}, |
| 65 | + outputs=[outputParam1, outputParam2], |
| 66 | + ) |
| 67 | + |
| 68 | + assert cb_step.properties.Outputs["output1"].expr == { |
| 69 | + "Get": "Steps.MyCallbackStep.OutputParameters['output1']" |
| 70 | + } |
| 71 | + assert cb_step.properties.Outputs["output2"].expr == { |
| 72 | + "Get": "Steps.MyCallbackStep.OutputParameters['output2']" |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +def test_pipeline_interpolates_callback_outputs(): |
| 77 | + parameter = ParameterString("MyStr") |
| 78 | + outputParam1 = CallbackOutput(output_name="output1", output_type=CallbackOutputTypeEnum.String) |
| 79 | + outputParam2 = CallbackOutput(output_name="output2", output_type=CallbackOutputTypeEnum.String) |
| 80 | + cb_step1 = CallbackStep( |
| 81 | + name="MyCallbackStep1", |
| 82 | + depends_on=["TestStep"], |
| 83 | + sqs_queue_url="https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 84 | + inputs={"arg1": "foo"}, |
| 85 | + outputs=[outputParam1], |
| 86 | + ) |
| 87 | + cb_step2 = CallbackStep( |
| 88 | + name="MyCallbackStep2", |
| 89 | + depends_on=["TestStep"], |
| 90 | + sqs_queue_url="https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 91 | + inputs={"arg1": cb_step1.properties.Outputs["output1"]}, |
| 92 | + outputs=[outputParam2], |
| 93 | + ) |
| 94 | + |
| 95 | + pipeline = Pipeline( |
| 96 | + name="MyPipeline", |
| 97 | + parameters=[parameter], |
| 98 | + steps=[cb_step1, cb_step2], |
| 99 | + sagemaker_session=sagemaker_session_mock, |
| 100 | + ) |
| 101 | + |
| 102 | + assert json.loads(pipeline.definition()) == { |
| 103 | + "Version": "2020-12-01", |
| 104 | + "Metadata": {}, |
| 105 | + "Parameters": [{"Name": "MyStr", "Type": "String"}], |
| 106 | + "PipelineExperimentConfig": { |
| 107 | + "ExperimentName": {"Get": "Execution.PipelineName"}, |
| 108 | + "TrialName": {"Get": "Execution.PipelineExecutionId"}, |
| 109 | + }, |
| 110 | + "Steps": [ |
| 111 | + { |
| 112 | + "Name": "MyCallbackStep1", |
| 113 | + "Type": "Callback", |
| 114 | + "Arguments": {"arg1": "foo"}, |
| 115 | + "DependsOn": ["TestStep"], |
| 116 | + "SqsQueueUrl": "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 117 | + "OutputParameters": [{"OutputName": "output1", "OutputType": "String"}], |
| 118 | + }, |
| 119 | + { |
| 120 | + "Name": "MyCallbackStep2", |
| 121 | + "Type": "Callback", |
| 122 | + "Arguments": {"arg1": {"Get": "Steps.MyCallbackStep1.OutputParameters['output1']"}}, |
| 123 | + "DependsOn": ["TestStep"], |
| 124 | + "SqsQueueUrl": "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue", |
| 125 | + "OutputParameters": [{"OutputName": "output2", "OutputType": "String"}], |
| 126 | + }, |
| 127 | + ], |
| 128 | + } |
0 commit comments