forked from garnaat/placebo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request garnaat#41 from Miserlou/decorator
Adds @placebo_session decorator
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import placebo | ||
import boto3 | ||
import os | ||
import functools | ||
|
||
PLACEBO_DIR = os.path.join(os.path.dirname(__file__), 'placebo') | ||
|
||
|
||
def placebo_session(function): | ||
""" | ||
Decorator to help do testing with placebo. | ||
Simply wrap the function you want to test and make sure to add | ||
a "session" argument so the decorator can pass the placebo session. | ||
Accepts the following environment variables to configure placebo: | ||
PLACEBO_MODE: set to "record" to record AWS calls and save them | ||
PLACEBO_PROFILE: optionally set an AWS credential profile to record with | ||
""" | ||
|
||
@functools.wraps(function) | ||
def wrapper(*args, **kwargs): | ||
session_kwargs = { | ||
'region_name': os.environ.get('AWS_DEFAULT_REGION', 'us-east-1') | ||
} | ||
profile_name = os.environ.get('PLACEBO_PROFILE', None) | ||
if profile_name: | ||
session_kwargs['profile_name'] = profile_name | ||
|
||
session = boto3.Session(**session_kwargs) | ||
|
||
self = args[0] | ||
prefix = self.__class__.__name__ + '.' + function.__name__ | ||
record_dir = os.path.join(PLACEBO_DIR, prefix) | ||
|
||
if not os.path.exists(record_dir): | ||
os.makedirs(record_dir) | ||
|
||
pill = placebo.attach(session, data_path=record_dir) | ||
|
||
if os.environ.get('PLACEBO_MODE') == 'record': | ||
pill.record() | ||
else: | ||
pill.playback() | ||
|
||
kwargs['session'] = session | ||
|
||
return function(*args, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import unittest | ||
import os | ||
import shutil | ||
|
||
import boto3 | ||
import mock | ||
|
||
from placebo.utils import placebo_session | ||
|
||
class TestUtils(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.environ = {} | ||
self.environ_patch = mock.patch('os.environ', self.environ) | ||
self.environ_patch.start() | ||
credential_path = os.path.join(os.path.dirname(__file__), 'cfg', | ||
'aws_credentials') | ||
self.environ['AWS_SHARED_CREDENTIALS_FILE'] = credential_path | ||
self.environ['PLACEBO_MODE'] = 'record' | ||
self.session = boto3.Session(profile_name='foobar', | ||
region_name='us-west-2') | ||
|
||
@placebo_session | ||
def test_decorator(self, session): | ||
|
||
# Tear it up.. | ||
PLACEBO_TEST_DIR = os.path.join(os.getcwd(),'placebo', 'placebo') | ||
prefix = 'TestUtils.test_decorator' | ||
record_dir = os.path.join(PLACEBO_TEST_DIR, prefix) | ||
self.assertTrue(os.path.exists(record_dir)) | ||
|
||
# Tear it down.. | ||
shutil.rmtree(PLACEBO_TEST_DIR) |