32
32
import java .util .ArrayList ;
33
33
import java .util .Arrays ;
34
34
import java .util .Collections ;
35
- import java .util .HashMap ;
35
+ import java .util .TreeMap ;
36
36
import java .util .List ;
37
37
import java .util .Map ;
38
38
import java .util .concurrent .TimeUnit ;
39
39
import java .util .stream .Collectors ;
40
40
41
41
import static org .elasticsearch .test .hamcrest .RegexMatcher .matches ;
42
42
import static org .hamcrest .Matchers .allOf ;
43
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
43
44
import static org .hamcrest .Matchers .containsString ;
44
45
import static org .hamcrest .Matchers .equalTo ;
45
46
import static org .hamcrest .Matchers .greaterThan ;
50
51
import static org .hamcrest .Matchers .hasSize ;
51
52
52
53
/**
53
- * Tests {@code DeprecationLogger} uses the {@code ThreadContext} to add response headers.
54
+ * Tests that deprecation message are returned via response headers, and can be indexed into a data stream .
54
55
*/
55
56
public class DeprecationHttpIT extends ESRestTestCase {
56
57
58
+ /**
59
+ * Same as <code>DeprecationIndexingAppender#DEPRECATION_MESSAGES_DATA_STREAM</code>, but that class isn't visible from here.
60
+ */
61
+ private static final String DATA_STREAM_NAME = ".logs-deprecation.elasticsearch-default" ;
62
+
57
63
/**
58
64
* Check that configuring deprecation settings causes a warning to be added to the
59
65
* response headers.
@@ -93,8 +99,7 @@ public void testDeprecatedSettingsReturnWarnings() throws IOException {
93
99
equalTo (
94
100
"["
95
101
+ setting .getKey ()
96
- + "] setting was deprecated in Elasticsearch and will be removed in a "
97
- + "future release! "
102
+ + "] setting was deprecated in Elasticsearch and will be removed in a future release! "
98
103
+ "See the breaking changes documentation for the next major version."
99
104
)
100
105
);
@@ -242,8 +247,117 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
242
247
assertBusy (() -> {
243
248
Response response ;
244
249
try {
245
- client ().performRequest (new Request ("POST" , "/.logs-deprecation-elasticsearch/_refresh?ignore_unavailable=true" ));
246
- response = client ().performRequest (new Request ("GET" , "/.logs-deprecation-elasticsearch/_search" ));
250
+ client ().performRequest (new Request ("POST" , "/" + DATA_STREAM_NAME + "/_refresh?ignore_unavailable=true" ));
251
+ response = client ().performRequest (new Request ("GET" , "/" + DATA_STREAM_NAME + "/_search" ));
252
+ } catch (Exception e ) {
253
+ // It can take a moment for the index to be created. If it doesn't exist then the client
254
+ // throws an exception. Translate it into an assertion error so that assertBusy() will
255
+ // continue trying.
256
+ throw new AssertionError (e );
257
+ }
258
+ assertOK (response );
259
+
260
+ ObjectMapper mapper = new ObjectMapper ();
261
+ final JsonNode jsonNode = mapper .readTree (response .getEntity ().getContent ());
262
+
263
+ final int hits = jsonNode .at ("/hits/total/value" ).intValue ();
264
+ assertThat (hits , greaterThan (0 ));
265
+
266
+ List <Map <String , Object >> documents = new ArrayList <>();
267
+
268
+ for (int i = 0 ; i < hits ; i ++) {
269
+ final JsonNode hit = jsonNode .at ("/hits/hits/" + i + "/_source" );
270
+
271
+ final Map <String , Object > document = new TreeMap <>();
272
+ hit .fields ().forEachRemaining (entry -> document .put (entry .getKey (), entry .getValue ().textValue ()));
273
+
274
+ documents .add (document );
275
+ }
276
+
277
+ logger .warn (documents );
278
+ assertThat (documents , hasSize (2 ));
279
+
280
+ assertThat (
281
+ documents ,
282
+ hasItems (
283
+ allOf (
284
+ hasKey ("@timestamp" ),
285
+ hasKey ("elasticsearch.cluster.name" ),
286
+ hasKey ("elasticsearch.cluster.uuid" ),
287
+ hasEntry ("elasticsearch.http.request.x_opaque_id" , "some xid" ),
288
+ hasEntry ("elasticsearch.event.category" , "other" ),
289
+ hasKey ("elasticsearch.node.id" ),
290
+ hasKey ("elasticsearch.node.name" ),
291
+ hasEntry ("data_stream.dataset" , "deprecation.elasticsearch" ),
292
+ hasEntry ("data_stream.namespace" , "default" ),
293
+ hasEntry ("data_stream.type" , "logs" ),
294
+ hasEntry ("ecs.version" , "1.7" ),
295
+ hasEntry ("event.code" , "deprecated_settings" ),
296
+ hasEntry ("event.dataset" , "deprecation.elasticsearch" ),
297
+ hasEntry ("log.level" , "DEPRECATION" ),
298
+ hasKey ("log.logger" ),
299
+ hasEntry ("message" , "[deprecated_settings] usage is deprecated. use [settings] instead" )
300
+ ),
301
+ allOf (
302
+ hasKey ("@timestamp" ),
303
+ hasKey ("elasticsearch.cluster.name" ),
304
+ hasKey ("elasticsearch.cluster.uuid" ),
305
+ hasEntry ("elasticsearch.http.request.x_opaque_id" , "some xid" ),
306
+ hasEntry ("elasticsearch.event.category" , "api" ),
307
+ hasKey ("elasticsearch.node.id" ),
308
+ hasKey ("elasticsearch.node.name" ),
309
+ hasEntry ("data_stream.dataset" , "deprecation.elasticsearch" ),
310
+ hasEntry ("data_stream.namespace" , "default" ),
311
+ hasEntry ("data_stream.type" , "logs" ),
312
+ hasEntry ("ecs.version" , "1.7" ),
313
+ hasEntry ("event.code" , "deprecated_route" ),
314
+ hasEntry ("event.dataset" , "deprecation.elasticsearch" ),
315
+ hasEntry ("log.level" , "DEPRECATION" ),
316
+ hasKey ("log.logger" ),
317
+ hasEntry ("message" , "[/_test_cluster/deprecated_settings] exists for deprecated tests" )
318
+ )
319
+ )
320
+ );
321
+ }, 30 , TimeUnit .SECONDS );
322
+ } finally {
323
+ configureWriteDeprecationLogsToIndex (null );
324
+ client ().performRequest (new Request ("DELETE" , "_data_stream/" + DATA_STREAM_NAME ));
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Check that log messages about REST API compatibility are recorded to an index
330
+ */
331
+ public void testCompatibleMessagesCanBeIndexed () throws Exception {
332
+ try {
333
+ configureWriteDeprecationLogsToIndex (true );
334
+
335
+ final Request compatibleRequest = new Request ("GET" , "/_test_cluster/deprecated_settings" );
336
+ final RequestOptions compatibleOptions = compatibleRequest .getOptions ()
337
+ .toBuilder ()
338
+ .addHeader ("X-Opaque-Id" , "some xid" )
339
+ .build ();
340
+ compatibleRequest .setOptions (compatibleOptions );
341
+ compatibleRequest .setEntity (
342
+ buildSettingsRequest (Collections .singletonList (TestDeprecationHeaderRestAction .TEST_DEPRECATED_SETTING_TRUE1 ), true )
343
+ );
344
+ Response deprecatedApiResponse = client ().performRequest (compatibleRequest );
345
+ assertOK (deprecatedApiResponse );
346
+
347
+ final List <String > deprecatedWarnings = getWarningHeaders (deprecatedApiResponse .getHeaders ());
348
+ final List <String > actualWarningValues = deprecatedWarnings .stream ()
349
+ .map (s -> HeaderWarning .extractWarningValueFromWarningHeader (s , true ))
350
+ .collect (Collectors .toList ());
351
+ assertThat (
352
+ actualWarningValues ,
353
+ containsInAnyOrder (TestDeprecationHeaderRestAction .DEPRECATED_ENDPOINT , TestDeprecationHeaderRestAction .DEPRECATED_USAGE )
354
+ );
355
+
356
+ assertBusy (() -> {
357
+ Response response ;
358
+ try {
359
+ client ().performRequest (new Request ("POST" , "/" + DATA_STREAM_NAME + "/_refresh?ignore_unavailable=true" ));
360
+ response = client ().performRequest (new Request ("GET" , "/" + DATA_STREAM_NAME + "/_search" ));
247
361
} catch (Exception e ) {
248
362
// It can take a moment for the index to be created. If it doesn't exist then the client
249
363
// throws an exception. Translate it into an assertion error so that assertBusy() will
@@ -263,7 +377,7 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
263
377
for (int i = 0 ; i < hits ; i ++) {
264
378
final JsonNode hit = jsonNode .at ("/hits/hits/" + i + "/_source" );
265
379
266
- final Map <String , Object > document = new HashMap <>();
380
+ final Map <String , Object > document = new TreeMap <>();
267
381
hit .fields ().forEachRemaining (entry -> document .put (entry .getKey (), entry .getValue ().textValue ()));
268
382
269
383
documents .add (document );
@@ -280,14 +394,15 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
280
394
hasKey ("elasticsearch.cluster.name" ),
281
395
hasKey ("elasticsearch.cluster.uuid" ),
282
396
hasEntry ("elasticsearch.http.request.x_opaque_id" , "some xid" ),
397
+ hasEntry ("elasticsearch.event.category" , "other" ),
283
398
hasKey ("elasticsearch.node.id" ),
284
399
hasKey ("elasticsearch.node.name" ),
285
- hasEntry ("data_stream.dataset" , "elasticsearch. deprecation" ),
400
+ hasEntry ("data_stream.dataset" , "deprecation.elasticsearch " ),
286
401
hasEntry ("data_stream.namespace" , "default" ),
287
402
hasEntry ("data_stream.type" , "logs" ),
288
403
hasEntry ("ecs.version" , "1.7" ),
289
404
hasEntry ("event.code" , "deprecated_settings" ),
290
- hasEntry ("event.dataset" , "elasticsearch. deprecation" ),
405
+ hasEntry ("event.dataset" , "deprecation.elasticsearch " ),
291
406
hasEntry ("log.level" , "DEPRECATION" ),
292
407
hasKey ("log.logger" ),
293
408
hasEntry ("message" , "[deprecated_settings] usage is deprecated. use [settings] instead" )
@@ -297,14 +412,15 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
297
412
hasKey ("elasticsearch.cluster.name" ),
298
413
hasKey ("elasticsearch.cluster.uuid" ),
299
414
hasEntry ("elasticsearch.http.request.x_opaque_id" , "some xid" ),
415
+ hasEntry ("elasticsearch.event.category" , "api" ),
300
416
hasKey ("elasticsearch.node.id" ),
301
417
hasKey ("elasticsearch.node.name" ),
302
- hasEntry ("data_stream.dataset" , "elasticsearch. deprecation" ),
418
+ hasEntry ("data_stream.dataset" , "deprecation.elasticsearch " ),
303
419
hasEntry ("data_stream.namespace" , "default" ),
304
420
hasEntry ("data_stream.type" , "logs" ),
305
421
hasEntry ("ecs.version" , "1.7" ),
306
422
hasEntry ("event.code" , "deprecated_route" ),
307
- hasEntry ("event.dataset" , "elasticsearch. deprecation" ),
423
+ hasEntry ("event.dataset" , "deprecation.elasticsearch " ),
308
424
hasEntry ("log.level" , "DEPRECATION" ),
309
425
hasKey ("log.logger" ),
310
426
hasEntry ("message" , "[/_test_cluster/deprecated_settings] exists for deprecated tests" )
@@ -314,7 +430,7 @@ public void testDeprecationMessagesCanBeIndexed() throws Exception {
314
430
}, 30 , TimeUnit .SECONDS );
315
431
} finally {
316
432
configureWriteDeprecationLogsToIndex (null );
317
- client ().performRequest (new Request ("DELETE" , "/ _data_stream/.logs-deprecation-elasticsearch ?expand_wildcards=hidden" ));
433
+ client ().performRequest (new Request ("DELETE" , "_data_stream/" + DATA_STREAM_NAME + " ?expand_wildcards=hidden" ));
318
434
}
319
435
}
320
436
0 commit comments