This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
report_test.go
221 lines (214 loc) · 5.49 KB
/
report_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
package main
import (
"reflect"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func Test_formatSDKName(t *testing.T) {
tests := []struct {
n string
want string
}{
{
n: "sentry.python",
want: "sentry-python",
},
{
n: "sentry.javascript.node",
want: "sentry-javascript",
},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
if got := formatSDKName(tt.n); got != tt.want {
t.Errorf("formatSDKName() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getAppDetails(t *testing.T) {
tests := []struct {
name string
path string
sdkInfo SDKInfo
want AppDetails
}{
{
name: "Python Django",
path: "result/python/django/20210923-152931-snbclwa/baseline",
sdkInfo: SDKInfo{
Name: "sentry.python",
Version: "1.3.0",
},
want: AppDetails{
Language: "python",
Framework: "django",
SdkName: "sentry-python",
SdkVersion: "1.3.0",
},
},
{
name: "JavaScript Express",
path: "result/javascript/express/20210923-145159-yaubxsi/baseline",
sdkInfo: SDKInfo{
Name: "sentry.javascript",
Version: "6.11.0",
},
want: AppDetails{
Language: "javascript",
Framework: "express",
SdkName: "sentry-javascript",
SdkVersion: "6.11.0",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getAppDetails(tt.path, tt.sdkInfo); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getConfiguration() = %v, want %v", got, tt.want)
}
})
}
}
func Test_percentDiff(t *testing.T) {
type args struct {
start time.Duration
final time.Duration
}
tests := []struct {
name string
args args
want float64
}{
{
name: "example percentage",
args: args{
start: 31201976,
final: 35903631,
},
want: 15.07,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := percentDiff(tt.args.start, tt.args.final); got != tt.want {
t.Errorf("percentDiff() = %v, want %v", got, tt.want)
}
})
}
}
func TestFormatHTTP(t *testing.T) {
tests := []struct {
Name string
Req string
Want string
}{
{
Name: "Empty string",
Req: "",
Want: "",
},
{
Name: "No body",
Req: "HEAD / HTTP 1.0",
Want: "HEAD / HTTP 1.0",
},
{
Name: "Non-JSON body",
Req: "GET / HTTP 1.1\r\n\r\n" +
"<html></html>",
Want: "GET / HTTP 1.1\r\n\r\n" +
"<html></html>",
},
{
Name: "Sentry envelope", // multiple line-separated JSON objects
Req: "POST /api/1/envelope/ HTTP/1.1\r\n" +
"Host: relay:5000\r\n" +
"Content-Length: 1514\r\n" +
"Content-Type: application/x-sentry-envelope\r\n" +
"Sentry-Trace: 24077aaa50fa477c848c2b28af6fe9ce-a0bb7bbdb148ae20-\r\n" +
"User-Agent: sentry.python/1.3.0\r\n" +
"X-Sentry-Auth: Sentry sentry_key=sentry, sentry_version=7, sentry_client=sentry.python/1.3.0\r\n\r\n" +
`{"event_id":"9c0f672b97e4463ebe92af551a064ceb","sent_at":"2021-09-24T23:01:24.230070Z"}` + "\n" +
`{"type":"transaction","content_type":"application/json","length":8487}` + "\n" +
`{"type":"transaction","transaction":"/update","contexts":{"trace":{"trace_id":"c81fbb46bbef431da70909d59954bfbb","span_id":"96114f6cf78209b8","parent_span_id":null,"op":"http.server","description":null,"status":"ok"},"runtime":{"name":"CPython","version":"3.9.1"}}}`,
Want: "POST /api/1/envelope/ HTTP/1.1\r\n" +
"Host: relay:5000\r\n" +
"Content-Length: 1514\r\n" +
"Content-Type: application/x-sentry-envelope\r\n" +
"Sentry-Trace: 24077aaa50fa477c848c2b28af6fe9ce-a0bb7bbdb148ae20-\r\n" +
"User-Agent: sentry.python/1.3.0\r\n" +
"X-Sentry-Auth: Sentry sentry_key=sentry, sentry_version=7, sentry_client=sentry.python/1.3.0\r\n\r\n" +
`{
"event_id": "9c0f672b97e4463ebe92af551a064ceb",
"sent_at": "2021-09-24T23:01:24.230070Z"
}
{
"type": "transaction",
"content_type": "application/json",
"length": 8487
}
{
"type": "transaction",
"transaction": "/update",
"contexts": {
"trace": {
"trace_id": "c81fbb46bbef431da70909d59954bfbb",
"span_id": "96114f6cf78209b8",
"parent_span_id": null,
"op": "http.server",
"description": null,
"status": "ok"
},
"runtime": {
"name": "CPython",
"version": "3.9.1"
}
}
}
`,
},
{
Name: "Zipkin spans", // single JSON array
Req: "POST /api/v2/spans HTTP/1.1\r\n" +
"Host: relay:5000\r\n" +
"Content-Length: 33123\r\n" +
"Content-Type: application/json\r\n" +
"User-Agent: python-requests/2.26.0\r\n\r\n" +
`[{"traceId": "743ff6051814ca3357ef9852c599fc82", "id": "739fe5bba76a3a7c", "name": "^update$", "timestamp": 1632518156885583, "duration": 2006248, "localEndpoint": {"serviceName": "unknown_service"}, "kind": "SERVER", "tags": {"http.method": "GET"}, "debug": true}]`,
Want: "POST /api/v2/spans HTTP/1.1\r\n" +
"Host: relay:5000\r\n" +
"Content-Length: 33123\r\n" +
"Content-Type: application/json\r\n" +
"User-Agent: python-requests/2.26.0\r\n\r\n" +
`[
{
"traceId": "743ff6051814ca3357ef9852c599fc82",
"id": "739fe5bba76a3a7c",
"name": "^update$",
"timestamp": 1632518156885583,
"duration": 2006248,
"localEndpoint": {
"serviceName": "unknown_service"
},
"kind": "SERVER",
"tags": {
"http.method": "GET"
},
"debug": true
}
]
`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
got := formatHTTP(tt.Req)
if diff := cmp.Diff(tt.Want, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}