@@ -307,6 +307,7 @@ describe('Unit Tests', function() {
307307
308308 mockSQSSendMessage ( { } ) ;
309309 mockSQSSendMessageBatch ( { } ) ;
310+ mockCloudWatch ( ) ;
310311 } ) ;
311312
312313 afterEach ( function ( ) {
@@ -321,6 +322,7 @@ describe('Unit Tests', function() {
321322 pawsStub . restore ( SSM , 'getParameter' ) ;
322323 pawsStub . restore ( SQS , 'sendMessage' ) ;
323324 sqsSendMessageBatchStub . restore ( ) ;
325+ pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
324326 } ) ;
325327
326328 describe ( 'Credential file caching tests' , function ( ) {
@@ -575,6 +577,10 @@ describe('Unit Tests', function() {
575577 } ) ;
576578 } ) ;
577579 describe ( 'Poll Request Tests' , function ( ) {
580+ afterEach ( function ( ) {
581+ restoreDDB ( ) ;
582+ } ) ;
583+
578584 it ( 'poll request success, single state' , function ( done ) {
579585 mockDDB ( ) ;
580586 let ctx = {
@@ -584,7 +590,6 @@ describe('Unit Tests', function() {
584590 done ( ) ;
585591 } ,
586592 succeed : function ( ) {
587- restoreDDB ( ) ;
588593 done ( ) ;
589594 }
590595 } ;
@@ -607,21 +612,24 @@ describe('Unit Tests', function() {
607612 it ( 'poll request error, single state' , function ( done ) {
608613 const fakeFun = function ( _params , callback ) { return callback ( null , { data :null } ) ; } ;
609614 const updateItemStub = sinon . stub ( ) . callsFake ( fakeFun ) ;
615+ const errorLogSpy = sinon . spy ( AlLogger , 'error' ) ;
610616 mockDDB ( null , null , updateItemStub ) ;
611617 let ctx = {
612618 invokedFunctionArn : pawsMock . FUNCTION_ARN ,
613619 fail : function ( error ) {
614620 assert . fail ( 'Invocation should succeed.' ) ;
621+ errorLogSpy . restore ( ) ;
622+ done ( ) ;
615623 } ,
616624 succeed : function ( ) {
617625 sinon . assert . calledOnce ( updateItemStub ) ;
618- restoreDDB ( ) ;
626+ // Verify that collector.done called the context succeed.
627+ assert . equal ( ctx . getRemainingTimeInMillis . callCount , 1 ) ;
628+ sinon . assert . calledWith ( AlLogger . error , 'PAWS000303 Error handling poll request: Error getting logs' ) ;
629+ errorLogSpy . restore ( ) ;
619630 done ( ) ;
620-
621631 } ,
622- getRemainingTimeInMillis : function ( ) {
623- return 5000 ;
624- }
632+ getRemainingTimeInMillis : sinon . stub ( ) . returns ( 5000 )
625633 } ;
626634
627635 const testEvent = {
@@ -637,7 +645,6 @@ describe('Unit Tests', function() {
637645 var collector = new TestCollector ( ctx , creds ) ;
638646 collector . mockGetLogsError = 'Error getting logs' ;
639647 collector . handleEvent ( testEvent ) ;
640- // Verify that collector.done called the context succeed.
641648 assert ( ctx . getRemainingTimeInMillis . callCount , 1 ) ;
642649 assert ( AlLogger . error . calledWith ( `PAWS000303 Error handling poll request: ${ JSON . stringify ( collector . mockGetLogsError ) } ` ) ) ;
643650 assert ( ctx . succeed . calledOnce ) ;
@@ -654,7 +661,6 @@ describe('Unit Tests', function() {
654661 done ( ) ;
655662 } ,
656663 succeed : function ( ) {
657- restoreDDB ( ) ;
658664 done ( ) ;
659665 }
660666 } ;
@@ -713,7 +719,8 @@ describe('Unit Tests', function() {
713719 succeed : function ( ) {
714720 sinon . assert . calledThrice ( mockSendLogmsgs ) ;
715721 sinon . assert . calledThrice ( mockSendLmcstats ) ;
716- restoreDDB ( ) ;
722+ mockSendLogmsgs . restore ( ) ;
723+ mockSendLmcstats . restore ( ) ;
717724 done ( ) ;
718725 }
719726 } ;
@@ -748,7 +755,6 @@ describe('Unit Tests', function() {
748755
749756 it ( 'Check sendCollectorStatus method call only after Five failed attempt' , function ( done ) {
750757 mockDDB ( ) ;
751- mockCloudWatch ( ) ;
752758 let ctx = {
753759 invokedFunctionArn : pawsMock . FUNCTION_ARN ,
754760 fail : function ( error ) {
@@ -760,8 +766,6 @@ describe('Unit Tests', function() {
760766 sinon . assert . calledOnce ( mockPawsGetLogs ) ;
761767 mockPawsGetLogs . restore ( ) ;
762768 mockSendCollectorStatus . restore ( ) ;
763- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
764- restoreDDB ( ) ;
765769 done ( ) ;
766770 } ,
767771 getRemainingTimeInMillis : function ( ) {
@@ -795,7 +799,6 @@ describe('Unit Tests', function() {
795799
796800 it ( 'Check sendCollectorStatus method not call if failed attempt less < 5' , function ( done ) {
797801 mockDDB ( ) ;
798- mockCloudWatch ( ) ;
799802 let ctx = {
800803 invokedFunctionArn : pawsMock . FUNCTION_ARN ,
801804 fail : function ( error ) {
@@ -807,8 +810,6 @@ describe('Unit Tests', function() {
807810 sinon . assert . calledOnce ( mockPawsGetLogs ) ;
808811 mockPawsGetLogs . restore ( ) ;
809812 mockSendCollectorStatus . restore ( ) ;
810- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
811- restoreDDB ( ) ;
812813 done ( ) ;
813814 } ,
814815 getRemainingTimeInMillis : function ( ) {
@@ -842,7 +843,6 @@ describe('Unit Tests', function() {
842843
843844 it ( 'Check if retry_count get added in state for existing collector and it will not call mockSendCollectorStatus method' , function ( done ) {
844845 mockDDB ( ) ;
845- mockCloudWatch ( ) ;
846846 let ctx = {
847847 invokedFunctionArn : pawsMock . FUNCTION_ARN ,
848848 fail : function ( error ) {
@@ -854,8 +854,6 @@ describe('Unit Tests', function() {
854854 sinon . assert . calledOnce ( mockPawsGetLogs ) ;
855855 mockPawsGetLogs . restore ( ) ;
856856 mockSendCollectorStatus . restore ( ) ;
857- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
858- restoreDDB ( ) ;
859857 done ( ) ;
860858 } ,
861859 getRemainingTimeInMillis : function ( ) {
@@ -916,16 +914,13 @@ describe('Unit Tests', function() {
916914 } ) ;
917915
918916
919- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
920-
921917 TestCollector . load ( ) . then ( function ( creds ) {
922918 var collector = new TestCollector ( ctx , creds ) ;
923919 const nextState = { state : 'new-state' } ;
924920 collector . batchLogProcess ( [ 'log1' , 'log2' ] , nextState , 900 , ( err , newState , nextinvocationTimeout ) => {
925921 sinon . assert . calledOnce ( uploadS3ObjectMock ) ;
926922 assert . equal ( newState , nextState ) ;
927923 assert . equal ( nextinvocationTimeout , 900 ) ;
928- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
929924 processLog . restore ( ) ;
930925 uploadS3ObjectMock . restore ( ) ;
931926 done ( ) ;
@@ -954,7 +949,6 @@ describe('Unit Tests', function() {
954949 function fakeFn ( messages , formatFun , hostmetaElems , callback ) {
955950 return callback ( ingestError ) ;
956951 } ) ;
957- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
958952
959953 TestCollector . load ( ) . then ( function ( creds ) {
960954 var collector = new TestCollector ( ctx , creds ) ;
@@ -963,7 +957,6 @@ describe('Unit Tests', function() {
963957 assert . equal ( err . errorCode , 'AWSC0018' ) ;
964958 assert . equal ( err . httpErrorCode , 404 ) ;
965959 processLog . restore ( ) ;
966- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
967960 done ( ) ;
968961 } ) ;
969962 } ) ;
@@ -979,12 +972,10 @@ describe('Unit Tests', function() {
979972 done ( ) ;
980973 }
981974 } ;
982- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
983975 TestCollector . load ( ) . then ( function ( creds ) {
984976 var collector = new TestCollector ( ctx , creds ) ;
985977 collector . reportApiThrottling ( function ( error ) {
986978 assert . equal ( null , error ) ;
987- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
988979 done ( ) ;
989980 } ) ;
990981 } ) ;
@@ -1037,12 +1028,10 @@ describe('Unit Tests', function() {
10371028 }
10381029 } ;
10391030
1040- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
10411031 TestCollector . load ( ) . then ( function ( creds ) {
10421032 var collector = new TestCollector ( ctx , creds ) ;
10431033 collector . reportCollectionDelay ( '2020-01-26T12:08:31.316Z' , function ( error ) {
10441034 assert . equal ( null , error ) ;
1045- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
10461035 done ( ) ;
10471036 } ) ;
10481037 } ) ;
@@ -1061,12 +1050,10 @@ describe('Unit Tests', function() {
10611050 } ;
10621051
10631052 let errorObj = { name :'OktaApiError' , status : 401 , errorCode :'E0000011' , errorSummary :'Invalid token provided' } ;
1064- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
10651053 TestCollector . load ( ) . then ( function ( creds ) {
10661054 var collector = new TestCollector ( ctx , creds ) ;
10671055 collector . reportClientError ( errorObj , function ( error ) {
10681056 assert . equal ( errorObj . errorCode , 'E0000011' ) ;
1069- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
10701057 done ( ) ;
10711058 } ) ;
10721059 } ) ;
@@ -1084,13 +1071,11 @@ describe('Unit Tests', function() {
10841071 } ;
10851072
10861073 let errorObj = { name :'StatusCodeError' , statusCode : 401 , errorCode :'E0000011' , message : '401 - undefined' } ;
1087- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
10881074 TestCollector . load ( ) . then ( function ( creds ) {
10891075 var collector = new TestCollector ( ctx , creds ) ;
10901076 collector . reportErrorToIngestApi ( errorObj , function ( error ) {
10911077 assert . equal ( errorObj . errorCode , 'E0000011' ) ;
10921078 assert . equal ( null , error ) ;
1093- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
10941079 done ( ) ;
10951080 } ) ;
10961081 } ) ;
@@ -1108,15 +1093,10 @@ describe('Unit Tests', function() {
11081093 }
11091094 } ;
11101095
1111- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => {
1112- callback ( null , { } ) ;
1113- } ) ;
1114-
11151096 TestCollector . load ( ) . then ( function ( creds ) {
11161097 var collector = new TestCollector ( ctx , creds ) ;
11171098 collector . reportDuplicateLogCount ( 6 , function ( error ) {
11181099 assert . equal ( null , error ) ;
1119- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
11201100 done ( ) ;
11211101 } ) ;
11221102 } ) ;
@@ -1133,13 +1113,11 @@ describe('Unit Tests', function() {
11331113 done ( ) ;
11341114 }
11351115 } ;
1136- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
11371116 TestCollector . load ( ) . then ( function ( creds ) {
11381117 var collector = new TestCollector ( ctx , creds ) ;
11391118 const status = 'ok' ;
11401119 collector . reportCollectorStatus ( status , function ( error ) {
11411120 assert . equal ( null , error ) ;
1142- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
11431121 done ( ) ;
11441122 } ) ;
11451123 } ) ;
@@ -1423,13 +1401,11 @@ describe('Unit Tests', function() {
14231401 done ( ) ;
14241402 }
14251403 } ;
1426- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( null ) ) ;
14271404 PawsCollector . load ( ) . then ( ( creds ) => {
14281405 var collector = new TestCollectorNoOverrides ( ctx , creds ) ;
14291406 collector . removeDuplicatedItem ( pawsMock . MOCK_LOGS , 'Id' , ( error , uniqueLogs ) => {
14301407 assert . equal ( error , null ) ;
14311408 assert . equal ( uniqueLogs . length , 0 ) ;
1432- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
14331409 done ( ) ;
14341410 } ) ;
14351411 } ) ;
@@ -1467,13 +1443,11 @@ describe('Unit Tests', function() {
14671443 done ( ) ;
14681444 }
14691445 } ;
1470- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( null ) ) ;
14711446 PawsCollector . load ( ) . then ( ( creds ) => {
14721447 var collector = new TestCollectorNoOverrides ( ctx , creds ) ;
14731448 collector . removeDuplicatedItem ( pawsMock . MOCK_LOGS , 'Id' , ( error , uniqueLogs ) => {
14741449 assert . equal ( error , null ) ;
14751450 assert . equal ( uniqueLogs . length , 1 ) ;
1476- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
14771451 done ( ) ;
14781452 } ) ;
14791453 } ) ;
@@ -1511,12 +1485,10 @@ describe('Unit Tests', function() {
15111485 done ( ) ;
15121486 }
15131487 } ;
1514- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( null ) ) ;
15151488 PawsCollector . load ( ) . then ( ( creds ) => {
15161489 var collector = new TestCollectorNoOverrides ( ctx , creds ) ;
15171490 collector . removeDuplicatedItem ( pawsMock . MOCK_LOGS , 'Id' , ( error , uniqueLogs ) => {
15181491 assert . notEqual ( error , null ) ;
1519- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
15201492 done ( ) ;
15211493 } ) ;
15221494 } ) ;
@@ -1668,8 +1640,6 @@ describe('Unit Tests', function() {
16681640 let processLog = sinon . stub ( m_al_aws . AlAwsCollector . prototype , 'processLog' ) . onFirstCall ( ) . callsFake ( fakeFunSuccess )
16691641 . onSecondCall ( ) . callsFake ( fakeFunSuccess ) . onThirdCall ( ) . callsFake ( fakeFunError ) ;
16701642
1671- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
1672-
16731643 const fakeFun = function ( _params ) {
16741644 return new Promise ( ( resolve , reject ) => {
16751645 resolve ( null ) ;
@@ -1690,7 +1660,6 @@ describe('Unit Tests', function() {
16901660 sinon . assert . calledThrice ( processLog ) ;
16911661 sinon . assert . callCount ( batchWriteItemStub , 4 ) ;
16921662 assert . equal ( ingestError , err ) ;
1693- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
16941663 processLog . restore ( ) ;
16951664 done ( ) ;
16961665 } ) ;
@@ -1726,8 +1695,6 @@ describe('Unit Tests', function() {
17261695 let processLog = sinon . stub ( m_al_aws . AlAwsCollector . prototype , 'processLog' ) . onFirstCall ( ) . callsFake ( fakeFunSuccess )
17271696 . onSecondCall ( ) . callsFake ( fakeFunSuccess ) . onThirdCall ( ) . callsFake ( fakeFunError ) ;
17281697
1729- pawsStub . mock ( CloudWatch , 'putMetricData' , ( params , callback ) => callback ( ) ) ;
1730-
17311698 const fakeFun = function ( _params ) {
17321699 return new Promise ( ( resolve , reject ) => {
17331700 resolve ( null ) ;
@@ -1748,7 +1715,6 @@ describe('Unit Tests', function() {
17481715 sinon . assert . calledThrice ( processLog ) ;
17491716 sinon . assert . callCount ( batchWriteItemStub , 0 ) ;
17501717 assert . equal ( ingestError , err ) ;
1751- pawsStub . restore ( CloudWatch , 'putMetricData' ) ;
17521718 processLog . restore ( ) ;
17531719 done ( ) ;
17541720 } ) ;
0 commit comments