From 6dc9146f2001ad144288f05ad045c10a9529cc20 Mon Sep 17 00:00:00 2001 From: Matthias Luebken Date: Wed, 31 May 2017 10:17:51 +0200 Subject: [PATCH 1/4] Initial commit A simple helloworld example including unit test. --- .eslintrc.json | 3 ++- README.md | 1 + aws-node-helloworld-tested/README.md | 21 ++++++++++++++++++++ aws-node-helloworld-tested/handler.js | 13 ++++++++++++ aws-node-helloworld-tested/handler.test.js | 23 ++++++++++++++++++++++ aws-node-helloworld-tested/package.json | 14 +++++++++++++ aws-node-helloworld-tested/serverless.yml | 18 +++++++++++++++++ package.json | 5 +++++ 8 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 aws-node-helloworld-tested/README.md create mode 100644 aws-node-helloworld-tested/handler.js create mode 100644 aws-node-helloworld-tested/handler.test.js create mode 100644 aws-node-helloworld-tested/package.json create mode 100644 aws-node-helloworld-tested/serverless.yml diff --git a/.eslintrc.json b/.eslintrc.json index 9d9cf5e6f..d2e830b31 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,7 +1,8 @@ { "extends": "airbnb-base", "env": { - "node": true + "node": true, + "mocha": true }, "rules": { "strict": "off", diff --git a/README.md b/README.md index 351e4797e..1323a7f64 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder- | [Aws Fetch File And Store In S3](https://github.com/serverless/examples/tree/master/aws-node-fetch-file-and-store-in-s3)
Fetch an image from remote source (URL) and then upload the image to a S3 bucket. | nodeJS | | [Aws Function Compiled With Babel](https://github.com/serverless/examples/tree/master/aws-node-function-compiled-with-babel)
Demonstrating how to compile all your code with Babel | nodeJS | | [Aws Github Webhook Listener](https://github.com/serverless/examples/tree/master/aws-node-github-webhook-listener)
Extend your github repositories with this github webhook listener | nodeJS | +| [Aws Node Helloworld Tested](https://github.com/serverless/examples/tree/master/aws-node-helloworld-tested)
A simple example showing a http helloworld including a unit test. | nodeJS | | [Aws Iot Event](https://github.com/serverless/examples/tree/master/aws-node-iot-event)
Example on how to setup a AWS IoT Rule to send events to a Lambda function | nodeJS | | [Aws Node Rekognition Analysis S3 Image](https://github.com/serverless/examples/tree/master/aws-node-rekognition-analysis-s3-image)
Analyse an Image from an S3 Bucket with Amazon Rekognition | nodeJS | | [Aws Node Restapi Mongodb](https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb)
Serverless REST API with MongoDB using Mongoose and Bluebird | nodeJS | diff --git a/aws-node-helloworld-tested/README.md b/aws-node-helloworld-tested/README.md new file mode 100644 index 000000000..c691adbd4 --- /dev/null +++ b/aws-node-helloworld-tested/README.md @@ -0,0 +1,21 @@ +# AWS Node Helloworld Tested + +A simple example showing a http helloworld including a unit test. + +## Install + + serverless install -u https://github.com/serverless/examples/tree/master/aws-node-helloworld-tested -n helloworld + +## Deploy + + cd helloworld + serverless deploy + +## Testout endpoint + sls info ... + curl ... + +## Run local unit tests + + npm test + diff --git a/aws-node-helloworld-tested/handler.js b/aws-node-helloworld-tested/handler.js new file mode 100644 index 000000000..bbd62edf0 --- /dev/null +++ b/aws-node-helloworld-tested/handler.js @@ -0,0 +1,13 @@ +'use strict'; + +module.exports.helloworld = (event, context, callback) => { + const response = { + statusCode: 200, + body: JSON.stringify({ + message: 'Go Serverless v1.0! Your function executed successfully!', + input: event, + }), + }; + + callback(null, response); +}; diff --git a/aws-node-helloworld-tested/handler.test.js b/aws-node-helloworld-tested/handler.test.js new file mode 100644 index 000000000..a5abbafad --- /dev/null +++ b/aws-node-helloworld-tested/handler.test.js @@ -0,0 +1,23 @@ +const assert = require('assert'); + +const hander = require('./handler'); + +describe('Handler', () => { + describe('#helloworld', () => { + it('should return 200 status code', (done) => { + const callback = (err, result) => { + assert.equal('200', result.statusCode); + done(); + }; + hander.helloworld({}, {}, callback); + }); + + it('should return the hellworld message as json', (done) => { + const callback = (err, result) => { + assert.equal('Go Serverless v1.0! Your function executed successfully!', JSON.parse(result.body).message); + done(); + }; + hander.helloworld({}, {}, callback); + }); + }); +}); diff --git a/aws-node-helloworld-tested/package.json b/aws-node-helloworld-tested/package.json new file mode 100644 index 000000000..23a0451f4 --- /dev/null +++ b/aws-node-helloworld-tested/package.json @@ -0,0 +1,14 @@ +{ + "name": "aws-node-helloworld-tested", + "version": "0.0.1", + "description": "A simple example showing a http helloworld including a unit test.", + "main": "handler.js", + "scripts": { + "test": "mocha handler.test.js" + }, + "author": "Matthias Luebken", + "license": "MIT", + "devDependencies": { + "mocha": "^3.4.2" + } +} \ No newline at end of file diff --git a/aws-node-helloworld-tested/serverless.yml b/aws-node-helloworld-tested/serverless.yml new file mode 100644 index 000000000..388ac2e27 --- /dev/null +++ b/aws-node-helloworld-tested/serverless.yml @@ -0,0 +1,18 @@ +# Welcome to serverless. Read the docs at https://serverless.com/framework/docs/ + +service: helloworld + +# The `provider` block defines where your service will be deployed +provider: + name: aws + runtime: nodejs6.10 + +# The `functions` block defines what code to deploy +functions: + helloWorld: + handler: handler.helloworld + # The `events` block defines how to trigger the handler.helloWorld code + events: + - http: + path: hello-world + method: get \ No newline at end of file diff --git a/package.json b/package.json index 1c56a555e..059ab9e07 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,11 @@ "markdown-magic": "^0.1.13" }, "devDependencies": { + "eslint": "^3.19.0", + "eslint-config-airbnb": "^15.0.1", + "eslint-plugin-import": "^2.3.0", + "eslint-plugin-jsx-a11y": "^5.0.3", + "eslint-plugin-react": "^7.0.1", "markdown-magic": "^0.1.14" } } From 636a9d415956209acd7be5088860ac778bf33916 Mon Sep 17 00:00:00 2001 From: Matthias Luebken Date: Wed, 31 May 2017 10:30:59 +0200 Subject: [PATCH 2/4] updated readme --- aws-node-helloworld-tested/README.md | 37 ++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/aws-node-helloworld-tested/README.md b/aws-node-helloworld-tested/README.md index c691adbd4..e0fa18656 100644 --- a/aws-node-helloworld-tested/README.md +++ b/aws-node-helloworld-tested/README.md @@ -4,18 +4,41 @@ A simple example showing a http helloworld including a unit test. ## Install - serverless install -u https://github.com/serverless/examples/tree/master/aws-node-helloworld-tested -n helloworld + $ serverless install -u https://github.com/serverless/examples/tree/master/aws-node-helloworld-tested -n helloworld ## Deploy - cd helloworld - serverless deploy + $ cd helloworld + $ serverless deploy + Serverless: Packaging service... + Serverless: Creating Stack... + Serverless: Checking Stack create progress... + ..... + Serverless: Stack create finished... + Serverless: Uploading CloudFormation file to S3... + Serverless: Uploading artifacts... + Serverless: Uploading service .zip file to S3 (1.36 KB)... + Serverless: Updating Stack... + Serverless: Checking Stack update progress... + ............................... + Serverless: Stack update finished... + Service Information + service: hellworld + stage: dev + region: us-east-1 + api keys: + None + endpoints: + GET - https://.execute-api.us-east-1.amazonaws.com/dev/hello-world + functions: + helloWorld: hellworld-dev-helloWorld ## Testout endpoint - sls info ... - curl ... -## Run local unit tests + $ curl -sL http://.execute-api.us-east-1.amazonaws.com/dev/hello-world | jq .message + "Go Serverless v1.0! Your function executed successfully!" + - npm test +## Run local unit tests + npm test \ No newline at end of file From 8548917d63167b9f5083a1359a00fdfc8127ec2e Mon Sep 17 00:00:00 2001 From: Matthias Luebken Date: Wed, 31 May 2017 11:26:37 +0200 Subject: [PATCH 3/4] using grep to parse for endpoint --- aws-node-helloworld-tested/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-node-helloworld-tested/README.md b/aws-node-helloworld-tested/README.md index e0fa18656..cc1c62ca1 100644 --- a/aws-node-helloworld-tested/README.md +++ b/aws-node-helloworld-tested/README.md @@ -35,7 +35,7 @@ A simple example showing a http helloworld including a unit test. ## Testout endpoint - $ curl -sL http://.execute-api.us-east-1.amazonaws.com/dev/hello-world | jq .message + $ curl -sL $(sls info |grep -oEi 'http.*') | jq .message "Go Serverless v1.0! Your function executed successfully!" From 725f1ae07bb150f367b3da913c8dee0c2288eb01 Mon Sep 17 00:00:00 2001 From: Matthias Luebken Date: Wed, 31 May 2017 18:16:55 +0200 Subject: [PATCH 4/4] exclude tests for packaging --- aws-node-helloworld-tested/serverless.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aws-node-helloworld-tested/serverless.yml b/aws-node-helloworld-tested/serverless.yml index 388ac2e27..f690aef69 100644 --- a/aws-node-helloworld-tested/serverless.yml +++ b/aws-node-helloworld-tested/serverless.yml @@ -7,6 +7,11 @@ provider: name: aws runtime: nodejs6.10 +# Exclude tests for deployment +package: + exclude: + - "*.test.js" + # The `functions` block defines what code to deploy functions: helloWorld: