|
| 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 | +"""Integration test for run conditions (StepDependency triggers). |
| 14 | +
|
| 15 | +A Processing step fails on purpose. A step that depends on it with the Failed trigger |
| 16 | +runs, and a step with a plain (Completed) dependency does not. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import absolute_import |
| 20 | + |
| 21 | +import logging |
| 22 | +import time |
| 23 | +import uuid |
| 24 | + |
| 25 | +from sagemaker.core import image_uris |
| 26 | +from sagemaker.core.processing import Processor |
| 27 | +from sagemaker.mlops.workflow import StepDependency, TriggerCondition |
| 28 | +from sagemaker.mlops.workflow.pipeline import Pipeline |
| 29 | +from sagemaker.mlops.workflow.steps import ProcessingStep |
| 30 | + |
| 31 | + |
| 32 | +def _shell_step(name, command, image_uri, role, pipeline_session, depends_on=None): |
| 33 | + processor = Processor( |
| 34 | + role=role, |
| 35 | + image_uri=image_uri, |
| 36 | + instance_count=1, |
| 37 | + instance_type="ml.m5.large", |
| 38 | + entrypoint=["/bin/sh", "-c", command], |
| 39 | + sagemaker_session=pipeline_session, |
| 40 | + ) |
| 41 | + return ProcessingStep(name=name, step_args=processor.run(), depends_on=depends_on) |
| 42 | + |
| 43 | + |
| 44 | +def test_failed_trigger_runs_after_upstream_failure(pipeline_session, role, sklearn_latest_version): |
| 45 | + stamp = uuid.uuid4().hex[:8] |
| 46 | + image_uri = image_uris.retrieve( |
| 47 | + framework="sklearn", |
| 48 | + region=pipeline_session.boto_region_name, |
| 49 | + version=sklearn_latest_version, |
| 50 | + py_version="py3", |
| 51 | + instance_type="ml.m5.large", |
| 52 | + ) |
| 53 | + |
| 54 | + work = _shell_step("Work", "exit 1", image_uri, role, pipeline_session) |
| 55 | + on_failure = _shell_step( |
| 56 | + "OnFailure", |
| 57 | + "echo recovered", |
| 58 | + image_uri, |
| 59 | + role, |
| 60 | + pipeline_session, |
| 61 | + depends_on=[StepDependency(work, TriggerCondition.FAILED)], |
| 62 | + ) |
| 63 | + on_success = _shell_step( |
| 64 | + "OnSuccess", "echo done", image_uri, role, pipeline_session, depends_on=[work] |
| 65 | + ) |
| 66 | + pipeline = Pipeline( |
| 67 | + name=f"integ-run-conditions-{stamp}", |
| 68 | + steps=[work, on_failure, on_success], |
| 69 | + sagemaker_session=pipeline_session, |
| 70 | + ) |
| 71 | + |
| 72 | + try: |
| 73 | + pipeline.upsert(role_arn=role) |
| 74 | + execution = pipeline.start() |
| 75 | + |
| 76 | + status = None |
| 77 | + deadline = time.time() + 1800 |
| 78 | + while time.time() < deadline: |
| 79 | + status = execution.describe()["PipelineExecutionStatus"] |
| 80 | + if status in ("Succeeded", "Failed", "Stopped", "CompletedWithFailures"): |
| 81 | + break |
| 82 | + time.sleep(30) |
| 83 | + |
| 84 | + step_status = {s["StepName"]: s["StepStatus"] for s in execution.list_steps()} |
| 85 | + assert status == "CompletedWithFailures", f"status={status}, steps={step_status}" |
| 86 | + assert step_status["Work"] == "Failed" |
| 87 | + assert step_status["OnFailure"] == "Succeeded" |
| 88 | + assert "OnSuccess" not in step_status |
| 89 | + finally: |
| 90 | + try: |
| 91 | + pipeline.delete() |
| 92 | + except Exception as e: |
| 93 | + logging.warning("Failed to delete pipeline %s: %s", pipeline.name, e) |
0 commit comments