This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentry_test.go
127 lines (121 loc) · 3.04 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
121
122
123
124
125
126
127
package reporter
import (
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"runtime/debug"
"testing"
"time"
"github.com/pkg/errors"
log "gopkg.in/inconshreveable/log15.v2"
"github.com/exoscale/go-reporter/logger"
"github.com/exoscale/go-reporter/sentry"
)
func TestSentryReporting(t *testing.T) {
// Create a new webserver logging requests
listener, err := net.Listen("tcp", "127.0.0.1:44444")
if err != nil {
t.Fatalf("Listen() error:\n%+v", err)
}
requests := make(chan []byte)
mux := http.NewServeMux()
mux.HandleFunc("/api/sentry/", func(w http.ResponseWriter, r *http.Request) {
// We don't really care about the method (POST) and
// the URL (/api/sentry/store/). Let's send back the
// body.
switch r.Header.Get("Content-Type") {
case "application/octet-stream":
buf := &bytes.Buffer{}
b64 := base64.NewDecoder(base64.StdEncoding, r.Body)
deflate, _ := zlib.NewReader(b64)
_, _ = io.Copy(buf, deflate)
deflate.Close()
requests <- buf.Bytes()
case "application/json":
body, _ := ioutil.ReadAll(r.Body)
requests <- body
}
// Send back 200
})
go func() {
_ = http.Serve(listener, mux)
}()
// Initialize a reporter
r, err := New(Configuration{
Logging: logger.Configuration{
Console: true,
Level: logger.Lvl(log.LvlDebug),
},
Sentry: sentry.Configuration{
DSN: fmt.Sprintf("http://public:secret@%s/sentry",
listener.Addr().String()),
Tags: map[string]string{"platform": "test"},
},
})
t.Logf("http://public:secret@%s/sentry\n",
listener.Addr().String())
if err != nil {
t.Fatalf("New() error:\n%+v", err)
}
// Trigger an error
cases := []struct {
err string
message string
expected string
}{
{"an error", "my first error", "my first error: an error"},
{"an error", "", "an error"},
}
for _, tc := range cases {
_ = r.Error(errors.New(tc.err), tc.message, "alfred", "batman")
timeout := time.After(500 * time.Millisecond)
select {
case r := <-requests:
type sentryBody struct {
Message string
EventID string `json:"event_id"`
Project string
Tags [][]string
}
var got sentryBody
if err := json.Unmarshal(r, &got); err != nil {
t.Fatalf("Unmarshal() error:\n%+v", err)
}
if got.Message != tc.expected {
t.Errorf("sentry.message == %q but expected %q", got.Message, tc.expected)
}
if got.Project != "sentry" {
t.Errorf("sentry.project == %q but expected %q", got.Project, "sentry")
}
found := false
for i, tagpair := range got.Tags {
if len(tagpair) != 2 {
t.Errorf("sentry.tags contains a non-tuple, at index %d", i+1)
continue
}
key, value := tagpair[0], tagpair[1]
if key == "platform" && value == "test" {
found = true
}
}
if !found {
t.Errorf("sentry.tags doesn't contain platform,test")
}
case <-timeout:
debug.SetTraceback("all")
panic("timeout!")
}
select {
case r := <-requests:
t.Fatalf("Error() triggered an additional Sentry request:\n%+v", r)
case <-timeout:
// OK
}
}
}