-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
242 lines (199 loc) · 5.9 KB
/
main_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"log"
"os"
"reflect"
"testing"
"github.com/paybyphone/asg53/teststubs"
)
// testMessageJSON is a test SNS message in JSON form.
//
/// Metadata is mocked separately.
const testMessageJSON = `
{
"EC2InstanceId": "i-123456789",
"AutoScalingGroupName": "ASGName",
"LifecycleHookName": "Lifecycle",
"LifecycleActionToken": "Token"
}
`
// testMetadataJSON is a test SNS message in JSON form. The outer event is not
// currently mocked.
const testMetadataJSON = `
{
"HostedZoneID": "ABCDEF0123456789",
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "{{.InstanceID}}.example.com.",
"TTL": 3600,
"Type": "A",
"ResourceRecords": [
{
"Value": "{{.InstancePublicIPAddress}}"
}
]
}
},
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "www.example.com.",
"TTL": 3600,
"Type": "CNAME",
"ResourceRecords": [
{
"Value": "{{.InstanceID}}.example.com."
}
]
}
}
]
}
`
// testAwsClient returns a mock *awsClient with the services stubbed from the
// teststubs package.
func testAwsClient() *awsClient {
client := awsClient{}
client.EC2 = teststubs.CreateTestEC2InstanceMock()
client.AutoScaling = teststubs.CreateTestAutoScalingMock()
client.Route53 = teststubs.CreateTestRoute53Mock()
return &client
}
func TestFetchEC2InstanceData(t *testing.T) {
instanceID := "i-123456789"
client := testAwsClient()
instance, err := client.FetchEC2InstanceData(instanceID)
if err != nil {
t.Fatalf("Bad: %v", err)
}
if *instance.InstanceId != instanceID {
t.Fatalf("Expected InstanceId to be %s, got %s", instanceID, *instance.InstanceId)
}
}
func TestFetchEC2InstanceData_shouldError(t *testing.T) {
instanceID := "bad"
client := testAwsClient()
_, err := client.FetchEC2InstanceData(instanceID)
if err == nil {
t.Fatal("Expected error, got none")
}
}
func TestSendRoute53ChangeBatch(t *testing.T) {
metadata, err := parseSNSMetadata([]byte(testMetadataJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
batch := metadata.Changes
zoneID := metadata.HostedZoneID
client := testAwsClient()
if err := client.SendRoute53ChangeBatch(zoneID, batch); err != nil {
t.Fatalf("Expected no error, got #%v", err)
}
}
func TestSendRoute53ChangeBatch_shouldError(t *testing.T) {
metadata, err := parseSNSMetadata([]byte(testMetadataJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
batch := metadata.Changes
zoneID := "bad"
client := testAwsClient()
if err := client.SendRoute53ChangeBatch(zoneID, batch); err == nil {
t.Fatal("Expected error, got none")
}
}
func TestWaitForRoute53Sync(t *testing.T) {
id := "foobar"
client := testAwsClient()
if err := client.WaitForRoute53Sync(id); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
func TestWaitForRoute53Sync_shouldError(t *testing.T) {
id := "bad"
client := testAwsClient()
if err := client.WaitForRoute53Sync(id); err == nil {
t.Fatal("Expected error, got none")
}
}
func TestCompleteAutoscalingAction(t *testing.T) {
message, err := parseInnerSNSMessage([]byte(testMessageJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
result := "CONTINUE"
client := testAwsClient()
if err := client.CompleteAutoscalingAction(message, result); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}
func TestCompleteAutoscalingAction_shouldError(t *testing.T) {
message, err := parseInnerSNSMessage([]byte(testMessageJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
result := "bad"
client := testAwsClient()
if err := client.CompleteAutoscalingAction(message, result); err == nil {
t.Fatal("Expected error, got none")
}
}
func TestPopulate(t *testing.T) {
instanceID := "i-123456789"
client := testAwsClient()
metadata, err := parseSNSMetadata([]byte(testMetadataJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
expected := &instanceData{
client: client,
HostedZoneID: metadata.HostedZoneID,
batch: metadata.Changes,
InstanceID: "i-123456789",
InstancePrivateIPAddress: "10.0.0.1",
InstancePublicIPAddress: "54.0.0.1",
}
actual, err := populate(client, instanceID, metadata.HostedZoneID, metadata.Changes)
if err != nil {
t.Fatalf("Bad: %v", err)
}
if reflect.DeepEqual(expected, actual) == false {
t.Fatalf("Expected %#v, got %#v", expected, actual)
}
}
func TestWriteTemplateFields(t *testing.T) {
message, err := parseInnerSNSMessage([]byte(testMessageJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
metadata, err := parseSNSMetadata([]byte(testMetadataJSON))
if err != nil {
panic(fmt.Errorf("Bad JSON in test: %v", err))
}
batch := metadata.Changes
instanceID := message.EC2InstanceID
client := testAwsClient()
data, err := populate(client, instanceID, metadata.HostedZoneID, metadata.Changes)
if err != nil {
t.Fatalf("Bad: %v", err)
}
if err := data.WriteTemplateFields(); err != nil {
t.Fatalf("Bad: %v", err)
}
if *batch[0].ResourceRecordSet.Name != "i-123456789.example.com." {
t.Fatalf("Expected batch[0].ResourceRecordSet.Name to be i-123456789.example.com., got %s", *batch[0].ResourceRecordSet.Name)
}
if *batch[0].ResourceRecordSet.ResourceRecords[0].Value != "54.0.0.1" {
t.Fatalf("Expected batch[0].ResourceRecordSet.ResourceRecords[0].Value to be 54.0.0.1, got %s", *batch[0].ResourceRecordSet.ResourceRecords[0].Value)
}
if *batch[1].ResourceRecordSet.ResourceRecords[0].Value != "i-123456789.example.com." {
t.Fatalf("Expected batch[1].ResourceRecordSet.ResourceRecords[0].Value to be i-123456789.example.com., got %s", *batch[1].ResourceRecordSet.ResourceRecords[0].Value)
}
}
func TestMain(m *testing.M) {
log.SetOutput(os.Stderr)
os.Exit(m.Run())
}