forked from mozilla-services/heka-mozsvc-plugins
/
cloudwatch_test.go
232 lines (199 loc) · 6.73 KB
/
cloudwatch_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
/***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2012
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Ben Bangert (bbangert@mozilla.com)
#
# ***** END LICENSE BLOCK *****/
package heka_mozsvc_plugins
import (
"code.google.com/p/go-uuid/uuid"
"code.google.com/p/gomock/gomock"
"errors"
ts "github.com/mozilla-services/heka-mozsvc-plugins/testsupport"
"github.com/mozilla-services/heka/message"
"github.com/mozilla-services/heka/pipeline"
pipeline_ts "github.com/mozilla-services/heka/testsupport"
gs "github.com/rafrombrc/gospec/src/gospec"
"net/http"
"os"
"strings"
"time"
)
type InputTestHelper struct {
Msg *message.Message
Pack *pipeline.PipelinePack
AddrStr string
ResolvedAddrStr string
MockHelper *ts.MockPluginHelper
MockInputRunner *ts.MockInputRunner
MockDecoderSet *ts.MockDecoderSet
Decoder pipeline.DecoderRunner
PackSupply chan *pipeline.PipelinePack
DecodeChan chan *pipeline.PipelinePack
}
var awsResponse = `
<GetMetricStatisticsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
<GetMetricStatisticsResult>
<Datapoints>
<member>
<Timestamp>2013-06-25T17:18:00Z</Timestamp>
<SampleCount>837721.0</SampleCount>
<Unit>Seconds</Unit>
<Minimum>8.0E-6</Minimum>
<Maximum>59.929617</Maximum>
<Average>0.006249934529515202</Average>
</member>
</Datapoints>
<Label>Latency</Label>
</GetMetricStatisticsResult>
<ResponseMetadata>
<RequestId>6d0916bd-ddfe-11e2-bb4d-cb095c9ec687</RequestId>
</ResponseMetadata>
</GetMetricStatisticsResponse>
`
var simpleJsonPayload = `
{"Datapoints":[{"MetricName":"Testval","Timestamp":"Fri Jul 12 12:59:52 2013","Value":7.82636926e-06,"Unit":"Kilobytes"}]}
`
var awsSuccessResponse = `
<PutMetricDataResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
<ResponseMetadata>
<RequestId>6d0916bd-ddfe-11e2-bb4d-cb095c9ec687</RequestId>
</ResponseMetadata>
</PutMetricDataResponse>
`
func getTestMessage() *message.Message {
hostname, _ := os.Hostname()
field, _ := message.NewField("foo", "bar", "")
msg := &message.Message{}
msg.SetType("TEST")
msg.SetTimestamp(time.Now().UnixNano())
msg.SetUuid(uuid.NewRandom())
msg.SetLogger("GoSpec")
msg.SetSeverity(int32(6))
msg.SetPayload("Test Payload")
msg.SetEnvVersion("0.8")
msg.SetPid(int32(os.Getpid()))
msg.SetHostname(hostname)
msg.AddField(field)
return msg
}
type RespCloser struct {
*strings.Reader
}
func (r *RespCloser) Close() error {
return nil
}
func CloudwatchInputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
c.Specify("A CloudwatchInput", func() {
input := new(CloudwatchInput)
inputConfig := input.ConfigStruct().(*CloudwatchInputConfig)
inputConfig.MetricName = "Test"
inputConfig.Statistics = []string{"Average"}
inputConfig.PollInterval = "1ms"
inputConfig.Region = "us-east-1"
inputConfig.Namespace = "Testing"
err := input.Init(inputConfig)
c.Assume(err, gs.IsNil)
serv := ts.NewMockAWSService(ctrl)
input.cw.Service = serv
ith := new(InputTestHelper)
recycleChan := make(chan *pipeline.PipelinePack, 500)
// set up mock helper, decoder set, and packSupply channel
ith.MockHelper = ts.NewMockPluginHelper(ctrl)
ith.MockInputRunner = ts.NewMockInputRunner(ctrl)
ith.Decoder = ts.NewMockDecoderRunner(ctrl)
ith.PackSupply = make(chan *pipeline.PipelinePack, 1)
ith.DecodeChan = make(chan *pipeline.PipelinePack)
ith.MockDecoderSet = ts.NewMockDecoderSet(ctrl)
ith.Msg = getTestMessage()
ith.Pack = pipeline.NewPipelinePack(recycleChan)
c.Specify("can receive a set of metrics", func() {
ith.PackSupply <- ith.Pack
resp := new(http.Response)
resp.Body = &RespCloser{strings.NewReader(awsResponse)}
resp.StatusCode = 200
// Setup the mock response
ith.MockInputRunner.EXPECT().InChan().Return(ith.PackSupply)
ith.MockInputRunner.EXPECT().Inject(ith.Pack)
serv.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(resp, nil)
go func() {
input.Run(ith.MockInputRunner, ith.MockHelper)
}()
ith.PackSupply <- ith.Pack
close(input.stopChan)
c.Expect(ith.Pack.Message.GetLogger(), gs.Equals, "Testing")
c.Expect(ith.Pack.Message.GetPayload(), gs.Equals, "Test")
val, _ := ith.Pack.Message.GetFieldValue("Unit")
c.Expect(val.(string), gs.Equals, "Seconds")
val, _ = ith.Pack.Message.GetFieldValue("SampleCount")
c.Expect(val.(float64), gs.Equals, float64(837721.0))
})
})
c.Specify("A CloudwatchOutput", func() {
mockOutputRunner := ts.NewMockOutputRunner(ctrl)
mockHelper := ts.NewMockPluginHelper(ctrl)
inChan := make(chan *pipeline.PipelinePack, 1)
recycleChan := make(chan *pipeline.PipelinePack, 1)
mockOutputRunner.EXPECT().InChan().Return(inChan)
msg := getTestMessage()
pack := pipeline.NewPipelinePack(recycleChan)
pack.Message = msg
pack.Decoded = true
output := new(CloudwatchOutput)
outputConfig := output.ConfigStruct().(*CloudwatchOutputConfig)
outputConfig.Retries = 3
outputConfig.Backlog = 10
outputConfig.Namespace = "Test"
outputConfig.Region = "us-east-1"
err := output.Init(outputConfig)
c.Assume(err, gs.IsNil)
serv := ts.NewMockAWSService(ctrl)
output.cw.Service = serv
c.Specify("can send a batch of metrics", func() {
resp := new(http.Response)
resp.Body = &RespCloser{strings.NewReader(awsSuccessResponse)}
resp.StatusCode = 200
pack.Message.SetPayload(simpleJsonPayload)
serv.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(resp, nil)
mockOutputRunner.EXPECT().LogMessage(gomock.Any())
finished := make(chan bool)
inChan <- pack
go func() {
output.Run(mockOutputRunner, mockHelper)
finished <- true
}()
<-recycleChan
close(inChan)
<-finished
})
c.Specify("can retry failed operations", func() {
resp := new(http.Response)
resp.Body = &RespCloser{strings.NewReader(awsSuccessResponse)}
resp.StatusCode = 200
err := errors.New("Oops, not working")
pack.Message.SetPayload(simpleJsonPayload)
serv.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Times(3).Return(resp, err)
mockOutputRunner.EXPECT().LogMessage(gomock.Any())
mockOutputRunner.EXPECT().LogError(gomock.Any())
finished := make(chan bool)
inChan <- pack
go func() {
output.Run(mockOutputRunner, mockHelper)
finished <- true
}()
<-recycleChan
close(inChan)
<-finished
})
})
}