mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
204 lines
7.0 KiB
JavaScript
204 lines
7.0 KiB
JavaScript
'use strict';
|
|
|
|
const expect = require('chai').expect;
|
|
const sinon = require('sinon');
|
|
const AwsProvider = require('../provider/awsProvider');
|
|
const AwsDeploy = require('../deploy');
|
|
const Serverless = require('../../../Serverless');
|
|
const testUtils = require('../../../../tests/utils');
|
|
|
|
describe('updateStack', () => {
|
|
let serverless;
|
|
let awsDeploy;
|
|
const tmpDirPath = testUtils.getTmpDirPath();
|
|
|
|
beforeEach(() => {
|
|
serverless = new Serverless();
|
|
serverless.config.servicePath = 'foo';
|
|
serverless.setProvider('aws', new AwsProvider(serverless));
|
|
const options = {
|
|
stage: 'dev',
|
|
region: 'us-east-1',
|
|
};
|
|
awsDeploy = new AwsDeploy(serverless, options);
|
|
|
|
awsDeploy.deployedFunctions = [{ name: 'first', zipFileKey: 'zipFileOfFirstFunction' }];
|
|
awsDeploy.bucketName = 'deployment-bucket';
|
|
serverless.service.service = `service-${(new Date()).getTime().toString()}`;
|
|
serverless.config.servicePath = tmpDirPath;
|
|
awsDeploy.serverless.service.package.artifactDirectoryName = 'somedir';
|
|
awsDeploy.serverless.cli = new serverless.classes.CLI();
|
|
});
|
|
|
|
describe('#createFallback()', () => {
|
|
it('should create a stack with the CF template URL', () => {
|
|
const compiledTemplateFileName = 'compiled-cloudformation-template.json';
|
|
|
|
const createStackStub = sinon
|
|
.stub(awsDeploy.provider, 'request').resolves();
|
|
sinon.stub(awsDeploy, 'monitorStack').resolves();
|
|
|
|
return awsDeploy.createFallback().then(() => {
|
|
expect(createStackStub.calledOnce).to.be.equal(true);
|
|
expect(createStackStub.calledWithExactly(
|
|
'CloudFormation',
|
|
'createStack',
|
|
{
|
|
StackName: awsDeploy.provider.naming.getStackName(),
|
|
OnFailure: 'ROLLBACK',
|
|
Capabilities: [
|
|
'CAPABILITY_IAM',
|
|
'CAPABILITY_NAMED_IAM',
|
|
],
|
|
Parameters: [],
|
|
TemplateURL: `https://s3.amazonaws.com/${awsDeploy.bucketName}/${awsDeploy.serverless
|
|
.service.package.artifactDirectoryName}/${compiledTemplateFileName}`,
|
|
Tags: [{ Key: 'STAGE', Value: awsDeploy.options.stage }],
|
|
},
|
|
awsDeploy.options.stage,
|
|
awsDeploy.options.region
|
|
)).to.be.equal(true);
|
|
awsDeploy.provider.request.restore();
|
|
awsDeploy.monitorStack.restore();
|
|
});
|
|
});
|
|
|
|
it('should include custom stack tags', () => {
|
|
awsDeploy.serverless.service.provider.stackTags = { STAGE: 'overridden', tag1: 'value1' };
|
|
|
|
const createStackStub = sinon
|
|
.stub(awsDeploy.provider, 'request').resolves();
|
|
sinon.stub(awsDeploy, 'monitorStack').resolves();
|
|
|
|
return awsDeploy.createFallback().then(() => {
|
|
expect(createStackStub.args[0][2].Tags)
|
|
.to.deep.equal([
|
|
{ Key: 'STAGE', Value: 'overridden' },
|
|
{ Key: 'tag1', Value: 'value1' },
|
|
]);
|
|
awsDeploy.provider.request.restore();
|
|
awsDeploy.monitorStack.restore();
|
|
});
|
|
});
|
|
|
|
it('should use CloudFormation service role if it is specified', () => {
|
|
awsDeploy.serverless.service.provider.cfnRole = 'arn:aws:iam::123456789012:role/myrole';
|
|
|
|
const createStackStub = sinon.stub(awsDeploy.provider, 'request').resolves();
|
|
sinon.stub(awsDeploy, 'monitorStack').resolves();
|
|
|
|
return awsDeploy.createFallback().then(() => {
|
|
expect(createStackStub.args[0][2].RoleARN)
|
|
.to.equal('arn:aws:iam::123456789012:role/myrole');
|
|
awsDeploy.provider.request.restore();
|
|
awsDeploy.monitorStack.restore();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#update()', () => {
|
|
let updateStackStub;
|
|
|
|
beforeEach(() => {
|
|
updateStackStub = sinon
|
|
.stub(awsDeploy.provider, 'request').resolves();
|
|
sinon.stub(awsDeploy, 'monitorStack').resolves();
|
|
});
|
|
|
|
afterEach(() => {
|
|
updateStackStub.restore();
|
|
awsDeploy.monitorStack.restore();
|
|
});
|
|
|
|
it('should update the stack', () => awsDeploy.update()
|
|
.then(() => {
|
|
const compiledTemplateFileName = 'compiled-cloudformation-template.json';
|
|
expect(updateStackStub.calledOnce).to.be.equal(true);
|
|
expect(updateStackStub.calledWithExactly(
|
|
'CloudFormation',
|
|
'updateStack',
|
|
{
|
|
StackName: awsDeploy.provider.naming.getStackName(),
|
|
Capabilities: [
|
|
'CAPABILITY_IAM',
|
|
'CAPABILITY_NAMED_IAM',
|
|
],
|
|
Parameters: [],
|
|
TemplateURL: `https://s3.amazonaws.com/${awsDeploy.bucketName}/${awsDeploy.serverless
|
|
.service.package.artifactDirectoryName}/${compiledTemplateFileName}`,
|
|
Tags: [{ Key: 'STAGE', Value: awsDeploy.options.stage }],
|
|
},
|
|
awsDeploy.options.stage,
|
|
awsDeploy.options.region
|
|
)).to.be.equal(true);
|
|
})
|
|
);
|
|
|
|
it('should include custom stack tags and policy', () => {
|
|
awsDeploy.serverless.service.provider.stackTags = { STAGE: 'overridden', tag1: 'value1' };
|
|
awsDeploy.serverless.service.provider.stackPolicy = [{
|
|
Effect: 'Allow',
|
|
Principal: '*',
|
|
Action: 'Update:*',
|
|
Resource: '*',
|
|
}];
|
|
|
|
return awsDeploy.update().then(() => {
|
|
expect(updateStackStub.args[0][2].Tags)
|
|
.to.deep.equal([
|
|
{ Key: 'STAGE', Value: 'overridden' },
|
|
{ Key: 'tag1', Value: 'value1' },
|
|
]);
|
|
expect(updateStackStub.args[0][2].StackPolicyBody)
|
|
.to.equal(
|
|
'{"Statement":[{"Effect":"Allow","Principal":"*","Action":"Update:*","Resource":"*"}]}'
|
|
);
|
|
});
|
|
});
|
|
|
|
it('should success if no changes to stack happened', () => {
|
|
awsDeploy.monitorStack.restore();
|
|
sinon.stub(awsDeploy, 'monitorStack')
|
|
.rejects(new Error('No updates are to be performed.'));
|
|
|
|
return awsDeploy.update();
|
|
});
|
|
|
|
it('should use CloudFormation service role if it is specified', () => {
|
|
awsDeploy.serverless.service.provider.cfnRole = 'arn:aws:iam::123456789012:role/myrole';
|
|
|
|
return awsDeploy.update().then(() => {
|
|
expect(updateStackStub.args[0][2].RoleARN)
|
|
.to.equal('arn:aws:iam::123456789012:role/myrole');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#updateStack()', () => {
|
|
it('should fallback to createStack if createLater flag exists', () => {
|
|
awsDeploy.createLater = true;
|
|
const createFallbackStub = sinon
|
|
.stub(awsDeploy, 'createFallback').resolves();
|
|
const updateStub = sinon
|
|
.stub(awsDeploy, 'update').resolves();
|
|
|
|
return awsDeploy.updateStack().then(() => {
|
|
expect(createFallbackStub.calledOnce).to.be.equal(true);
|
|
expect(updateStub.called).to.be.equal(false);
|
|
awsDeploy.update.restore();
|
|
});
|
|
});
|
|
|
|
it('should run promise chain in order', () => {
|
|
const updateStub = sinon
|
|
.stub(awsDeploy, 'update').resolves();
|
|
|
|
return awsDeploy.updateStack().then(() => {
|
|
expect(updateStub.calledOnce).to.be.equal(true);
|
|
|
|
awsDeploy.update.restore();
|
|
});
|
|
});
|
|
});
|
|
});
|