Skip to content

Commit a96db4a

Browse files
committed
add test suite for new method
1 parent 83fb9f9 commit a96db4a

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ export class Alarm extends AlarmBase {
384384
// if this is a region-agnostic stack, we can't assume anything about stat.account
385385
// and therefore we assume its a cross-account call
386386
if (Token.isUnresolved(stackAccount)) {
387-
console.log(stackAccount);
388387
return true;
389388
}
390389

packages/@aws-cdk/aws-cloudwatch/test/cross-environment.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ const a = new Metric({ namespace: 'Test', metricName: 'ACount' });
77
let stack1: Stack;
88
let stack2: Stack;
99
let stack3: Stack;
10+
let stack4: Stack;
1011
describe('cross environment', () => {
1112
beforeEach(() => {
1213
stack1 = new Stack(undefined, undefined, { env: { region: 'pluto', account: '1234' } });
1314
stack2 = new Stack(undefined, undefined, { env: { region: 'mars', account: '5678' } });
1415
stack3 = new Stack(undefined, undefined, { env: { region: 'pluto', account: '0000' } });
16+
stack4 = new Stack(undefined, undefined);
1517
});
1618

1719
describe('in graphs', () => {
@@ -403,6 +405,107 @@ describe('cross environment', () => {
403405
});
404406
}).toThrow(/Cannot create an Alarm based on a MathExpression which specifies a searchAccount or searchRegion/);
405407
});
408+
409+
describe('accountId requirements', () => {
410+
test('metric account is not defined', () => {
411+
const metric = new Metric({
412+
namespace: 'Test',
413+
metricName: 'ACount',
414+
});
415+
416+
new Alarm(stack4, 'Alarm', {
417+
threshold: 1,
418+
evaluationPeriods: 1,
419+
metric,
420+
});
421+
422+
// Alarm will be defined as legacy alarm.
423+
Template.fromStack(stack4).hasResourceProperties('AWS::CloudWatch::Alarm', {
424+
Threshold: 1,
425+
EvaluationPeriods: 1,
426+
MetricName: 'ACount',
427+
Namespace: 'Test',
428+
});
429+
});
430+
431+
test('metric account is defined and stack account is token', () => {
432+
const metric = new Metric({
433+
namespace: 'Test',
434+
metricName: 'ACount',
435+
account: '123456789',
436+
});
437+
438+
new Alarm(stack4, 'Alarm', {
439+
threshold: 1,
440+
evaluationPeriods: 1,
441+
metric,
442+
});
443+
444+
// Alarm will be defined as modern alarm.
445+
Template.fromStack(stack4).hasResourceProperties('AWS::CloudWatch::Alarm', {
446+
Metrics: Match.anyValue(),
447+
});
448+
});
449+
450+
test('metric account is attached to stack account', () => {
451+
const metric = new Metric({
452+
namespace: 'Test',
453+
metricName: 'ACount',
454+
});
455+
456+
new Alarm(stack4, 'Alarm', {
457+
threshold: 1,
458+
evaluationPeriods: 1,
459+
metric: metric.attachTo(stack4),
460+
});
461+
462+
// Alarm will be defined as legacy alarm.
463+
Template.fromStack(stack4).hasResourceProperties('AWS::CloudWatch::Alarm', {
464+
Threshold: 1,
465+
EvaluationPeriods: 1,
466+
MetricName: 'ACount',
467+
Namespace: 'Test',
468+
});
469+
});
470+
471+
test('metric account === stack account, but both are tokens', () => {
472+
const metric = new Metric({
473+
namespace: 'Test',
474+
metricName: 'ACount',
475+
account: stack4.account,
476+
});
477+
478+
new Alarm(stack4, 'Alarm', {
479+
threshold: 1,
480+
evaluationPeriods: 1,
481+
metric,
482+
});
483+
484+
// Alarm will be defined as modern alarm, since there is no way of knowing that the two tokens are equal.
485+
Template.fromStack(stack4).hasResourceProperties('AWS::CloudWatch::Alarm', {
486+
Metrics: Match.anyValue(),
487+
});
488+
});
489+
490+
test('metric account !== stack account', () => {
491+
const metric = new Metric({
492+
namespace: 'Test',
493+
metricName: 'ACount',
494+
account: '123456789',
495+
});
496+
497+
new Alarm(stack1, 'Alarm', {
498+
threshold: 1,
499+
evaluationPeriods: 1,
500+
metric,
501+
});
502+
503+
// Alarm will be defined as modern alarm.
504+
Template.fromStack(stack1).hasResourceProperties('AWS::CloudWatch::Alarm', {
505+
Metrics: Match.anyValue(),
506+
});
507+
});
508+
});
406509
});
407510
});
408511

0 commit comments

Comments
 (0)