forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_events_test.go
183 lines (174 loc) · 5.68 KB
/
instance_events_test.go
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
// Copyright (c) 2016-2022 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package autospotting
import (
"encoding/json"
"errors"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
)
func TestParseEventData(t *testing.T) {
expectedInstanceID := "i-123456"
expectedNotMatchedError := errors.New("this code shoudn't be reached")
instanceAction := aws.String(TerminateTerminationNotificationAction)
instanceState := "running"
tests := []struct {
name string
eventType string
cloudWatchEvent events.CloudWatchEvent
expectedInstanceID *string
expectedInstanceState *string
expectedError error
}{
{
name: "Invalid Detail in CloudWatch event",
cloudWatchEvent: events.CloudWatchEvent{
Detail: []byte(""),
},
},
{
name: "DetailType is not matched",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: "not matching",
Detail: []byte("{}"),
},
expectedError: expectedNotMatchedError,
},
{
name: "Detail is Amazon EC2 State Change Events with no instanceID and no State",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: InstanceStateChangeNotificationMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{})
return data
}(),
},
expectedInstanceID: nil,
expectedInstanceState: nil,
expectedError: expectedNotMatchedError,
},
{
name: "Detail is Amazon EC2 State Change Events with instanceID and instanceState",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: InstanceStateChangeNotificationMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{
InstanceID: aws.String(expectedInstanceID),
State: &instanceState,
})
return data
}(),
},
expectedInstanceID: &expectedInstanceID,
expectedInstanceState: aws.String("running"),
expectedError: nil,
},
{
name: "Detail is Amazon EC2 Spot Instance Interruption Events with no InstanceAction",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: SpotInstanceInterruptionWarningMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{
InstanceID: aws.String(expectedInstanceID),
})
return data
}(),
},
expectedInstanceID: nil,
expectedInstanceState: nil,
expectedError: expectedNotMatchedError,
},
{
name: "Detail is Amazon EC2 Spot Instance Interruption Events with InstanceID and InstanceAction",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: SpotInstanceInterruptionWarningMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{
InstanceID: aws.String(expectedInstanceID),
InstanceAction: instanceAction,
})
return data
}(),
},
expectedInstanceID: &expectedInstanceID,
expectedInstanceState: nil,
expectedError: nil,
},
{
name: "Detail is Amazon EC2 Instance Rebalance Recommendation Events with no InstanceID",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: InstanceRebalanceRecommendationMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{})
return data
}(),
},
expectedInstanceID: nil,
expectedInstanceState: nil,
expectedError: expectedNotMatchedError,
},
{
name: "Detail is Amazon EC2 Spot Instance Interruption Events with InstanceID",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: InstanceRebalanceRecommendationMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{
InstanceID: aws.String(expectedInstanceID),
})
return data
}(),
},
expectedInstanceID: &expectedInstanceID,
expectedInstanceState: nil,
expectedError: nil,
},
{
name: "Detail is Events Delivered Via CloudTrail",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: AWSAPICallCloudTrailMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{})
return data
}(),
},
expectedInstanceID: nil,
expectedInstanceState: nil,
expectedError: nil,
},
{
name: "Detail is Amazon CloudWatch Events Scheduled Events",
cloudWatchEvent: events.CloudWatchEvent{
DetailType: ScheduledEventMessage,
Detail: func() json.RawMessage {
data, _ := json.Marshal(instanceData{})
return data
}(),
},
expectedInstanceID: nil,
expectedInstanceState: nil,
expectedError: nil,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
eventTypeCode, instanceID, instanceState, _ := parseEventData(tc.cloudWatchEvent)
if eventTypeCode == "" && (instanceID != nil ||
instanceState != nil) {
t.Errorf("Expected nil instanceID and InstanceState, actual: %s, %s", *instanceID, *instanceState)
}
if eventTypeCode == InstanceStateChangeNotificationCode && (*tc.expectedInstanceID != *instanceID || *tc.expectedInstanceState != *instanceState) {
t.Errorf("InstanceID expected: %v\nactual: %v", tc.expectedInstanceID, instanceID)
t.Errorf("InstanceState expected: %v\nactual: %v", tc.expectedInstanceState, instanceID)
}
if (eventTypeCode == SpotInstanceInterruptionWarningCode ||
eventTypeCode == InstanceRebalanceRecommendationCode) && *tc.expectedInstanceID != *instanceID {
t.Errorf("InstanceID expected: %v\nactual: %v", tc.expectedInstanceID, instanceID)
}
if (eventTypeCode == AWSAPICallCloudTrailCode ||
eventTypeCode == ScheduledEventCode) && tc.expectedInstanceID != instanceID {
t.Errorf("InstanceID expected: %v\nactual: %v", tc.expectedInstanceID, instanceID)
}
})
}
}