Skip to content

Commit 5dae10d

Browse files
committed
[TEST] add warnings check to ESTestCase
We are currenlty checking that no deprecation warnings are emitted in our query tests. That can be moved to ESTestCase (disabled in ESIntegTestCase) as it allows us to easily catch where our tests use deprecated features and assert on the expected warnings.
1 parent 6a27628 commit 5dae10d

File tree

27 files changed

+190
-198
lines changed

27 files changed

+190
-198
lines changed

core/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.apache.logging.log4j.LogManager;
2323
import org.apache.logging.log4j.Logger;
24-
import org.apache.logging.log4j.message.ParameterizedMessage;
2524
import org.elasticsearch.common.SuppressLoggerChecks;
2625
import org.elasticsearch.common.util.concurrent.ThreadContext;
2726

@@ -42,7 +41,7 @@ public class DeprecationLogger {
4241
*
4342
* https://tools.ietf.org/html/rfc7234#section-5.5
4443
*/
45-
public static final String DEPRECATION_HEADER = "Warning";
44+
public static final String WARNING_HEADER = "Warning";
4645

4746
/**
4847
* This is set once by the {@code Node} constructor, but it uses {@link CopyOnWriteArraySet} to ensure that tests can run in parallel.
@@ -128,7 +127,7 @@ void deprecated(Set<ThreadContext> threadContexts, String message, Object... par
128127

129128
while (iterator.hasNext()) {
130129
try {
131-
iterator.next().addResponseHeader(DEPRECATION_HEADER, formattedMessage);
130+
iterator.next().addResponseHeader(WARNING_HEADER, formattedMessage);
132131
} catch (IllegalStateException e) {
133132
// ignored; it should be removed shortly
134133
}

core/src/test/java/org/elasticsearch/action/admin/indices/template/BWCTemplateTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ public void testBeatsTemplatesBWC() throws Exception {
4242
client().prepareIndex("packetbeat-foo", "doc", "1").setSource("message", "foo").get();
4343
client().prepareIndex("filebeat-foo", "doc", "1").setSource("message", "foo").get();
4444
client().prepareIndex("winlogbeat-foo", "doc", "1").setSource("message", "foo").get();
45+
assertWarnings("Deprecated field [template] used, replaced by [index_patterns]");
4546
}
4647

4748
public void testLogstashTemplatesBWC() throws Exception {
4849
String ls5x = copyToStringFromClasspath("/org/elasticsearch/action/admin/indices/template/logstash-5.0.template.json");
4950
client().admin().indices().preparePutTemplate("logstash-5x").setSource(ls5x).get();
5051
client().prepareIndex("logstash-foo", "doc", "1").setSource("message", "foo").get();
52+
assertWarnings("Deprecated field [template] used, replaced by [index_patterns]");
5153
}
5254

5355
}

core/src/test/java/org/elasticsearch/common/ParseFieldTests.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import org.elasticsearch.test.ESTestCase;
2222

23+
import java.io.IOException;
24+
2325
import static org.hamcrest.CoreMatchers.is;
2426
import static org.hamcrest.CoreMatchers.not;
2527
import static org.hamcrest.CoreMatchers.sameInstance;
2628
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
2729

2830
public class ParseFieldTests extends ESTestCase {
29-
public void testParse() {
31+
public void testParse() throws IOException {
3032
String name = "foo_bar";
3133
ParseField field = new ParseField(name);
3234
String[] deprecated = new String[]{"barFoo", "bar_foo", "Foobar"};
@@ -42,33 +44,21 @@ public void testParse() {
4244
assertThat(withDeprecations.match("foo bar"), is(false));
4345
for (String deprecatedName : deprecated) {
4446
assertThat(withDeprecations.match(deprecatedName), is(true));
47+
assertWarnings("Deprecated field [" + deprecatedName + "] used, expected [foo_bar] instead");
4548
}
4649
}
4750

48-
public void testAllDeprecated() {
51+
public void testAllDeprecated() throws IOException {
4952
String name = "like_text";
50-
51-
boolean withDeprecatedNames = randomBoolean();
5253
String[] deprecated = new String[]{"text", "same_as_text"};
53-
String[] allValues;
54-
if (withDeprecatedNames) {
55-
String[] newArray = new String[1 + deprecated.length];
56-
newArray[0] = name;
57-
System.arraycopy(deprecated, 0, newArray, 1, deprecated.length);
58-
allValues = newArray;
59-
} else {
60-
allValues = new String[] {name};
61-
}
62-
63-
ParseField field;
64-
if (withDeprecatedNames) {
65-
field = new ParseField(name).withDeprecation(deprecated).withAllDeprecated("like");
66-
} else {
67-
field = new ParseField(name).withAllDeprecated("like");
68-
}
69-
70-
assertThat(field.match(randomFrom(allValues)), is(true));
71-
assertThat(field.match("not a field name"), is(false));
54+
ParseField field = new ParseField(name).withDeprecation(deprecated).withAllDeprecated("like");
55+
assertFalse(field.match("not a field name"));
56+
assertTrue(field.match("text"));
57+
assertWarnings("Deprecated field [text] used, replaced by [like]");
58+
assertTrue(field.match("same_as_text"));
59+
assertWarnings("Deprecated field [same_as_text] used, replaced by [like]");
60+
assertTrue(field.match("like_text"));
61+
assertWarnings("Deprecated field [like_text] used, replaced by [like]");
7262
}
7363

7464
public void testGetAllNamesIncludedDeprecated() {

core/src/test/java/org/elasticsearch/common/logging/DeprecationLoggerTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class DeprecationLoggerTests extends ESTestCase {
4141

4242
private final DeprecationLogger logger = new DeprecationLogger(Loggers.getLogger(getClass()));
4343

44+
@Override
45+
protected boolean enableWarningsCheck() {
46+
//this is a low level test for the deprecation logger, setup and checks are done manually
47+
return false;
48+
}
49+
4450
public void testAddsHeaderWithThreadContext() throws IOException {
4551
String msg = "A simple message [{}]";
4652
String param = randomAsciiOfLengthBetween(1, 5);
@@ -54,7 +60,7 @@ public void testAddsHeaderWithThreadContext() throws IOException {
5460
Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();
5561

5662
assertEquals(1, responseHeaders.size());
57-
assertEquals(formatted, responseHeaders.get(DeprecationLogger.DEPRECATION_HEADER).get(0));
63+
assertEquals(formatted, responseHeaders.get(DeprecationLogger.WARNING_HEADER).get(0));
5864
}
5965
}
6066

@@ -74,7 +80,7 @@ public void testAddsCombinedHeaderWithThreadContext() throws IOException {
7480

7581
assertEquals(1, responseHeaders.size());
7682

77-
List<String> responses = responseHeaders.get(DeprecationLogger.DEPRECATION_HEADER);
83+
List<String> responses = responseHeaders.get(DeprecationLogger.WARNING_HEADER);
7884

7985
assertEquals(2, responses.size());
8086
assertEquals(formatted, responses.get(0));
@@ -93,7 +99,7 @@ public void testCanRemoveThreadContext() throws IOException {
9399
logger.deprecated(expected);
94100

95101
Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();
96-
List<String> responses = responseHeaders.get(DeprecationLogger.DEPRECATION_HEADER);
102+
List<String> responses = responseHeaders.get(DeprecationLogger.WARNING_HEADER);
97103

98104
// ensure it works (note: concurrent tests may be adding to it, but in different threads, so it should have no impact)
99105
assertThat(responses, hasSize(atLeast(1)));
@@ -104,7 +110,7 @@ public void testCanRemoveThreadContext() throws IOException {
104110
logger.deprecated(unexpected);
105111

106112
responseHeaders = threadContext.getResponseHeaders();
107-
responses = responseHeaders.get(DeprecationLogger.DEPRECATION_HEADER);
113+
responses = responseHeaders.get(DeprecationLogger.WARNING_HEADER);
108114

109115
assertThat(responses, hasSize(atLeast(1)));
110116
assertThat(responses, hasItem(expected));

core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
import org.elasticsearch.common.ParseFieldMatcher;
2323
import org.elasticsearch.common.ParseFieldMatcherSupplier;
2424
import org.elasticsearch.common.ParsingException;
25-
import org.elasticsearch.common.logging.DeprecationLogger;
26-
import org.elasticsearch.common.settings.Settings;
27-
import org.elasticsearch.common.util.concurrent.ThreadContext;
2825
import org.elasticsearch.common.xcontent.AbstractObjectParser.NoContextParser;
2926
import org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser;
3027
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
@@ -38,8 +35,6 @@
3835
import java.util.Arrays;
3936
import java.util.List;
4037

41-
import static org.hamcrest.Matchers.equalTo;
42-
import static org.hamcrest.Matchers.hasItem;
4338
import static org.hamcrest.Matchers.hasSize;
4439

4540
public class ObjectParserTests extends ESTestCase {
@@ -224,26 +219,16 @@ public void setTest(int test) {
224219
}
225220

226221
public void testDeprecationWarnings() throws IOException {
227-
try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
228-
DeprecationLogger.setThreadContext(threadContext);
229-
class TestStruct {
230-
public String test;
231-
}
232-
ObjectParser<TestStruct, ParseFieldMatcherSupplier> objectParser = new ObjectParser<>("foo");
233-
TestStruct s = new TestStruct();
234-
235-
objectParser.declareField((i, v, c) -> v.test = i.text(), new ParseField("test", "old_test"), ObjectParser.ValueType.STRING);
236-
237-
XContentParser parser = createParser(XContentType.JSON.xContent(), "{\"old_test\" : \"foo\"}");
238-
objectParser.parse(parser, s, () -> ParseFieldMatcher.EMPTY);
239-
240-
assertEquals("foo", s.test);
241-
242-
final List<String> warnings = threadContext.getResponseHeaders().get(DeprecationLogger.DEPRECATION_HEADER);
243-
assertThat(warnings, hasSize(1));
244-
assertThat(warnings, hasItem(equalTo("Deprecated field [old_test] used, expected [test] instead")));
245-
DeprecationLogger.removeThreadContext(threadContext);
222+
class TestStruct {
223+
public String test;
246224
}
225+
ObjectParser<TestStruct, ParseFieldMatcherSupplier> objectParser = new ObjectParser<>("foo");
226+
TestStruct s = new TestStruct();
227+
XContentParser parser = createParser(XContentType.JSON.xContent(), "{\"old_test\" : \"foo\"}");
228+
objectParser.declareField((i, v, c) -> v.test = i.text(), new ParseField("test", "old_test"), ObjectParser.ValueType.STRING);
229+
objectParser.parse(parser, s, () -> ParseFieldMatcher.EMPTY);
230+
assertEquals("foo", s.test);
231+
assertWarnings("Deprecated field [old_test] used, expected [test] instead");
247232
}
248233

249234
public void testFailOnValueType() throws IOException {

core/src/test/java/org/elasticsearch/index/analysis/AnalysisRegistryTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void testOverrideDefaultIndexAnalyzerIsUnsupported() {
104104
assertTrue(e.getMessage().contains("[index.analysis.analyzer.default_index] is not supported"));
105105
}
106106

107-
public void testBackCompatOverrideDefaultIndexAnalyzer() {
107+
public void testBackCompatOverrideDefaultIndexAnalyzer() throws IOException {
108108
Version version = VersionUtils.randomVersionBetween(random(), VersionUtils.getFirstVersion(),
109109
VersionUtils.getPreviousVersion(Version.V_5_0_0_alpha1));
110110
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
@@ -113,6 +113,8 @@ public void testBackCompatOverrideDefaultIndexAnalyzer() {
113113
assertThat(indexAnalyzers.getDefaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
114114
assertThat(indexAnalyzers.getDefaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
115115
assertThat(indexAnalyzers.getDefaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
116+
assertWarnings("setting [index.analysis.analyzer.default_index] is deprecated, use [index.analysis.analyzer.default] " +
117+
"instead for index [index]");
116118
}
117119

118120
public void testOverrideDefaultSearchAnalyzer() {
@@ -125,7 +127,7 @@ public void testOverrideDefaultSearchAnalyzer() {
125127
assertThat(indexAnalyzers.getDefaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
126128
}
127129

128-
public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() {
130+
public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() throws IOException {
129131
Version version = VersionUtils.randomVersionBetween(random(), VersionUtils.getFirstVersion(),
130132
VersionUtils.getPreviousVersion(Version.V_5_0_0_alpha1));
131133
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
@@ -137,6 +139,8 @@ public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() {
137139
assertThat(indexAnalyzers.getDefaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
138140
assertThat(indexAnalyzers.getDefaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
139141
assertThat(indexAnalyzers.getDefaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
142+
assertWarnings("setting [index.analysis.analyzer.default_index] is deprecated, use [index.analysis.analyzer.default] " +
143+
"instead for index [index]");
140144
}
141145

142146
public void testConfigureCamelCaseTokenFilter() throws IOException {

core/src/test/java/org/elasticsearch/index/mapper/DynamicTemplateTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.index.mapper.DynamicTemplate.XContentFieldType;
2727
import org.elasticsearch.test.ESTestCase;
2828

29+
import java.io.IOException;
2930
import java.util.Collections;
3031
import java.util.HashMap;
3132
import java.util.Map;
@@ -43,7 +44,7 @@ public void testParseUnknownParam() throws Exception {
4344
assertEquals("Illegal dynamic template parameter: [random_param]", e.getMessage());
4445
}
4546

46-
public void testParseUnknownMatchType() {
47+
public void testParseUnknownMatchType() throws IOException {
4748
Map<String, Object> templateDef = new HashMap<>();
4849
templateDef.put("match_mapping_type", "short");
4950
templateDef.put("mapping", Collections.singletonMap("store", true));

core/src/test/java/org/elasticsearch/index/mapper/MapperServiceTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ public void testMappingDepthExceedsLimit() throws Throwable {
160160
assertThat(e.getMessage(), containsString("Limit of mapping depth [1] in index [test1] has been exceeded"));
161161
}
162162

163-
public void testUnmappedFieldType() {
163+
public void testUnmappedFieldType() throws IOException {
164164
MapperService mapperService = createIndex("index").mapperService();
165165
assertThat(mapperService.unmappedFieldType("keyword"), instanceOf(KeywordFieldType.class));
166166
assertThat(mapperService.unmappedFieldType("long"), instanceOf(NumberFieldType.class));
167167
// back compat
168168
assertThat(mapperService.unmappedFieldType("string"), instanceOf(KeywordFieldType.class));
169+
assertWarnings("[unmapped_type:string] should be replaced with [unmapped_type:keyword]");
169170
}
170171

171172
public void testMergeWithMap() throws Throwable {
@@ -206,9 +207,7 @@ public void testOtherDocumentMappersOnlyUpdatedWhenChangingFieldType() throws IO
206207
.startObject("properties")
207208
.startObject("field")
208209
.field("type", "text")
209-
.startObject("norms")
210-
.field("enabled", false)
211-
.endObject()
210+
.field("norms", false)
212211
.endObject()
213212
.endObject().endObject().bytes());
214213

core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public void testFromJsonCoerceIsDeprecated() throws IOException {
422422
"}";
423423

424424
parseQuery(json);
425-
assertWarningHeaders("Deprecated field [coerce] used, replaced by [validation_method]");
425+
assertWarnings("Deprecated field [coerce] used, replaced by [validation_method]");
426426
}
427427

428428
public void testFromJsonIgnoreMalformedIsDeprecated() throws IOException {
@@ -440,7 +440,7 @@ public void testFromJsonIgnoreMalformedIsDeprecated() throws IOException {
440440
" }\n" +
441441
"}";
442442
parseQuery(json);
443-
assertWarningHeaders("Deprecated field [ignore_malformed] used, replaced by [validation_method]");
443+
assertWarnings("Deprecated field [ignore_malformed] used, replaced by [validation_method]");
444444
}
445445

446446
@Override

core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public void testOptimizeBboxIsDeprecated() throws IOException {
330330
" }\n" +
331331
"}";
332332
parseQuery(json);
333-
assertWarningHeaders("Deprecated field [optimize_bbox] used, replaced by [no replacement: " +
333+
assertWarnings("Deprecated field [optimize_bbox] used, replaced by [no replacement: " +
334334
"`optimize_bbox` is no longer supported due to recent improvements]");
335335
}
336336

@@ -347,7 +347,7 @@ public void testFromCoerceIsDeprecated() throws IOException {
347347
" }\n" +
348348
"}";
349349
parseQuery(json);
350-
assertWarningHeaders("Deprecated field [coerce] used, replaced by [validation_method]");
350+
assertWarnings("Deprecated field [coerce] used, replaced by [validation_method]");
351351
}
352352

353353
public void testFromJsonIgnoreMalformedIsDeprecated() throws IOException {
@@ -363,7 +363,7 @@ public void testFromJsonIgnoreMalformedIsDeprecated() throws IOException {
363363
" }\n" +
364364
"}";
365365
parseQuery(json);
366-
assertWarningHeaders("Deprecated field [ignore_malformed] used, replaced by [validation_method]");
366+
assertWarnings("Deprecated field [ignore_malformed] used, replaced by [validation_method]");
367367
}
368368

369369
@Override

core/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void testDeprecatedXContent() throws IOException {
140140
builder.endObject();
141141
builder.endObject();
142142
parseQuery(builder.string());
143-
assertWarningHeaders("Deprecated field [normalize] used, replaced by [validation_method]");
143+
assertWarnings("Deprecated field [normalize] used, replaced by [validation_method]");
144144
}
145145

146146
public void testParsingAndToQueryParsingExceptions() throws IOException {
@@ -266,7 +266,7 @@ public void testFromJsonIgnoreMalformedDeprecated() throws IOException {
266266
" }\n" +
267267
"}";
268268
parseQuery(json);
269-
assertWarningHeaders("Deprecated field [ignore_malformed] used, replaced by [validation_method]");
269+
assertWarnings("Deprecated field [ignore_malformed] used, replaced by [validation_method]");
270270
}
271271

272272
public void testFromJsonCoerceDeprecated() throws IOException {
@@ -282,7 +282,7 @@ public void testFromJsonCoerceDeprecated() throws IOException {
282282
" }\n" +
283283
"}";
284284
parseQuery(json);
285-
assertWarningHeaders("Deprecated field [coerce] used, replaced by [validation_method]");
285+
assertWarnings("Deprecated field [coerce] used, replaced by [validation_method]");
286286
}
287287

288288
@Override

core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.lucene.search.Query;
2424
import org.apache.lucene.search.join.ScoreMode;
2525
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
26-
import org.elasticsearch.common.ParseFieldMatcher;
2726
import org.elasticsearch.common.compress.CompressedXContent;
2827
import org.elasticsearch.common.xcontent.ToXContent;
2928
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -151,7 +150,7 @@ public void testDeprecatedXContent() throws IOException {
151150
builder.endObject();
152151
HasParentQueryBuilder queryBuilder = (HasParentQueryBuilder) parseQuery(builder.string());
153152
assertEquals("foo", queryBuilder.type());
154-
assertWarningHeaders("Deprecated field [type] used, expected [parent_type] instead");
153+
assertWarnings("Deprecated field [type] used, expected [parent_type] instead");
155154
}
156155

157156
public void testToQueryInnerQueryType() throws IOException {

core/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void testFromJsonDeprecatedSyntax() throws IOException {
165165
assertEquals(testQuery, parsed);
166166

167167
parseQuery(contentString);
168-
assertWarningHeaders("Deprecated field [_type] used, expected [type] instead");
168+
assertWarnings("Deprecated field [_type] used, expected [type] instead");
169169

170170
//array of types can also be called types rather than type
171171
final String contentString2 = "{\n" +
@@ -178,6 +178,6 @@ public void testFromJsonDeprecatedSyntax() throws IOException {
178178
assertEquals(testQuery, parsed);
179179

180180
parseQuery(contentString2);
181-
assertWarningHeaders("Deprecated field [types] used, expected [type] instead");
181+
assertWarnings("Deprecated field [types] used, expected [type] instead");
182182
}
183183
}

0 commit comments

Comments
 (0)