forked from hypertrace/javaagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppServerTest.groovy
365 lines (266 loc) · 10.8 KB
/
AppServerTest.groovy
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.hypertrace.agent.smoketest
import okhttp3.MediaType
import okhttp3.RequestBody
import static org.junit.Assume.assumeTrue
import io.opentelemetry.proto.trace.v1.Span
import okhttp3.Request
import org.junit.runner.RunWith
import spock.lang.Shared
import spock.lang.Unroll
@RunWith(AppServerTestRunner)
abstract class AppServerTest extends SmokeTest {
@Shared
String jdk
@Shared
String serverVersion
def setupSpec() {
def appServer = AppServerTestRunner.currentAppServer(this.getClass())
serverVersion = appServer.version()
jdk = appServer.jdk()
startTarget(jdk, serverVersion)
}
def cleanupSpec() {
stopTarget()
}
boolean testSmoke() {
true
}
boolean testAsyncSmoke() {
true
}
boolean testException() {
true
}
boolean testRequestWebInfWebXml() {
true
}
//TODO add assert that server spans were created by servers, not by servlets
@Unroll
def "#appServer smoke test on JDK #jdk"(String appServer, String jdk) {
assumeTrue(testSmoke())
String url = "http://localhost:${target.getMappedPort(8080)}/app/greeting"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
String responseBody = response.body().string()
then: "There is one trace"
traceIds.size() == 1
and: "trace id is present in the HTTP headers as reported by the called endpoint"
responseBody.contains(traceIds.find())
and: "Server spans in the distributed trace"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 2
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/greeting')) == 1
traces.countSpansByName(getSpanName('/app/headers')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
and: "Client and server spans for the remote call"
traces.countFilteredAttributes("http.url", "http://localhost:8080/app/headers") == 2
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer request response capture test smoke test on JDK #jdk"(String appServer, String jdk) {
assumeTrue(testSmoke())
String url = "http://localhost:${target.getMappedPort(8080)}/app/echo"
MediaType JSON = MediaType.parse("application/json; charset=utf-8")
String stringBody = "{\"greeting\" : \"Hello\",\"name\" : \"John\"}"
RequestBody requestBody = RequestBody.create(stringBody, JSON);
def request = new Request.Builder().url(url).post(requestBody).build();
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
String responseBody = response.body().string()
String headerValue = new String(Base64.getDecoder().decode(response.header("Header-Dump")));
then: "There is one trace"
traceIds.size() == 1
and: "trace id is present in the HTTP headers as reported by the called endpoint"
headerValue.contains(traceIds.find())
and: "Server spans in the distributed trace"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 2
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/echo')) == 1
traces.countSpansByName(getSpanName('/app/headers')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
and: "Client and server spans for the remote call"
traces.countFilteredAttributes("http.url", "http://localhost:8080/app/headers") == 2
and: "response body attribute should be present"
traces.countFilteredAttributes("http.response.body") == 1
and: "request body attribute should be present"
traces.countFilteredAttributes("http.request.body") == 1
and: "Request body should be same as sent content"
traces.getFilteredAttributeValue("http.request.body") == stringBody
and: "Response body should be same as sent content"
traces.getFilteredAttributeValue("http.response.body") == stringBody
and: "Response body should be same as received response body"
traces.getFilteredAttributeValue("http.response.body") == responseBody
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer test static file found on JDK #jdk"(String appServer, String jdk) {
String url = "http://localhost:${target.getMappedPort(8080)}/app/hello.txt"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
String responseBody = response.body().string()
then: "There is one trace"
traceIds.size() == 1
and: "Response contains Hello"
responseBody.contains("Hello")
and: "There is one server span"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 1
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/hello.txt')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer test static file not found on JDK #jdk"(String appServer, String jdk) {
String url = "http://localhost:${target.getMappedPort(8080)}/app/file-that-does-not-exist"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
then: "There is one trace"
traceIds.size() == 1
and: "Response code is 404"
response.code() == 404
and: "There is one server span"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 1
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/file-that-does-not-exist')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer test request for WEB-INF/web.xml on JDK #jdk"(String appServer, String jdk) {
assumeTrue(testRequestWebInfWebXml())
String url = "http://localhost:${target.getMappedPort(8080)}/app/WEB-INF/web.xml"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
then: "There is one trace"
traceIds.size() == 1
and: "Response code is 404"
response.code() == 404
and: "There is one server span"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 1
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/WEB-INF/web.xml')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer test request with error JDK #jdk"(String appServer, String jdk) {
assumeTrue(testException())
String url = "http://localhost:${target.getMappedPort(8080)}/app/exception"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
then: "There is one trace"
traceIds.size() == 1
and: "Response code is 500"
response.code() == 500
and: "There is one server span"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 1
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/exception')) == 1
and: "There is one exception"
traces.countFilteredEventAttributes('exception.message', 'This is expected') == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer test request outside deployed application JDK #jdk"(String appServer, String jdk) {
String url = "http://localhost:${target.getMappedPort(8080)}/this-is-definitely-not-there-but-there-should-be-a-trace-nevertheless"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
then: "There is one trace"
traceIds.size() == 1
and: "Response code is 404"
response.code() == 404
and: "There is one server span"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 1
and: "Expected span names"
traces.countSpansByName(getSpanName('/this-is-definitely-not-there-but-there-should-be-a-trace-nevertheless')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
@Unroll
def "#appServer async smoke test on JDK #jdk"(String appServer, String jdk) {
assumeTrue(testAsyncSmoke())
String url = "http://localhost:${target.getMappedPort(8080)}/app/asyncgreeting"
def request = new Request.Builder().url(url).get().build()
when:
def response = CLIENT.newCall(request).execute()
TraceInspector traces = new TraceInspector(waitForTraces())
Set<String> traceIds = traces.traceIds
String responseBody = response.body().string()
then: "There is one trace"
traceIds.size() == 1
and: "trace id is present in the HTTP headers as reported by the called endpoint"
responseBody.contains(traceIds.find())
and: "Server spans in the distributed trace"
traces.countSpansByKind(Span.SpanKind.SPAN_KIND_SERVER) == 2
and: "Expected span names"
traces.countSpansByName(getSpanName('/app/asyncgreeting')) == 1
traces.countSpansByName(getSpanName('/app/headers')) == 1
and: "The span for the initial web request"
traces.countFilteredAttributes("http.url", url) == 1
and: "Client and server spans for the remote call"
traces.countFilteredAttributes("http.url", "http://localhost:8080/app/headers") == 2
cleanup:
response?.close()
where:
[appServer, jdk] << getTestParams()
}
protected abstract String getSpanName(String path);
protected List<List<Object>> getTestParams() {
return [
[serverVersion, jdk]
]
}
}