/
alarm.test.ts
322 lines (274 loc) · 8.58 KB
/
alarm.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { Match, Template, Annotations } from '@aws-cdk/assertions';
import { Duration, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Alarm, IAlarm, IAlarmAction, Metric, MathExpression, IMetric } from '../lib';
const testMetric = new Metric({
namespace: 'CDK/Test',
metricName: 'Metric',
});
describe('Alarm', () => {
test('alarm does not accept a math expression with more than 10 metrics', () => {
const stack = new Stack();
const usingMetrics: Record<string, IMetric> = {};
for (const i of [...Array(15).keys()]) {
const metricName = `metric${i}`;
usingMetrics[metricName] = new Metric({
namespace: 'CDK/Test',
metricName: metricName,
});
}
const math = new MathExpression({
expression: 'a',
usingMetrics,
});
expect(() => {
new Alarm(stack, 'Alarm', {
metric: math,
threshold: 1000,
evaluationPeriods: 3,
});
}).toThrow(/Alarms on math expressions cannot contain more than 10 individual metrics/);
});
test('non ec2 instance related alarm does not accept EC2 action', () => {
const stack = new Stack();
const alarm = new Alarm(stack, 'Alarm', {
metric: testMetric,
threshold: 1000,
evaluationPeriods: 2,
});
expect(() => {
alarm.addAlarmAction(new Ec2TestAlarmAction('arn:aws:automate:us-east-1:ec2:reboot'));
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
});
test('non ec2 instance related alarm does not accept EC2 action in other partitions', () => {
const stack = new Stack();
const alarm = new Alarm(stack, 'Alarm', {
metric: testMetric,
threshold: 1000,
evaluationPeriods: 2,
});
expect(() => {
alarm.addAlarmAction(new Ec2TestAlarmAction('arn:aws-us-gov:automate:us-east-1:ec2:reboot'));
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
expect(() => {
alarm.addAlarmAction(new Ec2TestAlarmAction('arn:aws-cn:automate:us-east-1:ec2:reboot'));
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
});
test('can make simple alarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
new Alarm(stack, 'Alarm', {
metric: testMetric,
threshold: 1000,
evaluationPeriods: 3,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 300,
Statistic: 'Average',
Threshold: 1000,
});
});
test('override metric period in Alarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
new Alarm(stack, 'Alarm', {
metric: testMetric.with({ period: Duration.minutes(10) }),
threshold: 1000,
evaluationPeriods: 3,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 600,
Statistic: 'Average',
Threshold: 1000,
});
});
test('override statistic Alarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
new Alarm(stack, 'Alarm', {
metric: testMetric.with({ statistic: 'max' }),
threshold: 1000,
evaluationPeriods: 3,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 300,
Statistic: 'Maximum',
ExtendedStatistic: Match.absent(),
Threshold: 1000,
});
});
test('can use percentile in Alarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
new Alarm(stack, 'Alarm', {
metric: testMetric.with({ statistic: 'P99' }),
threshold: 1000,
evaluationPeriods: 3,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 300,
Statistic: Match.absent(),
ExtendedStatistic: 'p99',
Threshold: 1000,
});
});
test('can set DatapointsToAlarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
new Alarm(stack, 'Alarm', {
metric: testMetric,
threshold: 1000,
evaluationPeriods: 3,
datapointsToAlarm: 2,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
DatapointsToAlarm: 2,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 300,
Statistic: 'Average',
Threshold: 1000,
});
});
test('can add actions to alarms', () => {
// GIVEN
const stack = new Stack();
// WHEN
const alarm = new Alarm(stack, 'Alarm', {
metric: testMetric,
threshold: 1000,
evaluationPeriods: 2,
});
alarm.addAlarmAction(new TestAlarmAction('A'));
alarm.addInsufficientDataAction(new TestAlarmAction('B'));
alarm.addOkAction(new TestAlarmAction('C'));
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
AlarmActions: ['A'],
InsufficientDataActions: ['B'],
OKActions: ['C'],
});
});
test('can make alarm directly from metric', () => {
// GIVEN
const stack = new Stack();
// WHEN
testMetric.with({
statistic: 'min',
period: Duration.seconds(10),
}).createAlarm(stack, 'Alarm', {
threshold: 1000,
evaluationPeriods: 2,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 2,
MetricName: 'Metric',
Namespace: 'CDK/Test',
Period: 10,
Statistic: 'Minimum',
Threshold: 1000,
});
});
test('can use percentile string to make alarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
testMetric.with({
statistic: 'p99.9',
}).createAlarm(stack, 'Alarm', {
threshold: 1000,
evaluationPeriods: 2,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ExtendedStatistic: 'p99.9',
});
});
test('can use a generic string for extended statistic to make alarm', () => {
// GIVEN
const stack = new Stack();
// WHEN
testMetric.with({
statistic: 'tm99.9999999999',
}).createAlarm(stack, 'Alarm', {
threshold: 1000,
evaluationPeriods: 2,
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Statistic: Match.absent(),
ExtendedStatistic: 'tm99.9999999999',
});
});
test('metric warnings are added to Alarm', () => {
const stack = new Stack(undefined, 'MyStack');
const m = new MathExpression({ expression: 'oops' });
// WHEN
new Alarm(stack, 'MyAlarm', {
metric: m,
evaluationPeriods: 1,
threshold: 1,
});
// THEN
const template = Annotations.fromStack(stack);
template.hasWarning('/MyStack/MyAlarm', Match.stringLikeRegexp("Math expression 'oops' references unknown identifiers"));
});
test('metric warnings are not added to MetricsInsights Alarm', () => {
// GIVEN
const stack = new Stack();
const m = new MathExpression({ expression: 'Select MAX(cpu_usage_user) FROM SCHEMA(CWAgent, cpu,host)' });
// WHEN
new Alarm(stack, 'MyAlarm', {
metric: m,
evaluationPeriods: 1,
threshold: 1,
});
// THEN
const template = Annotations.fromStack(stack);
template.hasNoWarning('/MyStack/MyAlarm', Match.stringLikeRegexp("Math expression 'Select MAX(cpu_usage_user) FROM SCHEMA(CWAgent, cpu,host)' references unknown identifiers"));
});
});
class TestAlarmAction implements IAlarmAction {
constructor(private readonly arn: string) {
}
public bind(_scope: Construct, _alarm: IAlarm) {
return { alarmActionArn: this.arn };
}
}
class Ec2TestAlarmAction implements IAlarmAction {
constructor(private readonly arn: string) {
}
public bind(_scope: Construct, _alarm: IAlarm) {
return { alarmActionArn: this.arn };
}
}