aws-lambda-runner 1.4.5 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9deef3294eaceddab1deed86424fd6125708787c
4
- data.tar.gz: e2f1e5c9186acc26de923ec024cf8ee7aa8700e6
3
+ metadata.gz: c5f197efab92eddcd95b273da0e8b246b1e6f6aa
4
+ data.tar.gz: 6d5bc45491b52c7cce37100c25766c41e3a1e7ad
5
5
  SHA512:
6
- metadata.gz: 3953eea6de12d1bd95403d1734ed2ca885ce0f329f7dc48b029e51d50c6a42a271bf77a8c8dd1c6c8413e737e02bcec0a11aedaff1fac5e5af6381d6d945a1be
7
- data.tar.gz: 08ba985ea2454f49586071c800d2116943d35e1f610c260c7a2219e8cb816007120e08880217f3a4d0e40b56b79d9f5e80a26d2d9c1451300f26ab75ab8fc243
6
+ metadata.gz: 383a7964ea00cf6f5df36b43d0d52a06d1455b46d748a74177a927b71202a96c9e2fa738e1b510af05b1c175d9a10eaba18c791256b24d57dad4de9d494a2e63
7
+ data.tar.gz: 6d7435e028758058c7fd7a0aa0e723aaeb5cd2d464fd197fd01d95e79d157cfded220049fc03732cc8663955fa8fa1f249ad9861183cc045c6d7d223d38723e2
@@ -136,7 +136,7 @@ var getJobStatus = function (result) {
136
136
  return { status: status, data: responseBody };
137
137
  };
138
138
 
