Skip to content

Commit 269462f

Browse files
committed
QA: Test template creation during rolling restart (#30850)
Adds a test that we create the appropriate x-pack templates during the rolling restart from the pre-6.2 OSS-zip distribution to the new zip distribution that contains xpack. This is one way to answer the question "does xpack acting sanely during the rolling upgrade and after it?" It isn't as good as full exercising xpack but it is fairly simple and would have caught #30832. Relates to #30731
1 parent dea702f commit 269462f

File tree

2 files changed

+124
-20
lines changed

2 files changed

+124
-20
lines changed

qa/rolling-upgrade/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ for (Version version : bwcVersions.wireCompatible) {
7272
Task oldClusterTestRunner = tasks.getByName("${baseName}#oldClusterTestRunner")
7373
oldClusterTestRunner.configure {
7474
systemProperty 'tests.rest.suite', 'old_cluster'
75+
systemProperty 'tests.upgrade_from_version', version.toString().replace('-SNAPSHOT', '')
7576
}
7677

7778
Closure configureUpgradeCluster = {String name, Task lastRunner, int stopNode, Closure unicastSeed ->
@@ -96,6 +97,7 @@ for (Version version : bwcVersions.wireCompatible) {
9697
oneThirdUpgradedTestRunner.configure {
9798
systemProperty 'tests.rest.suite', 'mixed_cluster'
9899
systemProperty 'tests.first_round', 'true'
100+
systemProperty 'tests.upgrade_from_version', version.toString().replace('-SNAPSHOT', '')
99101
finalizedBy "${baseName}#oldClusterTestCluster#node1.stop"
100102
}
101103

@@ -108,6 +110,7 @@ for (Version version : bwcVersions.wireCompatible) {
108110
twoThirdsUpgradedTestRunner.configure {
109111
systemProperty 'tests.rest.suite', 'mixed_cluster'
110112
systemProperty 'tests.first_round', 'false'
113+
systemProperty 'tests.upgrade_from_version', version.toString().replace('-SNAPSHOT', '')
111114
finalizedBy "${baseName}#oldClusterTestCluster#node2.stop"
112115
}
113116

@@ -119,6 +122,7 @@ for (Version version : bwcVersions.wireCompatible) {
119122
Task upgradedClusterTestRunner = tasks.getByName("${baseName}#upgradedClusterTestRunner")
120123
upgradedClusterTestRunner.configure {
121124
systemProperty 'tests.rest.suite', 'upgraded_cluster'
125+
systemProperty 'tests.upgrade_from_version', version.toString().replace('-SNAPSHOT', '')
122126
/*
123127
* Force stopping all the upgraded nodes after the test runner
124128
* so they are alive during the test.

qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java

Lines changed: 120 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@
2222
import org.apache.http.entity.ContentType;
2323
import org.apache.http.nio.entity.NStringEntity;
2424
import org.apache.http.util.EntityUtils;
25+
import org.elasticsearch.common.xcontent.DeprecationHandler;
26+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
import org.elasticsearch.common.xcontent.json.JsonXContent;
2529
import org.junit.Before;
30+
import org.elasticsearch.Version;
2631

2732
import java.io.IOException;
33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
import java.util.List;
36+
import java.util.Map;
2837

2938
import static org.hamcrest.Matchers.equalTo;
39+
import static org.hamcrest.Matchers.hasSize;
3040
import static org.junit.Assume.assumeThat;
3141
import static java.util.Collections.emptyMap;
3242
import static java.util.Collections.singletonMap;
@@ -37,11 +47,9 @@
3747
*/
3848
public class XPackIT extends AbstractRollingTestCase {
3949
@Before
40-
public void skipIfNotXPack() {
50+
public void skipIfNotZip() {
4151
assumeThat("test is only supported if the distribution contains xpack",
4252
System.getProperty("tests.distribution"), equalTo("zip"));
43-
assumeThat("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway",
44-
CLUSTER_TYPE, equalTo(ClusterType.UPGRADED));
4553
/*
4654
* *Mostly* we want this for when we're upgrading from pre-6.3's
4755
* zip distribution which doesn't contain xpack to post 6.3's zip
@@ -51,17 +59,87 @@ public void skipIfNotXPack() {
5159
}
5260

5361
/**
54-
* Test a basic feature (SQL) which doesn't require any trial license.
55-
* Note that the test methods on this class can run in any order so we
56-
* <strong>might</strong> have already installed a trial license.
62+
* Tests that xpack is able to work itself into a sane state during the
63+
* upgrade by testing that it is able to create all of the templates that
64+
* it needs. This isn't a very strong assertion of sanity, but it is better
65+
* than nothing and should catch a few sad cases.
66+
* <p>
67+
* The trouble is that when xpack isn't able to create the templates that
68+
* it needs it retries over and over and over again. This can
69+
* <strong>really</strong> slow things down. This test asserts that xpack
70+
* was able to create the templates so it <strong>shouldn't</strong> be
71+
* spinning trying to create things and slowing down the rest of the
72+
* system.
73+
*/
74+
public void testIndexTemplatesCreated() throws Exception {
75+
Version upgradeFromVersion =
76+
Version.fromString(System.getProperty("tests.upgrade_from_version"));
77+
boolean upgradeFromVersionHasXPack = upgradeFromVersion.onOrAfter(Version.V_6_3_0);
78+
assumeFalse("this test doesn't really prove anything if the starting version has xpack and it is *much* more complex to maintain",
79+
upgradeFromVersionHasXPack);
80+
assumeFalse("since we're upgrading from a version without x-pack it won't have any templates",
81+
CLUSTER_TYPE == ClusterType.OLD);
82+
83+
List<String> expectedTemplates = new ArrayList<>();
84+
// Watcher creates its templates as soon as the first watcher node connects
85+
expectedTemplates.add(".triggered_watches");
86+
expectedTemplates.add(".watch-history-7");
87+
expectedTemplates.add(".watches");
88+
if (masterIsNewVersion()) {
89+
// Everything else waits until the master is upgraded to create its templates
90+
expectedTemplates.add(".ml-anomalies-");
91+
expectedTemplates.add(".ml-meta");
92+
expectedTemplates.add(".ml-notifications");
93+
expectedTemplates.add(".ml-state");
94+
expectedTemplates.add("logstash-index-template");
95+
expectedTemplates.add("security-index-template");
96+
expectedTemplates.add("security_audit_log");
97+
}
98+
Collections.sort(expectedTemplates);
99+
100+
/*
101+
* The index templates are created asynchronously after startup and
102+
* while this is usually fast we use assertBusy here just in case
103+
* they aren't created by the time this test is run.
104+
*/
105+
assertBusy(() -> {
106+
List<String> actualTemplates;
107+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(
108+
NamedXContentRegistry.EMPTY,
109+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
110+
client().performRequest("GET", "/_template").getEntity().getContent())) {
111+
actualTemplates = new ArrayList<>(parser.map().keySet());
112+
}
113+
Collections.sort(actualTemplates);
114+
/*
115+
* This test asserts that the templates match *exactly* to force
116+
* us to keep the list of templates up to date. Most templates
117+
* aren't likely to cause a problem on upgrade but it is better
118+
* to be safe and make sure they are all created than to be sorry
119+
* and miss a bug that causes one to be missed on upgrade.
120+
*
121+
* We sort the templates so the error message is easy to read.
122+
*/
123+
assertEquals(expectedTemplates, actualTemplates);
124+
});
125+
}
126+
127+
/**
128+
* Test a basic feature (SQL) after the upgrade which only requires the
129+
* "default" basic license. Note that the test methods on this class can
130+
* run in any order so we <strong>might</strong> have already installed a
131+
* trial license.
57132
*/
58-
public void testBasicFeature() throws IOException {
133+
public void testBasicFeatureAfterUpgrade() throws IOException {
134+
assumeThat("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway",
135+
CLUSTER_TYPE, equalTo(ClusterType.UPGRADED));
136+
59137
client().performRequest("POST", "/sql_test/doc/_bulk", singletonMap("refresh", "true"),
60-
new NStringEntity("{\"index\":{}}\n"
61-
+ "{\"f\": \"1\"}\n"
62-
+ "{\"index\":{}}\n"
63-
+ "{\"f\": \"2\"}\n",
64-
ContentType.APPLICATION_JSON));
138+
new NStringEntity("{\"index\":{}}\n"
139+
+ "{\"f\": \"1\"}\n"
140+
+ "{\"index\":{}}\n"
141+
+ "{\"f\": \"2\"}\n",
142+
ContentType.APPLICATION_JSON));
65143

66144
HttpEntity sql = new NStringEntity(
67145
"{\"query\": \"SELECT * FROM sql_test WHERE f > 1 ORDER BY f ASC\"}",
@@ -72,16 +150,20 @@ public void testBasicFeature() throws IOException {
72150
}
73151

74152
/**
75-
* Test creating a trial license and using it. This is interesting because
76-
* our other tests test cover starting a new cluster with the default
77-
* distribution and enabling the trial license but this test is the only
78-
* one that can upgrade from the oss distribution to the default
79-
* distribution with xpack and the create a trial license. We don't
80-
* <strong>do</strong> a lot with the trial license because for the most
81-
* part those things are tested elsewhere, off in xpack. But we do use the
82-
* trial license a little bit to make sure that it works.
153+
* Test creating a trial license after the upgrade and a feature (ML) that
154+
* requires the license. Our other tests test cover starting a new cluster
155+
* with the default distribution and enabling the trial license but this
156+
* test is the only one tests the rolling upgrade from the oss distribution
157+
* to the default distribution with xpack and then creating of a trial
158+
* license. We don't <strong>do</strong> a lot with the trial license
159+
* because for the most part those things are tested elsewhere, off in
160+
* xpack. But we do use the trial license a little bit to make sure that
161+
* creating it worked properly.
83162
*/
84163
public void testTrialLicense() throws IOException {
164+
assumeThat("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway",
165+
CLUSTER_TYPE, equalTo(ClusterType.UPGRADED));
166+
85167
client().performRequest("POST", "/_xpack/license/start_trial", singletonMap("acknowledge", "true"));
86168

87169
String noJobs = EntityUtils.toString(
@@ -106,4 +188,22 @@ public void testTrialLicense() throws IOException {
106188
+ "}\n", ContentType.APPLICATION_JSON);
107189
client().performRequest("PUT", "/_xpack/ml/anomaly_detectors/test_job", emptyMap(), createJob);
108190
}
191+
192+
/**
193+
* Has the master been upgraded to the new version?
194+
*/
195+
private boolean masterIsNewVersion() throws IOException {
196+
Map<?, ?> map;
197+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(
198+
NamedXContentRegistry.EMPTY,
199+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
200+
client().performRequest("GET", "/_nodes/_master").getEntity().getContent())) {
201+
map = parser.map();
202+
}
203+
map = (Map<?, ?>) map.get("nodes");
204+
assertThat(map.values(), hasSize(1));
205+
map = (Map<?, ?>) map.values().iterator().next();
206+
Version masterVersion = Version.fromString(map.get("version").toString());
207+
return Version.CURRENT.equals(masterVersion);
208+
}
109209
}

0 commit comments

Comments
 (0)