From 9bfdfc7c88afe3a9b7a6aded6d4f86598af89da3 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 1 Jun 2017 11:22:41 +0900 Subject: [PATCH 1/8] Modify from `var` to `const` - Only variables that can be modified - It is the target declared in global --- test/main.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/main.js b/test/main.js index c5ea073d..2eaacd01 100644 --- a/test/main.js +++ b/test/main.js @@ -1,17 +1,14 @@ 'use strict' -var path = require('path') -var os = require('os') -var chai = require('chai') -var program = require('commander') -var fs = require('fs-extra') -var Hoek = require('hoek') -var lambda = require(path.join(__dirname, '..', 'lib', 'main')) -var Zip = require('node-zip') - -var assert = chai.assert - -var originalProgram = { +const path = require('path') +const os = require('os') +const fs = require('fs-extra') +const Hoek = require('hoek') +const lambda = require(path.join(__dirname, '..', 'lib', 'main')) +const Zip = require('node-zip') +const assert = require('chai').assert + +const originalProgram = { environment: 'development', accessKey: 'key', secretKey: 'secret', @@ -33,6 +30,7 @@ var originalProgram = { prebuiltDirectory: '' } +var program = require('commander') var codeDirectory = lambda._codeDirectory() function _timeout (params) { From e9651190d11f4561c5ac3dea27aeaebe319afa7c Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 1 Jun 2017 11:26:36 +0900 Subject: [PATCH 2/8] Modify a declaration place as aws-mock is used for other tests --- test/main.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/main.js b/test/main.js index 2eaacd01..2dcde6fc 100644 --- a/test/main.js +++ b/test/main.js @@ -7,6 +7,7 @@ const Hoek = require('hoek') const lambda = require(path.join(__dirname, '..', 'lib', 'main')) const Zip = require('node-zip') const assert = require('chai').assert +const awsMock = require('aws-sdk-mock') const originalProgram = { environment: 'development', @@ -782,7 +783,6 @@ describe('lib/main', function () { }) describe('_updateScheduleEvents', function () { - const aws = require('aws-sdk-mock') const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events')) const eventSourcesJsonValue = { ScheduleEvents: [{ @@ -796,13 +796,13 @@ describe('lib/main', function () { var schedule = null before(function () { - aws.mock('CloudWatchEvents', 'putRule', function (params, callback) { + awsMock.mock('CloudWatchEvents', 'putRule', function (params, callback) { callback(null, {}) }) - aws.mock('CloudWatchEvents', 'putTargets', function (params, callback) { + awsMock.mock('CloudWatchEvents', 'putTargets', function (params, callback) { callback(null, {}) }) - aws.mock('Lambda', 'addPermission', function (params, callback) { + awsMock.mock('Lambda', 'addPermission', function (params, callback) { callback(null, {}) }) @@ -816,8 +816,8 @@ describe('lib/main', function () { after(function () { fs.unlinkSync('event_sources.json') - aws.restore('CloudWatchEvents') - aws.restore('Lambda') + awsMock.restore('CloudWatchEvents') + awsMock.restore('Lambda') }) it('program.eventSourceFile is empty value', function () { From ab3c0fac95f1f5b45f7d1641aef73328da249603 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 6 Jun 2017 12:37:15 +0900 Subject: [PATCH 3/8] Modify to assign function to variable --- test/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.js b/test/main.js index 2dcde6fc..c9c9202c 100644 --- a/test/main.js +++ b/test/main.js @@ -34,7 +34,7 @@ const originalProgram = { var program = require('commander') var codeDirectory = lambda._codeDirectory() -function _timeout (params) { +const _timeout = function (params) { // Even if timeout is set for the whole test for Windows, // if it is set in local it will be valid. // For Windows, do not set it with local. From 578cb1fcdd41fb9a316d86517e07397ef0c8b2b0 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 6 Jun 2017 12:40:35 +0900 Subject: [PATCH 4/8] Add function to set mock For use in other tests --- test/main.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/main.js b/test/main.js index c9c9202c..955b8ab0 100644 --- a/test/main.js +++ b/test/main.js @@ -43,6 +43,18 @@ const _timeout = function (params) { } } +const _mockSetting = () => { + awsMock.mock('CloudWatchEvents', 'putRule', (params, callback) => { + callback(null, {}) + }) + awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => { + callback(null, {}) + }) + awsMock.mock('Lambda', 'addPermission', (params, callback) => { + callback(null, {}) + }) +} + /* global before, after, beforeEach, afterEach, describe, it */ describe('lib/main', function () { if (process.platform === 'win32') { @@ -796,15 +808,7 @@ describe('lib/main', function () { var schedule = null before(function () { - awsMock.mock('CloudWatchEvents', 'putRule', function (params, callback) { - callback(null, {}) - }) - awsMock.mock('CloudWatchEvents', 'putTargets', function (params, callback) { - callback(null, {}) - }) - awsMock.mock('Lambda', 'addPermission', function (params, callback) { - callback(null, {}) - }) + _mockSetting() fs.writeFileSync( 'event_sources.json', From f409734c6a68e05d8be2787e42dbe256c280b1af Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 6 Jun 2017 17:08:37 +0900 Subject: [PATCH 5/8] Add aws-mock setting to use --- test/main.js | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/test/main.js b/test/main.js index 955b8ab0..7ce77f82 100644 --- a/test/main.js +++ b/test/main.js @@ -43,6 +43,45 @@ const _timeout = function (params) { } } +// It does not completely reproduce the response of the actual API. +const lambdaMockSettings = { + addPermission: {}, + getFunction: { + Code: {}, + Configuration: {}, + FunctionArn: 'Lambda.getFunction.mock.FunctionArn' + }, + createFunction: { + FunctionArn: 'Lambda.createFunction.mock.FunctionArn', + FunctionName: 'Lambda.createFunction.mock.FunctionName' + }, + listEventSourceMappings: { + EventSourceMappings: [{ + EventSourceArn: 'Lambda.listEventSourceMappings.mock.EventSourceArn', + UUID: 'Lambda.listEventSourceMappings.mock.UUID' + }] + }, + updateFunctionCode: { + FunctionArn: 'Lambda.updateFunctionCode.mock.FunctionArn', + FunctionName: 'Lambda.updateFunctionCode.mock.FunctionName' + }, + updateFunctionConfiguration: { + FunctionArn: 'Lambda.updateFunctionConfiguration.mock.FunctionArn', + FunctionName: 'Lambda.updateFunctionConfiguration.mock.FunctionName' + }, + createEventSourceMapping: { + EventSourceArn: 'Lambda.createEventSourceMapping.mock.EventSourceArn', + FunctionName: 'Lambda.createEventSourceMapping.mock.EventSourceArn' + }, + updateEventSourceMapping: { + EventSourceArn: 'Lambda.updateEventSourceMapping.mock.EventSourceArn', + FunctionName: 'Lambda.updateEventSourceMapping.mock.EventSourceArn' + }, + deleteEventSourceMapping: { + EventSourceArn: 'Lambda.deleteEventSourceMapping.mock.EventSourceArn', + FunctionName: 'Lambda.deleteEventSourceMapping.mock.EventSourceArn' + } +} const _mockSetting = () => { awsMock.mock('CloudWatchEvents', 'putRule', (params, callback) => { callback(null, {}) @@ -50,8 +89,11 @@ const _mockSetting = () => { awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => { callback(null, {}) }) - awsMock.mock('Lambda', 'addPermission', (params, callback) => { - callback(null, {}) + + Object.keys(lambdaMockSettings).forEach((method) => { + awsMock.mock('Lambda', method, (params, callback) => { + callback(null, lambdaMockSettings[method]) + }) }) } From 192c2c70db87be90d0beec0445b41ba6e47cda7e Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 6 Jun 2017 17:14:12 +0900 Subject: [PATCH 6/8] Add _updateEventSources test using mock --- test/main.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/test/main.js b/test/main.js index 7ce77f82..74ebbb60 100644 --- a/test/main.js +++ b/test/main.js @@ -821,7 +821,39 @@ describe('lib/main', function () { }) }) - describe('_updateEventSources', function () { + describe('_updateEventSources', () => { + const eventSourcesJsonValue = { + EventSourceMappings: [{ + EventSourceArn: lambdaMockSettings + .listEventSourceMappings + .EventSourceMappings[0] + .EventSourceArn, + StartingPosition: 'LATEST', + BatchSize: 100, + Enabled: true + }] + } + + let awsLambda = null + + before(() => { + fs.writeFileSync( + 'event_sources.json', + JSON.stringify(eventSourcesJsonValue) + ) + _mockSetting() + + awsLambda = new (require('aws-sdk')).Lambda({ + apiVersion: '2015-03-31' + }) + }) + + after(() => { + fs.unlinkSync('event_sources.json') + awsMock.restore('CloudWatchEvents') + awsMock.restore('Lambda') + }) + it('program.eventSourceFile is empty value', function () { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) @@ -833,6 +865,51 @@ describe('lib/main', function () { const expected = { err: null, results: [] } assert.deepEqual(actual, expected) }) + + it('simple test with mock (In case of new addition)', (done) => { + program.eventSourceFile = 'event_sources.json' + const eventSourceList = lambda._eventSourceList(program) + lambda._updateEventSources( + awsLambda, + 'functionName', + [], + eventSourceList.EventSourceMappings, + (err, results) => { + assert.isUndefined(err) + assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping]) + done() + } + ) + }) + + it('simple test with mock (In case of deletion)', (done) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + {}, + (err, results) => { + assert.isUndefined(err) + assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping]) + done() + } + ) + }) + + it('simple test with mock (In case of update)', (done) => { + program.eventSourceFile = 'event_sources.json' + const eventSourceList = lambda._eventSourceList(program) + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + eventSourceList.EventSourceMappings, + (err, results) => { + assert.isUndefined(err) + assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping]) + done() + } + ) }) }) From 6364bf6d400dd0d6c36cbb122039bcfe6789fbbd Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 6 Jun 2017 17:31:50 +0900 Subject: [PATCH 7/8] Refactoring such as modifing to Arrow function --- test/main.js | 68 +++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/test/main.js b/test/main.js index 74ebbb60..70cffb7b 100644 --- a/test/main.js +++ b/test/main.js @@ -854,17 +854,21 @@ describe('lib/main', function () { awsMock.restore('Lambda') }) - it('program.eventSourceFile is empty value', function () { + it('program.eventSourceFile is empty value', (done) => { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) - return new Promise(function (resolve) { - lambda._updateEventSources(lambda, '', [], eventSourceList.EventSourceMappings, function (err, results) { - resolve({ err: err, results: results }) - }) - }).then(function (actual) { - const expected = { err: null, results: [] } - assert.deepEqual(actual, expected) - }) + lambda._updateEventSources( + awsLambda, + '', + [], + eventSourceList.EventSourceMappings, + (err, results) => { + assert.isNull(err) + assert.deepEqual(results, []) + done() + } + ) + }) it('simple test with mock (In case of new addition)', (done) => { program.eventSourceFile = 'event_sources.json' @@ -913,7 +917,7 @@ describe('lib/main', function () { }) }) - describe('_updateScheduleEvents', function () { + describe('_updateScheduleEvents', () => { const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events')) const eventSourcesJsonValue = { ScheduleEvents: [{ @@ -924,47 +928,51 @@ describe('lib/main', function () { }] } - var schedule = null - - before(function () { - _mockSetting() + let schedule = null + before(() => { fs.writeFileSync( 'event_sources.json', JSON.stringify(eventSourcesJsonValue) ) + _mockSetting() schedule = new ScheduleEvents(require('aws-sdk')) }) - after(function () { + after(() => { fs.unlinkSync('event_sources.json') awsMock.restore('CloudWatchEvents') awsMock.restore('Lambda') }) - it('program.eventSourceFile is empty value', function () { + it('program.eventSourceFile is empty value', (done) => { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) - return new Promise(function (resolve) { - lambda._updateScheduleEvents(schedule, '', eventSourceList.ScheduleEvents, function (err, results) { - resolve({ err: err, results: results }) - }) - }).then(function (actual) { - const expected = { err: null, results: [] } - assert.deepEqual(actual, expected) - }) + lambda._updateScheduleEvents( + schedule, + '', + eventSourceList.ScheduleEvents, + (err, results) => { + assert.isNull(err) + assert.deepEqual(results, []) + done() + } + ) }) - it('simple test with mock', function () { + it('simple test with mock', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function' - return new Promise(function (resolve) { - lambda._updateScheduleEvents(schedule, functionArn, eventSourceList.ScheduleEvents, function (err, results) { - resolve({ err: err, results: results }) - }) - }).then(function (actual) { + return new Promise((resolve) => { + lambda._updateScheduleEvents( + schedule, + functionArn, + eventSourceList.ScheduleEvents, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { const expected = { err: null, results: [Object.assign( From d4101ede4079802b6a51602aaea459e78ce14b31 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 6 Jun 2017 17:34:00 +0900 Subject: [PATCH 8/8] Add TODO comment --- test/main.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/main.js b/test/main.js index 70cffb7b..6b9bb454 100644 --- a/test/main.js +++ b/test/main.js @@ -1026,4 +1026,8 @@ describe('lib/main', function () { }) }) }) + + describe('Lambda.prototype.deploy()', () => { + it('TODO: Add test. Since the current deploy function is hard to test, skip') + }) })