Skip to content

Commit

Permalink
Merge pull request #30 from mathom/config
Browse files Browse the repository at this point in the history
Rework the boto session and config handling
  • Loading branch information
Rich Jones committed Feb 26, 2016
2 parents 01907c2 + 979573a commit 1feb16c
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ python:
- "2.7"
# command to install dependencies
install:
- "pip install setuptools --upgrade; pip install coveralls; python setup.py install"
- "pip install setuptools --upgrade; pip install -r test_requirements.txt; python setup.py install"
# command to run tests
script: nosetests --with-coverage --cover-package=zappa
after_success:
Expand Down
7 changes: 2 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
boto3==1.2.3
botocore==1.3.26
boto3==1.2.5
botocore==1.3.30
docutils==0.12
filechunkio==1.6
futures==3.0.4
jmespath==0.9.0
nose==1.3.7
placebo==0.7.2
python-dateutil==2.4.2
requests>=2.9.1
six==1.10.0
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
with open(os.path.join(os.path.dirname(__file__), 'requirements.txt')) as f:
required = f.read().splitlines()

with open(os.path.join(os.path.dirname(__file__), 'test_requirements.txt')) as f:
test_required = f.read().splitlines()

setup(
name='zappa',
version='0.11.2',
packages=['zappa'],
install_requires=required,
install_requires=required,
tests_require=test_required,
test_suite='nose.collector',
include_package_data=True,
license='MIT License',
description='Serverless WSGI With AWS Lambda + API Gateway',
Expand Down
4 changes: 4 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coveralls==1.1
mock==1.3.0
nose==1.3.7
placebo==0.7.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"status_code": 201,
"data": {
"id": "j27idab94h",
"name": "123",
"description": "123 Zappa",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "ded1717d-dc3a-11e5-afc9-35deb4d0a175"
},
"createdDate": {
"hour": 21,
"__class__": "datetime",
"month": 2,
"second": 50,
"microsecond": 0,
"year": 2016,
"day": 25,
"minute": 41
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"status_code": 200,
"data": {
"items": [
{
"path": "/",
"id": "g1atjiylwd"
}
],
"ResponseMetadata": {
"HTTPStatusCode": 200,
"RequestId": "dee74371-dc3a-11e5-adc8-c7e9cc4e6647"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 404,
"data": {
"ResponseMetadata": {
"HTTPStatusCode": 404,
"RequestId": "dec9f7a4-dc3a-11e5-ae49-899567c90292"
},
"Error": {
"Message": "Invalid REST API identifier specified",
"Code": "NotFoundException"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"status_code": 201,
"data": {
"statusCode": "200",
"responseTemplates": {
"text/html": "#set($inputRoot = $input.path('$'))\n$inputRoot.Content"
},
"selectionPattern": "",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df27f5f3-dc3a-11e5-adc8-c7e9cc4e6647"
},
"responseParameters": {
"method.response.header.Content-Type": "integration.response.body.Content-Type",
"method.response.header.Location": "integration.response.body.Location",
"method.response.header.X-Frame-Options": "integration.response.body.X-Frame-Options",
"method.response.header.Status": "integration.response.body.Status",
"method.response.header.Set-Cookie": "integration.response.body.Set-Cookie"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"status_code": 201,
"data": {
"cacheKeyParameters": [],
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df135c7e-dc3a-11e5-afc9-35deb4d0a175"
},
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:12345:function:helloworld/invocations",
"httpMethod": "POST",
"requestTemplates": {
"application/json": "{\n \"body\" : $input.json('$'),\n \"headers\": {\n #foreach($header in $input.params().header.keySet())\n \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"method\": \"$context.httpMethod\",\n \"params\": {\n #foreach($param in $input.params().path.keySet())\n \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"query\": {\n #foreach($queryParam in $input.params().querystring.keySet())\n \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end\n \n #end\n } \n}",
"application/x-www-form-urlencoded": "#set($rawPostData = $input.path('$'))\n{\n \"body\" : \"$rawPostData\",\n \"headers\": {\n #foreach($header in $input.params().header.keySet())\n \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"method\": \"$context.httpMethod\",\n \"params\": {\n #foreach($param in $input.params().path.keySet())\n \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"query\": {\n #foreach($queryParam in $input.params().querystring.keySet())\n \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end\n \n #end\n } \n}"
},
"cacheNamespace": "none",
"credentials": "arn:aws:iam::12345:role/ZappaLambdaExecution",
"type": "AWS",
"requestParameters": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"status_code": 201,
"data": {
"responseModels": {
"text/html": "Empty"
},
"statusCode": "200",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df1e3225-dc3a-11e5-ae49-899567c90292"
},
"responseParameters": {
"method.response.header.Content-Type": false,
"method.response.header.Location": false,
"method.response.header.X-Frame-Options": false,
"method.response.header.Status": false,
"method.response.header.Set-Cookie": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"status_code": 201,
"data": {
"apiKeyRequired": false,
"httpMethod": "GET",
"authorizationType": "NONE",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df048f72-dc3a-11e5-adc8-c7e9cc4e6647"
}
}
}
65 changes: 41 additions & 24 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import unittest
import mock

from .utils import placebo_session

Expand Down Expand Up @@ -33,33 +34,36 @@ def test_create_lambda_package(self):

def test_load_credentials(self):
z = Zappa()
z.aws_region = 'us-east-1'
z.load_credentials()
self.assertEqual(z.boto_session.region_name, 'us-east-1')
self.assertEqual(z.aws_region, 'us-east-1')

z.aws_region = 'eu-west-1'
z.load_credentials()
self.assertEqual(z.boto_session.region_name, 'eu-west-1')
self.assertEqual(z.aws_region, 'eu-west-1')

creds = {
'AWS_ACCESS_KEY_ID': 'AK123',
'AWS_SECRET_ACCESS_KEY': 'JKL456',
'AWS_DEFAULT_REGION': 'us-west-1'
}
with mock.patch.dict('os.environ', creds):
z.aws_region = None
z.load_credentials()
loaded_creds = z.boto_session._session.get_credentials()

credentials = '[default]\naws_access_key_id = AK123\naws_secret_access_key = JKL456'
config = '[default]\noutput = json\nregion = us-east-1'

credentials_file = open('credentials', 'w')
credentials_file.write(credentials)
credentials_file.close()

config_file = open('config', 'w')
config_file.write(config)
config_file.close()

z.load_credentials('credentials', 'config')

os.remove('credentials')
os.remove('config')

self.assertTrue((z.access_key == "AK123"))
self.assertTrue((z.secret_key == "JKL456"))
self.assertTrue((z.aws_region == 'us-east-1'))
self.assertEqual(loaded_creds.access_key, 'AK123')
self.assertEqual(loaded_creds.secret_key, 'JKL456')
self.assertEqual(z.boto_session.region_name, 'us-west-1')

@placebo_session
def test_upload_remove_s3(self, session):
bucket_name = 'test_zappa_upload_s3'
z = Zappa()
z = Zappa(session)
zip_path = z.create_lambda_zip()
res = z.upload_to_s3(zip_path, bucket_name, session)
res = z.upload_to_s3(zip_path, bucket_name)
os.remove(zip_path)
self.assertTrue(res)
s3 = session.resource('s3')
Expand All @@ -72,15 +76,28 @@ def test_upload_remove_s3(self, session):
Bucket=bucket_name,
Key=zip_path,
)
res = z.remove_from_s3(zip_path, bucket_name, session)
res = z.remove_from_s3(zip_path, bucket_name)
self.assertTrue(res)

@placebo_session
def test_create_iam_roles(self, session):
z = Zappa()
arn = z.create_iam_roles(session)
z = Zappa(session)
arn = z.create_iam_roles()
self.assertEqual(arn, "arn:aws:iam::123:role/{}".format(z.role_name))

@placebo_session
def test_create_api_gateway_routes(self, session):
z = Zappa(session)
z.parameter_depth = 1
z.integration_response_codes = [200]
z.method_response_codes = [200]
z.http_methods = ['GET']
z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:helloworld'
with mock.patch('time.time', return_value=123.456):
api_id = z.create_api_gateway_routes(lambda_arn)
self.assertEqual(api_id, 'j27idab94h')

def test_policy_json(self):
# ensure the policy docs are valid JSON
json.loads(ASSUME_POLICY)
Expand Down
4 changes: 3 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def placebo_session(function):

@functools.wraps(function)
def wrapper(*args, **kwargs):
session_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
Expand Down

0 comments on commit 1feb16c

Please sign in to comment.