2016-06-01 11:30:55 +02:00

570 lines
20 KiB
JavaScript

'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const os = require('os');
const path = require('path');
const AWS = require('aws-sdk');
const BbPromise = require('bluebird');
const AwsDeploy = require('../awsDeploy');
const Serverless = require('../../../Serverless');
const serverless = new Serverless();
serverless.init();
const awsDeploy = new AwsDeploy(serverless);
const tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString());
const serverlessEnvYamlPath = path.join(tmpDirPath, 'serverless.env.yaml');
const serverlessEnvYaml = {
vars: {},
stages: {
dev: {
vars: {},
regions: {
aws_useast1: {
vars: {},
},
},
},
},
};
serverless.service.service = `service-${(new Date).getTime().toString()}`;
serverless.config.servicePath = tmpDirPath;
serverless.utils.writeFileSync(serverlessEnvYamlPath, serverlessEnvYaml);
awsDeploy.options = {
stage: 'dev',
region: 'us-east-1',
};
awsDeploy.CloudFormation = new AWS.CloudFormation({ region: 'us-east-1' });
BbPromise.promisifyAll(awsDeploy.CloudFormation, { suffix: 'Promised' });
describe('#create()', () => {
it('should create stack', () => {
const createStackStub = sinon
.stub(awsDeploy.CloudFormation, 'createStackPromised').returns(BbPromise.resolve());
return awsDeploy.create().then(() => {
expect(createStackStub.calledOnce).to.be.equal(true);
awsDeploy.CloudFormation.createStackPromised.restore();
});
});
});
describe('#monitorCreate()', () => {
it('should keep monitoring until CREATE_COMPLETE stack status', () => {
const describeStub = sinon.stub(awsDeploy.CloudFormation, 'describeStacksPromised');
const cfDataMock = {
StackId: 'new-service-dev',
};
const DescribeReturn = {
Stacks: [
{
StackStatus: 'CREATE_IN_PROGRESS',
},
],
};
const finalDescribeReturn = {
Stacks: [
{
StackStatus: 'CREATE_COMPLETE',
},
],
};
describeStub.onCall(0).returns(BbPromise.resolve(DescribeReturn));
describeStub.onCall(1).returns(BbPromise.resolve(DescribeReturn));
describeStub.onCall(2).returns(BbPromise.resolve(finalDescribeReturn));
return awsDeploy.monitorCreate(cfDataMock, 10).then((stack) => {
expect(describeStub.callCount).to.be.equal(3);
expect(stack.StackStatus).to.be.equal('CREATE_COMPLETE');
awsDeploy.CloudFormation.describeStacksPromised.restore();
});
});
it('should throw an error if CloudFormation returned unusual stack status', () => {
const describeStub = sinon.stub(awsDeploy.CloudFormation, 'describeStacksPromised');
const cfDataMock = {
StackId: 'new-service-dev',
};
const DescribeReturn = {
Stacks: [
{
StackStatus: 'CREATE_IN_PROGRESS',
},
],
};
const finalDescribeReturn = {
Stacks: [
{
StackStatus: 'UNUSUAL_STATUS',
},
],
};
describeStub.onCall(0).returns(BbPromise.resolve(DescribeReturn));
describeStub.onCall(1).returns(BbPromise.resolve(DescribeReturn));
describeStub.onCall(2).returns(BbPromise.resolve(finalDescribeReturn));
return awsDeploy.monitorCreate(cfDataMock, 10).catch((e) => {
expect(e.name).to.be.equal('ServerlessError');
expect(describeStub.callCount).to.be.equal(3);
awsDeploy.CloudFormation.describeStacksPromised.restore();
});
});
});
describe('#addOutputVars()', () => {
it('should addOutputVariables to memory and serverless.env.yaml', () => {
const cfDataMock = {
Outputs: [
{
OutputKey: 'IamRoleArnLambda',
OutputValue: 'someValue',
},
],
};
awsDeploy.serverless.service.environment = {
vars: {},
stages: {
dev: {
vars: {},
regions: {
aws_useast1: {
vars: {},
},
},
},
},
};
return awsDeploy.addOutputVars(cfDataMock)
.then(() => awsDeploy.serverless.yamlParser.parse(serverlessEnvYamlPath))
.then((yaml) => {
// assert var added to memory
expect(awsDeploy.serverless.service.environment.stages.dev
.regions.aws_useast1.vars.iamRoleArnLambda).to.be.equal('someValue');
// assert var added to file
expect(yaml.stages.dev
.regions.aws_useast1.vars.iamRoleArnLambda).to.be.equal('someValue');
});
});
});
describe('#createStack()', () => {
it('should resolve if stack already created', () => {
const createStub = sinon
.stub(awsDeploy, 'create').returns(BbPromise.resolve());
awsDeploy.serverless.service.environment.stages.dev
.regions.aws_useast1.vars.iamRoleArnLambda = true;
return awsDeploy.createStack().then(() => {
awsDeploy.serverless.service.environment.stages.dev
.regions.aws_useast1.vars.iamRoleArnLambda = false;
expect(createStub.called).to.be.equal(false);
awsDeploy.create.restore();
});
});
it('should run promise chain in order', () => {
const createStub = sinon
.stub(awsDeploy, 'create').returns(BbPromise.resolve());
const monitorStub = sinon
.stub(awsDeploy, 'monitorCreate').returns(BbPromise.resolve());
const addOutputvarsStub = sinon
.stub(awsDeploy, 'addOutputVars').returns(BbPromise.resolve());
return awsDeploy.createStack().then(() => {
expect(createStub.calledOnce).to.be.equal(true);
expect(monitorStub.calledAfter(createStub)).to.be.equal(true);
expect(addOutputvarsStub.calledAfter(monitorStub)).to.be.equal(true);
awsDeploy.create.restore();
awsDeploy.monitorCreate.restore();
awsDeploy.addOutputVars.restore();
});
});
});
//
// describe('#create()', () => {
// let awsResourcesDeploy;
//
// before(() => {
// awsResourcesDeploy = new AwsResourcesDeploy(serverless);
// awsResourcesDeploy.serverless.init();
// });
// it('should have commands', () => expect(awsResourcesDeploy.commands).to.be.not.empty);
//
// it('should have hooks', () => expect(awsResourcesDeploy.hooks).to.be.not.empty);
//
// it('should run all promises in order', () => {
// const validateStub = sinon
// .stub(awsResourcesDeploy, 'validate').returns(BbPromise.resolve());
// const createOrUpdateStub = sinon
// .stub(awsResourcesDeploy, 'createOrUpdate').returns(BbPromise.resolve());
// const monitorStub = sinon
// .stub(awsResourcesDeploy, 'monitor').returns(BbPromise.resolve());
// const addOutputVariablesStub = sinon
// .stub(awsResourcesDeploy, 'addOutputVariables').returns(BbPromise.resolve());
// const finishStub = sinon
// .stub(awsResourcesDeploy, 'finish').returns(BbPromise.resolve());
//
// return awsResourcesDeploy.hooks['resources:resources']({ testProp: true }).then(() => {
// expect(awsResourcesDeploy.options.testProp).to.be.equal(true);
// expect(validateStub.calledOnce).to.be.equal(true);
// expect(createOrUpdateStub.calledAfter(validateStub)).to.be.equal(true);
// expect(monitorStub.calledAfter(createOrUpdateStub)).to.be.equal(true);
// expect(addOutputVariablesStub.calledAfter(monitorStub)).to.be.equal(true);
// expect(finishStub.calledAfter(addOutputVariablesStub)).to.be.equal(true);
//
// awsResourcesDeploy.validate.restore();
// awsResourcesDeploy.createOrUpdate.restore();
// awsResourcesDeploy.monitor.restore();
// awsResourcesDeploy.addOutputVariables.restore();
// awsResourcesDeploy.finish.restore();
// });
// });
// });
//
// describe('#validate()', () => {
// before(() => {
// awsResourcesDeploy.serverless.service.resources.aws = true;
// awsResourcesDeploy.options.stage = 'dev';
// awsResourcesDeploy.options.region = 'aws_useast_1';
// awsResourcesDeploy.serverless.service.environment = {
// vars: {},
// stages: {
// dev: {
// vars: {},
// regions: {
// aws_useast_1: {
// vars: {},
// },
// },
// },
// },
// };
// });
//
// it('should generate greeting if interactive', () => {
// awsResourcesDeploy.serverless.config.interactive = true;
// const greetingStub = sinon.stub(awsResourcesDeploy.serverless.cli, 'asciiGreeting');
// return awsResourcesDeploy.validate().then(() => {
// expect(greetingStub.calledOnce).to.be.equal(true);
// awsResourcesDeploy.serverless.cli.asciiGreeting.restore();
// awsResourcesDeploy.serverless.config.interactive = false;
// });
// });
//
// it('should NOT generate greeting if not interactive', () => {
// const greetingStub = sinon.stub(awsResourcesDeploy.serverless.cli, 'asciiGreeting');
// return awsResourcesDeploy.validate().then(() => {
// expect(greetingStub.notCalled).to.be.equal(true);
// awsResourcesDeploy.serverless.cli.asciiGreeting.restore();
// });
// });
//
// it('should attach CloudFormation instance to context', () => awsResourcesDeploy.validate()
// .then(() => expect(typeof awsResourcesDeploy.CloudFormation).to.not.be.equal('undefined'))
// );
//
// it('should log "Deploying Resources to AWS..."', () => {
// const logStub = sinon.stub(awsResourcesDeploy.serverless.cli, 'log');
// return awsResourcesDeploy.validate().then(() => {
// expect(logStub.calledOnce).to.be.equal(true);
// awsResourcesDeploy.serverless.cli.log.restore();
// });
// });
//
// // it('should start spinner', () => {
// // const spinnerStub = sinon.stub(awsResourcesDeploy.serverless.cli, 'spinner');
// // return awsResourcesDeploy.validate().then(() => {
// // expect(spinnerStub.calledOnce).to.be.equal(true);
// // awsResourcesDeploy.serverless.cli.spinner.restore();
// // });
// // });
//
// it('should throw an error if compiled resources are missing', () => {
// awsResourcesDeploy.serverless.service.resources.aws = false;
// expect(() => awsResourcesDeploy.validate()).to.throw(Error);
// awsResourcesDeploy.serverless.service.resources.aws = true;
// });
//
// it('should throw an error if stage is missing', () => {
// awsResourcesDeploy.options.stage = false;
// expect(() => awsResourcesDeploy.validate()).to.throw(Error);
// awsResourcesDeploy.options.stage = 'dev';
// });
//
// it('should throw an error if region is missing', () => {
// awsResourcesDeploy.options.region = false;
// expect(() => awsResourcesDeploy.validate()).to.throw(Error);
// awsResourcesDeploy.options.region = 'aws_useast1';
// });
//
// it('should throw an error if stage does not exist in service', () => {
// awsResourcesDeploy.options.stage = 'prod';
// expect(() => awsResourcesDeploy.validate()).to.throw(Error);
// awsResourcesDeploy.options.stage = 'dev';
// });
//
// it('should throw an error if region does not exist in service', () => {
// awsResourcesDeploy.options.region = 'aws_uswest2';
// expect(() => awsResourcesDeploy.validate()).to.throw(Error);
// awsResourcesDeploy.options.region = 'aws_useast1';
// });
// });
//
// describe('#createOrUpdate()', () => {
// beforeEach(() => {
// awsResourcesDeploy.options.stage = 'dev';
// awsResourcesDeploy.options.region = 'us-east-1';
// awsResourcesDeploy.serverless.service.service = 'new-service';
// awsResourcesDeploy.serverless.service.resources.aws = {};
// const config = {
// region: 'us-east-1',
// };
// awsResourcesDeploy.CloudFormation = new AWS.CloudFormation(config);
// BbPromise.promisifyAll(awsResourcesDeploy.CloudFormation, { suffix: 'Promised' });
// });
//
// afterEach(() => {
// awsResourcesDeploy.CloudFormation.describeStackResourcesPromised.restore();
// });
// it('should throw any other CloudFormation error', () => {
// const errorMock = {
// message: '',
// };
//
// // the stub should return a promise rejection not an actual Error, so we're mocking it.
// // notice the use of stub.returns rather than stub.throws
// const describeStub = sinon
// .stub(awsResourcesDeploy.CloudFormation, 'describeStackResourcesPromised')
// .returns(BbPromise.reject(errorMock));
//
// // we're not asserting throwing an error, we're asserting rejection handling
// // notice the use of .catch instead of .then
// return awsResourcesDeploy.createOrUpdate().catch((e) => {
// // we're not using expect.to.throw, but rather asserting we're getting the right rejection
// expect(e.name).to.be.equal('ServerlessError');
// expect(awsResourcesDeploy.stackName).to.be.equal('new-service-dev');
// expect(describeStub.calledOnce).to.be.equal(true);
// const paramsMock = {
// StackName: 'new-service-dev',
// };
// expect(describeStub.calledWith(paramsMock)).to.be.equal(true);
// });
// });
//
// it('should create stack if it does not exist', () => {
// const errorMock = {
// message: 'does not exist',
// };
//
// sinon.stub(awsResourcesDeploy.CloudFormation, 'describeStackResourcesPromised')
// .returns(BbPromise.reject(errorMock));
//
// const createStackStub = sinon.stub(awsResourcesDeploy.CloudFormation, 'createStackPromised');
//
// return awsResourcesDeploy.createOrUpdate().then(() => {
// expect(createStackStub.calledOnce).to.be.equal(true);
// awsResourcesDeploy.CloudFormation.createStackPromised.restore();
// });
// });
//
// it('should update stack if it already exists', () => {
// sinon.stub(awsResourcesDeploy.CloudFormation, 'describeStackResourcesPromised')
// .returns(BbPromise.resolve());
//
// const updateStackStub = sinon.stub(awsResourcesDeploy.CloudFormation, 'updateStackPromised');
//
// return awsResourcesDeploy.createOrUpdate().then(() => {
// expect(updateStackStub.calledOnce).to.be.equal(true);
// const paramsMock = {
// Capabilities: [
// 'CAPABILITY_IAM',
// ],
// Parameters: [],
// TemplateBody: JSON.stringify(awsResourcesDeploy.serverless.service.resources.aws),
// StackName: 'new-service-dev',
// };
// expect(updateStackStub.calledWith(paramsMock)).to.be.equal(true);
// awsResourcesDeploy.CloudFormation.updateStackPromised.restore();
// });
// });
//
// it('should resolve if no updates to be performed', () => {
// const errorMock = {
// message: 'No updates are to be performed.',
// };
//
// sinon.stub(awsResourcesDeploy.CloudFormation, 'describeStackResourcesPromised')
// .returns(BbPromise.reject(errorMock));
//
// return awsResourcesDeploy.createOrUpdate().then((noUpdatesString) => {
// expect(noUpdatesString).to.be.equal('No resource updates are to be performed.');
// });
// });
// });
//
// describe('#monitor()', () => {
// beforeEach(() => {
// const config = {
// region: 'us-east-1',
// };
// awsResourcesDeploy.CloudFormation = new AWS.CloudFormation(config);
// BbPromise.promisifyAll(awsResourcesDeploy.CloudFormation, { suffix: 'Promised' });
// });
//
// it('should log and resolve if no updates to be performed', () => {
// const logStub = sinon.stub(awsResourcesDeploy.serverless.cli, 'log');
// const cfDataMock = 'No resource updates are to be performed.';
// return awsResourcesDeploy.monitor(cfDataMock).then(() => {
// expect(logStub.calledOnce).to.be.equal(true);
// awsResourcesDeploy.serverless.cli.log.restore();
// });
// });
//
// it('should keep monitoring until CREATE_COMPLETE stack status', () => {
// const describeStub = sinon.stub(awsResourcesDeploy.CloudFormation, 'describeStacksPromised');
// const cfDataMock = {
// StackId: 'new-service-dev',
// };
// const DescribeReturn = {
// Stacks: [
// {
// StackStatus: 'CREATE_IN_PROGRESS',
// },
// ],
// };
// const finalDescribeReturn = {
// Stacks: [
// {
// StackStatus: 'CREATE_COMPLETE',
// },
// ],
// };
//
// describeStub.onCall(0).returns(BbPromise.resolve(DescribeReturn));
// describeStub.onCall(1).returns(BbPromise.resolve(DescribeReturn));
// describeStub.onCall(2).returns(BbPromise.resolve(finalDescribeReturn));
//
// return awsResourcesDeploy.monitor(cfDataMock, 10).then((stack) => {
// expect(describeStub.callCount).to.be.equal(3);
// expect(stack.StackStatus).to.be.equal('CREATE_COMPLETE');
// awsResourcesDeploy.CloudFormation.describeStacksPromised.restore();
// });
// });
//
// it('should keep monitoring until UPDATE_COMPLETE stack status', () => {
// const describeStub = sinon.stub(awsResourcesDeploy.CloudFormation, 'describeStacksPromised');
// const cfDataMock = {
// StackId: 'new-service-dev',
// };
// const DescribeReturn = {
// Stacks: [
// {
// StackStatus: 'UPDATE_IN_PROGRESS',
// },
// ],
// };
// const finalDescribeReturn = {
// Stacks: [
// {
// StackStatus: 'UPDATE_COMPLETE',
// },
// ],
// };
//
// describeStub.onCall(0).returns(BbPromise.resolve(DescribeReturn));
// describeStub.onCall(1).returns(BbPromise.resolve(DescribeReturn));
// describeStub.onCall(2).returns(BbPromise.resolve(finalDescribeReturn));
//
// return awsResourcesDeploy.monitor(cfDataMock, 10).then((stack) => {
// expect(describeStub.callCount).to.be.equal(3);
// expect(stack.StackStatus).to.be.equal('UPDATE_COMPLETE');
// awsResourcesDeploy.CloudFormation.describeStacksPromised.restore();
// });
// });
//
// it('should throw an error if CloudFormation returned unusual stack status', () => {
// const describeStub = sinon.stub(awsResourcesDeploy.CloudFormation, 'describeStacksPromised');
// const cfDataMock = {
// StackId: 'new-service-dev',
// };
// const DescribeReturn = {
// Stacks: [
// {
// StackStatus: 'UPDATE_IN_PROGRESS',
// },
// ],
// };
// const finalDescribeReturn = {
// Stacks: [
// {
// StackStatus: 'UNUSUAL_STATUS',
// },
// ],
// };
//
// describeStub.onCall(0).returns(BbPromise.resolve(DescribeReturn));
// describeStub.onCall(1).returns(BbPromise.resolve(DescribeReturn));
// describeStub.onCall(2).returns(BbPromise.resolve(finalDescribeReturn));
//
// return awsResourcesDeploy.monitor(cfDataMock, 10).catch((e) => {
// expect(e.name).to.be.equal('ServerlessError');
// expect(describeStub.callCount).to.be.equal(3);
// awsResourcesDeploy.CloudFormation.describeStacksPromised.restore();
// });
// });
// });
//
// describe('#addOutputVariables()', () => {
// it('should addOutputVariables to serverless.env.yaml', () => {
// const tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString());
// const serverlessEnvYamlPath = path.join(tmpDirPath, 'serverless.env.yaml');
// const serverlessEnvYaml = {
// vars: {},
// stages: {
// dev: {
// vars: {},
// regions: {
// aws_useast1: {
// vars: {},
// },
// },
// },
// },
// };
// awsResourcesDeploy.options = {
// stage: 'dev',
// region: 'aws_useast1',
// };
// awsResourcesDeploy.serverless.utils.writeFileSync(serverlessEnvYamlPath, serverlessEnvYaml);
// const cfDataMock = {
// Outputs: [
// {
// OutputKey: 'IamRoleArnLambda',
// OutputValue: 'someValue',
// },
// ],
// };
// awsResourcesDeploy.serverless.config.servicePath = tmpDirPath;
// return awsResourcesDeploy.addOutputVariables(cfDataMock)
// .then(() => awsResourcesDeploy.serverless.yamlParser.parse(serverlessEnvYamlPath))
// .then((yaml) => expect(yaml.stages.dev.regions.aws_useast1.vars.iamRoleArnLambda)
// .to.be.equal('someValue'));
// });
// });
//
// describe('#finish()', () => {
// it('should log success message', () => {
// const logStub = sinon.stub(awsResourcesDeploy.serverless.cli, 'log');
// return awsResourcesDeploy.finish().then(() => {
// expect(logStub
// .calledWith('Successfully deployed resources to AWS in the specified stage/region'))
// .to.be.equal(true);
// awsResourcesDeploy.serverless.cli.log.restore();
// });
// });
// });