def test_simple_choice_new_3_3(): test = StateMachine( Comment= "This is a simple state machine with a single choice and three end states." ) result_a = Task( "ResultA", Resource="arn:aws:lambda:us-east-1:123456789012:function:A").end() result_b1 = test.add_state( Task("ResultB1", Resource="arn:aws:lambda:us-east-1:123456789012:function:B1")) result_b1.then( Task("ResultB2", Resource="arn:aws:lambda:us-east-1:123456789012:function:B2").end( )) unknown = Fail("Unknown", Error="Unhandled Case", Cause="Unknown Value") decision = test.start_with(Choice("TheBeginning")) decision.if_(VariablePath("$.value") == "A").then(result_a) decision.if_(VariablePath("$.value") == "B").then(result_b1) decision.else_(unknown) expected = state_machine_body("simple-choice") actual = test.to_dict() assert actual == expected
def test_parallel(): name = "FunWithMath" test = Parallel( name, Branches=[ StateMachine(StartAt="Add", States={ "Add": Task("Add", Resource="arn:aws:states:::task:Add", End=True) }), StateMachine( StartAt="Subtract", States={ "Subtract": Task("Subtract", Resource="arn:aws:states:::task:Subtract", End=True) }, ), ], Next="NextState", ) _load_and_test_vector(kind="parallel", name=name, value=test)
def test_simple_map(): test = StateMachine( Comment="Simple state machine with one map state", StartAt="ValidateAll", States=dict(ValidateAll=Map( "ValidateAll", InputPath="$.detail", ItemsPath="$.shipped", MaxConcurrency=0, Iterator=StateMachine( StartAt="Validate", States=dict(Validate=Task( "Validate", Resource= "arn:aws:lambda:us-east-1:123456789012:function:ship-val", End=True)), ), ResultPath="$.detail.shipped", End=True, )), ) expected = state_machine_body("simple-map") actual = test.to_dict() assert actual == expected
def test_three_tasks(): expected = state_machine_body("three-tasks") test = StateMachine( Comment="This is a state machine with three simple tasks.", StartAt="TaskOne", States=dict( TaskOne=Task( "TaskOne", Resource="arn:aws:lambda:us-east-1:123456789012:function:One", Next="TaskTwo"), TaskTwo=Task( "TaskTwo", Resource="arn:aws:lambda:us-east-1:123456789012:function:Two", Next="TaskThree"), TaskThree=Task( "TaskThree", Resource="arn:aws:lambda:us-east-1:123456789012:function:Three", End=True), ), ) actual = test.to_dict() assert actual == expected
def test_simple_parallel(): expected = state_machine_body("simple-parallel") test = StateMachine( Comment="Parallel Example.", StartAt="LookupCustomerInfo", States=dict(LookupCustomerInfo=Parallel( "LookupCustomerInfo", End=True, Branches=[ StateMachine( StartAt="LookupAddress", States=dict(LookupAddress=Task( "LookupAddress", Resource= "arn:aws:lambda:us-east-1:123456789012:function:AddressFinder", End=True, )), ), StateMachine( StartAt="LookupPhone", States=dict(LookupPhone=Task( "LookupPhone", Resource= "arn:aws:lambda:us-east-1:123456789012:function:PhoneFinder", End=True, )), ), ], )), ) actual = test.to_dict() assert actual == expected
def test_accretion_builder_new_1(): parse_requirements = Task("ParseRequirements", Resource=PARSE_REQUIREMENTS_RESOURCE) build_python = Parallel("BuildPython", ResultPath="$.BuildResults") build_python_36 = build_python.add_branch() build_python_36.start_with( Task("BuildPython36", Resource=BUILD_PYTHON_36_RESOURCE)).end() build_python_37 = build_python.add_branch() build_python_37.start_with( Task("BuildPython37", Resource=BUILD_PYTHON_37_RESOURCE)).end() unknown_language = Fail("UnknownLanguage", Cause="Invalid language") test = StateMachine(Comment="Artifact Builder") select_language = test.start_with(parse_requirements).then( Choice("SelectLanguage")) # TODO: Auto-add children to parent if they were added before the choice was added to parent # TODO: Add Choice.elseif_() ? select_language.if_( VariablePath("$.Language") == "python").then(build_python) select_language.else_(unknown_language) build_python.end() compare_state_machine("accretion_builder", test)
def test_three_tasks_new_1(): test = StateMachine( Comment="This is a state machine with three simple tasks.") task_one = test.start_with( Task("TaskOne", Resource="arn:aws:lambda:us-east-1:123456789012:function:One")) task_two = task_one.then( Task("TaskTwo", Resource="arn:aws:lambda:us-east-1:123456789012:function:Two")) task_three = task_two.then( Task("TaskThree", Resource="arn:aws:lambda:us-east-1:123456789012:function:Three")) task_three.end() expected = state_machine_body("three-tasks") actual = test.to_dict() assert actual == expected
def test_hello_world(): expected = state_machine_body("hello-world") test = StateMachine( Comment="A simple minimal example of the States language", StartAt="Hello World", States={ "Hello World": Task("Hello World", Resource= "arn:aws:lambda:us-east-1:123456789012:function:HelloWorld", End=True) }, ) actual = test.to_dict() assert actual == expected
def test_accretion_builder(): test = StateMachine( Comment="Artifact Builder", StartAt="ParseRequirements", States=dict( ParseRequirements=Task("ParseRequirements", Resource=PARSE_REQUIREMENTS_RESOURCE, Next="SelectLanguage"), SelectLanguage=Choice( "SelectLanguage", Choices=[ choice_rules.StringEquals(Variable="$.Language", Value="python", Next="BuildPython") ], Default="UnknownLanguage", ), UnknownLanguage=Fail("UnknownLanguage", Cause="Invalid language"), BuildPython=Parallel( "BuildPython", Branches=[ StateMachine( StartAt="BuildPython36", States=dict(BuildPython36=Task( "BuildPython36", Resource=BUILD_PYTHON_36_RESOURCE, End=True)), ), StateMachine( StartAt="BuildPython37", States=dict(BuildPython37=Task( "BuildPython37", Resource=BUILD_PYTHON_37_RESOURCE, End=True)), ), ], ResultPath="$.BuildResults", End=True, ), ), ) compare_state_machine("accretion_builder", test)
def test_accretion_listener_new_1(): test = StateMachine(Comment="Replication Listener") event_filter = test.start_with( Task("Filter", Resource=EVENT_FILTER_RESOURCE, ResultPath="$")) skip_check = event_filter.then(Choice("ShouldProcess")) skip_check.else_(Succeed("IgnoreEvent", Comment="Ignore this event")) locate_artifact = skip_check.if_( VariablePath("$.ProcessEvent") == True).then( Task("LocateArtifact", Resource=ARTIFACT_LOCATOR_RESOURCE, ResultPath="$.Artifact")) artifact_check = locate_artifact.then(Choice("ArtifactCheck")) publisher = artifact_check.if_( VariablePath("$.Artifact.Found") == True).then( Task("PublishNewVersion", Resource=LAYER_VERSION_PUBLISHER_RESOURCE, ResultPath="$.Layer")) publisher.then( Task( "Notify", Resource="arn:aws:states:::sns:publish", Parameters=Parameters(TopicArn=NOTIFY_TOPIC, Message=JsonPath("$.Layer")), )).end() artifact_check.if_( all_( VariablePath("$.Artifact.Found") == False, VariablePath("$.Artifact.ReadAttempts") > 15)).then( Fail("ReplicationTimeout", Error="Timed out waiting for artifact to replicate")) waiter = artifact_check.else_(Wait("WaitForReplication", Seconds=60)) waiter.then(locate_artifact) compare_state_machine("accretion_listener", test)
def test_simple_choice(): test = StateMachine( Comment= "This is a simple state machine with a single choice and three end states.", StartAt="TheBeginning", States=dict( TheBeginning=Choice( "TheBeginning", Choices=[ choice_rules.StringEquals(Variable="$.value", Value="A", Next="ResultA"), choice_rules.StringEquals(Variable="$.value", Value="B", Next="ResultB1"), ], Default="Unknown", ), ResultA=Task( "ResultA", Resource="arn:aws:lambda:us-east-1:123456789012:function:A", End=True), ResultB1=Task( "ResultB1", Resource="arn:aws:lambda:us-east-1:123456789012:function:B1", Next="ResultB2"), ResultB2=Task( "ResultB2", Resource="arn:aws:lambda:us-east-1:123456789012:function:B2", End=True), Unknown=Fail("Unknown", Error="Unhandled Case", Cause="Unknown Value"), ), ) expected = state_machine_body("simple-choice") actual = test.to_dict() assert actual == expected
def test_parallel_new_1(): name = "FunWithMath" branch_1 = StateMachine() branch_1.start_with(Task("Add", Resource="arn:aws:states:::task:Add")).end() branch_2 = StateMachine() branch_2.start_with( Task("Subtract", Resource="arn:aws:states:::task:Subtract")).end() test = Parallel(name) test.add_branch(branch_1) test.add_branch(branch_2) test.Next = "NextState" _load_and_test_vector(kind="parallel", name=name, value=test)
def test_simple_map_new_1(): validate_task = Task( "Validate", Resource="arn:aws:lambda:us-east-1:123456789012:function:ship-val") state_iterator = StateMachine() state_iterator.start_with(validate_task).end() mapper = Map( "ValidateAll", InputPath="$.detail", ItemsPath="$.shipped", MaxConcurrency=0, Iterator=state_iterator, ResultPath="$.detail.shipped", ) test = StateMachine(Comment="Simple state machine with one map state") test.start_with(mapper).end() expected = state_machine_body("simple-map") actual = test.to_dict() assert actual == expected
def test_map(path_converter): name = "Validate-All" test = Map( name, InputPath=path_converter("$.detail"), ItemsPath=path_converter("$.shipped"), MaxConcurrency=0, Iterator=StateMachine( StartAt="Validate", States={ "Validate": Task("Validate", Resource= "arn:aws:lambda:us-east-1:123456789012:function:ship-val", End=True) }, ), ResultPath=path_converter("$.detail.shipped"), End=True, ) _load_and_test_vector(kind="map", name=name, value=test)
def test_required_values(kwargs): test = StateMachine(**kwargs) with pytest.raises(InvalidDefinitionError) as excinfo: test.to_dict()
def test_simple_parallel_new_2(): lookup_address = StateMachine() lookup_address.start_with( Task("LookupAddress", Resource= "arn:aws:lambda:us-east-1:123456789012:function:AddressFinder") ).end() lookup_phone = StateMachine() lookup_phone.start_with( Task( "LookupPhone", Resource="arn:aws:lambda:us-east-1:123456789012:function:PhoneFinder" )).end() parallel_run = Parallel("LookupCustomerInfo") parallel_run.add_branch(lookup_address) parallel_run.add_branch(lookup_phone) test = StateMachine(Comment="Parallel Example.") test.start_with(parallel_run).end() expected = state_machine_body("simple-parallel") actual = test.to_dict() assert actual == expected
def test_accretion_listener(): test = StateMachine( Comment="Replication Listener", StartAt="Filter", States={ "Filter": Task("Filter", Resource=EVENT_FILTER_RESOURCE, ResultPath="$", Next="ShouldProcess"), "ShouldProcess": Choice( "ShouldProcess", Choices=[ choice_rules.BooleanEquals(Variable="$.ProcessEvent", Value=True, Next="LocateArtifact") ], Default="IgnoreEvent", ), "IgnoreEvent": Succeed("IgnoreEvent", Comment="Ignore this event"), "LocateArtifact": Task("LocateArtifact", Resource=ARTIFACT_LOCATOR_RESOURCE, ResultPath="$.Artifact", Next="ArtifactCheck"), "ArtifactCheck": Choice( "ArtifactCheck", Choices=[ choice_rules.BooleanEquals(Variable="$.Artifact.Found", Value=True, Next="PublishNewVersion"), choice_rules.And( Rules=[ choice_rules.BooleanEquals( Variable="$.Artifact.Found", Value=False), choice_rules.NumericGreaterThan( Variable="$.Artifact.ReadAttempts", Value=15), ], Next="ReplicationTimeout", ), ], Default="WaitForReplication", ), "ReplicationTimeout": Fail("ReplicationTimeout", Error="Timed out waiting for artifact to replicate"), "WaitForReplication": Wait("WaitForReplication", Seconds=60, Next="LocateArtifact"), "PublishNewVersion": Task("PublishNewVersion", Resource=LAYER_VERSION_PUBLISHER_RESOURCE, ResultPath="$.Layer", Next="Notify"), "Notify": Task( "Notify", Resource="arn:aws:states:::sns:publish", Parameters=Parameters(**{ "TopicArn": NOTIFY_TOPIC, "Message.$": "$.Layer" }), End=True, ), }, ) compare_state_machine("accretion_listener", test)