Skip to content

Commit

Permalink
Merge pull request serverless#989 from minibikini/master
Browse files Browse the repository at this point in the history
Fixes tests
  • Loading branch information
eahefnawy committed Apr 11, 2016
2 parents 5ad328a + c7362fd commit f9bc100
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 104 deletions.
2 changes: 1 addition & 1 deletion tests/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('All Tests', function() {
require('./tests/actions/EndpointDeploy');
require('./tests/actions/EventDeploy');
require('./tests/actions/ProjectInit');
require('./tests/actions/ProjectInstall'); // Broken until Serverless-Starter is fixed
require('./tests/actions/ProjectInstall');
require('./tests/actions/ResourcesDiff');
require('./tests/actions/PluginCreate');
require('./tests/actions/FunctionRollback');
Expand Down
137 changes: 45 additions & 92 deletions tests/tests/actions/ProjectInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
* - Deletes the CF stack created by the private
*/

let Serverless = require('../../../lib/Serverless'),
SError = require('../../../lib/Error'),
path = require('path'),
os = require('os'),
AWS = require('aws-sdk'),
uuid = require('node-uuid'),
utils = require('../../../lib/utils/index'),
assert = require('chai').assert,
shortid = require('shortid'),
config = require('../../config');
const Serverless = require('../../../lib/Serverless'),
BbPromise = require('bluebird'),
os = require('os'),
AWS = require('aws-sdk'),
uuid = require('node-uuid'),
assert = require('chai').assert,
config = require('../../config');

// Instantiate
let serverless = new Serverless({
Expand All @@ -30,130 +27,86 @@ let serverless = new Serverless({
*/

let validateEvent = function(evt) {
assert.equal(true, typeof evt.options.name !== 'undefined');
assert.equal(true, typeof evt.options.region !== 'undefined');
assert.equal(true, typeof evt.options.noExeCf !== 'undefined');
assert.equal(true, typeof evt.options.stage !== 'undefined');
assert.equal(true, typeof evt.data !== 'undefined');
assert.isDefined(evt.options.name);
assert.isDefined(evt.options.region);
assert.isDefined(evt.options.noExeCf);
assert.isDefined(evt.options.stage);
assert.isDefined(evt.data);
};

/**
* Test Cleanup
* - Remove Stage CloudFormation Stack
*/

let cleanup = function(project, cb) {
const cleanup = () => {
if(config.noExecuteCf) return;

AWS.config.update({
region: project.getVariables().projectBucketRegion,
region: config.region,
accessKeyId: config.awsAdminKeyId,
secretAccessKey: config.awsAdminSecretKey
});

// Delete Region Bucket
let s3 = new AWS.S3();
const cloudformation = new AWS.CloudFormation();

// Delete All Objects in Bucket first, this is required
s3.listObjects({
Bucket: project.getVariables().projectBucket
}, function(err, data) {
if (err) return console.log(err);
const StackName = serverless.getProject()
.getRegion(config.stage, config.region)
.getVariables().resourcesStackName;

let params = {
Bucket: project.getVariables().projectBucket
};
params.Delete = {};
params.Delete.Objects = [];

data.Contents.forEach(function(content) {
params.Delete.Objects.push({Key: content.Key});
});
s3.deleteObjects(params, function(err, data) {
if (err) return console.log(err);

// Delete Bucket
s3.deleteBucket({
Bucket: project.getVariables().projectBucket
}, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred

// Delete CloudFormation Resources Stack
let cloudformation = new AWS.CloudFormation();
cloudformation.deleteStack({
StackName: project.getRegion(config.stage, config.region).getVariables().resourcesStackName
}, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
return cb();
});
});
});
});
return BbPromise.fromCallback(cb => cloudformation.deleteStack({StackName}, cb));
};

/**
* Tests
*/

describe('Test action: Project Install', function() {
this.timeout(0);

before(function(done) {
before(() => {
process.chdir(os.tmpdir());

serverless.init().then(function(){
done();
});
return serverless.init();
});

after(function(done) {
done();
});
after(cleanup);

describe('Project Install', function() {
it('should install an existing project in temp directory', function(done) {
describe('Project Install', () => {

this.timeout(0);
it('should install an existing project in temp directory', () => {

let name = ('testprj-' + uuid.v4()).replace(/-/g, '');
const name = ('testprj-' + uuid.v4()).replace(/-/g, '');

let evt = {
const evt = {
options: {
name: name,
stage: config.stage,
region: config.region,
profile: config.profile_development,
noExeCf: config.noExecuteCf,
project: 'serverless-starter'
name: name,
stage: config.stage,
region: config.region,
profile: config.profile_development,
noExeCf: config.noExecuteCf,
project: 'serverless-starter'
}
};

serverless.actions.projectInstall(evt)
.then(function(evt) {
return serverless.actions.projectInstall(evt)
.then((evt) => {

let project = serverless.getProject();
let stage = project.getStage(config.stage);
let region = project.getRegion(config.stage, config.region);
const project = serverless.getProject();
const stage = project.getStage(config.stage);
const region = project.getRegion(config.stage, config.region);

assert.equal(true, typeof project.getVariables().project != 'undefined');
assert.isDefined(project.getVariables().project);

assert.equal(true, typeof stage.getVariables().stage != 'undefined');
assert.equal(true, typeof region.getVariables().region != 'undefined');
if (!config.noExecuteCf) {
assert.equal(true, typeof region.getVariables().iamRoleArnLambda != 'undefined');
assert.equal(true, typeof region.getVariables().resourcesStackName != 'undefined');
assert.isDefined(stage.getVariables().stage);
assert.isDefined(region.getVariables().region);

if (!config.noExecuteCf) {
assert.isDefined(region.getVariables().iamRoleArnLambda);
assert.isDefined(region.getVariables().resourcesStackName);
}

// Validate Event
validateEvent(evt);

evt.options.noExeCf ? done() : cleanup(serverless, done);

})
.catch(SError, function(e) {
done(e);
})
.error(function(e) {
done(e);
});
});
});
Expand Down
44 changes: 33 additions & 11 deletions tests/tests/classes/ProviderAws.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe('Test Serverless ProviderAws Class', function() {
credentialsFiles;
this.timeout(0);

before(function() {
before(() => {
environmentVars = JSON.stringify(process.env); // save for later - be sure not to disrupt existing environment
credentialsFiles = prepareCredentialsFile();
return testUtils.createTestProject(testConfig)
Expand All @@ -297,7 +297,7 @@ describe('Test Serverless ProviderAws Class', function() {
process.env = JSON.parse(environmentVars);
}
});

describe('Configuration Credentials Tests', function() {
it('Extracts credentials in AWS format from the project configuration', function() {
credentials = {};
Expand Down Expand Up @@ -340,43 +340,65 @@ describe('Test Serverless ProviderAws Class', function() {
});
describe('Profile Credentials Tests', function() {
for(let prefixIdx in environmentPrefixes) {

let prefix = environmentPrefixes[prefixIdx].toUpperCase();
it('Loads the credentials of the profile extracted from the environment using variable prefix "' + prefix + '"', function() {

it('Loads the credentials of the profile extracted from the environment using variable prefix "' + prefix + '"', () => {
let expected = buildExpectedProfileCredentials(prefix, null);
credentials = {};
setProfileEnvironmentVariable(prefix, null);
provider.addProfileCredentials(credentials, prefix);
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);

return provider.addProfileCredentials(credentials, prefix).then(() => {
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);
});

});

for(let stageIdx in stages) {

let stage = stages[stageIdx].toUpperCase();

it('Loads the credentials of the profile extracted from the environment using variable prefix "' + prefix + '" and the stage modifier "' + stage + '"', function() {
let expected = buildExpectedProfileCredentials(prefix, stage);
credentials = {};

setProfileEnvironmentVariable(prefix, stage);
provider.addProfileCredentials(credentials, prefix + '_' + stage);
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);

return provider.addProfileCredentials(credentials, prefix + '_' + stage).then(() => {
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);
});
});
}
}

setProfilePrefixEnvVariable(profilePrefixEnvValue);

for(let prefixIdx in environmentPrefixes) {

let prefix = environmentPrefixes[prefixIdx].toUpperCase();

it('Loads the credentials of the "' + profilePrefixEnvValue + '" prefixed profile extracted from the environment using variable prefix "' + prefix + '"', function() {
let expected = buildExpectedProfileCredentials(prefix, null);
credentials = {};
setProfileEnvironmentVariable(prefix, null);
provider.addProfileCredentials(credentials, prefix);
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);

return provider.addProfileCredentials(credentials, prefix).then(() => {
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);
});
});

for(let stageIdx in stages) {

let stage = stages[stageIdx].toUpperCase();

it('Loads the credentials of the "' + profilePrefixEnvValue + '" prefixed profile extracted from the environment using variable prefix "' + prefix + '" and the stage modifier "' + stage + '"', function() {
let expected = buildExpectedProfileCredentials(prefix, stage);
credentials = {};
setProfileEnvironmentVariable(prefix, stage);
provider.addProfileCredentials(credentials, prefix + '_' + stage);
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);

return provider.addProfileCredentials(credentials, prefix + '_' + stage).then(() => {
checkResults(credentials, expected.accessKeyId, expected.secretAccessKey, expected.sessionToken);
});
});
}
}
Expand Down

0 comments on commit f9bc100

Please sign in to comment.