Use boto, ensure with moto

Use boto, ensure with moto

Published Sept. 24, 2015 in Cloud, Development - Last update on Sept. 23, 2015.

I work since short time at Outscale, it is a cloud provider using a Amazon like API. Like HP Eucalyptus, Outscale made the choice to copy/paste AWS's API and takes benefits of the most used cloud API with:

  • A well known API with many third part tools
  • A simple transition between them and AWS

I already workt with boto and Outscale is pretty easy to use with it, but as dev I love to build strong app starting by unit tests. And the most simple solution for write tests using boto is moto: a library that allows your python tests to easily mock out the boto library.

For 95% of boto's users, this library could be explain simply by examples, as they made in their README.

Imagine you have a function that you use to launch new ec2 instances:

import boto

def add_servers(ami_id, count):
    conn = boto.connect_ec2('the_key', 'the_secret')
    for index in range(count):

To test it:

from . import add_servers

def test_add_servers():
    add_servers('ami-1234abcd', 2)

    conn = boto.connect_ec2('the_key', 'the_secret')
    reservations = conn.get_all_instances()
    assert len(reservations) == 2
    instance1 = reservations[0].instances[0]
    assert instance1.image_id == 'ami-1234abcd'

What else ?

Moto works with 4 ways:

  • Decorator: As explain above a simple decorator allows to mock out simply
  • Context manager: Mock via with statement
  • Manually: Launch and shut down with start() and stop() methods
  • Stand-alone server: Create an HTTP proxy which mock out the API

Each method has pros and cons but the 3 firsts are usable only in Python, they are various methods to make the same thing. The stand-alone server has the advantage to create a persistent and shared environment, being a seperate process you can use it with multiple clients and its components will not be cleaned after Python thread's end.

Note about the server

It is not a real HTTP server but an HTTP proxy, you must not change your endpoint's URL but your proxy settings. It is using Flask as server and Jinja for create response, well know tools.

Reference and links


Post your comment

Comment as . Log out.