Skip to content

Commit

Permalink
Refactor rolling upgrade tests to make it easier to customize (#108393)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed May 8, 2024
1 parent e2c19f2 commit 2f94aee
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.upgrades;

import com.carrotsearch.randomizedtesting.annotations.Name;

import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.FeatureFlag;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
import org.junit.ClassRule;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;

import java.util.function.Supplier;

public abstract class AbstractRollingUpgradeTestCase extends ParameterizedRollingUpgradeTestCase {

private static final TemporaryFolder repoDirectory = new TemporaryFolder();

private static final ElasticsearchCluster cluster = ElasticsearchCluster.local()
.distribution(DistributionType.DEFAULT)
.version(getOldClusterTestVersion())
.nodes(NODE_NUM)
.setting("path.repo", new Supplier<>() {
@Override
@SuppressForbidden(reason = "TemporaryFolder only has io.File methods, not nio.File")
public String get() {
return repoDirectory.getRoot().getPath();
}
})
.setting("xpack.security.enabled", "false")
.feature(FeatureFlag.TIME_SERIES_MODE)
.build();

@ClassRule
public static TestRule ruleChain = RuleChain.outerRule(repoDirectory).around(cluster);

protected AbstractRollingUpgradeTestCase(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
}

@Override
protected ElasticsearchCluster getUpgradeCluster() {
return cluster;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;

public class ClusterFeatureMigrationIT extends ParameterizedRollingUpgradeTestCase {
public class ClusterFeatureMigrationIT extends AbstractRollingUpgradeTestCase {

@Before
public void checkMigrationVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;

public class DesiredNodesUpgradeIT extends ParameterizedRollingUpgradeTestCase {
public class DesiredNodesUpgradeIT extends AbstractRollingUpgradeTestCase {

private final int desiredNodesVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import static org.hamcrest.Matchers.equalTo;

public class DownsampleIT extends ParameterizedRollingUpgradeTestCase {
public class DownsampleIT extends AbstractRollingUpgradeTestCase {

private static final String FIXED_INTERVAL = "1h";
private String index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

public class FeatureUpgradeIT extends ParameterizedRollingUpgradeTestCase {
public class FeatureUpgradeIT extends AbstractRollingUpgradeTestCase {

public FeatureUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* the co-ordinating node if older nodes were included in the system
*/
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/103473")
public class FieldCapsIT extends ParameterizedRollingUpgradeTestCase {
public class FieldCapsIT extends AbstractRollingUpgradeTestCase {

public FieldCapsIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import static org.hamcrest.CoreMatchers.equalTo;

public class HealthNodeUpgradeIT extends ParameterizedRollingUpgradeTestCase {
public class HealthNodeUpgradeIT extends AbstractRollingUpgradeTestCase {

public HealthNodeUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Locale;
import java.util.Map;

public class IgnoredMetaFieldRollingUpgradeIT extends ParameterizedRollingUpgradeTestCase {
public class IgnoredMetaFieldRollingUpgradeIT extends AbstractRollingUpgradeTestCase {

private static final String TERMS_AGG_QUERY = Strings.format("""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* xpack rolling restart tests. We should work on a way to remove this
* duplication but for now we have no real way to share code.
*/
public class IndexingIT extends ParameterizedRollingUpgradeTestCase {
public class IndexingIT extends AbstractRollingUpgradeTestCase {

public IndexingIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,45 @@
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.features.NodeFeature;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.FeatureFlag;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
import org.elasticsearch.test.cluster.util.Version;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.test.rest.TestFeatureService;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.IntStream;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

public abstract class ParameterizedRollingUpgradeTestCase extends ESRestTestCase {
protected static final int NODE_NUM = 3;
private static final String OLD_CLUSTER_VERSION = System.getProperty("tests.old_cluster_version");

private static final TemporaryFolder repoDirectory = new TemporaryFolder();

private static final int NODE_NUM = 3;

private static final ElasticsearchCluster cluster = ElasticsearchCluster.local()
.distribution(DistributionType.DEFAULT)
.version(getOldClusterTestVersion())
.nodes(NODE_NUM)
.setting("path.repo", new Supplier<>() {
@Override
@SuppressForbidden(reason = "TemporaryFolder only has io.File methods, not nio.File")
public String get() {
return repoDirectory.getRoot().getPath();
}
})
.setting("xpack.security.enabled", "false")
.feature(FeatureFlag.TIME_SERIES_MODE)
.build();

@ClassRule
public static TestRule ruleChain = RuleChain.outerRule(repoDirectory).around(cluster);

@ParametersFactory(shuffle = false)
public static Iterable<Object[]> parameters() {
return IntStream.rangeClosed(0, NODE_NUM).boxed().map(n -> new Object[] { n }).toList();
}

private static final Set<Integer> upgradedNodes = new HashSet<>();
private static TestFeatureService oldClusterTestFeatureService = null;
private static boolean upgradeFailed = false;
private static IndexVersion oldIndexVersion;

private final int requestedUpgradedNodes;

protected ParameterizedRollingUpgradeTestCase(@Name("upgradedNodes") int upgradedNodes) {
this.requestedUpgradedNodes = upgradedNodes;
}

@ParametersFactory(shuffle = false)
public static Iterable<Object[]> parameters() {
return IntStream.rangeClosed(0, NODE_NUM).boxed().map(n -> new Object[] { n }).toList();
}

protected abstract ElasticsearchCluster getUpgradeCluster();

@Before
public void extractOldClusterFeatures() {
if (isOldCluster() && oldClusterTestFeatureService == null) {
Expand Down Expand Up @@ -135,7 +106,7 @@ public void upgradeNode() throws Exception {
if (upgradedNodes.add(n)) {
try {
logger.info("Upgrading node {} to version {}", n, Version.CURRENT);
cluster.upgradeNodeToVersion(n, Version.CURRENT);
getUpgradeCluster().upgradeNodeToVersion(n, Version.CURRENT);
} catch (Exception e) {
upgradeFailed = true;
throw e;
Expand Down Expand Up @@ -199,7 +170,7 @@ protected static boolean isUpgradedCluster() {

@Override
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
return getUpgradeCluster().getHttpAddresses();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.notNullValue;

public class SnapshotBasedRecoveryIT extends ParameterizedRollingUpgradeTestCase {
public class SnapshotBasedRecoveryIT extends AbstractRollingUpgradeTestCase {

public SnapshotBasedRecoveryIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class SystemIndicesUpgradeIT extends ParameterizedRollingUpgradeTestCase {
public class SystemIndicesUpgradeIT extends AbstractRollingUpgradeTestCase {

public SystemIndicesUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class TsdbIT extends ParameterizedRollingUpgradeTestCase {
public class TsdbIT extends AbstractRollingUpgradeTestCase {

public TsdbIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM;
import static org.hamcrest.Matchers.is;

public class UpgradeWithOldIndexSettingsIT extends ParameterizedRollingUpgradeTestCase {
public class UpgradeWithOldIndexSettingsIT extends AbstractRollingUpgradeTestCase {

public UpgradeWithOldIndexSettingsIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;

public class VectorSearchIT extends ParameterizedRollingUpgradeTestCase {
public class VectorSearchIT extends AbstractRollingUpgradeTestCase {
public VectorSearchIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Basic tests for simple xpack functionality that are only run if the
* cluster is the on the default distribution.
*/
public class XPackIT extends ParameterizedRollingUpgradeTestCase {
public class XPackIT extends AbstractRollingUpgradeTestCase {

public XPackIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.inference.TaskType;
import org.elasticsearch.test.http.MockWebServer;
import org.elasticsearch.upgrades.ParameterizedRollingUpgradeTestCase;
import org.elasticsearch.upgrades.AbstractRollingUpgradeTestCase;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.core.Strings.format;

public class InferenceUpgradeTestCase extends ParameterizedRollingUpgradeTestCase {
public class InferenceUpgradeTestCase extends AbstractRollingUpgradeTestCase {

public InferenceUpgradeTestCase(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
Expand Down

0 comments on commit 2f94aee

Please sign in to comment.