-
Notifications
You must be signed in to change notification settings - Fork 2
/
elnginx_test.go
270 lines (225 loc) · 8.32 KB
/
elnginx_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"bytes"
"fmt"
"github.com/rochacon/elastic-nginx/config"
"github.com/tsuru/commandmocker"
"gopkg.in/amz.v1/aws"
"gopkg.in/amz.v1/ec2"
"gopkg.in/amz.v1/ec2/ec2test"
"gopkg.in/check.v1"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
func Test(t *testing.T) {
check.TestingT(t)
}
type S struct {
ec2 *ec2.EC2
logOutput *bytes.Buffer
instance_ids []string
testPath string
testServer *ec2test.Server
}
var _ = check.Suite(&S{})
func (s *S) SetUpSuite(c *check.C) {
var err error
s.testServer, err = ec2test.NewServer()
if err != nil {
panic(err)
}
s.instance_ids = s.testServer.NewInstances(1, "t1.micro", "ami-00000", ec2test.Running, nil)
s.testPath = c.MkDir()
AWSRegion = "test"
Region = aws.USEast
Region.EC2Endpoint = s.testServer.URL()
Config = &config.Config{
TopicArn: "arn:test",
AutoSubscribe: false,
Upstreams: []config.Upstream{
config.Upstream{
AutoScalingGroupARN: "arn:asg-test",
ContainerFolder: path.Join(s.testPath, "testupstreamcontainer"),
File: path.Join(s.testPath, "testupstreamfile"),
Name: "test",
},
},
}
}
func (s *S) SetUpTest(c *check.C) {
s.logOutput = &bytes.Buffer{}
log.SetOutput(s.logOutput)
os.MkdirAll(Config.Upstreams[0].ContainerFolder, 0755)
}
func (s *S) TearDownTest(c *check.C) {
os.RemoveAll(s.testPath)
}
func (s *S) TearDownSuite(c *check.C) {
s.testServer.Quit()
}
func newRequest(method, url string, b io.Reader, c *check.C) (*httptest.ResponseRecorder, *http.Request) {
request, err := http.NewRequest(method, url, b)
c.Assert(err, check.IsNil)
request.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
return recorder, request
}
func readBody(b io.Reader, c *check.C) string {
body, err := ioutil.ReadAll(b)
c.Assert(err, check.IsNil)
return string(body)
}
func (s *S) TestReadMessageWithLaunchJSON(c *check.C) {
cmd, err := commandmocker.Add("sudo", "service nginx reload")
c.Assert(err, check.IsNil)
defer commandmocker.Remove(cmd)
payload := `{"TopicArn":"arn:test","Message":` +
`"{\"AutoScalingGroupARN\":\"arn:asg-test\",\"Event\":\"autoscaling:EC2_INSTANCE_LAUNCH\",` +
`\"EC2InstanceId\":\"%s\"}"}`
b := strings.NewReader(fmt.Sprintf(payload, s.instance_ids[0]))
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, fmt.Sprintf(`Added instance "%s".`, s.instance_ids[0]))
c.Assert(recorder.Code, check.Equals, 200)
// Check upstreams file
upstream := Config.Upstreams[0]
content, err := ioutil.ReadFile(upstream.File)
c.Assert(err, check.IsNil)
serverLine := fmt.Sprintf("server %s.internal.invalid:80 max_fails=3 fail_timeout=60s; # %s\n", s.instance_ids[0], s.instance_ids[0])
c.Assert(string(content), check.Equals, fmt.Sprintf("upstream %s {\n %s}\n", upstream.Name, serverLine))
// Check run NGINX reload
c.Assert(commandmocker.Ran(cmd), check.Equals, true)
}
func (s *S) TestAddInstance(c *check.C) {
u := Config.Upstreams[0]
i := &ec2.Instance{InstanceId: "i-00000", PrivateDNSName: "test.internal"}
err := addInstance(u, i)
c.Assert(err, check.IsNil)
content, err := ioutil.ReadFile(getUpstreamFilenameForInstance(u, i))
c.Assert(err, check.IsNil)
c.Assert(string(content), check.Equals, "server test.internal:80 max_fails=3 fail_timeout=60s; # i-00000\n")
}
func (s *S) TestReadMessageWithTerminateJSON(c *check.C) {
cmd, err := commandmocker.Add("sudo", "service nginx reload")
c.Assert(err, check.IsNil)
defer commandmocker.Remove(cmd)
// Setup instance file
u := Config.Upstreams[0]
instance := &ec2.Instance{InstanceId: s.instance_ids[0], PrivateDNSName: "test.internal"}
if err := addInstance(u, instance); err != nil {
c.Error(err)
}
payload := `{"TopicArn":"arn:test","Message":
"{\"AutoScalingGroupARN\":\"arn:asg-test\",\"Event\":\"autoscaling:EC2_INSTANCE_TERMINATE\",\"EC2InstanceId\":\"%s\"}"}`
b := strings.NewReader(fmt.Sprintf(payload, s.instance_ids[0]))
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, fmt.Sprintf(`Removed instance "%s".`, s.instance_ids[0]))
c.Assert(recorder.Code, check.Equals, 200)
// Check upstreams file
content, err := ioutil.ReadFile(u.File)
c.Assert(err, check.IsNil)
c.Assert(string(content), check.Equals, fmt.Sprintf("upstream %s {\n}\n", u.Name))
// Check run NGINX reload
c.Assert(commandmocker.Ran(cmd), check.Equals, true)
}
func (s *S) TestRemoveInstance(c *check.C) {
u := Config.Upstreams[0]
// Setup test instance
i := &ec2.Instance{InstanceId: "i-00000", PrivateDNSName: "test.internal"}
err := addInstance(u, i)
c.Assert(err, check.IsNil)
// Remove instance
err = rmInstance(u, i)
c.Assert(err, check.IsNil)
_, err = os.Stat(getUpstreamFilenameForInstance(u, i))
c.Assert(os.IsNotExist(err), check.Equals, true)
}
func (s *S) TestRemoveInstanceWithoutConfigFile(c *check.C) {
u := Config.Upstreams[0]
i := &ec2.Instance{InstanceId: "i-00000", PrivateDNSName: "test.internal"}
// Remove instance
err := rmInstance(u, i)
c.Assert(err, check.ErrorMatches, "Instance \"i-00000\" not found in config.")
}
func (s *S) TestReadMessageWithInvalidJSON(c *check.C) {
b := strings.NewReader("")
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, "Invalid JSON.\n")
c.Assert(recorder.Code, check.Equals, 400)
}
func (s *S) TestReadMessageWithInvalidMessageJSON(c *check.C) {
b := strings.NewReader(`{"TopicArn":"arn:test", "Message":""}`)
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, "Invalid Message field JSON.\n")
c.Assert(recorder.Code, check.Equals, 400)
}
func (s *S) TestReadMessageFromInvalidTopicArn(c *check.C) {
b := strings.NewReader(`{"TopicArn":"invalid","Message":"{}"}`)
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, "No handler for the specified ARN (\"invalid\") found.\n")
c.Assert(recorder.Code, check.Equals, 404)
}
func (s *S) TestReadMessageFromInvalidAutoScalingGroupName(c *check.C) {
b := strings.NewReader(`{"TopicArn":"arn:test","Message":"{\"AutoScalingGroupARN\":\"arn:asg-invalid\"}"}`)
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, "Invalid Auto Scaling Group ARN \"arn:asg-invalid\".\n")
c.Assert(recorder.Code, check.Equals, 400)
}
func (s *S) TestGetInstance(c *check.C) {
i, err := getInstance(s.instance_ids[0])
c.Assert(err, check.IsNil)
c.Assert(i.InstanceId, check.Equals, s.instance_ids[0])
}
func (s *S) TestGetUpstreamFilenameForInstance(c *check.C) {
u := Config.Upstreams[0]
i := &ec2.Instance{InstanceId: "i-00000"}
path := getUpstreamFilenameForInstance(u, i)
c.Assert(path, check.Equals, filepath.Join(u.ContainerFolder, "i-00000.upstream"))
}
func (s *S) TestAutoSubscribe(c *check.C) {
Config.AutoSubscribe = true
subscriptionUrlCalled := make(chan bool)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
subscriptionUrlCalled <- true
}))
defer server.Close()
b := strings.NewReader(fmt.Sprintf(`{"TopicArn":"arn:test","Type":"SubscriptionConfirmation","SubscribeURL":"%s"}`, server.URL))
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
c.Assert(<-subscriptionUrlCalled, check.Equals, true)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, `Subscribed to "arn:test".`)
c.Assert(recorder.Code, check.Equals, 202)
}
func (s *S) TestAutoSubscribeOff(c *check.C) {
Config.AutoSubscribe = false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("This should not be called")
}))
defer server.Close()
b := strings.NewReader(fmt.Sprintf(`{"TopicArn":"arn:test","Type":"SubscriptionConfirmation","SubscribeURL":"%s"}`, server.URL))
recorder, request := newRequest("POST", "/", b, c)
readMessage(recorder, request)
body := readBody(recorder.Body, c)
c.Assert(body, check.Equals, "")
c.Assert(recorder.Code, check.Equals, 200)
}