def test_left_and_right_primitives(): cond = ConditionEquals(left=2, right=1) assert cond.to_request() == { "Type": "Equals", "LeftValue": 2, "RightValue": 1, }
def test_condition_equals(): param = ParameterInteger(name="MyInt") cond = ConditionEquals(left=param, right=1) assert cond.to_request() == { "Type": "Equals", "LeftValue": { "Get": "Parameters.MyInt" }, "RightValue": 1, }
def test_condition_equals_parameter(): param1 = ParameterInteger(name="MyInt1") param2 = ParameterInteger(name="MyInt2") cond = ConditionEquals(left=param1, right=param2) assert cond.to_request() == { "Type": "Equals", "LeftValue": { "Get": "Parameters.MyInt1" }, "RightValue": { "Get": "Parameters.MyInt2" }, }
def test_fail_step_with_join_fn_in_error_message(): param = ParameterInteger(name="MyInt", default_value=2) cond = ConditionEquals(left=param, right=1) step_cond = ConditionStep( name="CondStep", conditions=[cond], if_steps=[], else_steps=[], ) step_fail = FailStep( name="FailStep", error_message=Join(on=": ", values=[ "Failed due to xxx == yyy returns", step_cond.properties.Outcome ]), ) pipeline = Pipeline( name="MyPipeline", steps=[step_cond, step_fail], parameters=[param], ) _expected_dsl = [ { "Name": "CondStep", "Type": "Condition", "Arguments": { "Conditions": [{ "Type": "Equals", "LeftValue": { "Get": "Parameters.MyInt" }, "RightValue": 1 }], "IfSteps": [], "ElseSteps": [], }, }, { "Name": "FailStep", "Type": "Fail", "Arguments": { "ErrorMessage": { "Std:Join": { "On": ": ", "Values": [ "Failed due to xxx == yyy returns", { "Get": "Steps.CondStep.Outcome" }, ], } } }, }, ] assert json.loads(pipeline.definition())["Steps"] == _expected_dsl
def test_two_step_fail_pipeline_with_str_err_msg(sagemaker_session, role, pipeline_name): param = ParameterInteger(name="MyInt", default_value=2) cond = ConditionEquals(left=param, right=1) step_fail = FailStep( name="FailStep", error_message="Failed due to hitting in else branch", ) step_cond = ConditionStep( name="CondStep", conditions=[cond], if_steps=[], else_steps=[step_fail], ) pipeline = Pipeline( name=pipeline_name, steps=[step_cond], sagemaker_session=sagemaker_session, parameters=[param], ) try: response = pipeline.create(role) pipeline_arn = response["PipelineArn"] execution = pipeline.start(parameters={}) response = execution.describe() assert response["PipelineArn"] == pipeline_arn try: execution.wait(delay=30, max_attempts=60) except WaiterError: pass execution_steps = execution.list_steps() assert len(execution_steps) == 2 for execution_step in execution_steps: if execution_step["StepName"] == "CondStep": assert execution_step["StepStatus"] == "Succeeded" continue assert execution_step["StepName"] == "FailStep" assert execution_step["StepStatus"] == "Failed" assert execution_step[ "FailureReason"] == "Failed due to hitting in else branch" metadata = execution_steps[0]["Metadata"]["Fail"] assert metadata[ "ErrorMessage"] == "Failed due to hitting in else branch" # Check FailureReason field in ListPipelineExecutions executions = sagemaker_session.sagemaker_client.list_pipeline_executions( PipelineName=pipeline.name)["PipelineExecutionSummaries"] assert len(executions) == 1 assert executions[0]["PipelineExecutionStatus"] == "Failed" assert ("Step failure: One or multiple steps failed" in executions[0]["PipelineExecutionFailureReason"]) finally: try: pipeline.delete() except Exception: pass
def test_invalid_pipeline_depended_on_fail_step(sagemaker_session, role, pipeline_name): param = ParameterInteger(name="MyInt", default_value=2) cond = ConditionEquals(left=param, right=1) step_fail = FailStep( name="FailStep", error_message="Failed pipeline execution", ) step_cond = ConditionStep( name="CondStep", conditions=[cond], if_steps=[], else_steps=[], depends_on=["FailStep"], ) pipeline = Pipeline( name=pipeline_name, steps=[step_cond, step_fail], sagemaker_session=sagemaker_session, parameters=[param], ) try: with pytest.raises(Exception) as error: pipeline.create(role) assert "CondStep can not depends on FailStep" in str(error.value) finally: try: pipeline.delete() except Exception: pass
def test_condition_not(): param = ParameterString(name="MyStr") cond_eq = ConditionEquals(left=param, right="foo") cond_not = ConditionNot(expression=cond_eq) assert cond_not.to_request() == { "Type": "Not", "Expression": { "Type": "Equals", "LeftValue": { "Get": "Parameters.MyStr" }, "RightValue": "foo", }, }
def test_condition_step(): param = ParameterInteger(name="MyInt") cond = ConditionEquals(left=param, right=1) step1 = CustomStep("MyStep1") step2 = CustomStep("MyStep2") cond_step = ConditionStep( name="MyConditionStep", depends_on=["TestStep"], conditions=[cond], if_steps=[step1], else_steps=[step2], ) cond_step.add_depends_on(["SecondTestStep"]) assert cond_step.to_request() == { "Name": "MyConditionStep", "Type": "Condition", "DependsOn": ["TestStep", "SecondTestStep"], "Arguments": { "Conditions": [ { "Type": "Equals", "LeftValue": { "Get": "Parameters.MyInt" }, "RightValue": 1, }, ], "IfSteps": [ { "Name": "MyStep1", "Type": "Training", "Arguments": {}, }, ], "ElseSteps": [{ "Name": "MyStep2", "Type": "Training", "Arguments": {}, }], }, } assert cond_step.properties.Outcome.expr == { "Get": "Steps.MyConditionStep.Outcome" }