@@ -426,4 +426,157 @@ describe('#compileAlarms', () => {
426
426
expect ( resources ) . to . have . property ( 'MyAlarm' ) ;
427
427
expect ( consoleLogSpy . callCount ) . equal ( 0 ) ;
428
428
} ) ;
429
+
430
+ it ( 'should generate CloudWatch Alarms with nameTemplate' , ( ) => {
431
+ const genStateMachine = name => ( {
432
+ name,
433
+ definition : {
434
+ StartAt : 'A' ,
435
+ States : {
436
+ A : {
437
+ Type : 'Pass' ,
438
+ End : true ,
439
+ } ,
440
+ } ,
441
+ } ,
442
+ alarms : {
443
+ topics : {
444
+ ok : '${self:service}-${opt:stage}-alerts-ok' ,
445
+ alarm : '${self:service}-${opt:stage}-alerts-alarm' ,
446
+ insufficientData : '${self:service}-${opt:stage}-alerts-missing' ,
447
+ } ,
448
+ nameTemplate : '$[stateMachineName]-$[cloudWatchMetricName]-alarm' ,
449
+ metrics : [
450
+ 'executionsTimedOut' ,
451
+ 'executionsFailed' ,
452
+ 'executionsAborted' ,
453
+ 'executionThrottled' ,
454
+ 'executionsSucceeded' ,
455
+ ] ,
456
+ } ,
457
+ } ) ;
458
+
459
+ serverless . service . stepFunctions = {
460
+ stateMachines : {
461
+ myStateMachine : genStateMachine ( 'stateCustomName1' ) ,
462
+ } ,
463
+ } ;
464
+
465
+ serverlessStepFunctions . compileAlarms ( ) ;
466
+ const resources = serverlessStepFunctions . serverless . service
467
+ . provider . compiledCloudFormationTemplate . Resources ;
468
+
469
+ expect ( resources . StateCustomName1ExecutionsTimedOutAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionsTimedOut-alarm' ) ;
470
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionsTimedOutAlarm ) ;
471
+ expect ( resources . StateCustomName1ExecutionsFailedAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionsFailed-alarm' ) ;
472
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionsFailedAlarm ) ;
473
+ expect ( resources . StateCustomName1ExecutionsAbortedAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionsAborted-alarm' ) ;
474
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionsAbortedAlarm ) ;
475
+ expect ( resources . StateCustomName1ExecutionThrottledAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionThrottled-alarm' ) ;
476
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionThrottledAlarm ) ;
477
+ expect ( consoleLogSpy . callCount ) . equal ( 0 ) ;
478
+ } ) ;
479
+
480
+ it ( 'should generate CloudWatch Alarms with invalid nameTemplate' , ( ) => {
481
+ const genStateMachine = name => ( {
482
+ name,
483
+ definition : {
484
+ StartAt : 'A' ,
485
+ States : {
486
+ A : {
487
+ Type : 'Pass' ,
488
+ End : true ,
489
+ } ,
490
+ } ,
491
+ } ,
492
+ alarms : {
493
+ topics : {
494
+ ok : '${self:service}-${opt:stage}-alerts-ok' ,
495
+ alarm : '${self:service}-${opt:stage}-alerts-alarm' ,
496
+ insufficientData : '${self:service}-${opt:stage}-alerts-missing' ,
497
+ } ,
498
+ nameTemplate : '$[stateMachineName]-$[invalidProp]-alarm' ,
499
+ metrics : [
500
+ 'executionsTimedOut' ,
501
+ 'executionsFailed' ,
502
+ 'executionsAborted' ,
503
+ 'executionThrottled' ,
504
+ 'executionsSucceeded' ,
505
+ ] ,
506
+ } ,
507
+ } ) ;
508
+
509
+ serverless . service . stepFunctions = {
510
+ stateMachines : {
511
+ myStateMachine : genStateMachine ( 'stateCustomName2' ) ,
512
+ } ,
513
+ } ;
514
+
515
+ serverlessStepFunctions . compileAlarms ( ) ;
516
+ const resources = serverlessStepFunctions . serverless . service
517
+ . provider . compiledCloudFormationTemplate . Resources ;
518
+
519
+ expect ( resources . StateCustomName2ExecutionsTimedOutAlarm . Properties ) . not . have . property ( 'AlarmName' ) ;
520
+ validateCloudWatchAlarm ( resources . StateCustomName2ExecutionsTimedOutAlarm ) ;
521
+ expect ( resources . StateCustomName2ExecutionsFailedAlarm . Properties ) . not . have . property ( 'AlarmName' ) ;
522
+ validateCloudWatchAlarm ( resources . StateCustomName2ExecutionsFailedAlarm ) ;
523
+ expect ( resources . StateCustomName2ExecutionsAbortedAlarm . Properties ) . not . have . property ( 'AlarmName' ) ;
524
+ validateCloudWatchAlarm ( resources . StateCustomName2ExecutionsAbortedAlarm ) ;
525
+ expect ( resources . StateCustomName2ExecutionThrottledAlarm . Properties ) . not . have . property ( 'AlarmName' ) ;
526
+ validateCloudWatchAlarm ( resources . StateCustomName2ExecutionThrottledAlarm ) ;
527
+ expect ( consoleLogSpy . callCount ) . equal ( 5 ) ;
528
+ } ) ;
529
+
530
+ it ( 'should generate CloudWatch Alarms with custom alarm name' , ( ) => {
531
+ const genStateMachine = name => ( {
532
+ name,
533
+ definition : {
534
+ StartAt : 'A' ,
535
+ States : {
536
+ A : {
537
+ Type : 'Pass' ,
538
+ End : true ,
539
+ } ,
540
+ } ,
541
+ } ,
542
+ alarms : {
543
+ topics : {
544
+ ok : '${self:service}-${opt:stage}-alerts-ok' ,
545
+ alarm : '${self:service}-${opt:stage}-alerts-alarm' ,
546
+ insufficientData : '${self:service}-${opt:stage}-alerts-missing' ,
547
+ } ,
548
+ nameTemplate : '$[stateMachineName]-$[cloudWatchMetricName]-alarm' ,
549
+ metrics : [
550
+ {
551
+ metric : 'executionsTimedOut' ,
552
+ alarmName : 'mycustom-name' ,
553
+ } ,
554
+ 'executionsFailed' ,
555
+ 'executionsAborted' ,
556
+ 'executionThrottled' ,
557
+ 'executionsSucceeded' ,
558
+ ] ,
559
+ } ,
560
+ } ) ;
561
+
562
+ serverless . service . stepFunctions = {
563
+ stateMachines : {
564
+ myStateMachine : genStateMachine ( 'stateCustomName1' ) ,
565
+ } ,
566
+ } ;
567
+
568
+ serverlessStepFunctions . compileAlarms ( ) ;
569
+ const resources = serverlessStepFunctions . serverless . service
570
+ . provider . compiledCloudFormationTemplate . Resources ;
571
+
572
+ expect ( resources . StateCustomName1ExecutionsTimedOutAlarm . Properties . AlarmName ) . to . be . equal ( 'mycustom-name' ) ;
573
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionsTimedOutAlarm ) ;
574
+ expect ( resources . StateCustomName1ExecutionsFailedAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionsFailed-alarm' ) ;
575
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionsFailedAlarm ) ;
576
+ expect ( resources . StateCustomName1ExecutionsAbortedAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionsAborted-alarm' ) ;
577
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionsAbortedAlarm ) ;
578
+ expect ( resources . StateCustomName1ExecutionThrottledAlarm . Properties . AlarmName ) . to . be . equal ( 'stateCustomName1-ExecutionThrottled-alarm' ) ;
579
+ validateCloudWatchAlarm ( resources . StateCustomName1ExecutionThrottledAlarm ) ;
580
+ expect ( consoleLogSpy . callCount ) . equal ( 0 ) ;
581
+ } ) ;
429
582
} ) ;
0 commit comments