This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mocks_test.go
295 lines (259 loc) · 7.87 KB
/
mocks_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"bytes"
"compress/flate"
"compress/gzip"
"encoding/base64"
"encoding/json"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/firehose/firehoseiface"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
"github.com/bradfitz/gomemcache/memcache"
"github.com/twitchscience/scoop_protocol/spade"
"github.com/twitchscience/spade/cache/elastimemcache"
"github.com/twitchscience/spade/consumer"
"github.com/twitchscience/spade/geoip"
"github.com/twitchscience/spade/lookup"
"github.com/twitchscience/spade/reporter"
)
type sentRecord struct {
source string
data string
}
// s3Mock returns the fetcherConfigs defined in main_test.go from the ConfigBucket bucket.
type s3Mock struct {
sentRecords chan sentRecord
fetcherConfigs map[string][]byte
s3iface.S3API
}
func newS3Mock(sentRecords chan sentRecord) (*s3Mock, error) {
m := &s3Mock{
sentRecords: sentRecords,
fetcherConfigs: make(map[string][]byte),
}
for key, val := range fetcherConfigs {
buf := &bytes.Buffer{}
w := gzip.NewWriter(buf)
_, err := w.Write([]byte(val))
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
m.fetcherConfigs[key] = buf.Bytes()
}
return m, nil
}
func (m *s3Mock) GetObject(input *s3.GetObjectInput) (*s3.GetObjectOutput, error) {
if aws.StringValue(input.Bucket) == "ConfigBucket" {
output := m.fetcherConfigs[aws.StringValue(input.Key)]
return &s3.GetObjectOutput{
Body: ioutil.NopCloser(bytes.NewBuffer(output)),
}, nil
}
return nil, nil
}
// s3UploaderMock captures all files uploaded and sends them along with the bucket name
// to sentRecords.
type s3UploaderMock struct {
sentRecords chan sentRecord
s3manageriface.UploaderAPI
}
func (m *s3UploaderMock) Upload(input *s3manager.UploadInput, options ...func(*s3manager.Uploader)) (
*s3manager.UploadOutput, error) {
reader, err := gzip.NewReader(input.Body)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
m.sentRecords <- sentRecord{
source: aws.StringValue(input.Bucket),
data: string(body),
}
return nil, nil
}
// snsMock forwards any received messages to sentRecords with the TopicArn.
type snsMock struct {
sentRecords chan sentRecord
snsiface.SNSAPI
}
func (m *snsMock) Publish(input *sns.PublishInput) (*sns.PublishOutput, error) {
m.sentRecords <- sentRecord{
source: aws.StringValue(input.TopicArn),
data: aws.StringValue(input.Message),
}
return nil, nil
}
// elasticacheMock returns a dummy elasticache endpoint.
type elasticacheMock struct {
elasticacheiface.ElastiCacheAPI
}
func (m *elasticacheMock) DescribeCacheClusters(input *elasticache.DescribeCacheClustersInput) (
*elasticache.DescribeCacheClustersOutput, error) {
if *input.CacheClusterId == "eccluster" && *input.ShowCacheNodeInfo {
return &elasticache.DescribeCacheClustersOutput{
CacheClusters: []*elasticache.CacheCluster{
{
CacheNodes: []*elasticache.CacheNode{
{
Endpoint: &elasticache.Endpoint{
Address: aws.String("sdf"),
Port: aws.Int64(123),
},
},
},
},
},
}, nil
}
return nil, nil
}
// mockKinesis writes all records received to sentRecords along with the stream name.
type mockKinesis struct {
sentRecords chan sentRecord
kinesisiface.KinesisAPI
}
func (m *mockKinesis) PutRecords(input *kinesis.PutRecordsInput) (
*kinesis.PutRecordsOutput, error) {
streamName := aws.StringValue(input.StreamName)
res := kinesis.PutRecordsOutput{
Records: make([]*kinesis.PutRecordsResultEntry, len(input.Records)),
}
for i, record := range input.Records {
m.sentRecords <- sentRecord{streamName, string(record.Data)}
res.Records[i] = &kinesis.PutRecordsResultEntry{
ErrorCode: aws.String(""),
}
}
return &res, nil
}
type kinesisFactory struct {
sentRecords chan sentRecord
}
func (f *kinesisFactory) New(region, role string) kinesisiface.KinesisAPI {
if region == "us-west-2" && role == "" {
return &mockKinesis{sentRecords: f.sentRecords}
}
return nil
}
// mockFirehose writes all records received to sentRecords along with the stream name.
type mockFirehose struct {
sentRecords chan sentRecord
firehoseiface.FirehoseAPI
}
func (m *mockFirehose) PutRecordBatch(input *firehose.PutRecordBatchInput) (
*firehose.PutRecordBatchOutput, error) {
streamName := aws.StringValue(input.DeliveryStreamName)
res := firehose.PutRecordBatchOutput{
RequestResponses: make([]*firehose.PutRecordBatchResponseEntry, len(input.Records)),
}
for i, record := range input.Records {
m.sentRecords <- sentRecord{streamName, string(record.Data)}
res.RequestResponses[i] = &firehose.PutRecordBatchResponseEntry{
ErrorCode: aws.String(""),
}
}
return &res, nil
}
type firehoseFactory struct {
sentRecords chan sentRecord
}
func (f *firehoseFactory) New(region, role string) firehoseiface.FirehoseAPI {
if region == "us-east-1" && role == "iam:role:spade-test-2" {
return &mockFirehose{sentRecords: f.sentRecords}
}
return nil
}
// loginFetcher returns user IDs from fetcherUserIDs in main_test.go.
type loginFetcher struct{}
func (l *loginFetcher) FetchInt64(args map[string]string) (int64, error) {
return fetcherUserIDs[args["login"]], nil
}
func valueFetcherFactory(l lookup.JSONValueFetcherConfig, s reporter.StatsLogger) (lookup.ValueFetcher, error) {
return &loginFetcher{}, nil
}
// resultPipeMock encodes records and returns them on its ReadChannel().
type resultPipeMock struct {
c chan *consumer.Result
}
func (m *resultPipeMock) ReadChannel() <-chan *consumer.Result {
return m.c
}
func (m *resultPipeMock) Close() {}
func (m *resultPipeMock) WriteBatch(batch []testParam) error {
transBatch := make([]spade.Event, len(batch))
for i, param := range batch {
transBatch[i] = param.event
// This leaves the extra whitespace in the event, but ehn.
transBatch[i].Data = base64.StdEncoding.EncodeToString([]byte(param.event.Data))
}
encData, err := json.Marshal(transBatch)
if err != nil {
return err
}
data := bytes.Buffer{}
data.WriteByte(1)
compressor, err := flate.NewWriter(&data, flate.BestSpeed)
if err != nil {
return err
}
if _, err = compressor.Write(encData); err != nil {
return err
}
if err = compressor.Close(); err != nil {
return err
}
m.c <- &consumer.Result{
Data: data.Bytes(),
Error: nil,
}
return nil
}
// geoipMock returns a dummy location.
type geoipMock struct {
geoip.GeoLookup
}
func (m *geoipMock) GetRegion(ip string) string { return "CA" }
func (m *geoipMock) GetCountry(ip string) string { return "US" }
func (m *geoipMock) GetCity(ip string) string { return "SF" }
func (m *geoipMock) GetAsn(ip string) string { return "ASN" }
func (m *geoipMock) Reload() error { return nil }
// memcacheMock returns values from cachedUserIDs in main_test.go.
type memcacheMock struct {
elastimemcache.MemcacheClient
}
func (m *memcacheMock) Get(key string) (*memcache.Item, error) {
if id, ok := cachedUserIDs[key]; ok {
return &memcache.Item{Value: []byte(id)}, nil
}
return nil, memcache.ErrCacheMiss
}
func (m *memcacheMock) Set(item *memcache.Item) error {
return nil
}
// selectorMock saves the servers called with SetServers for later comparison.
type selectorMock struct {
elastimemcache.ServerList
servers []string
}
func (m *selectorMock) SetServers(servers ...string) error {
m.servers = make([]string, len(servers))
copy(m.servers, servers)
return nil
}