Skip to content

Commit

Permalink
Add full cluster restart base class (#33577)
Browse files Browse the repository at this point in the history
This commit adds a base class for full cluster restart tests.
  • Loading branch information
jasontedor authored Sep 11, 2018
1 parent 6075e15 commit ea3fdc9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@
* version is started with the same data directories and then this is rerun
* with {@code tests.is_old_cluster} set to {@code false}.
*/
public class FullClusterRestartIT extends ESRestTestCase {
private final boolean runningAgainstOldCluster = Booleans.parseBoolean(System.getProperty("tests.is_old_cluster"));
private final Version oldClusterVersion = Version.fromString(System.getProperty("tests.old_cluster_version"));
private final boolean supportsLenientBooleans = oldClusterVersion.before(Version.V_6_0_0_alpha1);
public class FullClusterRestartIT extends AbstractFullClusterRestartTestCase {
private final boolean supportsLenientBooleans = getOldClusterVersion().before(Version.V_6_0_0_alpha1);
private static final Version VERSION_5_1_0_UNRELEASED = Version.fromString("5.1.0");

private String index;
Expand All @@ -81,29 +79,9 @@ public void setIndex() {
index = getTestName().toLowerCase(Locale.ROOT);
}

@Override
protected boolean preserveIndicesUponCompletion() {
return true;
}

@Override
protected boolean preserveSnapshotsUponCompletion() {
return true;
}

@Override
protected boolean preserveReposUponCompletion() {
return true;
}

@Override
protected boolean preserveTemplatesUponCompletion() {
return true;
}

public void testSearch() throws Exception {
int count;
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -169,7 +147,7 @@ public void testSearch() throws Exception {
}

public void testNewReplicasWork() throws Exception {
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -237,10 +215,10 @@ public void testNewReplicasWork() throws Exception {
*/
public void testAliasWithBadName() throws Exception {
assumeTrue("Can only test bad alias name if old cluster is on 5.1.0 or before",
oldClusterVersion.before(VERSION_5_1_0_UNRELEASED));
getOldClusterVersion().before(VERSION_5_1_0_UNRELEASED));

int count;
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -291,7 +269,7 @@ public void testAliasWithBadName() throws Exception {
Map<String, Object> searchRsp = entityAsMap(client().performRequest(new Request("GET", "/" + aliasName + "/_search")));
int totalHits = (int) XContentMapValues.extractValue("hits.total", searchRsp);
assertEquals(count, totalHits);
if (runningAgainstOldCluster == false) {
if (isRunningAgainstOldCluster() == false) {
// We can remove the alias.
Response response = client().performRequest(new Request("DELETE", "/" + index + "/_alias/" + aliasName));
assertEquals(200, response.getStatusLine().getStatusCode());
Expand All @@ -302,7 +280,7 @@ public void testAliasWithBadName() throws Exception {
}

public void testClusterState() throws Exception {
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
mappingsAndSettings.field("template", index);
Expand Down Expand Up @@ -341,14 +319,14 @@ public void testClusterState() throws Exception {
assertEquals("0", numberOfReplicas);
Version version = Version.fromId(Integer.valueOf((String) XContentMapValues.extractValue("metadata.indices." + index +
".settings.index.version.created", clusterState)));
assertEquals(oldClusterVersion, version);
assertEquals(getOldClusterVersion(), version);

}

public void testShrink() throws IOException {
String shrunkenIndex = index + "_shrunk";
int numDocs;
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -413,7 +391,7 @@ public void testShrink() throws IOException {
public void testShrinkAfterUpgrade() throws IOException {
String shrunkenIndex = index + "_shrunk";
int numDocs;
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -465,7 +443,7 @@ public void testShrinkAfterUpgrade() throws IOException {
int totalHits = (int) XContentMapValues.extractValue("hits.total", response);
assertEquals(numDocs, totalHits);

if (runningAgainstOldCluster == false) {
if (isRunningAgainstOldCluster() == false) {
response = entityAsMap(client().performRequest(new Request("GET", "/" + shrunkenIndex + "/_search")));
assertNoFailures(response);
totalShards = (int) XContentMapValues.extractValue("_shards.total", response);
Expand All @@ -490,7 +468,7 @@ public void testShrinkAfterUpgrade() throws IOException {
* </ol>
*/
public void testRollover() throws IOException {
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
Request createIndex = new Request("PUT", "/" + index + "-000001");
createIndex.setJsonEntity("{"
+ " \"aliases\": {"
Expand All @@ -511,7 +489,7 @@ public void testRollover() throws IOException {
bulkRequest.addParameter("refresh", "");
assertThat(EntityUtils.toString(client().performRequest(bulkRequest).getEntity()), containsString("\"errors\":false"));

if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
Request rolloverRequest = new Request("POST", "/" + index + "_write/_rollover");
rolloverRequest.setJsonEntity("{"
+ " \"conditions\": {"
Expand All @@ -529,7 +507,7 @@ public void testRollover() throws IOException {
Map<String, Object> count = entityAsMap(client().performRequest(countRequest));
assertNoFailures(count);

int expectedCount = bulkCount + (runningAgainstOldCluster ? 0 : bulkCount);
int expectedCount = bulkCount + (isRunningAgainstOldCluster() ? 0 : bulkCount);
assertEquals(expectedCount, (int) XContentMapValues.extractValue("hits.total", count));
}

Expand Down Expand Up @@ -688,7 +666,7 @@ public void testSingleDoc() throws IOException {
String docLocation = "/" + index + "/doc/1";
String doc = "{\"test\": \"test\"}";

if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
Request createDoc = new Request("PUT", docLocation);
createDoc.setJsonEntity(doc);
client().performRequest(createDoc);
Expand All @@ -703,7 +681,7 @@ public void testSingleDoc() throws IOException {
public void testEmptyShard() throws IOException {
final String index = "test_empty_shard";

if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
Settings.Builder settings = Settings.builder()
.put(IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1)
.put(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1)
Expand All @@ -726,7 +704,7 @@ public void testEmptyShard() throws IOException {
public void testRecovery() throws Exception {
int count;
boolean shouldHaveTranslog;
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
count = between(200, 300);
/* We've had bugs in the past where we couldn't restore
* an index without a translog so we randomize whether
Expand Down Expand Up @@ -772,7 +750,7 @@ public void testRecovery() throws Exception {
String countResponse = toStr(client().performRequest(countRequest));
assertThat(countResponse, containsString("\"total\":" + count));

if (false == runningAgainstOldCluster) {
if (false == isRunningAgainstOldCluster()) {
boolean restoredFromTranslog = false;
boolean foundPrimary = false;
Request recoveryRequest = new Request("GET", "/_cat/recovery/" + index);
Expand Down Expand Up @@ -800,7 +778,7 @@ public void testRecovery() throws Exception {
assertEquals("mismatch while checking for translog recovery\n" + recoveryResponse, shouldHaveTranslog, restoredFromTranslog);

String currentLuceneVersion = Version.CURRENT.luceneVersion.toString();
String bwcLuceneVersion = oldClusterVersion.luceneVersion.toString();
String bwcLuceneVersion = getOldClusterVersion().luceneVersion.toString();
if (shouldHaveTranslog && false == currentLuceneVersion.equals(bwcLuceneVersion)) {
int numCurrentVersion = 0;
int numBwcVersion = 0;
Expand Down Expand Up @@ -840,7 +818,7 @@ public void testRecovery() throws Exception {
*/
public void testSnapshotRestore() throws IOException {
int count;
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
// Create the index
count = between(200, 300);
indexRandomDocuments(count, true, true, i -> jsonBuilder().startObject().field("field", "value").endObject());
Expand All @@ -860,7 +838,7 @@ public void testSnapshotRestore() throws IOException {
// Stick a routing attribute into to cluster settings so we can see it after the restore
Request addRoutingSettings = new Request("PUT", "/_cluster/settings");
addRoutingSettings.setJsonEntity(
"{\"persistent\": {\"cluster.routing.allocation.exclude.test_attr\": \"" + oldClusterVersion + "\"}}");
"{\"persistent\": {\"cluster.routing.allocation.exclude.test_attr\": \"" + getOldClusterVersion() + "\"}}");
client().performRequest(addRoutingSettings);

// Stick a template into the cluster so we can see it after the restore
Expand All @@ -885,7 +863,7 @@ public void testSnapshotRestore() throws IOException {
templateBuilder.startObject("alias2"); {
templateBuilder.startObject("filter"); {
templateBuilder.startObject("term"); {
templateBuilder.field("version", runningAgainstOldCluster ? oldClusterVersion : Version.CURRENT);
templateBuilder.field("version", isRunningAgainstOldCluster() ? getOldClusterVersion() : Version.CURRENT);
}
templateBuilder.endObject();
}
Expand All @@ -898,7 +876,7 @@ public void testSnapshotRestore() throws IOException {
createTemplateRequest.setJsonEntity(Strings.toString(templateBuilder));
client().performRequest(createTemplateRequest);

if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
// Create the repo
XContentBuilder repoConfig = JsonXContent.contentBuilder().startObject(); {
repoConfig.field("type", "fs");
Expand All @@ -914,19 +892,19 @@ public void testSnapshotRestore() throws IOException {
client().performRequest(createRepoRequest);
}

Request createSnapshot = new Request("PUT", "/_snapshot/repo/" + (runningAgainstOldCluster ? "old_snap" : "new_snap"));
Request createSnapshot = new Request("PUT", "/_snapshot/repo/" + (isRunningAgainstOldCluster() ? "old_snap" : "new_snap"));
createSnapshot.addParameter("wait_for_completion", "true");
createSnapshot.setJsonEntity("{\"indices\": \"" + index + "\"}");
client().performRequest(createSnapshot);

checkSnapshot("old_snap", count, oldClusterVersion);
if (false == runningAgainstOldCluster) {
checkSnapshot("old_snap", count, getOldClusterVersion());
if (false == isRunningAgainstOldCluster()) {
checkSnapshot("new_snap", count, Version.CURRENT);
}
}

public void testHistoryUUIDIsAdded() throws Exception {
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -1022,7 +1000,7 @@ private void checkSnapshot(String snapshotName, int count, Version tookOnVersion
Map<String, Object> expectedClusterSettings = new HashMap<>();
expectedClusterSettings.put("transient", emptyMap());
expectedClusterSettings.put("persistent",
singletonMap("cluster.routing.allocation.exclude.test_attr", oldClusterVersion.toString()));
singletonMap("cluster.routing.allocation.exclude.test_attr", getOldClusterVersion().toString()));
if (expectedClusterSettings.equals(clusterSettingsResponse) == false) {
NotEqualMessageBuilder builder = new NotEqualMessageBuilder();
builder.compareMaps(clusterSettingsResponse, expectedClusterSettings);
Expand All @@ -1032,7 +1010,7 @@ private void checkSnapshot(String snapshotName, int count, Version tookOnVersion
// Check that the template was restored successfully
Map<String, Object> getTemplateResponse = entityAsMap(client().performRequest(new Request("GET", "/_template/test_template")));
Map<String, Object> expectedTemplate = new HashMap<>();
if (runningAgainstOldCluster && oldClusterVersion.before(Version.V_6_0_0_beta1)) {
if (isRunningAgainstOldCluster() && getOldClusterVersion().before(Version.V_6_0_0_beta1)) {
expectedTemplate.put("template", "evil_*");
} else {
expectedTemplate.put("index_patterns", singletonList("evil_*"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
package org.elasticsearch.upgrades;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
Expand All @@ -48,7 +46,6 @@
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.test.rest.ESRestTestCase;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -71,7 +68,7 @@
* The queries to test are specified in json format, which turns out to work because we tend break here rarely. If the
* json format of a query being tested here then feel free to change this.
*/
public class QueryBuilderBWCIT extends ESRestTestCase {
public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase {

private static final List<Object[]> CANDIDATES = new ArrayList<>();

Expand Down Expand Up @@ -145,32 +142,9 @@ private static void addCandidate(String querySource, QueryBuilder expectedQb) {
CANDIDATES.add(new Object[]{"{\"query\": {" + querySource + "}}", expectedQb});
}

private final Version oldClusterVersion = Version.fromString(System.getProperty("tests.old_cluster_version"));
private final boolean runningAgainstOldCluster = Booleans.parseBoolean(System.getProperty("tests.is_old_cluster"));

@Override
protected boolean preserveIndicesUponCompletion() {
return true;
}

@Override
protected boolean preserveSnapshotsUponCompletion() {
return true;
}

@Override
protected boolean preserveReposUponCompletion() {
return true;
}

@Override
protected boolean preserveTemplatesUponCompletion() {
return true;
}

public void testQueryBuilderBWC() throws Exception {
String index = "queries";
if (runningAgainstOldCluster) {
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
Expand Down Expand Up @@ -230,7 +204,7 @@ public void testQueryBuilderBWC() throws Exception {
byte[] qbSource = Base64.getDecoder().decode(queryBuilderStr);
try (InputStream in = new ByteArrayInputStream(qbSource, 0, qbSource.length)) {
try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), registry)) {
input.setVersion(oldClusterVersion);
input.setVersion(getOldClusterVersion());
QueryBuilder queryBuilder = input.readNamedWriteable(QueryBuilder.class);
assert in.read() == -1;
assertEquals(expectedQueryBuilder, queryBuilder);
Expand Down
Loading

0 comments on commit ea3fdc9

Please sign in to comment.