mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
323 lines
11 KiB
JavaScript
323 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
const expect = require('chai').expect;
|
|
const AwsProvider = require('../../../../provider/awsProvider');
|
|
const AwsCompileCloudWatchEventEvents = require('./index');
|
|
const Serverless = require('../../../../../../Serverless');
|
|
|
|
describe('awsCompileCloudWatchEventEvents', () => {
|
|
let serverless;
|
|
let awsCompileCloudWatchEventEvents;
|
|
|
|
beforeEach(() => {
|
|
serverless = new Serverless();
|
|
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
|
|
serverless.setProvider('aws', new AwsProvider(serverless));
|
|
awsCompileCloudWatchEventEvents = new AwsCompileCloudWatchEventEvents(serverless);
|
|
awsCompileCloudWatchEventEvents.serverless.service.service = 'new-service';
|
|
});
|
|
|
|
describe('#constructor()', () => {
|
|
it('should set the provider variable to an instance of AwsProvider', () =>
|
|
expect(awsCompileCloudWatchEventEvents.provider).to.be.instanceof(AwsProvider));
|
|
});
|
|
|
|
describe('#compileCloudWatchEventEvents()', () => {
|
|
it('should throw an error if cloudwatch event type is not an object', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: 42,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error);
|
|
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: '42',
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error);
|
|
});
|
|
|
|
it('should throw an error if the "event" property is not given', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: null,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error);
|
|
});
|
|
|
|
it('should create corresponding resources when cloudwatch events are given', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
},
|
|
},
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: true,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1.Type
|
|
).to.equal('AWS::Events::Rule');
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent2.Type
|
|
).to.equal('AWS::Events::Rule');
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources
|
|
.FirstLambdaPermissionEventsRuleCloudWatchEvent1.Type
|
|
).to.equal('AWS::Lambda::Permission');
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources
|
|
.FirstLambdaPermissionEventsRuleCloudWatchEvent2.Type
|
|
).to.equal('AWS::Lambda::Permission');
|
|
});
|
|
|
|
it('should respect enabled variable, defaulting to true', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
},
|
|
},
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: true,
|
|
},
|
|
},
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
|
|
.Properties.State
|
|
).to.equal('DISABLED');
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent2
|
|
.Properties.State
|
|
).to.equal('ENABLED');
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent3
|
|
.Properties.State
|
|
).to.equal('ENABLED');
|
|
});
|
|
|
|
it('should respect inputPath variable', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
inputPath: '$.stageVariables',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
|
|
.Properties.Targets[0].InputPath
|
|
).to.equal('$.stageVariables');
|
|
});
|
|
|
|
it('should respect input variable', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
input: '{"key":"value"}',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
|
|
.Properties.Targets[0].Input
|
|
).to.equal('{"key":"value"}');
|
|
});
|
|
|
|
it('should respect input variable as an object', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
input: {
|
|
key: 'value',
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
|
|
.Properties.Targets[0].Input
|
|
).to.equal('{"key":"value"}');
|
|
});
|
|
|
|
it('should throw an error when both Input and InputPath are set', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
input: {
|
|
key: 'value',
|
|
},
|
|
inputPath: '$.stageVariables',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
expect(() => awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents()).to.throw(Error);
|
|
});
|
|
|
|
it('should respect variables if multi-line variables is given', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [
|
|
{
|
|
cloudwatchEvent: {
|
|
event: {
|
|
source: ['aws.ec2'],
|
|
'detail-type': ['EC2 Instance State-change Notification \n with newline'],
|
|
detail: { state: ['pending'] },
|
|
},
|
|
enabled: false,
|
|
input: {
|
|
key: 'value\n',
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources
|
|
.FirstEventsRuleCloudWatchEvent1.Properties.EventPattern['detail-type'][0]
|
|
).to.equal('EC2 Instance State-change Notification with newline');
|
|
expect(awsCompileCloudWatchEventEvents.serverless.service
|
|
.provider.compiledCloudFormationTemplate.Resources
|
|
.FirstEventsRuleCloudWatchEvent1.Properties.Targets[0].Input
|
|
).to.equal('{"key":"value"}');
|
|
});
|
|
|
|
it('should not create corresponding resources when cloudwatch events are not given', () => {
|
|
awsCompileCloudWatchEventEvents.serverless.service.functions = {
|
|
first: {
|
|
events: [],
|
|
},
|
|
};
|
|
|
|
awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
|
|
|
|
expect(
|
|
awsCompileCloudWatchEventEvents.serverless.service.provider.compiledCloudFormationTemplate
|
|
.Resources
|
|
).to.deep.equal({});
|
|
});
|
|
});
|
|
});
|