@@ -2,7 +2,7 @@ import { EC2Client, Instance } from '@aws-sdk/client-ec2';
22import { mockClient } from 'aws-sdk-client-mock' ;
33import 'aws-sdk-client-mock-jest' ;
44import { handle } from './termination-warning' ;
5- import { SpotInterruptionWarning , SpotTerminationDetail } from './types' ;
5+ import { SpotInterruptionWarning , SpotTerminationDetail , InstanceStateChangeEvent , InstanceStateChangeDetail } from './types' ;
66import { metricEvent } from './metric-event' ;
77import { deregisterRunner } from './deregister' ;
88
@@ -36,7 +36,7 @@ const config = {
3636 ghesApiUrl : '' ,
3737} ;
3838
39- const event : SpotInterruptionWarning < SpotTerminationDetail > = {
39+ const spotEvent : SpotInterruptionWarning < SpotTerminationDetail > = {
4040 version : '0' ,
4141 id : '1' ,
4242 'detail-type' : 'EC2 Spot Instance Interruption Warning' ,
@@ -51,8 +51,23 @@ const event: SpotInterruptionWarning<SpotTerminationDetail> = {
5151 } ,
5252} ;
5353
54+ const stateChangeEvent : InstanceStateChangeEvent = {
55+ version : '0' ,
56+ id : '2' ,
57+ 'detail-type' : 'EC2 Instance State-change Notification' ,
58+ source : 'aws.ec2' ,
59+ account : '123456789012' ,
60+ time : '2015-11-11T21:30:00Z' ,
61+ region : 'us-east-1' ,
62+ resources : [ 'arn:aws:ec2:us-east-1b:instance/i-abcd1111' ] ,
63+ detail : {
64+ 'instance-id' : 'i-abcd1111' ,
65+ state : 'shutting-down' ,
66+ } ,
67+ } ;
68+
5469const instance : Instance = {
55- InstanceId : event . detail [ 'instance-id' ] ,
70+ InstanceId : 'i-abcd1111' ,
5671 InstanceType : 't2.micro' ,
5772 Tags : [
5873 { Key : 'Name' , Value : 'test-instance' } ,
@@ -68,28 +83,27 @@ describe('handle termination warning', () => {
6883 vi . clearAllMocks ( ) ;
6984 } ) ;
7085
71- it ( 'should log and create a metric for spot interruption events' , async ( ) => {
86+ it ( 'should emit metric for spot interruption events' , async ( ) => {
7287 vi . mocked ( getInstances ) . mockResolvedValue ( [ instance ] ) ;
73- await handle ( event , config ) ;
88+ await handle ( spotEvent , config ) ;
7489
75- expect ( metricEvent ) . toHaveBeenCalled ( ) ;
76- expect ( metricEvent ) . toHaveBeenCalledWith ( instance , event , 'SpotInterruptionWarning' , expect . anything ( ) ) ;
90+ expect ( metricEvent ) . toHaveBeenCalledWith ( instance , spotEvent , 'SpotInterruptionWarning' , expect . anything ( ) ) ;
7791 expect ( deregisterRunner ) . toHaveBeenCalledWith ( instance , config ) ;
7892 } ) ;
7993
80- it ( 'should log details and not create a metric ' , async ( ) => {
94+ it ( 'should not emit metric when createSpotWarningMetric is false ' , async ( ) => {
8195 vi . mocked ( getInstances ) . mockResolvedValue ( [ instance ] ) ;
8296
8397 const noMetricConfig = { ...config , createSpotWarningMetric : false } ;
84- await handle ( event , noMetricConfig ) ;
85- expect ( metricEvent ) . toHaveBeenCalledWith ( instance , event , undefined , expect . anything ( ) ) ;
98+ await handle ( spotEvent , noMetricConfig ) ;
99+ expect ( metricEvent ) . toHaveBeenCalledWith ( instance , spotEvent , undefined , expect . anything ( ) ) ;
86100 expect ( deregisterRunner ) . toHaveBeenCalledWith ( instance , noMetricConfig ) ;
87101 } ) ;
88102
89- it ( 'should not create a metric if filter not matched. ' , async ( ) => {
103+ it ( 'should not emit metric or deregister if filter not matched' , async ( ) => {
90104 vi . mocked ( getInstances ) . mockResolvedValue ( [ instance ] ) ;
91105
92- await handle ( event , {
106+ await handle ( spotEvent , {
93107 createSpotWarningMetric : true ,
94108 createSpotTerminationMetric : false ,
95109 tagFilters : { 'ghr:environment' : '_NO_MATCH_' } ,
@@ -105,12 +119,7 @@ describe('handle termination warning', () => {
105119 it ( 'should not emit metric for instance state-change events but still deregister' , async ( ) => {
106120 vi . mocked ( getInstances ) . mockResolvedValue ( [ instance ] ) ;
107121
108- const stateChangeEvent = {
109- ...event ,
110- 'detail-type' : 'EC2 Instance State-change Notification' as const ,
111- } ;
112-
113- await handle ( stateChangeEvent as unknown as SpotInterruptionWarning < SpotTerminationDetail > , config ) ;
122+ await handle ( stateChangeEvent , config ) ;
114123
115124 expect ( metricEvent ) . toHaveBeenCalledWith ( instance , stateChangeEvent , undefined , expect . anything ( ) ) ;
116125 expect ( deregisterRunner ) . toHaveBeenCalledWith ( instance , config ) ;
0 commit comments