Skip to content

Commit 60097ac

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 c5bb169 commit 60097ac

File tree

43 files changed

+243
-272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+243
-272
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/main/java/org/elasticsearch/common/xcontent/NamedXContentRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public <T, C> T parseNamedObject(Class<T> categoryClass, String name, XContentPa
153153
if (entry == null) {
154154
throw new UnknownNamedObjectException(parser.getTokenLocation(), categoryClass, name);
155155
}
156-
if (false == entry.name.match(name, false)) {
156+
if (false == entry.name.match(name)) {
157157
/* Note that this shouldn't happen because we already looked up the entry using the names but we need to call `match` anyway
158158
* because it is responsible for logging deprecation warnings. */
159159
throw new ParsingException(parser.getTokenLocation(),

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,6 +42,7 @@ 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 {
@@ -53,6 +54,7 @@ public void testLogstashTemplatesBWC() throws Exception {
5354

5455
client().admin().indices().preparePutTemplate("logstash-5x").setSource(ls5x).get();
5556
client().prepareIndex("logstash-foo", "doc", "1").setSource("message", "foo").get();
57+
assertWarnings("Deprecated field [template] used, replaced by [index_patterns]");
5658
}
5759

5860
}

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;
@@ -49,7 +50,7 @@ public void testParseUnknownParam() throws Exception {
4950
assertEquals("{\"match_mapping_type\":\"string\",\"mapping\":{\"store\":true}}", builder.string());
5051
}
5152

52-
public void testParseUnknownMatchType() {
53+
public void testParseUnknownMatchType() throws IOException {
5354
Map<String, Object> templateDef = new HashMap<>();
5455
templateDef.put("match_mapping_type", "short");
5556
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/BoolQueryBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public void testFromJsonEmptyQueryBody() throws IOException {
344344
assertEquals(query, 0, queryBuilder.mustNot().size());
345345
assertEquals(query, 0, queryBuilder.should().size());
346346
// we should have deprecation warning headers regardless of throwing an exception
347-
assertWarningHeaders("query malformed, empty clause found at [1:27]",
347+
assertWarnings("query malformed, empty clause found at [1:27]",
348348
"query malformed, empty clause found at [1:46]",
349349
"query malformed, empty clause found at [1:100]");
350350
}

0 commit comments

Comments
 (0)