def test_update(make_stubber, role_arn, error_code):
    stepfunctions_client = boto3.client('stepfunctions')
    stepfunctions_stubber = make_stubber(stepfunctions_client)
    state_machine = StepFunctionsStateMachine(stepfunctions_client)
    state_machine.state_machine_arn = 'test-arn'
    definition = {'Comment': 'test-definition'}

    stepfunctions_stubber.stub_update_state_machine(
        state_machine.state_machine_arn,
        definition,
        role_arn,
        error_code=error_code)

    if error_code is None:
        state_machine.update(definition, role_arn)
    else:
        with pytest.raises(ClientError) as exc_info:
            state_machine.update(definition, role_arn)
        assert exc_info.value.response['Error']['Code'] == error_code
コード例 #2
0
def usage_demo(state_machine_name, resources):
    """
    Creates and runs a Step Functions state machine that calls a Lambda function to
    retrieve message records from a DynamoDB table and record them as sent.
    The state machine is then updated to also send messages to an Amazon SQS
    queue and the state machine is run again.
    """
    state_machine = StepFunctionsStateMachine(boto3.client('stepfunctions'))
    table = boto3.resource('dynamodb').Table(resources['MessageTableName'])
    queue = boto3.resource('sqs').Queue(resources['SendQueueUrl'])

    state_machine_arn = state_machine.find(state_machine_name)
    if state_machine_arn is None:
        print("Create a message pump state machine.")
        definition = make_definition(resources, False)
        state_machine.create(state_machine_name, definition,
                             resources['StepRoleArn'])

    print("Put three messages in the message table.")
    for user_name, message in [
        ('wills', 'Brevity is the soul of wit.'),
        ('janea',
         'Let us never underestimate the power of a well-written letter.'),
        ('lewisc',
         "I have proved by actual trial that a letter, that takes an "
         "hour to write, takes only about 3 minutes to read!")
    ]:
        table.put_item(
            Item={
                'user_name': user_name,
                'message': message,
                'message_id': str(time.time_ns()),
                'sent': False
            })

    print("Start the state machine.")
    run_arn = state_machine.start_run(f"run-without-sqs-{time.time_ns()}")
    print("Wait a few seconds for the state machine to run...")
    time.sleep(10)
    print("Verify that the messages in DynamoDB are marked as sent.")
    messages = table.scan()['Items']
    pprint(messages)

    print("Stop the state machine.")
    state_machine.stop_run(run_arn, "Stop to update for demo.")
    runs = state_machine.list_runs('RUNNING')
    while runs:
        time.sleep(5)
        runs = state_machine.list_runs('RUNNING')

    print("Update the state machine so it sends messages to Amazon SQS.")
    definition = make_definition(resources, True)
    state_machine.update(definition)
    time.sleep(5)

    print("Reset the messages in the DynamoDB table to not sent.")
    for msg in table.scan()['Items']:
        table.update_item(Key={
            'user_name': msg['user_name'],
            'message_id': msg['message_id']
        },
                          UpdateExpression='SET sent=:s',
                          ExpressionAttributeValues={':s': False})

    print("Restart the state machine.")
    run_arn = state_machine.start_run(f"run-with-sqs-{time.time_ns()}")
    print("Wait for state machine to process messages...")
    time.sleep(15)
    print("Retrieve messages from Amazon SQS.")
    poll_for_messages(queue)

    print("Put another message in the table.")
    table.put_item(
        Item={
            'user_name': 'wills',
            'message': 'Action is eloquence.',
            'message_id': str(time.time_ns()),
            'sent': False
        })
    print("Give the state machine time to find and process the message.")
    time.sleep(15)
    print("Get messages from Amazon SQS.")
    poll_for_messages(queue)

    print("Stop the run.")
    state_machine.stop_run(run_arn, "Done with demo.")