Skip to content

Commit

Permalink
Ruby Project Template Changes
Browse files Browse the repository at this point in the history
WIP: Unit tests may have a notion of a central Gemfile that isn't
compatible with per-function Gemfile pattern that `sam build` expects.
  • Loading branch information
awood45 committed Dec 18, 2018
1 parent 09f1985 commit b159512
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cookiecutter SAM for Ruby Lambda functions

This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless Hello World App based on Serverless Application Model (SAM) and Python.
This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless Hello World App based on Serverless Application Model (SAM) and Ruby.

## Recommendations

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a sample template for {{ cookiecutter.project_name }} - Below is a brief
├── README.md <-- This instructions file
├── hello_world <-- Source code for a lambda function
│ ├── app.rb <-- Lambda function code
├── Gemfile <-- Ruby dependencies
├── Gemfile <-- Ruby dependencies
├── template.yaml <-- SAM template
└── tests <-- Unit tests
└── unit
Expand All @@ -31,27 +31,16 @@ Setup Ruby Version Manager from [Ruby Version Manager](http://rvm.io/)
Run following commands

```bash
rvm install ruby-2.5.0
rvm use ruby-2.5.0
rvm --default use 2.5.0
rvm install ruby-2.5.3
rvm use ruby-2.5.3
rvm --default use 2.5.3
```


### Installing dependencies

```sam-app``` comes with a Gemfile that defines the requirements and manages installing them.

```bash
gem install bundler
bundle install
bundle install --deployment --path hello_world/vendor/bundle
```

* Step 1 installs ```bundler```which provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.
* Step 2 creates a Gemfile.lock that locks down the versions and creates the full dependency closure.
* Step 3 installs the gems to ```hello_world/vendor/bundle```.
```sam-app``` comes with a Gemfile that defines the requirements and manages installing them. The `sam build` command will install the dependencies in your function Gemfile and vendor it for deployment.

**NOTE:** As you change your dependencies during development you'll need to make sure these steps are repeated in order to execute your Lambda and/or API Gateway locally.
**NOTE:** As you change your dependencies during development you'll need to run `sam build` again in order to execute your Lambda and/or API Gateway locally.

### Local development

Expand Down Expand Up @@ -124,12 +113,10 @@ aws cloudformation describe-stacks \

## Testing

We use [Mocha](http://gofreerange.com/mocha/docs) for testing our code and you can install it using gem: ``gem install mocha``

Next, we run our initial unit tests:
Run our initial unit tests:

```bash
ruby tests/unit/test_hello.rb
ruby tests/unit/test_handler.rb
```

**NOTE**: It is recommended to use a Ruby Version Manager to manage, and work with multiple ruby environments from interpreters to sets of gems
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "httparty"

group :test do
gem "test-unit"
gem "mocha"
end
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def lambda_handler(event:, context:)
raise error
end

return {
{
:statusCode => response.code,
:body => {
:message => "Hello World!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description: >
{{ cookiecutter.project_name }}
Sample SAM Template for {{ cookiecutter.project_name }}
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'json'
require 'test/unit'
require 'mocha/test_unit'

require_relative '../../hello_world/app'

class HelloWorldTest < Test::Unit::TestCase

def setup
@event = {
def event
{
body: 'eyJ0ZXN0IjoiYm9keSJ9',
resource: '/{proxy+}',
path: '/path/to/resource',
Expand All @@ -22,24 +22,24 @@ def setup
baz: 'qux'
},
headers: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
Accept-Encoding: 'gzip, deflate, sdch',
Accept-Language: 'en-US,en;q=0.8',
Cache-Control: 'max-age=0',
CloudFront-Forwarded-Proto: 'https',
CloudFront-Is-Desktop-Viewer: 'true',
CloudFront-Is-Mobile-Viewer: 'false',
CloudFront-Is-SmartTV-Viewer: 'false',
CloudFront-Is-Tablet-Viewer: 'false',
CloudFront-Viewer-Country: 'US',
Host: '1234567890.execute-api.us-east-1.amazonaws.com',
Upgrade-Insecure-Requests: '1',
User-Agent: 'Custom User Agent String',
Via: '1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)',
X-Amz-Cf-Id: 'cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==',
X-Forwarded-For: '127.0.0.1, 127.0.0.2',
X-Forwarded-Port: '443',
X-Forwarded-Proto: 'https'
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding' => 'gzip, deflate, sdch',
'Accept-Language' => 'en-US,en;q=0.8',
'Cache-Control' => 'max-age=0',
'CloudFront-Forwarded-Proto' => 'https',
'CloudFront-Is-Desktop-Viewer' => 'true',
'CloudFront-Is-Mobile-Viewer' => 'false',
'CloudFront-Is-SmartTV-Viewer' => 'false',
'CloudFront-Is-Tablet-Viewer' => 'false',
'CloudFront-Viewer-Country' => 'US',
'Host' => '1234567890.execute-api.us-east-1.amazonaws.com',
'Upgrade-Insecure-Requests' => '1',
'User-Agent' => 'Custom User Agent String',
'Via' => '1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)',
'X-Amz-Cf-Id' => 'cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==',
'X-Forwarded-For' => '127.0.0.1, 127.0.0.2',
'X-Forwarded-Port' => '443',
'X-Forwarded-Proto' => 'https'
},
requestContext: {
accountId: '123456789012',
Expand All @@ -49,17 +49,17 @@ def setup
requestTime: '09/Apr/2015:12:34:56 +0000',
requestTimeEpoch: 1428582896000,
identity: {
cognitoIdentityPoolId: "null",
accountId: "null",
cognitoIdentityId: "null",
caller: "null",
accessKey: "null",
cognitoIdentityPoolId: 'null',
accountId: 'null',
cognitoIdentityId: 'null',
caller: 'null',
accessKey: 'null',
sourceIp: '127.0.0.1',
cognitoAuthenticationType: "null",
cognitoAuthenticationProvider: "null",
userArn: "null",
cognitoAuthenticationType: 'null',
cognitoAuthenticationProvider: 'null',
userArn: 'null',
userAgent: 'Custom User Agent String',
user: "null"
user: 'null'
},
path: '/prod/path/to/resource',
resourcePath: '/{proxy+}',
Expand All @@ -68,24 +68,17 @@ def setup
protocol: 'HTTP/1.1'
}
}
end

@mock_response = {
:statusCode => 200,
:body => {
message: "Hello World!",
location: "1.1.1.1"
}.to_json
}

def mock_response
Object.new.tap do |mock|
mock.expects(:code).returns(200)
mock.expects(:body).returns('1.1.1.1')
end
end

def test_lambda_handler
expects(:lambda_handler).with(event:@event, context:"").returns(@mock_response)
response = lambda_handler(event:@event, context:"")
json_body = JSON.parse(response[:body])

assert_equal(200, response[:statusCode])
assert_equal("Hello World!", json_body["message"])
assert_equal("1.1.1.1", json_body["location"])
HTTParty.expects(:get).with('http://checkip.amazonaws.com/').returns(mock_response)
assert_equal(lambda_handler(event: event, context: ''), expected_result)
end
end

0 comments on commit b159512

Please sign in to comment.