11import { Stack } from "aws-cdk-lib" ;
22import { Template } from "aws-cdk-lib/assertions" ;
3+ import { Dimension } from "aws-cdk-lib/aws-cloudwatch" ;
34import { CfnWebACL } from "aws-cdk-lib/aws-wafv2" ;
45
5- import { WafV2Monitoring } from "../../../lib" ;
6+ import { AlarmWithAnnotation , WafV2Monitoring } from "../../../lib" ;
67import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil" ;
78import { TestMonitoringScope } from "../TestMonitoringScope" ;
89
9- test ( "snapshot test: no alarms" , ( ) => {
10+ test ( "snapshot test: alarms with REGIONAL ACL " , ( ) => {
1011 const stack = new Stack ( ) ;
1112 const acl = new CfnWebACL ( stack , "DummyAcl" , {
1213 name : "DummyAclName" ,
@@ -21,9 +22,31 @@ test("snapshot test: no alarms", () => {
2122
2223 const scope = new TestMonitoringScope ( stack , "Scope" ) ;
2324
24- const monitoring = new WafV2Monitoring ( scope , { acl, region : "us-east-1" } ) ;
25+ let numAlarmsCreated = 0 ;
26+
27+ const monitoring = new WafV2Monitoring ( scope , {
28+ acl,
29+ region : "us-east-1" ,
30+ addBlockedRequestsCountAlarm : {
31+ Warning : {
32+ maxErrorCount : 5 ,
33+ } ,
34+ } ,
35+ useCreatedAlarms : {
36+ consume ( alarms ) {
37+ expect ( getDimensions ( alarms [ 0 ] ) ) . toEqual ( [
38+ { name : "Region" , value : "us-east-1" } ,
39+ { name : "Rule" , value : "ALL" } ,
40+ { name : "WebACL" , value : "DummyAclName" } ,
41+ ] ) ;
42+
43+ numAlarmsCreated = alarms . length ;
44+ } ,
45+ } ,
46+ } ) ;
2547
2648 addMonitoringDashboardsToStack ( stack , monitoring ) ;
49+ expect ( numAlarmsCreated ) . toStrictEqual ( 1 ) ;
2750 expect ( Template . fromStack ( stack ) ) . toMatchSnapshot ( ) ;
2851} ) ;
2952
@@ -47,7 +70,44 @@ test("with REGIONAL ACL but no region prop, throws error", () => {
4770 ) ;
4871} ) ;
4972
50- test ( "snapshot test: all alarms" , ( ) => {
73+ test ( "with CLOUDFRONT ACL and region prop, does not include as dimension" , ( ) => {
74+ const stack = new Stack ( ) ;
75+ const acl = new CfnWebACL ( stack , "DummyAcl" , {
76+ name : "DummyAclName" ,
77+ defaultAction : { allow : { } } ,
78+ scope : "CLOUDFRONT" ,
79+ visibilityConfig : {
80+ sampledRequestsEnabled : true ,
81+ cloudWatchMetricsEnabled : true ,
82+ metricName : "DummyMetricName" ,
83+ } ,
84+ } ) ;
85+
86+ const scope = new TestMonitoringScope ( stack , "Scope" ) ;
87+
88+ const monitoring = new WafV2Monitoring ( scope , {
89+ acl,
90+ region : "us-west-2" ,
91+ addBlockedRequestsCountAlarm : {
92+ Warning : {
93+ maxErrorCount : 5 ,
94+ } ,
95+ } ,
96+ useCreatedAlarms : {
97+ consume ( alarms ) {
98+ expect ( getDimensions ( alarms [ 0 ] ) ) . toEqual ( [
99+ { name : "Rule" , value : "ALL" } ,
100+ { name : "WebACL" , value : "DummyAclName" } ,
101+ ] ) ;
102+ } ,
103+ } ,
104+ } ) ;
105+
106+ addMonitoringDashboardsToStack ( stack , monitoring ) ;
107+ expect ( Template . fromStack ( stack ) ) . toMatchSnapshot ( ) ;
108+ } ) ;
109+
110+ test ( "snapshot test: all alarms " , ( ) => {
51111 const stack = new Stack ( ) ;
52112 const acl = new CfnWebACL ( stack , "DummyAcl" , {
53113 name : "DummyAclName" ,
@@ -87,3 +147,7 @@ test("snapshot test: all alarms", () => {
87147 expect ( numAlarmsCreated ) . toStrictEqual ( 2 ) ;
88148 expect ( Template . fromStack ( stack ) ) . toMatchSnapshot ( ) ;
89149} ) ;
150+
151+ function getDimensions ( alarm : AlarmWithAnnotation ) : Dimension [ ] | undefined {
152+ return alarm . alarmDefinition . metric . toMetricConfig ( ) . metricStat ?. dimensions ;
153+ }
0 commit comments