77 Shading ,
88 TreatMissingData ,
99} from "aws-cdk-lib/aws-cloudwatch" ;
10+ import { Topic } from "aws-cdk-lib/aws-sns" ;
1011import { Construct } from "constructs" ;
1112
1213import {
@@ -15,11 +16,17 @@ import {
1516 AlarmFactoryDefaults ,
1617 CompositeAlarmOperator ,
1718 MetricFactoryDefaults ,
19+ multipleActions ,
1820 noopAction ,
21+ SnsAlarmActionStrategy ,
1922} from "../../../lib" ;
2023
2124const stack = new Stack ( ) ;
2225const construct = new Construct ( stack , "SampleConstruct" ) ;
26+
27+ const snsAction = new SnsAlarmActionStrategy ( {
28+ onAlarmTopic : new Topic ( stack , "Dummy2" ) ,
29+ } ) ;
2330const globalMetricDefaults : MetricFactoryDefaults = {
2431 namespace : "DummyNamespace" ,
2532} ;
@@ -39,6 +46,9 @@ const globalAlarmDefaultsWithDisambiguator: AlarmFactoryDefaults = {
3946 datapointsToAlarm : 6 ,
4047 // we do not care about alarm actions in this test
4148 action : noopAction ( ) ,
49+ disambiguatorAction : {
50+ DisambiguatedAction : snsAction ,
51+ } ,
4252} ;
4353const factory = new AlarmFactory ( construct , {
4454 globalMetricDefaults,
@@ -116,6 +126,7 @@ test("addAlarm: verify actions enabled", () => {
116126 ...props ,
117127 alarmNameSuffix : "DisabledByDefault" ,
118128 } ) ;
129+
119130 const template = Template . fromStack ( stack ) ;
120131 template . hasResourceProperties ( "AWS::CloudWatch::Alarm" , {
121132 ActionsEnabled : true ,
@@ -154,6 +165,7 @@ test("addAlarm: description can be overridden", () => {
154165 alarmNameSuffix : "Suffix6B" ,
155166 alarmDescriptionOverride : "New Description" ,
156167 } ) ;
168+
157169 expect ( alarm1 . alarmDescription ) . toEqual ( "Description" ) ;
158170 expect ( alarm2 . alarmDescription ) . toEqual ( "New Description" ) ;
159171} ) ;
@@ -179,6 +191,7 @@ test("addAlarm: evaluateLowSampleCountPercentile can be overridden", () => {
179191 evaluateLowSampleCountPercentile : true ,
180192 alarmNameSuffix : "TrueValue" ,
181193 } ) ;
194+
182195 expect ( Template . fromStack ( stack ) ) . toMatchSnapshot ( ) ;
183196} ) ;
184197
@@ -194,6 +207,7 @@ test("addAlarm: period override is propagated to alarm metric", () => {
194207 alarmNameSuffix : "TwoHoursPeriod" ,
195208 period : Duration . hours ( 2 ) ,
196209 } ) ;
210+
197211 const alarm1hConfig = ( alarm1h . alarm as Alarm ) . metric . toMetricConfig ( ) ;
198212 expect ( alarm1hConfig . metricStat ?. period ) . toStrictEqual ( Duration . hours ( 1 ) ) ;
199213 const alarm2hConfig = ( alarm2h . alarm as Alarm ) . metric . toMetricConfig ( ) ;
@@ -225,6 +239,7 @@ test("addAlarm: fill is propagated to alarm annotation", () => {
225239 comparisonOperator : ComparisonOperator . LESS_THAN_THRESHOLD ,
226240 fillAlarmRange : true ,
227241 } ) ;
242+
228243 expect ( alarmNone . annotation . fill ) . toBeUndefined ( ) ;
229244 expect ( alarmAbove . annotation . fill ) . toStrictEqual ( Shading . ABOVE ) ;
230245 expect ( alarmBelow . annotation . fill ) . toStrictEqual ( Shading . BELOW ) ;
@@ -245,6 +260,7 @@ test("addAlarm: annotation overrides are applied", () => {
245260 overrideAnnotationVisibility : false ,
246261 overrideAnnotationColor : "NewColor" ,
247262 } ) ;
263+
248264 expect ( alarm . annotation ) . toStrictEqual ( {
249265 color : "NewColor" ,
250266 label : "NewLabel" ,
@@ -266,8 +282,8 @@ test("addAlarm: check created alarms when minMetricSamplesToAlarm is used", () =
266282 comparisonOperator : ComparisonOperator . LESS_THAN_THRESHOLD ,
267283 minMetricSamplesToAlarm : 42 ,
268284 } ) ;
269- const template = Template . fromStack ( stack ) ;
270285
286+ const template = Template . fromStack ( stack ) ;
271287 template . hasResourceProperties ( "AWS::CloudWatch::Alarm" , {
272288 AlarmName : "DummyServiceAlarms-prefix-none" ,
273289 MetricName : "DummyMetric1" ,
@@ -285,6 +301,7 @@ test("addAlarm: check created alarms when minMetricSamplesToAlarm is used", () =
285301 Threshold : 42 ,
286302 TreatMissingData : "breaching" ,
287303 } ) ;
304+
288305 const alarmRuleCapture = new Capture ( ) ;
289306 template . hasResourceProperties ( "AWS::CloudWatch::CompositeAlarm" , {
290307 AlarmName : "DummyServiceAlarms-prefix-none-WithSamples" ,
@@ -345,5 +362,50 @@ test("addCompositeAlarm: snapshot for operator", () => {
345362 alarmNameSuffix : "CompositeOr" ,
346363 compositeOperator : CompositeAlarmOperator . OR ,
347364 } ) ;
365+
348366 expect ( Template . fromStack ( stack ) ) . toMatchSnapshot ( ) ;
349367} ) ;
368+
369+ test ( "addAlarm: original actionOverride with a different action gets preserved" , ( ) => {
370+ const originalActionOverride = new SnsAlarmActionStrategy ( {
371+ onAlarmTopic : new Topic ( stack , "Dummy1" ) ,
372+ } ) ;
373+
374+ const alarm = factory . addAlarm ( metric , {
375+ ...props ,
376+ alarmNameSuffix : "OriginalActionOverridePreserved" ,
377+ actionOverride : originalActionOverride ,
378+ } ) ;
379+
380+ expect ( alarm . action ) . toStrictEqual ( originalActionOverride ) ;
381+ } ) ;
382+
383+ test ( "addAlarm: original actionOverride with multipleActions gets preserved" , ( ) => {
384+ const action1 = snsAction ;
385+ const action2 = noopAction ( ) ;
386+
387+ const originalActionOverride = multipleActions ( action1 , action2 ) ;
388+
389+ const alarm = factory . addAlarm ( metric , {
390+ ...props ,
391+ alarmNameSuffix : "OriginalActionOverridePreservedInMultipleActions" ,
392+ actionOverride : originalActionOverride ,
393+ } ) ;
394+
395+ expect ( alarm . action ) . toStrictEqual ( multipleActions ( action1 , action2 ) ) ;
396+ } ) ;
397+
398+ test ( "addAlarm: disambigatorAction takes precedence over default action" , ( ) => {
399+ const stack = new Stack ( ) ;
400+ const factory = new AlarmFactory ( stack , {
401+ globalMetricDefaults,
402+ globalAlarmDefaults : globalAlarmDefaultsWithDisambiguator ,
403+ localAlarmNamePrefix : "prefix" ,
404+ } ) ;
405+ const alarm = factory . addAlarm ( metric , {
406+ ...props ,
407+ disambiguator : "DisambiguatedAction" ,
408+ } ) ;
409+
410+ expect ( alarm . action ) . toStrictEqual ( snsAction ) ;
411+ } ) ;
0 commit comments