Skip to content

Commit 696635f

Browse files
Merge pull request #171 from j0k3r/fix/avoid-log-no-alarms
Avoid alarms logs when no alarms are defined
2 parents f4ee72c + 8649a13 commit 696635f

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

lib/deploy/stepFunctions/compileAlarms.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ function getCloudWatchAlarms(
7474
}
7575

7676
function validateConfig(serverless, stateMachineName, alarmsObj) {
77-
if (!_.isObject(alarmsObj) ||
78-
!_.isObject(alarmsObj.topics) ||
77+
// no alarms defined at all
78+
if (!_.isObject(alarmsObj)) {
79+
return false;
80+
}
81+
82+
if (!_.isObject(alarmsObj.topics) ||
7983
!_.isArray(alarmsObj.metrics) ||
8084
!_.every(alarmsObj.metrics, _.isString)) {
8185
serverless.cli.consoleLog(

lib/deploy/stepFunctions/compileAlarms.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
const _ = require('lodash');
44
const expect = require('chai').expect;
5+
const sinon = require('sinon');
56
const Serverless = require('serverless/lib/Serverless');
67
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
78
const ServerlessStepFunctions = require('./../../index');
89

910
describe('#compileAlarms', () => {
11+
let consoleLogSpy;
1012
let serverless;
1113
let serverlessStepFunctions;
1214

1315
beforeEach(() => {
16+
consoleLogSpy = sinon.spy();
1417
serverless = new Serverless();
1518
serverless.servicePath = true;
1619
serverless.service.service = 'step-functions';
1720
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
1821
serverless.setProvider('aws', new AwsProvider(serverless));
19-
serverless.cli = { consoleLog: console.log };
22+
serverless.cli = { consoleLog: consoleLogSpy };
2023
const options = {
2124
stage: 'dev',
2225
region: 'ap-northeast-1',
@@ -87,6 +90,37 @@ describe('#compileAlarms', () => {
8790
validateCloudWatchAlarm(resources.StateMachineBeta2ExecutionsAbortedAlarm);
8891
expect(resources).to.have.property('StateMachineBeta2ExecutionThrottledAlarm');
8992
validateCloudWatchAlarm(resources.StateMachineBeta2ExecutionThrottledAlarm);
93+
94+
expect(consoleLogSpy.callCount).equal(0);
95+
});
96+
97+
it('should not generate logs when no CloudWatch Alarms are defiened', () => {
98+
const genStateMachine = (name) => ({
99+
name,
100+
definition: {
101+
StartAt: 'A',
102+
States: {
103+
A: {
104+
Type: 'Pass',
105+
End: true,
106+
},
107+
},
108+
},
109+
});
110+
111+
serverless.service.stepFunctions = {
112+
stateMachines: {
113+
myStateMachine1: genStateMachine('stateMachineBeta1'),
114+
myStateMachine2: genStateMachine('stateMachineBeta2'),
115+
},
116+
};
117+
118+
serverlessStepFunctions.compileAlarms();
119+
const resources = serverlessStepFunctions.serverless.service
120+
.provider.compiledCloudFormationTemplate.Resources;
121+
expect(_.keys(resources)).to.have.lengthOf(0);
122+
123+
expect(consoleLogSpy.callCount).equal(0);
90124
});
91125

92126
it('should not generate CloudWatch Alarms when alarms.topics is missing', () => {
@@ -119,6 +153,8 @@ describe('#compileAlarms', () => {
119153
const resources = serverlessStepFunctions.serverless.service
120154
.provider.compiledCloudFormationTemplate.Resources;
121155
expect(_.keys(resources)).to.have.lengthOf(0);
156+
157+
expect(consoleLogSpy.callCount).equal(2);
122158
});
123159

124160
it('should not generate CloudWatch Alarms when alarms.topics is empty', () => {
@@ -152,6 +188,8 @@ describe('#compileAlarms', () => {
152188
const resources = serverlessStepFunctions.serverless.service
153189
.provider.compiledCloudFormationTemplate.Resources;
154190
expect(_.keys(resources)).to.have.lengthOf(0);
191+
192+
expect(consoleLogSpy.callCount).equal(2);
155193
});
156194

157195
it('should not generate CloudWatch Alarms when alarms.metrics is missing', () => {
@@ -184,6 +222,8 @@ describe('#compileAlarms', () => {
184222
const resources = serverlessStepFunctions.serverless.service
185223
.provider.compiledCloudFormationTemplate.Resources;
186224
expect(_.keys(resources)).to.have.lengthOf(0);
225+
226+
expect(consoleLogSpy.callCount).equal(2);
187227
});
188228

189229
it('should not generate CloudWatch Alarms for unsupported metrics', () => {
@@ -225,5 +265,7 @@ describe('#compileAlarms', () => {
225265

226266
// but invalid metric names are skipped
227267
expect(_.keys(resources)).to.have.lengthOf(2);
268+
269+
expect(consoleLogSpy.callCount).equal(2);
228270
});
229271
});

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const _ = require('lodash');
44
const expect = require('chai').expect;
5+
const sinon = require('sinon');
56
const Serverless = require('serverless/lib/Serverless');
67
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
78
const ServerlessStepFunctions = require('./../../index');
@@ -16,7 +17,7 @@ describe('#compileIamRole', () => {
1617
serverless.service.service = 'step-functions';
1718
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
1819
serverless.setProvider('aws', new AwsProvider(serverless));
19-
serverless.cli = { consoleLog: console.log };
20+
serverless.cli = { consoleLog: sinon.spy() };
2021
const options = {
2122
stage: 'dev',
2223
region: 'ap-northeast-1',

0 commit comments

Comments
 (0)