mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
StageCreate, RegionCreate: Return event object, add tests to check and validate event object.
This commit is contained in:
parent
bd30a1da24
commit
263ecff02f
@ -92,13 +92,15 @@ usage: serverless region create`,
|
||||
.then(_this._promptRegion)
|
||||
.then(_this._validateAndPrepare)
|
||||
.then(_this._initAWS)
|
||||
.then(_this._createProjectBucket)
|
||||
.then(_this._createRegionBucket)
|
||||
.then(_this._putEnvFile)
|
||||
.then(_this._putCfFile)
|
||||
.then(_this._createCfStack)
|
||||
.then(_this._updateProjectJson)
|
||||
.then(function() {
|
||||
SCli.log('Successfully created region ' + _this.evt.region + ' within stage ' + _this.evt.stage);
|
||||
|
||||
return _this.evt;
|
||||
});
|
||||
}
|
||||
|
||||
@ -226,10 +228,10 @@ usage: serverless region create`,
|
||||
* - If it does not exist
|
||||
*/
|
||||
|
||||
_createProjectBucket() {
|
||||
this.evt.projectBucket = SUtils.generateRegionBucketName(this.evt.region, this.S._projectJson.domain);
|
||||
SCli.log('Creating a region bucket on S3: ' + this.evt.projectBucket + '...');
|
||||
return this.S3.sCreateBucket(this.evt.projectBucket);
|
||||
_createRegionBucket() {
|
||||
this.evt.regionBucket = SUtils.generateRegionBucketName(this.evt.region, this.S._projectJson.domain);
|
||||
SCli.log('Creating a region bucket on S3: ' + this.evt.regionBucket + '...');
|
||||
return this.S3.sCreateBucket(this.evt.regionBucket);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -244,7 +246,7 @@ usage: serverless region create`,
|
||||
SERVERLESS_DATA_MODEL_STAGE=${stage}`;
|
||||
|
||||
return this.S3.sPutEnvFile(
|
||||
this.evt.projectBucket,
|
||||
this.evt.regionBucket,
|
||||
this.S._projectJson.name,
|
||||
this.evt.stage,
|
||||
envFileContents);
|
||||
@ -257,7 +259,7 @@ SERVERLESS_DATA_MODEL_STAGE=${stage}`;
|
||||
_putCfFile() {
|
||||
return this.CF.sPutCfFile(
|
||||
this.S._projectRootPath,
|
||||
this.evt.projectBucket,
|
||||
this.evt.regionBucket,
|
||||
this.S._projectJson.name,
|
||||
this.evt.stage,
|
||||
'resources');
|
||||
@ -310,13 +312,14 @@ SERVERLESS_DATA_MODEL_STAGE=${stage}`;
|
||||
let regionObj = {
|
||||
region: _this.evt.region,
|
||||
iamRoleArnLambda: '',
|
||||
regionBucket: _this.evt.projectBucket,
|
||||
regionBucket: _this.evt.regionBucket,
|
||||
};
|
||||
|
||||
if (cfStackData) {
|
||||
for (let i = 0; i < cfStackData.Outputs.length; i++) {
|
||||
if (cfStackData.Outputs[i].OutputKey === 'IamRoleArnLambda') {
|
||||
regionObj.iamRoleArnLambda = cfStackData.Outputs[i].OutputValue;
|
||||
_this.evt.iamRoleLambdaArn = cfStackData.Outputs[i].OutputValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ usage: serverless stage create`,
|
||||
.then(_this._validateAndPrepare)
|
||||
.then(_this._initAWS)
|
||||
.then(_this._updateCfTemplate)
|
||||
.then(_this._createProjectBucket)
|
||||
.then(_this._createRegionBucket)
|
||||
.then(_this._putEnvFile)
|
||||
.then(_this._putCfFile)
|
||||
.then(_this._createCfStack)
|
||||
@ -226,9 +226,9 @@ usage: serverless stage create`,
|
||||
secretAccessKey: this.S._awsAdminSecretKey,
|
||||
};
|
||||
|
||||
this.CF = require('../utils/aws/CloudFormation')(awsConfig);
|
||||
this.Lambda = require('../utils/aws/Lambda')(awsConfig);
|
||||
this.S3 = require('../utils/aws/S3')(awsConfig);
|
||||
this.CF = require('../utils/aws/CloudFormation')(awsConfig);
|
||||
this.Lambda = require('../utils/aws/Lambda')(awsConfig);
|
||||
this.S3 = require('../utils/aws/S3')(awsConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -249,7 +249,7 @@ usage: serverless stage create`,
|
||||
if (cfTemplate.Parameters.aaProjectName.AllowedValues.indexOf(this.S._projectJson.name) == -1) {
|
||||
cfTemplate.Parameters.aaProjectName.AllowedValues.push(this.S._projectJson.name);
|
||||
}
|
||||
|
||||
console.log(cfTemplate.Parameters.aaStage);
|
||||
// Write it
|
||||
return SUtils.writeFile(
|
||||
projResoucesCfPath,
|
||||
@ -262,12 +262,11 @@ usage: serverless stage create`,
|
||||
* - if it does not exist
|
||||
*/
|
||||
|
||||
_createProjectBucket() {
|
||||
_createRegionBucket() {
|
||||
|
||||
this._regionBucket = SUtils.generateRegionBucketName(this.evt.stage, this.evt.region, this.S._projectJson.domain);
|
||||
SCli.log('Creating a region bucket on S3: ' + this._regionBucket + '...');
|
||||
return this.S3.sCreateBucket(this._regionBucket);
|
||||
|
||||
this.evt.regionBucket = SUtils.generateRegionBucketName(this.evt.stage, this.evt.region, this.S._projectJson.domain);
|
||||
SCli.log('Creating a region bucket on S3: ' + this.evt.regionBucket + '...');
|
||||
return this.S3.sCreateBucket(this.evt.regionBucket);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -282,7 +281,7 @@ usage: serverless stage create`,
|
||||
SERVERLESS_DATA_MODEL_STAGE=${stage}`;
|
||||
|
||||
return this.S3.sPutEnvFile(
|
||||
this._regionBucket,
|
||||
this.evt.regionBucket,
|
||||
this.S._projectJson.name,
|
||||
this.evt.stage,
|
||||
envFileContents);
|
||||
@ -295,7 +294,7 @@ SERVERLESS_DATA_MODEL_STAGE=${stage}`;
|
||||
_putCfFile() {
|
||||
return this.CF.sPutCfFile(
|
||||
this.S._projectRootPath,
|
||||
this._regionBucket,
|
||||
this.evt.regionBucket,
|
||||
this.S._projectJson.name,
|
||||
this.evt.stage,
|
||||
'resources');
|
||||
@ -349,7 +348,7 @@ SERVERLESS_DATA_MODEL_STAGE=${stage}`;
|
||||
regionObj = {
|
||||
region: _this.evt.region,
|
||||
iamRoleArnLambda: '',
|
||||
regionBucket: _this._regionBucket,
|
||||
regionBucket: _this.evt.regionBucket,
|
||||
};
|
||||
|
||||
if (cfStackData) {
|
||||
|
||||
@ -25,9 +25,8 @@ describe('All Tests', function() {
|
||||
|
||||
require('./tests/actions/ProjectCreate');
|
||||
require('./tests/actions/StageCreate');
|
||||
//require('./tests/actions/RegionCreate');
|
||||
//require('./tests/actions/VersionLambda');
|
||||
//require('./tests/actions/ModuleCreate');
|
||||
require('./tests/actions/RegionCreate');
|
||||
require('./tests/actions/ModuleCreate');
|
||||
//require('./tests/actions/FunctionDeploy');
|
||||
//require('./tests/actions/EnvList');
|
||||
//require('./tests/actions/EnvGet');
|
||||
|
||||
@ -17,14 +17,14 @@ let fs = require('fs'),
|
||||
*/
|
||||
|
||||
module.exports.createTestProject = function(config, npmInstallDirs) {
|
||||
let projectName = config.name,
|
||||
let projectName = ('testprj-' + uuid.v4()).replace(/-/g, ''),
|
||||
projectStage = config.stage,
|
||||
projectRegion = config.region,
|
||||
projectLambdaIAMRole = config.iamRoleArnLambda,
|
||||
projectDomain = config.domain;
|
||||
projectDomain = projectName + '.com';
|
||||
|
||||
// Create Test Project
|
||||
let tmpProjectPath = path.join(os.tmpdir(), projectName + '-' + uuid.v4());
|
||||
let tmpProjectPath = path.join(os.tmpdir(), projectName);
|
||||
|
||||
utils.sDebug('test_utils', 'Creating test project in ' + tmpProjectPath + '\n');
|
||||
|
||||
@ -48,6 +48,14 @@ module.exports.createTestProject = function(config, npmInstallDirs) {
|
||||
// Add project name to AllowedValues
|
||||
resourcesCF.Parameters.aaProjectName.AllowedValues.push(projectName);
|
||||
|
||||
// Add stages to AllowedValues
|
||||
resourcesCF.Parameters.aaStage.AllowedValues.push(config.stage);
|
||||
resourcesCF.Parameters.aaStage.AllowedValues.push(config.stage2);
|
||||
|
||||
// Add stages to AllowedValues
|
||||
resourcesCF.Parameters.aaDataModelStage.AllowedValues.push(config.stage);
|
||||
resourcesCF.Parameters.aaDataModelStage.AllowedValues.push(config.stage2);
|
||||
|
||||
return Promise.all([
|
||||
utils.writeFile(path.join(tmpProjectPath, 'cloudformation', 'lambdas-cf.json'), JSON.stringify(lambdasCF, null, 2)),
|
||||
utils.writeFile(path.join(tmpProjectPath, 'cloudformation', 'resources-cf.json'), JSON.stringify(resourcesCF, null, 2)),
|
||||
|
||||
@ -58,40 +58,41 @@ describe('Test action: Project Create', function() {
|
||||
|
||||
this.timeout(0);
|
||||
|
||||
let name = config.name + '-' + uuid.v4();
|
||||
let event = {
|
||||
let name = ('testprj-' + uuid.v4()).replace(/-/g, '');
|
||||
let domain = name + '.com';
|
||||
let event = {
|
||||
name: name,
|
||||
domain: config.domain,
|
||||
domain: domain,
|
||||
notificationEmail: config.notifyEmail,
|
||||
region: config.region,
|
||||
noExeCf: config.noExecuteCf,
|
||||
};
|
||||
|
||||
serverless.actions.projectCreate(event)
|
||||
.then(function(evt) {
|
||||
.then(function(evt) {
|
||||
|
||||
// Validate Event
|
||||
validateEvent(evt);
|
||||
// Validate Event
|
||||
validateEvent(evt);
|
||||
|
||||
let projectJson = utils.readAndParseJsonSync(path.join(os.tmpdir(), name, 's-project.json'));
|
||||
let region = false;
|
||||
let projectJson = utils.readAndParseJsonSync(path.join(os.tmpdir(), name, 's-project.json'));
|
||||
let region = false;
|
||||
|
||||
for (let i = 0; i < projectJson.stages.development.length; i++) {
|
||||
let stage = projectJson.stages[config.stage][i];
|
||||
if (stage.region === config.region) {
|
||||
region = stage.region;
|
||||
for (let i = 0; i < projectJson.stages.development.length; i++) {
|
||||
let stage = projectJson.stages[config.stage][i];
|
||||
if (stage.region === config.region) {
|
||||
region = stage.region;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert.isTrue(region !== false);
|
||||
done();
|
||||
})
|
||||
.catch(SError, function(e) {
|
||||
done(e);
|
||||
})
|
||||
.error(function(e) {
|
||||
done(e);
|
||||
});
|
||||
assert.isTrue(region !== false);
|
||||
done();
|
||||
})
|
||||
.catch(SError, function(e) {
|
||||
done(e);
|
||||
})
|
||||
.error(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -15,6 +15,22 @@ let Serverless = require('../../../lib/Serverless.js'),
|
||||
|
||||
let serverless;
|
||||
|
||||
/**
|
||||
* Validate Event
|
||||
* - Validate an event object's properties
|
||||
*/
|
||||
|
||||
let validateEvent = function(evt) {
|
||||
assert.equal(true, typeof evt.region != 'undefined');
|
||||
assert.equal(true, typeof evt.noExeCf != 'undefined');
|
||||
assert.equal(true, typeof evt.stage != 'undefined');
|
||||
assert.equal(true, typeof evt.regionBucket != 'undefined');
|
||||
|
||||
if (!config.noExecuteCf) {
|
||||
assert.equal(true, typeof evt.iamRoleLambdaArn != 'undefined');
|
||||
}
|
||||
};
|
||||
|
||||
describe('Test Action: Region Create', function() {
|
||||
|
||||
before(function(done) {
|
||||
@ -23,9 +39,9 @@ describe('Test Action: Region Create', function() {
|
||||
testUtils.createTestProject(config)
|
||||
.then(projPath => {
|
||||
this.timeout(0);
|
||||
process.chdir(projPath);
|
||||
// for some weird reason process.chdir adds /private/ before cwd path!!!!
|
||||
console.log(process.cwd());
|
||||
|
||||
process.chdir(projPath); // Ror some weird reason process.chdir adds /private/ before cwd path
|
||||
|
||||
serverless = new Serverless({
|
||||
interactive: false,
|
||||
awsAdminKeyId: config.awsAdminKeyId,
|
||||
@ -52,7 +68,8 @@ describe('Test Action: Region Create', function() {
|
||||
};
|
||||
|
||||
serverless.actions.regionCreate(event)
|
||||
.then(function() {
|
||||
.then(function(evt) {
|
||||
validateEvent(evt);
|
||||
done();
|
||||
})
|
||||
.catch(e => {
|
||||
|
||||
@ -23,6 +23,7 @@ let validateEvent = function(evt) {
|
||||
assert.equal(true, typeof evt.region != 'undefined');
|
||||
assert.equal(true, typeof evt.noExeCf != 'undefined');
|
||||
assert.equal(true, typeof evt.stage != 'undefined');
|
||||
assert.equal(true, typeof evt.regionBucket != 'undefined');
|
||||
|
||||
if (!config.noExecuteCf) {
|
||||
assert.equal(true, typeof evt.iamRoleLambdaArn != 'undefined');
|
||||
@ -61,7 +62,7 @@ describe('Test Action: Stage Create', function() {
|
||||
|
||||
let event = {
|
||||
stage: config.stage2,
|
||||
region: config.region2,
|
||||
region: config.region,
|
||||
noExeCf: config.noExecuteCf,
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user