| 1 |
package raven |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
type testInterface struct{} |
| 11 |
|
| 12 |
func (t *testInterface) Class() string { return "sentry.interfaces.Test" } |
| 13 |
func (t *testInterface) Culprit() string { return "codez" } |
| 14 |
|
| 15 |
func TestShouldExcludeErr(t *testing.T) { |
| 16 |
regexpStrs := []string{"ERR_TIMEOUT", "should.exclude", "(?i)^big$"} |
| 17 |
|
| 18 |
client := &Client{ |
| 19 |
Transport: newTransport(), |
| 20 |
Tags: nil, |
| 21 |
context: &context{}, |
| 22 |
queue: make(chan *outgoingPacket, MaxQueueBuffer), |
| 23 |
} |
| 24 |
|
| 25 |
if err := client.SetIgnoreErrors(regexpStrs); err != nil { |
| 26 |
t.Fatalf("invalid regexps %v: %v", regexpStrs, err) |
| 27 |
} |
| 28 |
|
| 29 |
testCases := []string{ |
| 30 |
"there was a ERR_TIMEOUT in handlers.go", |
| 31 |
"do not log should.exclude at all", |
| 32 |
"BIG", |
| 33 |
} |
| 34 |
|
| 35 |
for _, tc := range testCases { |
| 36 |
if !client.shouldExcludeErr(tc) { |
| 37 |
t.Fatalf("failed to exclude err %q with regexps %v", tc, regexpStrs) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestPacketJSON(t *testing.T) { |
| 43 |
packet := &Packet{ |
| 44 |
Project: "1", |
| 45 |
EventID: "2", |
| 46 |
Platform: "linux", |
| 47 |
Culprit: "caused_by", |
| 48 |
ServerName: "host1", |
| 49 |
Release: "721e41770371db95eee98ca2707686226b993eda", |
| 50 |
Environment: "production", |
| 51 |
Message: "test", |
| 52 |
Timestamp: Timestamp(time.Date(2000, 01, 01, 0, 0, 0, 0, time.UTC)), |
| 53 |
Level: ERROR, |
| 54 |
Logger: "com.getsentry.raven-go.logger-test-packet-json", |
| 55 |
Tags: []Tag{Tag{"foo", "bar"}}, |
| 56 |
Modules: map[string]string{"foo": "bar"}, |
| 57 |
Fingerprint: []string{"{{ default }}", "a-custom-fingerprint"}, |
| 58 |
Interfaces: []Interface{&Message{Message: "foo"}}, |
| 59 |
} |
| 60 |
|
| 61 |
packet.AddTags(map[string]string{"foo": "foo"}) |
| 62 |
packet.AddTags(map[string]string{"baz": "buzz"}) |
| 63 |
|
| 64 |
expected := `{"message":"test","event_id":"2","project":"1","timestamp":"2000-01-01T00:00:00.00","level":"error","logger":"com.getsentry.raven-go.logger-test-packet-json","platform":"linux","culprit":"caused_by","server_name":"host1","release":"721e41770371db95eee98ca2707686226b993eda","environment":"production","tags":[["foo","bar"],["foo","foo"],["baz","buzz"]],"modules":{"foo":"bar"},"fingerprint":["{{ default }}","a-custom-fingerprint"],"logentry":{"message":"foo"}}` |
| 65 |
j, err := packet.JSON() |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("JSON marshalling should not fail: %v", err) |
| 68 |
} |
| 69 |
actual := string(j) |
| 70 |
|
| 71 |
if actual != expected { |
| 72 |
t.Errorf("incorrect json; got %s, want %s", actual, expected) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestPacketJSONNilInterface(t *testing.T) { |
| 77 |
packet := &Packet{ |
| 78 |
Project: "1", |
| 79 |
EventID: "2", |
| 80 |
Platform: "linux", |
| 81 |
Culprit: "caused_by", |
| 82 |
ServerName: "host1", |
| 83 |
Release: "721e41770371db95eee98ca2707686226b993eda", |
| 84 |
Environment: "production", |
| 85 |
Message: "test", |
| 86 |
Timestamp: Timestamp(time.Date(2000, 01, 01, 0, 0, 0, 0, time.UTC)), |
| 87 |
Level: ERROR, |
| 88 |
Logger: "com.getsentry.raven-go.logger-test-packet-json", |
| 89 |
Tags: []Tag{Tag{"foo", "bar"}}, |
| 90 |
Modules: map[string]string{"foo": "bar"}, |
| 91 |
Fingerprint: []string{"{{ default }}", "a-custom-fingerprint"}, |
| 92 |
Interfaces: []Interface{&Message{Message: "foo"}, nil}, |
| 93 |
} |
| 94 |
|
| 95 |
expected := `{"message":"test","event_id":"2","project":"1","timestamp":"2000-01-01T00:00:00.00","level":"error","logger":"com.getsentry.raven-go.logger-test-packet-json","platform":"linux","culprit":"caused_by","server_name":"host1","release":"721e41770371db95eee98ca2707686226b993eda","environment":"production","tags":[["foo","bar"]],"modules":{"foo":"bar"},"fingerprint":["{{ default }}","a-custom-fingerprint"],"logentry":{"message":"foo"}}` |
| 96 |
j, err := packet.JSON() |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("JSON marshalling should not fail: %v", err) |
| 99 |
} |
| 100 |
actual := string(j) |
| 101 |
|
| 102 |
if actual != expected { |
| 103 |
t.Errorf("incorrect json; got %s, want %s", actual, expected) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestPacketInit(t *testing.T) { |
| 108 |
packet := &Packet{Message: "a", Interfaces: []Interface{&testInterface{}}} |
| 109 |
packet.Init("foo") |
| 110 |
|
| 111 |
if packet.Project != "foo" { |
| 112 |
t.Error("incorrect Project:", packet.Project) |
| 113 |
} |
| 114 |
if packet.Culprit != "codez" { |
| 115 |
t.Error("incorrect Culprit:", packet.Culprit) |
| 116 |
} |
| 117 |
if packet.ServerName == "" { |
| 118 |
t.Errorf("ServerName should not be empty") |
| 119 |
} |
| 120 |
if packet.Level != ERROR { |
| 121 |
t.Errorf("incorrect Level: got %s, want %s", packet.Level, ERROR) |
| 122 |
} |
| 123 |
if packet.Logger != "root" { |
| 124 |
t.Errorf("incorrect Logger: got %s, want %s", packet.Logger, "root") |
| 125 |
} |
| 126 |
if time.Time(packet.Timestamp).IsZero() { |
| 127 |
t.Error("Timestamp is zero") |
| 128 |
} |
| 129 |
if len(packet.EventID) != 32 { |
| 130 |
t.Error("incorrect EventID:", packet.EventID) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestSetDSN(t *testing.T) { |
| 135 |
client := &Client{} |
| 136 |
client.SetDSN("https://u:[email protected]/sentry/1") |
| 137 |
|
| 138 |
if client.url != "https://example.com/sentry/api/1/store/" { |
| 139 |
t.Error("incorrect url:", client.url) |
| 140 |
} |
| 141 |
if client.projectID != "1" { |
| 142 |
t.Error("incorrect projectID:", client.projectID) |
| 143 |
} |
| 144 |
if client.authHeader != "Sentry sentry_version=4, sentry_key=u, sentry_secret=p" { |
| 145 |
t.Error("incorrect authHeader:", client.authHeader) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestNewClient(t *testing.T) { |
| 150 |
client := newClient(nil) |
| 151 |
if client.sampleRate != 1.0 { |
| 152 |
t.Error("invalid default sample rate") |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestSetSampleRate(t *testing.T) { |
| 157 |
client := &Client{} |
| 158 |
err := client.SetSampleRate(0.2) |
| 159 |
|
| 160 |
if err != nil { |
| 161 |
t.Error("invalid sample rate") |
| 162 |
} |
| 163 |
|
| 164 |
if client.sampleRate != 0.2 { |
| 165 |
t.Error("incorrect sample rate: ", client.sampleRate) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestSetSampleRateInvalid(t *testing.T) { |
| 170 |
client := &Client{} |
| 171 |
err := client.SetSampleRate(-1.0) |
| 172 |
|
| 173 |
if err != ErrInvalidSampleRate { |
| 174 |
t.Error("invalid sample rate should return ErrInvalidSampleRate") |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func TestUnmarshalTag(t *testing.T) { |
| 179 |
actual := new(Tag) |
| 180 |
if err := json.Unmarshal([]byte(`["foo","bar"]`), actual); err != nil { |
| 181 |
t.Fatal("unable to decode JSON:", err) |
| 182 |
} |
| 183 |
|
| 184 |
expected := &Tag{Key: "foo", Value: "bar"} |
| 185 |
if !reflect.DeepEqual(actual, expected) { |
| 186 |
t.Errorf("incorrect Tag: wanted '%+v' and got '%+v'", expected, actual) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestUnmarshalTags(t *testing.T) { |
| 191 |
tests := []struct { |
| 192 |
Input string |
| 193 |
Expected Tags |
| 194 |
}{ |
| 195 |
{ |
| 196 |
`{"foo":"bar"}`, |
| 197 |
Tags{Tag{Key: "foo", Value: "bar"}}, |
| 198 |
}, |
| 199 |
{ |
| 200 |
`[["foo","bar"],["bar","baz"]]`, |
| 201 |
Tags{Tag{Key: "foo", Value: "bar"}, Tag{Key: "bar", Value: "baz"}}, |
| 202 |
}, |
| 203 |
} |
| 204 |
|
| 205 |
for _, test := range tests { |
| 206 |
var actual Tags |
| 207 |
if err := json.Unmarshal([]byte(test.Input), &actual); err != nil { |
| 208 |
t.Fatal("unable to decode JSON:", err) |
| 209 |
} |
| 210 |
|
| 211 |
if !reflect.DeepEqual(actual, test.Expected) { |
| 212 |
t.Errorf("incorrect Tags: wanted '%+v' and got '%+v'", test.Expected, actual) |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func TestMarshalTimestamp(t *testing.T) { |
| 218 |
timestamp := Timestamp(time.Date(2000, 01, 02, 03, 04, 05, 0, time.UTC)) |
| 219 |
expected := `"2000-01-02T03:04:05.00"` |
| 220 |
|
| 221 |
actual, err := json.Marshal(timestamp) |
| 222 |
if err != nil { |
| 223 |
t.Error(err) |
| 224 |
} |
| 225 |
|
| 226 |
if string(actual) != expected { |
| 227 |
t.Errorf("incorrect string; got %s, want %s", actual, expected) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func TestUnmarshalTimestamp(t *testing.T) { |
| 232 |
timestamp := `"2000-01-02T03:04:05.00"` |
| 233 |
expected := Timestamp(time.Date(2000, 01, 02, 03, 04, 05, 0, time.UTC)) |
| 234 |
|
| 235 |
var actual Timestamp |
| 236 |
err := json.Unmarshal([]byte(timestamp), &actual) |
| 237 |
if err != nil { |
| 238 |
t.Error(err) |
| 239 |
} |
| 240 |
|
| 241 |
if actual != expected { |
| 242 |
t.Errorf("incorrect string; got %s, want %s", actual.Format("2006-01-02 15:04:05 -0700"), expected.Format("2006-01-02 15:04:05 -0700")) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func TestNilClient(t *testing.T) { |
| 247 |
var client *Client = nil |
| 248 |
eventID, ch := client.Capture(nil, nil) |
| 249 |
if eventID != "" { |
| 250 |
t.Error("expected empty eventID:", eventID) |
| 251 |
} |
| 252 |
// wait on ch: no send should succeed immediately |
| 253 |
err := <-ch |
| 254 |
if err != nil { |
| 255 |
t.Error("expected nil err:", err) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func TestCaptureNil(t *testing.T) { |
| 260 |
var client *Client = DefaultClient |
| 261 |
eventID, ch := client.Capture(nil, nil) |
| 262 |
if eventID != "" { |
| 263 |
t.Error("expected empty eventID:", eventID) |
| 264 |
} |
| 265 |
// wait on ch: no send should succeed immediately |
| 266 |
err := <-ch |
| 267 |
if err != nil { |
| 268 |
t.Error("expected nil err:", err) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func TestCaptureNilError(t *testing.T) { |
| 273 |
var client *Client = DefaultClient |
| 274 |
eventID := client.CaptureError(nil, nil) |
| 275 |
if eventID != "" { |
| 276 |
t.Error("expected empty eventID:", eventID) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestNewPacketWithExtraSetsDefault(t *testing.T) { |
| 281 |
testCases := []struct { |
| 282 |
Extra Extra |
| 283 |
Expected Extra |
| 284 |
}{ |
| 285 |
// Defaults should be set when nil is passed |
| 286 |
{ |
| 287 |
Extra: nil, |
| 288 |
Expected: setExtraDefaults(Extra{}), |
| 289 |
}, |
| 290 |
// Defaults should be set when empty is passed |
| 291 |
{ |
| 292 |
Extra: Extra{}, |
| 293 |
Expected: setExtraDefaults(Extra{}), |
| 294 |
}, |
| 295 |
// Packet should always override default keys |
| 296 |
{ |
| 297 |
Extra: Extra{ |
| 298 |
"runtime.Version": "notagoversion", |
| 299 |
}, |
| 300 |
Expected: setExtraDefaults(Extra{}), |
| 301 |
}, |
| 302 |
// Packet should include our extra info |
| 303 |
{ |
| 304 |
Extra: Extra{ |
| 305 |
"extra.extra": "extra", |
| 306 |
}, |
| 307 |
Expected: setExtraDefaults(Extra{ |
| 308 |
"extra.extra": "extra", |
| 309 |
}), |
| 310 |
}, |
| 311 |
} |
| 312 |
|
| 313 |
for i, test := range testCases { |
| 314 |
packet := NewPacketWithExtra("packet", test.Extra) |
| 315 |
if !reflect.DeepEqual(packet.Extra, test.Expected) { |
| 316 |
t.Errorf("Case [%d]: Expected packet: %+v, got: %+v", i, test.Expected, packet.Extra) |
| 317 |
} |
| 318 |
} |
| 319 |
} |