From 0b591d0f05630fcdc996ef33b3d2bede9dd47223 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 15 Feb 2016 12:45:24 -0500 Subject: [PATCH 1/3] use-before-call-event --- placebo/pill.py | 30 ++++++++++-------------------- requirements.txt | 2 +- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/placebo/pill.py b/placebo/pill.py index 2c0cdd2..df48a6c 100644 --- a/placebo/pill.py +++ b/placebo/pill.py @@ -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 = [] @@ -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) @@ -141,23 +138,14 @@ def record(self, services='*', operations='*'): 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 - 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, self._uuid) self._mode = 'playback' def stop(self): @@ -169,9 +157,10 @@ def stop(self): event, unique_id=self._uuid) 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, self._uuid) + self.events = [] self._mode = None def _record_data(self, http_response, parsed, model, **kwargs): @@ -247,11 +236,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) diff --git a/requirements.txt b/requirements.txt index 9da4035..3d6bad8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -boto3==1.2.2 +boto3==1.2.3 mock==1.3.0 nose==1.3.7 From 6b07916caa6444ae1ff189b8bb97f0191f9f2fb8 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 15 Feb 2016 14:10:24 -0500 Subject: [PATCH 2/3] Fixing tests --- placebo/pill.py | 9 +++++---- tests/unit/test_pill.py | 2 ++ tests/unit/test_save.py | 8 ++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/placebo/pill.py b/placebo/pill.py index df48a6c..06b57f9 100644 --- a/placebo/pill.py +++ b/placebo/pill.py @@ -136,7 +136,7 @@ def record(self, services='*', operations='*'): LOG.debug('recording: %s', event) self.events.append(event) self._session.events.register( - event, self._record_data, self._uuid) + event, self._record_data, 'placebo-record-mode') def playback(self): if self.mode == 'record': @@ -145,7 +145,7 @@ def playback(self): event = 'before-call.*.*' self.events.append(event) self._session.events.register( - event, self._mock_request, self._uuid) + event, self._mock_request, 'placebo-playback-mode') self._mode = 'playback' def stop(self): @@ -154,12 +154,13 @@ 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._session: for event in self.events: - self._session.events.unregister(event, self._uuid) + self._session.events.unregister( + event, unique_id='placebo-playback-mode') self.events = [] self._mode = None diff --git a/tests/unit/test_pill.py b/tests/unit/test_pill.py index 4938e02..a673ff6 100644 --- a/tests/unit/test_pill.py +++ b/tests/unit/test_pill.py @@ -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): diff --git a/tests/unit/test_save.py b/tests/unit/test_save.py index a7e7b47..9b11313 100644 --- a/tests/unit/test_save.py +++ b/tests/unit/test_save.py @@ -78,8 +78,8 @@ 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() @@ -87,7 +87,6 @@ def test_ec2(self): 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) @@ -95,6 +94,7 @@ def test_ec2_multiple_responses(self): '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() @@ -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) From 2cde2f7cc22784e49226c87733355239279bb2dc Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 15 Feb 2016 17:03:16 -0500 Subject: [PATCH 3/3] bumping version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b604190..cf50378 100755 --- a/setup.py +++ b/setup.py @@ -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',