This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/
sentry_test.go
120 lines (103 loc) · 3.76 KB
/
sentry_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
/***** 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):
# Victor Ng (vng@mozilla.com)
# Rob Miller (rmiller@mozilla.com)
#
# ***** END LICENSE BLOCK *****/
package heka_mozsvc_plugins
import (
"github.com/mozilla-services/heka/message"
"github.com/mozilla-services/heka/pipeline"
pipeline_ts "github.com/mozilla-services/heka/pipeline/testsupport"
"github.com/mozilla-services/heka/plugins"
plugins_ts "github.com/mozilla-services/heka/plugins/testsupport"
"github.com/rafrombrc/gomock/gomock"
gs "github.com/rafrombrc/gospec/src/gospec"
)
const (
DSN = "http://username:password@localhost/2"
PAYLOAD = "not_real_encoded_data"
EPOCH_TS = 1358969429.508
)
func getSentryPack() (pack *pipeline.PipelinePack) {
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack = pipeline.NewPipelinePack(recycleChan)
pack.Message.SetType("sentry")
fDsn, _ := message.NewField("dsn", DSN, "uri")
pack.Message.AddField(fDsn)
pack.Message.SetPayload(PAYLOAD)
pack.Message.SetTimestamp(int64(EPOCH_TS * 1e9))
return
}
func getSentryPackWithoutDsn() (pack *pipeline.PipelinePack) {
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack = pipeline.NewPipelinePack(recycleChan)
pack.Message.SetType("sentry")
pack.Message.SetPayload(PAYLOAD)
pack.Message.SetTimestamp(int64(EPOCH_TS * 1e9))
return
}
func SentryOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
encoder := new(plugins.PayloadEncoder)
encoder.Init(new(plugins.PayloadEncoderConfig))
ctrl := gomock.NewController(t)
defer ctrl.Finish()
c.Specify("A SentryOutput", func() {
output := new(SentryOutput)
output.Init(output.ConfigStruct())
c.Specify("doesn't die with missing or invalid dsn", func() {
var err error
sentryMsg := &SentryMsg{}
pack := getSentryPack()
f := pack.Message.FindFirstField("dsn")
*f.Name = "other"
err = output.prepSentryMsg(pack, sentryMsg)
c.Expect(err.Error(), gs.Equals, "no `dsn` field")
f, _ = message.NewField("dsn", 42, "")
pack.Message.AddField(f)
err = output.prepSentryMsg(pack, sentryMsg)
c.Expect(err.Error(), gs.Equals, "`dsn` isn't a string")
_, err = output.getClient("http://localhost")
c.Expect(err.Error(), gs.Equals, "raven: dsn missing public key and/or private key")
})
c.Specify("calls CaptureMessage with the payload when it has a dsn", func() {
oth := plugins_ts.NewOutputTestHelper(ctrl)
inChan := make(chan *pipeline.PipelinePack, 1)
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().Encode(gomock.Any()).Return(
[]byte(PAYLOAD), nil)
pack := getSentryPack()
inChan <- pack
close(inChan)
output.Run(oth.MockOutputRunner, oth.MockHelper)
})
})
c.Specify("A SentryOutput with dsn config", func() {
output := new(SentryOutput)
conf := output.ConfigStruct().(*SentryOutputConfig)
conf.Dsn = DSN
output.Init(conf)
c.Specify("calls CaptureMessage with the payload when it has a dsn", func() {
oth := plugins_ts.NewOutputTestHelper(ctrl)
inChan := make(chan *pipeline.PipelinePack, 1)
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().Encode(gomock.Any()).Return(
[]byte(PAYLOAD), nil)
pack := getSentryPackWithoutDsn()
inChan <- pack
close(inChan)
output.Run(oth.MockOutputRunner, oth.MockHelper)
})
})
}