Skip to content

Commit

Permalink
Allow warnings about 'global' template in upgrade tests (#59242)
Browse files Browse the repository at this point in the history
These tests sometimes install a template so they can be compatible with older versions, but they run
amok of the occasionally installed "global" template which changes the default number of shards.

This commit adds `allowedWarnings` and allows these warnings to be present, but doesn't fail if they
are not (since the global template is only randomly installed).

Resolves #58807
Resolves #58258
  • Loading branch information
dakrone authored Jul 8, 2020
1 parent e50a033 commit bb1c53a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void testIndexing() throws IOException {
Request createTestIndex = new Request("PUT", "/test_index");
createTestIndex.setJsonEntity("{\"settings\": {\"index.number_of_replicas\": 0}}");
client().performRequest(createTestIndex);
allowedWarnings("index [test_index] matches multiple legacy templates [global, prevent-bwc-deprecation-template], " +
"composable templates will only match a single template");

String recoverQuickly = "{\"settings\": {\"index.unassigned.node_left.delayed_timeout\": \"100ms\"}}";
Request createIndexWithReplicas = new Request("PUT", "/index_with_replicas");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.elasticsearch.common.util.MockBigArrays;
import org.elasticsearch.common.util.MockPageCacheRecycler;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
Expand Down Expand Up @@ -430,6 +431,33 @@ protected final void assertWarnings(String... expectedWarnings) {
assertWarnings(true, expectedWarnings);
}

/**
* Allow the given warnings, but don't require their presence.
*/
protected final void allowedWarnings(String... allowedWarnings) {
if (enableWarningsCheck() == false) {
throw new IllegalStateException("unable to check warning headers if the test is not set to do so");
}
try {
final List<String> actualWarnings = threadContext.getResponseHeaders().get("Warning");
if (actualWarnings == null) {
return;
}
final Set<String> actualWarningValues =
actualWarnings.stream()
.map(s -> DeprecationLogger.extractWarningValueFromWarningHeader(s, true))
.map(DeprecationLogger::escapeAndEncode)
.collect(Collectors.toSet());
Set<String> expectedWarnings = new HashSet<>(Arrays.asList(allowedWarnings));
final Set<String> warningsNotExpected = Sets.difference(actualWarningValues, expectedWarnings);
assertThat("Found " + warningsNotExpected.size() + " unexpected warnings\nExpected: "
+ expectedWarnings + "\nActual: " + actualWarningValues,
warningsNotExpected.size(), equalTo(0));
} finally {
resetDeprecationLogger();
}
}

protected final void assertWarnings(boolean stripXContentPosition, String... expectedWarnings) {
if (enableWarningsCheck() == false) {
throw new IllegalStateException("unable to check warning headers if the test is not set to do so");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public void testIndexing() throws IOException {
Request createTestIndex = new Request("PUT", "/test_index");
createTestIndex.setJsonEntity("{\"settings\": {\"index.number_of_replicas\": 0}}");
client().performRequest(createTestIndex);
allowedWarnings("index [test_index] matches multiple legacy templates [global, xpack-prevent-bwc-deprecation-template], " +
"composable templates will only match a single template");

String recoverQuickly = "{\"settings\": {\"index.unassigned.node_left.delayed_timeout\": \"100ms\"}}";
Request createIndexWithReplicas = new Request("PUT", "/index_with_replicas");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public void testGeneratingTokensInOldCluster() throws Exception {
assertNotNull(refreshToken);

storeTokens(client(), 1, accessToken, refreshToken);
allowedWarnings("index [token_backwards_compatibility_it] matches multiple legacy templates " +
"[gen-tokens-old-cluster-template, global], composable templates will only match a single template");

responseMap = createTokens(client(), "test_user", "x-pack-test-password");
accessToken = (String) responseMap.get("access_token");
Expand Down Expand Up @@ -175,6 +177,8 @@ public void testInvalidatingTokensInOldCluster() throws Exception {
assertNotNull(refreshToken);

storeTokens(client(), 5, accessToken, refreshToken);
allowedWarnings("index [token_backwards_compatibility_it] matches multiple legacy templates " +
"[global, invalid-tokens-old-cluster-template], composable templates will only match a single template");

// invalidate access token
invalidateAccessToken(client(), accessToken);
Expand Down

0 comments on commit bb1c53a

Please sign in to comment.