139
- exports.request = function(req, res, opts, handler) {
139
+ exports.request = function(req, res, opts, handler, server) {
140
140
 
141
141
  if (req.method === 'POST') {
142
142
 
@@ -150,7 +150,9 @@ exports.request = function(req, res, opts, handler) {
150
150
  var terminationMessage = 'Terminating server at http://[localhost]:' + opts.port + ' for ' + opts['module-path'] + ' / ' + opts.handler;
151
151
  res.end(terminationMessage + '\n');
152
152
  console.info(terminationMessage);
153
- server.close();
153
+ if (server) {
154
+ server.close();
155
+ }
154
156
  })();
155
157
 
156
158
  } else if (req.method === 'GET') {
@@ -13,7 +13,11 @@ var opts = stdio.getopt({
13
13
  console.info(opts);
14
14
 
15
15
  var module = require(opts['module-path']);
16
- var server = http.createServer(function (req, res) {
17
- request.request(req, res, opts, module[opts.handler]);
18
- }).listen(opts.port);
16
+ var server = http.createServer();
17
+
18
+ server.on('request', function (req, res) {
19
+ request.request(req, res, opts, module[opts.handler], server);
20
+ });
21
+
22
+ server.listen(opts.port);
19
23
  console.info('Server running at http://[localhost]:' + opts.port + ' for ' + opts['module-path'] + ' / ' + opts.handler);
@@ -52,7 +52,7 @@ module LambdaRunner
52
52
  payload = JSON.generate({ event: event, context: context })
53
53
  id = RestClient.post(url, payload, content_type: :json).to_str
54
54
  loop do
55
- response = RestClient.get(url, params: { id: id })
55
+ response = RestClient.get(url, params: { id: id }) {|response, request, res| response}
56
56
  data = JSON.parse('['+response.body+']').first
57
57
 
58
58
  case response.code
@@ -99,6 +99,28 @@ module LambdaRunner
99
99
  event
100
100
  end
101
101
 
102
+ def self.dynamodb_event(key, new_image = nil, old_image = nil)
103
+ event = load_json('sample_dynamodb_event.json')
104
+ get_dynamodb_record(event['Records'][0], key, new_image, old_image)
105
+ event
106
+ end
107
+
108
+ private_class_method def self.get_dynamodb_record(record, key, new_image, old_image)
109
+ if new_image && old_image
110
+ record['dynamodb']['NewImage'] = new_image['NewImage']
111
+ record['dynamodb']['OldImage'] = old_image['OldImage']
112
+ record['eventName'] = 'MODIFY'
113
+ elsif new_image
114
+ record['dynamodb']['NewImage'] = new_image['NewImage']
115
+ record['eventName'] = 'INSERT'
116
+ else
117
+ record['dynamodb']['OldImage'] = old_image['OldImage']
118
+ record['eventName'] = 'REMOVE'
119
+ end
120
+ record['dynamodb']['Keys'] = key
121
+ record
122
+ end
123
+
102
124
  private
103
125
 
104
126
  def self.load_json(name)
@@ -0,0 +1,19 @@
1
+ {
2
+ "Records": [
3
+ {
4
+ "eventID": "e837fca4eb43e83b3ffb681d1daa701d",
5
+ "eventName": "INSERT",
6
+ "eventVersion": "1.0",
7
+ "eventSource": "aws:dynamodb",
8
+ "awsRegion": "eu-west-1",
9
+ "dynamodb": {
10
+ "Keys": {
11
+ },
12
+ "SequenceNumber": "0",
13
+ "SizeBytes": 149,
14
+ "StreamViewType": "NEW_AND_OLD_IMAGES"
15
+ },
16
+ "eventSourceARN": "arn:aws:dynamodb:eu-west-1:123456789:table/dynamodb-table/stream/2015-09-30T16:34:32.317"
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,69 @@
1
+ require 'rspec'
2
+ require_relative '../lib/lambda_runner'
3
+
4
+ describe LambdaRunner::Events do
5
+
6
+ it "should generate an S3 event" do
7
+ data = LambdaRunner::Events.s3_event("some-bucket", "some/key")
8
+ expect(data["Records"][0]["s3"]["bucket"]["name"]).to eq("some-bucket")
9
+ expect(data["Records"][0]["s3"]["bucket"]["arn"]).to eq("arn:aws:s3:::some-bucket")
10
+ expect(data["Records"][0]["s3"]["object"]["key"]).to eq("some/key")
11
+ end
12
+
13
+ it "should generate an SNS event (data body)" do
14
+ message = { "this_is" => "the message" }
15
+ data = LambdaRunner::Events.sns_event("some-arn", "some-id", "some-timestamp", message)
16
+ expect(data["Records"][0]["Sns"]["TopicArn"]).to eq("some-arn")
17
+ expect(data["Records"][0]["Sns"]["MessageId"]).to eq("some-id")
18
+ expect(data["Records"][0]["Sns"]["Timestamp"]).to eq("some-timestamp")
19
+ expect(JSON.parse data["Records"][0]["Sns"]["Message"]).to eq(message)
20
+ end
21
+
22
+ it "should generate an SNS event (string body)" do
23
+ message = { "this_is" => "the message" }
24
+ data = LambdaRunner::Events.sns_event("some-arn", "some-id", "some-timestamp", JSON.generate(message))
25
+ expect(data["Records"][0]["Sns"]["TopicArn"]).to eq("some-arn")
26
+ expect(data["Records"][0]["Sns"]["MessageId"]).to eq("some-id")
27
+ expect(data["Records"][0]["Sns"]["Timestamp"]).to eq("some-timestamp")
28
+ expect(JSON.parse data["Records"][0]["Sns"]["Message"]).to eq(message)
29
+ end
30
+
31
+ it "should generate an DynamoDB event for insert (data body)" do
32
+ new_image = { "NewImage" => { "id" => { "S" => "1234567ABC" }, "message" => { "S" => "updated message" } } }
33
+ key = { "id" => { "S" => "1234567ABC" } }
34
+
35
+ data = LambdaRunner::Events.dynamodb_event(key, new_image)
36
+
37
+ expect(data["Records"][0]["dynamodb"]["NewImage"]).to eq(new_image["NewImage"])
38
+ expect(data["Records"][0]["dynamodb"]["Keys"]).to eq(key)
39
+ expect(data["Records"][0]["eventName"]).to eq("INSERT")
40
+ expect(data["Records"][0]["dynamodb"]["SequenceNumber"]).to eq("0")
41
+ end
42
+
43
+ it "should generate an DynamoDB event for remove (data body)" do
44
+ old_image = { "OldImage" => { "id" => { "S" => "1234567ABC" }, "message" => { "S" => "message" } } }
45
+ key = { "id" => { "S" => "1234567ABC" } }
46
+
47
+ data = LambdaRunner::Events.dynamodb_event(key, nil, old_image)
48
+
49
+ expect(data["Records"][0]["dynamodb"]["OldImage"]).to eq(old_image["OldImage"])
50
+ expect(data["Records"][0]["dynamodb"]["Keys"]).to eq(key)
51
+ expect(data["Records"][0]["eventName"]).to eq("REMOVE")
52
+ expect(data["Records"][0]["dynamodb"]["SequenceNumber"]).to eq("0")
53
+ end
54
+
55
+ it "should generate an DynamoDB event for modify (data body)" do
56
+ old_image = { "OldImage" => { "id" => { "S" => "1234567ABC" }, "message" => { "S" => "message" } } }
57
+ new_image = { "NewImage" => { "id" => { "S" => "1234567ABC" }, "message" => { "S" => "updated message" } } }
58
+ key = { "id" => { "S" => "1234567ABC" } }
59
+
60
+ data = LambdaRunner::Events.dynamodb_event(key, new_image, old_image)
61
+
62
+ expect(data["Records"][0]["dynamodb"]["OldImage"]).to eq(old_image["OldImage"])
63
+ expect(data["Records"][0]["dynamodb"]["NewImage"]).to eq(new_image["NewImage"])
64
+ expect(data["Records"][0]["dynamodb"]["Keys"]).to eq(key)
65
+ expect(data["Records"][0]["eventName"]).to eq("MODIFY")
66
+ expect(data["Records"][0]["dynamodb"]["SequenceNumber"]).to eq("0")
67
+ end
68
+
69
+ end
@@ -0,0 +1,27 @@
1
+ require 'rspec'
2
+ require_relative '../lib/lambda_runner'
3
+
4
+ describe LambdaRunner::Runner do
5
+
6
+ before(:all) do
7
+ test_js = File.expand_path("test.js", File.dirname(__FILE__))
8
+ @under_test = LambdaRunner::Runner.new(test_js, "node_0_10_42_handler")
9
+ @under_test.start
10
+ end
11
+
12
+ after(:all) do
13
+ @under_test.stop
14
+ end
15
+
16
+ it "should emulate AWS Lambda (node.js v0.10.42) - success" do
17
+ r = @under_test.process_event({ succeed: { delay: 50, result: "ohai" } })
18
+ expect(r).to eq("ohai")
19
+ end
20
+
21
+ it "should emulate AWS Lambda (node.js v0.10.42) - failure" do
22
+ expect {
23
+ @under_test.process_event({ fail: { delay: 50, err: "oh noes" } })
24
+ }.to raise_error("oh noes")
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-lambda-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.5
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - andrew wheat
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-07-31 00:00:00.000000000 Z
14
+ date: 2017-02-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: process-helper
@@ -150,14 +150,16 @@ files:
150
150
  - js/startup.js
151
151
  - lib/lambda_runner.rb
152
152
  - samples/sample_cloudwatch_event.json
153
+ - samples/sample_dynamodb_event.json
153
154
  - samples/sample_s3_event.json
154
155
  - samples/sample_s3_test_event.json
155
156
  - samples/sample_scheduled_event.json
156
157
  - samples/sample_sns_event.json
157
- - spec/lambda-runner_spec.rb
158
+ - spec/lambdarunner_events_spec.rb
159
+ - spec/lambdarunner_runner_spec.rb
158
160
  homepage: https://github.com/bbc/aws-lambda-runner
159
161
  licenses:
160
- - Apache 2
162
+ - Apache-2.0
161
163
  metadata: {}
162
164
  post_install_message:
163
165
  rdoc_options: []
@@ -1,31 +0,0 @@
1
- require 'rspec'
2
- require_relative '../lib/lambda_runner'
3
-
4
- describe LambdaRunner::Events do
5
-
6
- it "should generate an S3 event" do
7
- data = LambdaRunner::Events.s3_event("some-bucket", "some/key")
8
- expect(data["Records"][0]["s3"]["bucket"]["name"]).to eq("some-bucket")
9
- expect(data["Records"][0]["s3"]["bucket"]["arn"]).to eq("arn:aws:s3:::some-bucket")
10
- expect(data["Records"][0]["s3"]["object"]["key"]).to eq("some/key")
11
- end
12
-
13
- it "should generate an SNS event (data body)" do
14
- message = { "this_is" => "the message" }
15
- data = LambdaRunner::Events.sns_event("some-arn", "some-id", "some-timestamp", message)
16
- expect(data["Records"][0]["Sns"]["TopicArn"]).to eq("some-arn")
17
- expect(data["Records"][0]["Sns"]["MessageId"]).to eq("some-id")
18
- expect(data["Records"][0]["Sns"]["Timestamp"]).to eq("some-timestamp")
19
- expect(JSON.parse data["Records"][0]["Sns"]["Message"]).to eq(message)
20
- end
21
-
22
- it "should generate an SNS event (string body)" do
23
- message = { "this_is" => "the message" }
24
- data = LambdaRunner::Events.sns_event("some-arn", "some-id", "some-timestamp", JSON.generate(message))
25
- expect(data["Records"][0]["Sns"]["TopicArn"]).to eq("some-arn")
26
- expect(data["Records"][0]["Sns"]["MessageId"]).to eq("some-id")
27
- expect(data["Records"][0]["Sns"]["Timestamp"]).to eq("some-timestamp")
28
- expect(JSON.parse data["Records"][0]["Sns"]["Message"]).to eq(message)
29
- end
30
-
31
- end