Code Monkey home page Code Monkey logo

placebo's Introduction

placebo

Build Status

Code Health

Placebo allows you to mock boto3 calls that look just like normal calls but actually have no effect at all. It does this by allowing you to record a set of calls and save them to a data file and then replay those calls later (e.g. in a unit test) without ever hitting the AWS endpoints.

Installation

$ pip install placebo

Quickstart

Placebo uses the event mechanism in botocore to do most of its work. To start with, you need a boto3 Session object.

import boto3
import placebo

session = boto3.Session()

Once you have a Session object, you can tell placebo about the Session like this:

pill = placebo.attach(session, data_path='/path/to/response/directory')

The data_path is a path to a directory where you want responses to be stored or that contains previously recorded responses you want to playback.

The attach function returns an instance of a Pill object. This object will be used to control all recording and playback of requests for all clients created by this session object.

The first thing you will probably want to do is record some requests. To do this, simply:

pill.record()

By default, the record method will cause all responses from all services to be recorded to the data_path. If you are only interested in responses from one certain services, you can limit the recording by passing in a list of service names.

pill.record(services='ec2,iam')

This would limit to recording to responses from the ec2 service and the iam service. If you want to restrict recording to only certain operations in a single service, you can do this:

pill.record(services='ec2', operations='DescribeInstances,DescribeKeyPairs')

From this point on, any clients that match the recording specification and are created from the session will be placebo-aware. To record responses, just create the client and use it as you normally would.

lambda = session.client('lambda')
lambda.list_functions()
... more lambda calls ...

Each response will be saved as an individual JSON data file in the data_path path you specified when you attached the session. Multiple responses from the same service and operation are stored as separate files and will be replayed in the same order on playback.

Later, to replay saved requests:

import boto3
import placebo

session = boto3.Session()
pill = placebo.attach(session, data_path='/path/to/response/directory')
pill.playback()
lambda = session.client('lambda')
lambda.list_functions()
... mocked response will be returned

You can also add mocked responses manually:

list_functions_response = [
    {
        "Version": "$LATEST",
        "CodeSha256": "I8Scq2g6ZKcPIvhKzvZqCiV4pDysxq4gZ+jLcMmDy5Y=",
        "FunctionName": "foobar",
        "MemorySize": 128,
        "CodeSize": 876521,
        "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:foobar",
        "Handler": "foobar.handler",
        "Role": "arn:aws:iam::123456789012:role/foobar-role",
        "Timeout": 30,
        "LastModified": "2015-11-06T22:30:32.164+0000",
        "Runtime": "python2.7",
        "Description": "Foos all of the bars"
    }]

pill.save_response(service='lambda', operation='ListFunctions',
                   response_data=list_functions_response, http_response=200)

You can add additional responses to a particular operation and the responses will be returned in order. The final parameter is the HTTP response code which is optional. The default value is 200.

placebo's People

Contributors

garnaat avatar gffhcks avatar ghicks-rmn avatar

Watchers

Rich Jones avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.