This commit is contained in:
ryanp 2015-09-01 22:16:31 -05:00
parent f8d5db033b
commit f7150b2b2a
11 changed files with 269 additions and 133 deletions

View File

@ -28,10 +28,7 @@
"test": "mocha tests/all"
},
"devDependencies": {
"adm-zip": "^0.4.7",
"chai": "^3.2.0",
"hoek": "^2.11.1",
"lodash": "^3.2.0",
"mocha": "^2.2.5"
},
"dependencies": {

View File

@ -1,6 +1,8 @@
'use strict';
require('config'); //init config
//TODO: doc on needing to set env vars
//TODO: must setup an env var file for unittest
require('./config'); //init config
describe('AllTests', function() {
before(function(done) {
@ -13,9 +15,9 @@ describe('AllTests', function() {
//require tests vs inline so we can run sequentially
// Require Tests ("new" must be last)
require('./cli/tag');
require('./cli/tag'); //does not create AWS resources
require('./cli/deploy_lambda');
require('./cli/deploy_api');
//require('./cli/deploy_api');
require('./cli/install');
require('./cli/env');
require('./cli/new');

View File

@ -5,27 +5,29 @@
* - Copies the test-prj template to your system's temp directory
* - Deploys an API based on the endpoints in the project
*/
var testUtils = require('../test_utils');
var testUtils = require('../test_utils'),
path = require('path'),
assert = require('chai').assert;
module.exports = function(testData, cb) {
var config = require('../config');
describe('Test deploy api command', function() {
describe('Test deploy api command', function() {
before(function() {
testData.projectPath = testUtils.createTestProject(
testData.name,
testData.region,
testData.stage,
testData.iamRoleARN,
testData.envBucket);
process.chdir(testData.projectPath);
});
before(function() {
config.projectPath = testUtils.createTestProject(
config.name,
config.region,
config.stage,
config.iamRoleARN,
config.envBucket);
process.chdir(config.projectPath);
});
after(function(done) {
cb(testData);
done();
});
after(function(done) {
done();
});
describe('Positive tests', function() {
it('Deploy REST API', function(done) {
this.timeout(0);
@ -35,7 +37,7 @@ module.exports = function(testData, cb) {
JawsError = require('../../lib/jaws-error');
// Test
JAWS.deployApi(testData.stage)
JAWS.deployApi(config.stage)
.then(function() {
done();
})
@ -47,4 +49,4 @@ module.exports = function(testData, cb) {
});
});
});
};
});

View File

@ -3,29 +3,30 @@
/**
* JAWS Test: Deploy Lambda Command
*/
var testUtils = require('../test_utils'),
path = require('path');
path = require('path'),
assert = require('chai').assert;
module.exports = function(testData, cb) {
var config = require('../config');
describe('Test "deploy lambda" command', function() {
before(function() {
testData.projectPath = testUtils.createTestProject(
testData.name,
testData.region,
testData.stage,
testData.iamRoleARN,
testData.envBucket);
process.chdir(path.join(testData.projectPath, 'back/users/lambdas/show'));
});
describe('Test "deploy lambda" command', function() {
after(function(done) {
cb(testData);
done();
});
before(function() {
config.projectPath = testUtils.createTestProject(
config.name,
config.region,
config.stage,
config.iamRoleARN,
config.envBucket);
process.chdir(path.join(config.projectPath, 'back/users/lambdas/show'));
});
after(function(done) {
done();
});
describe('Positive tests', function() {
it('Deploy Lambda', function(done) {
this.timeout(0);
@ -34,7 +35,7 @@ module.exports = function(testData, cb) {
var JAWS = require('../../lib/index.js');
// Test
JAWS.deployLambdas(testData.stage, false, false)
JAWS.deployLambdas(config.stage, false, false)
.then(function(d) {
done();
})
@ -43,4 +44,4 @@ module.exports = function(testData, cb) {
});
});
});
};
});

View File

@ -5,33 +5,34 @@
*/
var testUtils = require('../test_utils'),
path = require('path');
path = require('path'),
assert = require('chai').assert;
module.exports = function(testData, cb) {
var config = require('../config');
describe('Test "env" command', function() {
describe('Test "env" command', function() {
before(function() {
testData.projectPath = testUtils.createTestProject(
testData.name,
testData.region,
testData.stage,
testData.iamRoleARN,
testData.envBucket);
process.chdir(path.join(testData.projectPath, 'back/users/lambdas/show'));
});
before(function() {
config.projectPath = testUtils.createTestProject(
config.name,
config.region,
config.stage,
config.iamRoleARN,
config.envBucket);
process.chdir(path.join(config.projectPath, 'back/users/lambdas/show'));
});
after(function(done) {
cb(testData);
done();
});
after(function(done) {
done();
});
describe('Positive tests', function() {
it('Test env command', function(done) {
this.timeout(0);
var JAWS = require('../../lib/index.js');
JAWS.listEnv(testData.stage)
JAWS.listEnv(config.stage)
.then(function(d) {
done();
})
@ -40,4 +41,4 @@ module.exports = function(testData, cb) {
});
});
});
};
});

View File

@ -5,27 +5,28 @@
*/
var testUtils = require('../test_utils'),
path = require('path');
path = require('path'),
assert = require('chai').assert;
module.exports = function(testData, cb) {
var config = require('../config');
describe('Test "install" command', function() {
describe('Test "install" command', function() {
before(function() {
testData.projectPath = testUtils.createTestProject(
testData.name,
testData.region,
testData.stage,
testData.iamRoleARN,
testData.envBucket);
process.chdir(path.join(testData.projectPath, 'back/users/lambdas/show'));
});
before(function() {
config.projectPath = testUtils.createTestProject(
config.name,
config.region,
config.stage,
config.iamRoleARN,
config.envBucket);
process.chdir(path.join(config.projectPath, 'back/users/lambdas/show'));
});
after(function(done) {
cb(testData);
done();
});
after(function(done) {
done();
});
describe('Positive tests', function() {
it('Install module', function(done) {
this.timeout(0);
@ -44,4 +45,4 @@ module.exports = function(testData, cb) {
});
});
});
};
});

View File

@ -8,10 +8,9 @@
var path = require('path'),
os = require('os'),
assert = require('chai').assert,
shortid = require('shortid'),
AWS = require('aws-sdk');
shortid = require('shortid');
var config = require('./config');
var config = require('../config');
describe('Test new command', function() {

View File

@ -10,57 +10,58 @@ var testUtils = require('../test_utils'),
assert = require('chai').assert,
Promise = require('bluebird');
module.exports = function(testData, cb) {
var config = require('../config');
describe('Test "tag" command', function() {
describe('Test "tag" command', function() {
before(function(done) {
testData.projectPath = testUtils.createTestProject(
testData.name,
testData.region,
testData.stage,
testData.iamRoleARN,
testData.envBucket);
process.chdir(testData.projectPath);
before(function(done) {
config.projectPath = testUtils.createTestProject(
config.name,
config.region,
config.stage,
config.iamRoleARN,
config.envBucket);
// Get Lambda Paths
testData.lambda1 = path.join(testData.projectPath, 'back', 'users', 'lambdas', 'show', 'jaws.json');
testData.lambda2 = path.join(testData.projectPath, 'back', 'users', 'lambdas', 'signin', 'jaws.json');
testData.lambda3 = path.join(testData.projectPath, 'back', 'users', 'lambdas', 'signup', 'jaws.json');
done();
});
process.chdir(config.projectPath);
after(function(done) {
cb(testData);
done();
});
// Get Lambda Paths
config.lambda1 = path.join(config.projectPath, 'back', 'users', 'lambdas', 'show', 'jaws.json');
config.lambda2 = path.join(config.projectPath, 'back', 'users', 'lambdas', 'signin', 'jaws.json');
config.lambda3 = path.join(config.projectPath, 'back', 'users', 'lambdas', 'signup', 'jaws.json');
done();
});
it('tag lambdas', function (done) {
after(function(done) {
done();
});
describe('Positive tests', function() {
it('tag lambdas', function(done) {
this.timeout(0);
var JAWS = require('../../lib/index.js');
JAWS.tag('lambda', testData.lambda1, '')
.then(function () {
assert.equal(true, require(testData.lambda1).lambda.deploy);
assert.equal(false, require(testData.lambda2).lambda.deploy);
assert.equal(false, require(testData.lambda3).lambda.deploy);
JAWS.tag('lambda', config.lambda1, false)
.then(function() {
assert.equal(true, require(config.lambda1).lambda.deploy);
assert.equal(false, require(config.lambda2).lambda.deploy);
assert.equal(false, require(config.lambda3).lambda.deploy);
return JAWS.tagAll('lambda', false);
})
.then(function () {
assert.equal(true, require(testData.lambda1).lambda.deploy);
assert.equal(true, require(testData.lambda2).lambda.deploy);
assert.equal(true, require(testData.lambda3).lambda.deploy);
.then(function() {
assert.equal(true, require(config.lambda1).lambda.deploy);
assert.equal(true, require(config.lambda2).lambda.deploy);
assert.equal(true, require(config.lambda3).lambda.deploy);
return JAWS.tagAll('lambda', true);
})
.then(function () {
assert.equal(false, require(testData.lambda1).lambda.deploy);
assert.equal(false, require(testData.lambda2).lambda.deploy);
assert.equal(false, require(testData.lambda3).lambda.deploy);
.then(function() {
assert.equal(false, require(config.lambda1).lambda.deploy);
assert.equal(false, require(config.lambda2).lambda.deploy);
assert.equal(false, require(config.lambda3).lambda.deploy);
done();
})
.error(function (e) {
.error(function(e) {
done(e);
});
});
@ -70,23 +71,23 @@ module.exports = function(testData, cb) {
var JAWS = require('../../lib/index.js');
JAWS.tag('api', testData.lambda1)
JAWS.tag('api', config.lambda1)
.then(function() {
assert.equal(true, require(testData.lambda1).lambda.deploy);
assert.equal(false, require(testData.lambda2).lambda.deploy);
assert.equal(false, require(testData.lambda3).lambda.deploy);
assert.equal(true, require(config.lambda1).lambda.deploy);
assert.equal(false, require(config.lambda2).lambda.deploy);
assert.equal(false, require(config.lambda3).lambda.deploy);
return JAWS.tagAll('api', false);
})
.then(function() {
assert.equal(true, require(testData.lambda1).lambda.deploy);
assert.equal(true, require(testData.lambda2).lambda.deploy);
assert.equal(true, require(testData.lambda3).lambda.deploy);
assert.equal(true, require(config.lambda1).lambda.deploy);
assert.equal(true, require(config.lambda2).lambda.deploy);
assert.equal(true, require(config.lambda3).lambda.deploy);
return JAWS.tagAll('api', true);
})
.then(function() {
assert.equal(false, require(testData.lambda1).lambda.deploy);
assert.equal(false, require(testData.lambda2).lambda.deploy);
assert.equal(false, require(testData.lambda3).lambda.deploy);
assert.equal(false, require(config.lambda1).lambda.deploy);
assert.equal(false, require(config.lambda2).lambda.deploy);
assert.equal(false, require(config.lambda3).lambda.deploy);
done();
})
.error(function(e) {
@ -94,4 +95,4 @@ module.exports = function(testData, cb) {
});
});
});
};
});

View File

@ -3,8 +3,8 @@
var path = require('path'),
AWS = require('aws-sdk');
// Require ENV vars
require('dotenv').config({path: __dirname + '/.env'});
// Require ENV vars, can also set ENV vars in your IDE
require('dotenv').config({path: path.join(__dirname, '.env'), silent: true});
var config = {
name: 'test-prj',
@ -13,7 +13,7 @@ var config = {
region: 'us-east-1',
envBucket: process.env.TEST_JAWS_ENV_BUCKET,
profile: process.env.TEST_JAWS_PROFILE,
iamRoleARN: process.env.TEST_JAWS_IAM_ROLE,
iamRoleARN: process.env.TEST_JAWS_IAM_ROLE, //must manually create in IAM UI, TODO: create CF that creates this user
};
AWS.config.credentials = new AWS.SharedIniFileCredentials({

132
tests/deployApiTest-cf.json Normal file
View File

@ -0,0 +1,132 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this JAWS project",
"Parameters": {
"aaProjectName": {
"Type": "String",
"Default": "jaws",
"AllowedValues": [
"jaws"
]
},
"aaStage": {
"Type": "String",
"Default": "test"
},
"aaDataModelPrefix": {
"Type": "String",
"Default": "test",
"AllowedValues": [
"test"
]
},
"aaHostedZoneName": {
"Type": "String",
"Default": "myapp.com"
},
"aaNotficationEmail": {
"Type": "String",
"Default": "you@you.com"
},
"aaDefaultDynamoRWThroughput": {
"Type": "String",
"Default": "1"
}
},
"Resources": {
"LambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"apigateway.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Path": "/"
}
},
"Profile": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Path": "/",
"Roles": [
{
"Ref": "LambdaRole"
}
]
}
},
"Group": {
"Type": "AWS::IAM::Group",
"Properties": {
"Path": "/dataModel/"
}
},
"Policy": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": {
"Fn::Join": [
"_-_",
[
{
"Ref": "aaStage"
},
{
"Ref": "aaProjectName"
}
]
]
},
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction",
"iam:PassRole",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
},
"Roles": [
{
"Ref": "LambdaRole"
}
],
"Groups": [
{
"Ref": "Group"
}
]
}
}
},
"Outputs": {
"LambdaRoleARN": {
"Description": "ARN of the lambda IAM role",
"Value": {
"Fn::GetAtt": [
"LambdaRole",
"Arn"
]
}
}
}
}

View File

@ -12,9 +12,9 @@ var fs = require('fs'),
module.exports.createTestProject = function(projectName, projectRegion, projectStage, projectIAMRole, projectEnvBucket) {
// Create Test Project
var projectPath = path.join(os.tmpdir(), './', projectName);
var projectPath = path.join(os.tmpdir(), projectName);
if (fs.existsSync(projectPath)) {
del.sync([projectPath], { force: true });
del.sync([projectPath], {force: true});
}
// Copy test project to temp directory
@ -37,7 +37,7 @@ module.exports.createTestProject = function(projectName, projectRegion, projectS
fs.writeFileSync(path.join(projectPath, 'jaws.json'), projectJSON);
// Create admin.env file
fs.writeFileSync(path.join(projectPath, 'admin.env'), 'ADMIN_AWS_PROFILE=jawstest');
fs.writeFileSync(path.join(projectPath, 'admin.env'), 'ADMIN_AWS_PROFILE=' + process.env.TEST_JAWS_PROFILE);
return projectPath;
};