From b00e4f42cb65e08511dfddfe37ea69470104d962 Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Sat, 21 May 2016 23:17:53 +0200 Subject: [PATCH] basic deployment plugin mocked by SinonJs --- lib/classes/PluginManager.js | 2 +- lib/plugins/Plugins.json | 2 +- lib/plugins/deploy/README.md | 1 + lib/plugins/deploy/deploy.js | 41 +++++++++++++++++++++ lib/plugins/deploy/package.json | 13 +++++++ lib/plugins/deploy/tests/deploy.js | 57 ++++++++++++++++++++++++++++++ package.json | 3 +- 7 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 lib/plugins/deploy/README.md create mode 100644 lib/plugins/deploy/deploy.js create mode 100644 lib/plugins/deploy/package.json create mode 100644 lib/plugins/deploy/tests/deploy.js diff --git a/lib/classes/PluginManager.js b/lib/classes/PluginManager.js index 86f6fab4c..8af5f4fd6 100644 --- a/lib/classes/PluginManager.js +++ b/lib/classes/PluginManager.js @@ -52,7 +52,7 @@ class PluginManager { } addPlugin(Plugin) { - this.plugins.push(new Plugin()); + this.plugins.push(new Plugin(this.serverless)); this.loadCommands(Plugin); } diff --git a/lib/plugins/Plugins.json b/lib/plugins/Plugins.json index 1586c8614..ef704fef2 100644 --- a/lib/plugins/Plugins.json +++ b/lib/plugins/Plugins.json @@ -1,5 +1,5 @@ { "plugins": [ - "./HelloWorld/HelloWorld.js" + "./deploy/deploy.js" ] } diff --git a/lib/plugins/deploy/README.md b/lib/plugins/deploy/README.md new file mode 100644 index 000000000..4a8d593f3 --- /dev/null +++ b/lib/plugins/deploy/README.md @@ -0,0 +1 @@ +# Deploy \ No newline at end of file diff --git a/lib/plugins/deploy/deploy.js b/lib/plugins/deploy/deploy.js new file mode 100644 index 000000000..5028dfe59 --- /dev/null +++ b/lib/plugins/deploy/deploy.js @@ -0,0 +1,41 @@ +'use strict'; + +const AWS = require('aws-sdk'); +const BbPromise = require('bluebird'); +const Zip = require('node-zip'); + +class Deploy { + constructor(S) { + this.S = S; + this.commands = { + deploy: { + usage: 'deploy lambda zip.', + lifeCycleEvents: [ + 'deploy', + ], + }, + }; + + this.hooks = { + 'deploy:deploy': this.deploy, + }; + + const config = { + region: 'us-east-1', + }; + this.Lambda = new AWS.Lambda(config); + BbPromise.promisifyAll(this.Lambda, {suffix: 'Promised'}); + } + + deploy() { + return this.S.instances.service.getAllFunctions().forEach((f) => { + const params = { + FunctionName: `${this.S.instances.service.service}-${f}`, + ZipFile: new Zip(), + }; + return this.Lambda.updateFunctionCodePromised(params); + }); + } +} + +module.exports = Deploy; diff --git a/lib/plugins/deploy/package.json b/lib/plugins/deploy/package.json new file mode 100644 index 000000000..4af105d24 --- /dev/null +++ b/lib/plugins/deploy/package.json @@ -0,0 +1,13 @@ +{ + "name": "deploy", + "version": "0.0.1", + "description": "", + "license": "MIT", + "scripts": { + "test": "node_modules/.bin/mocha ./tests/deploy.js" + }, + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^2.4.5" + } +} diff --git a/lib/plugins/deploy/tests/deploy.js b/lib/plugins/deploy/tests/deploy.js new file mode 100644 index 000000000..9ba1c5cf4 --- /dev/null +++ b/lib/plugins/deploy/tests/deploy.js @@ -0,0 +1,57 @@ +'use strict'; + +const expect = require('chai').expect; +const sinon = require('sinon'); +const Deploy = require('../deploy'); +const BbPromise = require('bluebird'); +const Serverless = require('../../../Serverless'); + + + +describe('Deploy', () => { + let deploy; + let codeStub; + + beforeEach(() => { + const S = new Serverless(); + deploy = new Deploy(S); + codeStub = sinon.stub(deploy.Lambda, 'updateFunctionCodePromised') + .returns(BbPromise.resolve()); + S.instances.service.service = 'myService'; + S.instances.service.functions = { + create: { + handler: 'users.create', + description: 'fake function', + memory_size: 512, + timeout: 6, + }, + list: { + handler: 'users.list', + description: 'fake function', + memory_size: 1024, + timeout: 6, + }, + }; + }); + + afterEach(() => { + deploy.Lambda.updateFunctionCodePromised.restore(); + }); + + describe('#constructor()', () => { + it('should have commands', () => expect(deploy.commands).to.be.not.empty); + + it('should have hooks', () => expect(deploy.hooks).to.be.not.empty); + }); + + describe('#deploy()', () => { + it('should deploy lambda', () => { + deploy.deploy(); + expect(codeStub.calledTwice).to.be.equal(true); + expect(codeStub.args[0][0].FunctionName).to.be.equal('myService-create'); + expect(typeof codeStub.args[0][0].ZipFile).to.not.be.equal('undefined'); + expect(codeStub.args[1][0].FunctionName).to.be.equal('myService-list'); + expect(typeof codeStub.args[1][0].ZipFile).to.not.be.equal('undefined'); + }); + }); +}); diff --git a/package.json b/package.json index 5960154d3..a0a963dc7 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "replaceall": "^0.1.6", "shelljs": "^0.6.0", "shortid": "^2.2.2", - "traverse": "^0.6.6" + "traverse": "^0.6.6", + "sinon": "^1.17.4" } }