Skip to content

Commit

Permalink
Merge branch 'release-0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
garnaat committed Feb 15, 2016
2 parents f894cf1 + 2cde2f7 commit a17ef26
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
35 changes: 13 additions & 22 deletions placebo/pill.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def __init__(self, prefix=None, debug=False):
self._data_path = None
self._mode = None
self._session = None
self._save_make_request = None
self._index = {}
self.events = []
self.clients = []
Expand Down Expand Up @@ -118,8 +117,6 @@ def _create_client(self, class_attributes, base_classes, **kwargs):

def add_client(self, client):
self.clients.append(client)
if self._mode == 'playback':
self._playback_client(client)

def attach(self, session, data_path):
LOG.debug('attaching to session: %s', session)
Expand All @@ -139,25 +136,16 @@ def record(self, services='*', operations='*'):
LOG.debug('recording: %s', event)
self.events.append(event)
self._session.events.register(
event, self._record_data, self._uuid)

def _playback_client(self, client):
if self._save_make_request is None:
self._save_make_request = client._endpoint.make_request
client._endpoint.make_request = self._make_request
event, self._record_data, 'placebo-record-mode')

def playback(self):
# This is kind of sketchy. We need to short-circuit the request
# process in botocore so we don't make any network requests. The
# best way I have found is to mock out the make_request method of
# the Endpoint associated with the client but this is not a public
# attribute of the client so could change in the future.
if self.mode == 'record':
self.stop()
if self.mode is None:
LOG.debug('playback')
for client in self.clients:
self._playback_client(client)
event = 'before-call.*.*'
self.events.append(event)
self._session.events.register(
event, self._mock_request, 'placebo-playback-mode')
self._mode = 'playback'

def stop(self):
Expand All @@ -166,12 +154,14 @@ def stop(self):
if self._session:
for event in self.events:
self._session.events.unregister(
event, unique_id=self._uuid)
event, unique_id='placebo-record-mode')
self.events = []
elif self.mode == 'playback':
if self._save_make_request:
for client in self.clients:
client.make_request = self._save_make_request
if self._session:
for event in self.events:
self._session.events.unregister(
event, unique_id='placebo-playback-mode')
self.events = []
self._mode = None

def _record_data(self, http_response, parsed, model, **kwargs):
Expand Down Expand Up @@ -247,11 +237,12 @@ def load_response(self, service, operation):
return (FakeHttpResponse(response_data['status_code']),
response_data['data'])

def _make_request(self, model, request_dict):
def _mock_request(self, **kwargs):
"""
A mocked out make_request call that bypasses all network calls
and simply returns any mocked responses defined.
"""
model = kwargs.get('model')
service = model.service_model.endpoint_prefix
operation = model.name
LOG.debug('_make_request: %s.%s', service, operation)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
boto3==1.2.2
boto3==1.2.3
mock==1.3.0
nose==1.3.7
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='placebo',
version='0.5.0',
version='0.6.0',
description='Make boto3 calls that look real but have no effect',
long_description=open('README.md').read(),
author='Mitch Garnaat',
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_pill.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def test_record(self):
def test_playback(self):
self.pill.playback()
self.assertEqual(self.pill.mode, 'playback')
self.assertEqual(self.pill.events, ['before-call.*.*'])
self.pill.stop()
self.assertEqual(self.pill.events, [])

def test_clients(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ def test_ec2(self):
self.pill.save_response(
'ec2', 'DescribeAddresses', addresses_result_one)
self.assertEqual(len(os.listdir(self.data_path)), 1)
ec2_client = self.session.client('ec2')
self.pill.playback()
ec2_client = self.session.client('ec2')
result = ec2_client.describe_addresses()
self.assertEqual(result['Addresses'][0]['PublicIp'], '192.168.0.1')
result = ec2_client.describe_addresses()
self.assertEqual(result['Addresses'][0]['PublicIp'], '192.168.0.1')

def test_ec2_multiple_responses(self):
self.assertEqual(len(os.listdir(self.data_path)), 0)
ec2_client = self.session.client('ec2')
self.pill.save_response(
'ec2', 'DescribeKeyPairs', kp_result_one)
self.assertEqual(len(os.listdir(self.data_path)), 1)
self.pill.save_response(
'ec2', 'DescribeKeyPairs', kp_result_two)
self.assertEqual(len(os.listdir(self.data_path)), 2)
self.pill.playback()
ec2_client = self.session.client('ec2')
result = ec2_client.describe_key_pairs()
self.assertEqual(result['KeyPairs'][0]['KeyName'], 'foo')
result = ec2_client.describe_key_pairs()
Expand All @@ -104,10 +104,10 @@ def test_ec2_multiple_responses(self):

def test_multiple_clients(self):
self.assertEqual(len(os.listdir(self.data_path)), 0)
ec2_client = self.session.client('ec2')
iam_client = self.session.client('iam')
self.pill.save_response(
'ec2', 'DescribeAddresses', addresses_result_one)
self.pill.playback()
ec2_client = self.session.client('ec2')
iam_client = self.session.client('iam')
result = ec2_client.describe_addresses()
self.assertEqual(len(os.listdir(self.data_path)), 1)

0 comments on commit a17ef26

Please sign in to comment.