2014-11-02 21:31:15 +03:00
|
|
|
package logrus_sentry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/getsentry/raven-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
message = "error message"
|
|
|
|
server_name = "testserver.internal"
|
|
|
|
logger_name = "test.logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getTestLogger() *logrus.Logger {
|
|
|
|
l := logrus.New()
|
|
|
|
l.Out = ioutil.Discard
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2014-11-06 12:07:41 +03:00
|
|
|
func WithTestDSN(t *testing.T, tf func(string, <-chan *raven.Packet)) {
|
2014-11-02 21:31:15 +03:00
|
|
|
pch := make(chan *raven.Packet, 1)
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
defer req.Body.Close()
|
|
|
|
d := json.NewDecoder(req.Body)
|
|
|
|
p := &raven.Packet{}
|
|
|
|
err := d.Decode(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
pch <- p
|
|
|
|
}))
|
2014-11-06 12:07:41 +03:00
|
|
|
defer s.Close()
|
2014-11-02 21:31:15 +03:00
|
|
|
|
|
|
|
fragments := strings.SplitN(s.URL, "://", 2)
|
2014-11-06 12:07:41 +03:00
|
|
|
dsn := fmt.Sprintf(
|
|
|
|
"%s://public:secret@%s/sentry/project-id",
|
|
|
|
fragments[0],
|
|
|
|
fragments[1],
|
|
|
|
)
|
|
|
|
tf(dsn, pch)
|
2014-11-02 21:31:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpecialFields(t *testing.T) {
|
2014-11-06 12:07:41 +03:00
|
|
|
WithTestDSN(t, func(dsn string, pch <-chan *raven.Packet) {
|
|
|
|
logger := getTestLogger()
|
2014-11-02 21:31:15 +03:00
|
|
|
|
2014-11-06 12:07:41 +03:00
|
|
|
hook, err := NewSentryHook(dsn, []logrus.Level{
|
|
|
|
logrus.ErrorLevel,
|
|
|
|
})
|
2014-11-02 21:31:15 +03:00
|
|
|
|
2014-11-06 12:07:41 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
logger.Hooks.Add(hook)
|
|
|
|
logger.WithFields(logrus.Fields{
|
|
|
|
"server_name": server_name,
|
|
|
|
"logger": logger_name,
|
|
|
|
}).Error(message)
|
2014-11-02 21:31:15 +03:00
|
|
|
|
2014-11-06 12:07:41 +03:00
|
|
|
packet := <-pch
|
|
|
|
if packet.Logger != logger_name {
|
|
|
|
t.Errorf("logger should have been %s, was %s", logger_name, packet.Logger)
|
|
|
|
}
|
2014-11-02 21:31:15 +03:00
|
|
|
|
2014-11-06 12:07:41 +03:00
|
|
|
if packet.ServerName != server_name {
|
|
|
|
t.Errorf("server_name should have been %s, was %s", server_name, packet.ServerName)
|
|
|
|
}
|
|
|
|
})
|
2014-11-02 21:31:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSentryHandler(t *testing.T) {
|
2014-11-06 12:07:41 +03:00
|
|
|
WithTestDSN(t, func(dsn string, pch <-chan *raven.Packet) {
|
|
|
|
logger := getTestLogger()
|
|
|
|
hook, err := NewSentryHook(dsn, []logrus.Level{
|
|
|
|
logrus.ErrorLevel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
logger.Hooks.Add(hook)
|
2014-11-02 21:31:15 +03:00
|
|
|
|
2014-11-06 12:07:41 +03:00
|
|
|
logger.Error(message)
|
|
|
|
packet := <-pch
|
|
|
|
if packet.Message != message {
|
|
|
|
t.Errorf("message should have been %s, was %s", message, packet.Message)
|
|
|
|
}
|
|
|
|
})
|
2014-11-02 21:31:15 +03:00
|
|
|
}
|