@@ -7,11 +7,13 @@ const a = new Metric({ namespace: 'Test', metricName: 'ACount' });
77let stack1 : Stack ;
88let stack2 : Stack ;
99let stack3 : Stack ;
10+ let stack4 : Stack ;
1011describe ( '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 ( / C a n n o t c r e a t e a n A l a r m b a s e d o n a M a t h E x p r e s s i o n w h i c h s p e c i f i e s a s e a r c h A c c o u n t o r s e a r c h R e g i o n / ) ;
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