serverless/lib/plugins/aws/deploy/tests/configureStack.js
2016-10-19 08:32:06 +02:00

197 lines
6.4 KiB
JavaScript

'use strict';
const sinon = require('sinon');
const BbPromise = require('bluebird');
const path = require('path');
const expect = require('chai').expect;
const AwsProvider = require('../../../awsProvider/awsProvider');
const Serverless = require('../../../../Serverless');
const validate = require('../../lib/validate');
const configureStack = require('../lib/configureStack');
describe('#configureStack', () => {
let serverless;
const awsPlugin = {};
beforeEach(() => {
serverless = new Serverless();
awsPlugin.serverless = serverless;
awsPlugin.aws = new AwsProvider(serverless);
awsPlugin.options = {
stage: 'dev',
region: 'us-east-1',
};
Object.assign(awsPlugin, configureStack, validate);
});
it('should validate the region for the given S3 bucket', () => {
const bucketName = 'com.serverless.deploys';
const getBucketLocationStub = sinon
.stub(awsPlugin.aws, 'request').returns(
BbPromise.resolve({ LocationConstraint: awsPlugin.options.region })
);
awsPlugin.serverless.service.provider.deploymentBucket = bucketName;
return awsPlugin.configureStack()
.then(() => {
expect(getBucketLocationStub.args[0][0]).to.equal('S3');
expect(getBucketLocationStub.args[0][1]).to.equal('getBucketLocation');
expect(getBucketLocationStub.args[0][2].Bucket).to.equal(bucketName);
});
});
it('should reject an S3 bucket in the wrong region', () => {
const bucketName = 'com.serverless.deploys';
const createStackStub = sinon
.stub(awsPlugin.aws, 'request').returns(
BbPromise.resolve({ LocationConstraint: 'us-west-1' })
);
awsPlugin.serverless.service.provider.deploymentBucket = 'com.serverless.deploys';
return awsPlugin.configureStack()
.catch((err) => {
expect(createStackStub.args[0][0]).to.equal('S3');
expect(createStackStub.args[0][1]).to.equal('getBucketLocation');
expect(createStackStub.args[0][2].Bucket).to.equal(bucketName);
expect(err.message).to.contain('not in the same region');
})
.then(() => {});
});
it('should merge the IamRoleLambdaExecution template into the CloudFormation template', () => {
const IamRoleLambdaExecutionTemplate = awsPlugin.serverless.utils.readFileSync(
path.join(
__dirname,
'..',
'lib',
'iam-role-lambda-execution-template.json'
)
);
return awsPlugin.configureStack()
.then(() => {
expect(awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources.IamRoleLambdaExecution
).to.deep.equal(IamRoleLambdaExecutionTemplate.IamRoleLambdaExecution);
});
});
it('should merge IamPolicyLambdaExecution template into the CloudFormation template', () =>
awsPlugin.configureStack()
.then(() => {
// we check for the type here because a deep equality check will error out due to
// the updates which are made after the merge (they are tested in a separate test)
expect(awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources.IamPolicyLambdaExecution.Type
).to.deep.equal('AWS::IAM::Policy');
})
);
it('should update the necessary variables for the IamPolicyLambdaExecution', () =>
awsPlugin.configureStack()
.then(() => {
expect(awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources
.IamPolicyLambdaExecution
.Properties
.PolicyName
).to.equal(
`${
awsPlugin.options.stage
}-${
awsPlugin.serverless.service.service
}-lambda`
);
expect(awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources
.IamPolicyLambdaExecution
.Properties
.PolicyDocument
.Statement[0]
.Resource
).to.equal(`arn:aws:logs:${awsPlugin.options.region}:*:*`);
})
);
it('should add custom IAM policy statements', () => {
awsPlugin.serverless.service.provider.name = 'aws';
awsPlugin.serverless.service.provider.iamRoleStatements = [
{
Effect: 'Allow',
Action: [
'something:SomethingElse',
],
Resource: 'some:aws:arn:xxx:*:*',
},
];
return awsPlugin.configureStack()
.then(() => {
expect(awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources.IamPolicyLambdaExecution.Properties.PolicyDocument.Statement[1]
).to.deep.equal(awsPlugin.serverless.service.provider.iamRoleStatements[0]);
});
});
it('should use a custom bucket if specified', () => {
const bucketName = 'com.serverless.deploys';
awsPlugin.serverless.service.provider.deploymentBucket = bucketName;
const coreCloudFormationTemplate = awsPlugin.serverless.utils.readFileSync(
path.join(
__dirname,
'..',
'lib',
'core-cloudformation-template.json'
)
);
awsPlugin.serverless.service.provider
.compiledCloudFormationTemplate = coreCloudFormationTemplate;
sinon
.stub(awsPlugin.aws, 'request')
.returns(BbPromise.resolve({ LocationConstraint: awsPlugin.options.region }));
return awsPlugin.configureStack()
.then(() => {
expect(
awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Outputs.ServerlessDeploymentBucketName.Value
).to.equal(bucketName);
// eslint-disable-next-line no-unused-expressions
expect(
awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources.ServerlessDeploymentBucket
).to.not.exist;
});
});
it('should not add IamPolicyLambdaExecution', () => {
awsPlugin.serverless.service.provider.iamRoleARN = 'some:aws:arn:xxx:*:*';
return awsPlugin.configureStack()
.then(() => expect(
awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources.IamPolicyLambdaExecution
).to.not.exist);
});
it('should not add IamRole', () => {
awsPlugin.serverless.service.provider.iamRoleARN = 'some:aws:arn:xxx:*:*';
return awsPlugin.configureStack()
.then(() => expect(
awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources.IamRoleLambdaExecution
).to.not.exist);
});
});