basic deployment plugin mocked by SinonJs

This commit is contained in:
Eslam A. Hefnawy 2016-05-21 23:17:53 +02:00
parent 341844b02d
commit b00e4f42cb
7 changed files with 116 additions and 3 deletions

View File

@ -52,7 +52,7 @@ class PluginManager {
}
addPlugin(Plugin) {
this.plugins.push(new Plugin());
this.plugins.push(new Plugin(this.serverless));
this.loadCommands(Plugin);
}

View File

@ -1,5 +1,5 @@
{
"plugins": [
"./HelloWorld/HelloWorld.js"
"./deploy/deploy.js"
]
}

View File

@ -0,0 +1 @@
# Deploy

View File

@ -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;

View File

@ -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"
}
}

View File

@ -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');
});
});
});

View File

@ -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"
}
}