mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
This way the plugin simply adds the Resources to the CloudFormation template in the compileEvents lifecycle. The deploy plugin will use this resources to update the stack later on. This removes the dependency between the awsCompileS3Events plugin and the deploy plugin as the awsCompileS3Events plugin simply adds the resources to the section and forgets about it.
206 lines
5.4 KiB
JavaScript
206 lines
5.4 KiB
JavaScript
'use strict';
|
|
|
|
const expect = require('chai').expect;
|
|
const AwsCompileS3Events = require('../awsCompileS3Events');
|
|
const Serverless = require('../../../Serverless');
|
|
|
|
describe('awsCompileS3Events', () => {
|
|
let serverless;
|
|
let awsCompileS3Events;
|
|
|
|
const functionsObjectMock = {
|
|
first: {
|
|
events: {
|
|
aws: {
|
|
s3: [
|
|
'first-function-bucket1',
|
|
'first-function-bucket2',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
second: {
|
|
events: {
|
|
aws: {
|
|
s3: [
|
|
'second-function-bucket1',
|
|
'second-function-bucket2',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const serviceResourcesAwsResourcesObjectMock = {
|
|
Resources: {
|
|
firstfunctionbucket1: {
|
|
Type: 'AWS::S3::Bucket',
|
|
Properties: {
|
|
BucketName: 'new-service-first-function-bucket1-dev-us-east-1',
|
|
NotificationConfiguration: {
|
|
LambdaConfigurations: [
|
|
{
|
|
Event: 's3:ObjectCreated:*',
|
|
Function: {
|
|
'Fn::GetAtt': [
|
|
'first',
|
|
'Arn',
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
firstfunctionbucket1Permission: {
|
|
Type: 'AWS::Lambda::Permission',
|
|
Properties: {
|
|
FunctionName: {
|
|
'Fn::GetAtt': [
|
|
'first',
|
|
'Arn',
|
|
],
|
|
},
|
|
Action: 'lambda:InvokeFunction',
|
|
Principal: 's3.amazonaws.com',
|
|
},
|
|
},
|
|
|
|
firstfunctionbucket2: {
|
|
Type: 'AWS::S3::Bucket',
|
|
Properties: {
|
|
BucketName: 'new-service-first-function-bucket2-dev-us-east-1',
|
|
NotificationConfiguration: {
|
|
LambdaConfigurations: [
|
|
{
|
|
Event: 's3:ObjectCreated:*',
|
|
Function: {
|
|
'Fn::GetAtt': [
|
|
'first',
|
|
'Arn',
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
firstfunctionbucket2Permission: {
|
|
Type: 'AWS::Lambda::Permission',
|
|
Properties: {
|
|
FunctionName: {
|
|
'Fn::GetAtt': [
|
|
'first',
|
|
'Arn',
|
|
],
|
|
},
|
|
Action: 'lambda:InvokeFunction',
|
|
Principal: 's3.amazonaws.com',
|
|
},
|
|
},
|
|
|
|
secondfunctionbucket1: {
|
|
Type: 'AWS::S3::Bucket',
|
|
Properties: {
|
|
BucketName: 'new-service-second-function-bucket1-dev-us-east-1',
|
|
NotificationConfiguration: {
|
|
LambdaConfigurations: [
|
|
{
|
|
Event: 's3:ObjectCreated:*',
|
|
Function: {
|
|
'Fn::GetAtt': [
|
|
'second',
|
|
'Arn',
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
secondfunctionbucket1Permission: {
|
|
Type: 'AWS::Lambda::Permission',
|
|
Properties: {
|
|
FunctionName: {
|
|
'Fn::GetAtt': [
|
|
'second',
|
|
'Arn',
|
|
],
|
|
},
|
|
Action: 'lambda:InvokeFunction',
|
|
Principal: 's3.amazonaws.com',
|
|
},
|
|
},
|
|
|
|
secondfunctionbucket2: {
|
|
Type: 'AWS::S3::Bucket',
|
|
Properties: {
|
|
BucketName: 'new-service-second-function-bucket2-dev-us-east-1',
|
|
NotificationConfiguration: {
|
|
LambdaConfigurations: [
|
|
{
|
|
Event: 's3:ObjectCreated:*',
|
|
Function: {
|
|
'Fn::GetAtt': [
|
|
'second',
|
|
'Arn',
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
secondfunctionbucket2Permission: {
|
|
Type: 'AWS::Lambda::Permission',
|
|
Properties: {
|
|
FunctionName: {
|
|
'Fn::GetAtt': [
|
|
'second',
|
|
'Arn',
|
|
],
|
|
},
|
|
Action: 'lambda:InvokeFunction',
|
|
Principal: 's3.amazonaws.com',
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
beforeEach(() => {
|
|
serverless = new Serverless();
|
|
serverless.init();
|
|
serverless.service.resources = { aws: { Resources: {} } };
|
|
awsCompileS3Events = new AwsCompileS3Events(serverless);
|
|
awsCompileS3Events.serverless.service.service = 'new-service';
|
|
awsCompileS3Events.serverless.service.functions = functionsObjectMock;
|
|
});
|
|
|
|
describe('#compileS3Events()', () => {
|
|
const options = { stage: 'dev', region: 'us-east-1' };
|
|
|
|
it('should throw an error if the stage option is not given', () => {
|
|
expect(() => awsCompileS3Events.compileS3Events()).to.throw(Error);
|
|
});
|
|
|
|
it('should throw an error if the region option is not given', () => {
|
|
expect(() => awsCompileS3Events.compileS3Events()).to.throw(Error);
|
|
});
|
|
|
|
it('should throw an error if the aws resource is not available', () => {
|
|
awsCompileS3Events.serverless.service.resources.aws.Resources = false;
|
|
expect(() => awsCompileS3Events.compileS3Events()).to.throw(Error);
|
|
});
|
|
|
|
it('should create corresponding S3 bucket and permission resources', () => {
|
|
awsCompileS3Events.compileS3Events(options);
|
|
|
|
expect(
|
|
JSON.stringify(awsCompileS3Events.serverless.service.resources.aws.Resources)
|
|
).to.equal(
|
|
JSON.stringify(serviceResourcesAwsResourcesObjectMock.Resources)
|
|
);
|
|
});
|
|
});
|
|
});
|