Exemplo n.º 1
0
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
Exemplo n.º 2
0
def build() -> StateMachine:
    workflow = StateMachine(
        Comment=
        "This is a simple state machine with a single choice and three end states."
    )

    decision = workflow.start_with(Choice("TheBeginning"))

    decision.if_(VariablePath("$.value") == "A").then(
        Task("ResultA",
             Resource="arn:aws:lambda:us-east-1:123456789012:function:A")).end(
             )

    decision.if_(VariablePath("$.value") == "B").then(
        Task("ResultB1",
             Resource="arn:aws:lambda:us-east-1:123456789012:function:B1")
    ).then(
        Task("ResultB2",
             Resource="arn:aws:lambda:us-east-1:123456789012:function:B2")
    ).end()

    decision.else_(
        Fail("Unknown", Error="Unhandled Case", Cause="Unknown Value"))

    return workflow
Exemplo n.º 3
0
def test_and_many_rule_good_with_variable_1():
    var = VariablePath("$.value")
    test = all_(var < 30, var > 20, var != 22, var != 27)
    # TODO: Change this to use ChoiceRule.then() once they can exist outside of a parent
    test.Next = "NextState"

    _load_and_test_vector(kind="and", name="Many", value=test)
Exemplo n.º 4
0
def test_and_rule_good_with_variable():
    var = VariablePath("$.value")
    test = And(Rules=[var < 30, var > 20])
    # TODO: Change this to use ChoiceRule.then() once they can exist outside of a parent
    test.Next = "NextState"

    _load_and_test_vector(kind="and", name="Simple", value=test)
Exemplo n.º 5
0
def test_not_rule_good_with_variable():
    var = VariablePath("$.value")
    test = var != "Lorem ipsum dolor sit amet"
    # TODO: Change this to use ChoiceRule.then() once they can exist outside of a parent
    test.Next = "NextState"

    _load_and_test_vector(kind="not", name="Simple", value=test)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
def test_or_rule_good_with_variable_1():
    var = VariablePath("$.value")

    test = any_(var < 20, var > 30)
    # TODO: Change this to use ChoiceRule.then() once they can exist outside of a parent
    test.Next = "NextState"

    _load_and_test_vector(kind="or", name="Simple", value=test)
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
def test_timestamp_good_with_variable(name, op, value):
    test = op(VariablePath("$.value"), value)
    # TODO: Change this to use ChoiceRule.then() once they can exist outside of a parent
    test.Next = "NextState"

    _load_and_test_vector(kind="timestamp", name=name, value=test)
Exemplo n.º 10
0
from rhodes.choice_rules import VariablePath
from rhodes.structures import JsonPath

pytestmark = [pytest.mark.local, pytest.mark.functional]


RAW_PATH = "$.path.to.value"


class MyEnum(Enum):
    MY_PATH = RAW_PATH


@pytest.mark.parametrize(
    "path_value",
    (
        pytest.param(RAW_PATH, id="string"),
        pytest.param(MyEnum.MY_PATH, id="enum"),
        pytest.param(jsonpath_rw.parse(RAW_PATH), id="jsonpath_rw.JSONPath"),
        pytest.param(JsonPath(RAW_PATH), id="JsonPath"),
        pytest.param(VariablePath(RAW_PATH), id="VariablePath"),
    ),
)
def test_convert_to_json_path(path_value):
    expected = JsonPath(RAW_PATH)

    test = convert_to_json_path(path_value)

    # must be expected == test here because of VariablePath
    assert expected.path == test.path