Merge pull request #745 from minibikini/v0.5

adds a test for the FunctionRollback action
This commit is contained in:
Eslam λ Hefnawy 2016-03-07 17:18:16 +07:00
commit 2baf03571f
3 changed files with 108 additions and 11 deletions

View File

@ -70,8 +70,8 @@ module.exports = function(SPlugin, serverlessPath) {
],
parameters: [
{
parameter: 'name', // Only accepting paths makes it easier for plugin developers.
description: 'One or multiple function names',
parameter: 'name',
description: 'A function name to rollback',
position: '0'
}
]
@ -214,15 +214,15 @@ module.exports = function(SPlugin, serverlessPath) {
});
console.log('')
return this.cliPromptSelect('Select a version to rollback:', choices)
.then(item => item[0].value)
}
})
.then((item) => {
let rollbackTo = item[0].value,
params = {
FunctionName: rollbackTo.FunctionName,
Name: stage,
FunctionVersion: rollbackTo.Version,
};
.then((rollbackTo) => {
let params = {
FunctionName: rollbackTo.FunctionName,
Name: stage,
FunctionVersion: rollbackTo.Version,
};
if (rollbackTo.Version === deployedFunction.Version) throw new SError(`Selected version is the currently deployed version.`);
spinner.start(`Rolling back function "${this.func.getName()}" to v${rollbackTo.Version}`);
@ -234,8 +234,8 @@ module.exports = function(SPlugin, serverlessPath) {
if (reply.FunctionVersion === rollbackTo.Version) {
SCli.log(chalk.green(`Function "${this.func.getName()}" has been successfully rolled back from v${deployedFunction.Version} to v${rollbackTo.Version}`));
this.evt.data.follbackFrom = deployedFunction.Version;
this.evt.data.follbackTo = rollbackTo.Version;
this.evt.data.rollbackFrom = deployedFunction.Version;
this.evt.data.rollbackTo = rollbackTo.Version;
return this.evt
} else {
SCli.log(chalk.red(`Failed`));

View File

@ -33,4 +33,5 @@ describe('All Tests', function() {
require('./tests/actions/ProjectLifeCycle.js');
require('./tests/actions/ResourcesDiff');
require('./tests/actions/PluginCreate');
require('./tests/actions/FunctionRollback');
});

View File

@ -0,0 +1,96 @@
'use strict';
/**
* Test: Function Rollback Action
*/
let Serverless = require('../../../lib/Serverless.js'),
path = require('path'),
utils = require('../../../lib/utils/index'),
assert = require('chai').assert,
testUtils = require('../../test_utils'),
AWS = require('aws-sdk'),
BbPromise = require('bluebird'),
_ = require('lodash'),
config = require('../../config');
let serverless, rollbackFrom, rollbackTo;
/**
* Create Test Project
*/
const stage = config.stage,
region = config.region;
describe('Test Action: Function Rollback', function() {
this.timeout(0);
before(function() {
return testUtils.createTestProject(config, ['nodejscomponent'])
.then(projectPath => {
process.chdir(projectPath);
serverless = new Serverless({
projectPath,
interactive: false,
awsAdminKeyId: config.awsAdminKeyId,
awsAdminSecretKey: config.awsAdminSecretKey
});
return serverless.init()
})
.then(() => {
const FunctionName = serverless
.getProject()
.getFunction('function1')
.getDeployedName({stage, region});
const getDeployed = serverless.getProvider('aws')
.request('Lambda', 'getFunction', {FunctionName, Qualifier: stage}, stage, region)
.then((res) => res.Configuration);
const getVersions = serverless.getProvider('aws')
.request('Lambda', 'listVersionsByFunction', {FunctionName}, stage, region)
.then(reply => reply.Versions)
return BbPromise.all([getDeployed, getVersions]);
})
.spread((deployed, versions) => {
if (versions.length < 3) throw new Error("Need at least two deployed version")
const Role = serverless.getProject().getRegion(stage, region).getVariables().iamRoleArnLambda;
rollbackFrom = deployed.Version;
rollbackTo = _.chain(versions)
.reject({Version: '$LATEST'})
.reject({Version: rollbackFrom})
.filter({Role})
.sample()
.value()
.Version;
});
});
/**
* Tests
*/
describe('Function Rollback', function() {
it('should rollback function', function() {
let options = {
stage,
region,
name: 'function1',
version: rollbackTo
};
return serverless.actions.functionRollback(options)
.then((evt) => {
assert.equal(evt.data.rollbackFrom, rollbackFrom);
assert.equal(evt.data.rollbackTo, rollbackTo);
});
});
});
});