@@ -32,6 +32,9 @@ import spock.lang.Shared
32
32
import spock.lang.Specification
33
33
import spock.lang.Subject
34
34
35
+ import java.util.stream.Collectors
36
+ import java.util.stream.Stream
37
+
35
38
class EcsCloudWatchAlarmCacheClientSpec extends Specification {
36
39
@Subject
37
40
EcsCloudWatchAlarmCacheClient client
@@ -246,4 +249,58 @@ def 'should return metric alarms with actions matching the service'() {
246
249
)
247
250
}
248
251
252
+
253
+ def ' should return metric alarms for a service - single cluster with Custom Alarms/Cloudwatch Dimensions' () {
254
+ given :
255
+ def serviceName = ' my-service'
256
+ def serviceName2 = ' not-matching-service'
257
+
258
+ def ecsClusterName = ' my-cluster'
259
+ def metricAlarms = Set . of(
260
+ new MetricAlarm (). withAlarmName(" alarm-name" ). withAlarmArn(" alarmArn" )
261
+ .withAlarmActions(" arn:aws:sns:us-west-1:123456789012:${ serviceName} " )
262
+ .withDimensions([new Dimension (). withName(" ClusterName" ). withValue(ecsClusterName)]),
263
+ new MetricAlarm (). withAlarmName(" alarm-name-2" ). withAlarmArn(" alarmArn2" )
264
+ .withAlarmActions(" arn:aws:sns:us-west-1:123456789012:${ serviceName} " )
265
+ .withDimensions([new Dimension (). withName(" ClusterName" ). withValue(ecsClusterName)]),
266
+ new MetricAlarm (). withAlarmName(" alarm-name" ). withAlarmArn(" alarmArn3" )
267
+ .withAlarmActions(" arn:aws:sns:us-west-1:123456789012:${ serviceName2} " )
268
+ .withDimensions([new Dimension (). withName(" ClusterName" ). withValue(ecsClusterName)])
269
+ )
270
+ def metricAlarmCustomDimension = Set . of (
271
+ new MetricAlarm (). withAlarmName(" alarm-name-2-custom" ). withAlarmArn(" alarmArn2-custom" )
272
+ .withAlarmActions(" arn:aws:sns:us-west-1:123456789012:${ serviceName} " )
273
+ .withDimensions([new Dimension (). withName(" CustomDimension" ). withValue(" customValue" )]),
274
+ )
275
+
276
+ def keys = metricAlarms. collect { alarm ->
277
+ def key = Keys . getAlarmKey(ACCOUNT , REGION , alarm. getAlarmArn(), ecsClusterName)
278
+ def attributes = agent. convertMetricAlarmToAttributes(alarm, ACCOUNT , REGION )
279
+ [key, new DefaultCacheData (key, attributes, [:])]
280
+ }
281
+ def keysCustom = metricAlarmCustomDimension. collect { alarm ->
282
+ def key = Keys . getAlarmKey(ACCOUNT , REGION , alarm. getAlarmArn(), " " )
283
+ def attributes = agent. convertMetricAlarmToAttributes(alarm, ACCOUNT , REGION )
284
+ [key, new DefaultCacheData (key, attributes, [:])]
285
+ }
286
+
287
+ cacheView. filterIdentifiers(Keys.Namespace . ALARMS . ns, Keys . getAlarmKey(ACCOUNT , REGION , " *" , ecsClusterName)) >> keys* . first()
288
+ cacheView. filterIdentifiers(Keys.Namespace . ALARMS . ns, Keys . getAlarmKey(ACCOUNT , REGION , " *" , " " )) >> keysCustom* . first()
289
+ def combinedMetricIds = Stream . of( keys* . first(), keysCustom* . first())
290
+ .filter { it != null }
291
+ .flatMap { it. stream() }
292
+ .collect(Collectors . toList())
293
+
294
+ cacheView. getAll(Keys.Namespace . ALARMS . ns, combinedMetricIds) >> keys* . last() + keysCustom* . last()
295
+
296
+ when :
297
+ def metricAlarmsReturned = client. getMetricAlarms(serviceName, ACCOUNT , REGION , ecsClusterName)
298
+
299
+ then :
300
+ metricAlarmsReturned. size() == 3
301
+ metricAlarmsReturned* . alarmName. containsAll([" alarm-name" , " alarm-name-2" , " alarm-name-2-custom" ])
302
+ metricAlarmsReturned* . alarmArn. containsAll([" alarmArn" , " alarmArn2" ," alarmArn2-custom" ])
303
+ ! metricAlarmsReturned* . alarmArn. contains([" alarmArn3" ])
304
+ }
305
+
249
306
}
0 commit comments