19
19
package org .elasticsearch .upgrades ;
20
20
21
21
import org .apache .http .util .EntityUtils ;
22
- import org .elasticsearch .common .Booleans ;
22
+ import org .elasticsearch .common .xcontent .DeprecationHandler ;
23
+ import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
24
+ import org .elasticsearch .common .xcontent .XContentParser ;
25
+ import org .elasticsearch .common .xcontent .json .JsonXContent ;
23
26
import org .junit .Before ;
27
+ import org .elasticsearch .Version ;
24
28
import org .elasticsearch .client .Request ;
25
- import org .elasticsearch .client .Response ;
26
29
27
30
import java .io .IOException ;
28
- import java .nio .charset .StandardCharsets ;
31
+ import java .util .ArrayList ;
32
+ import java .util .Collections ;
33
+ import java .util .List ;
34
+ import java .util .Map ;
29
35
30
36
import static org .hamcrest .Matchers .equalTo ;
37
+ import static org .hamcrest .Matchers .hasSize ;
31
38
import static org .junit .Assume .assumeThat ;
32
39
33
40
/**
36
43
*/
37
44
public class XPackIT extends AbstractRollingTestCase {
38
45
@ Before
39
- public void skipIfNotXPack () {
46
+ public void skipIfNotZip () {
40
47
assumeThat ("test is only supported if the distribution contains xpack" ,
41
48
System .getProperty ("tests.distribution" ), equalTo ("zip" ));
42
- assumeThat ("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway" ,
43
- CLUSTER_TYPE , equalTo (ClusterType .UPGRADED ));
44
49
/*
45
50
* *Mostly* we want this for when we're upgrading from pre-6.3's
46
51
* zip distribution which doesn't contain xpack to post 6.3's zip
@@ -50,11 +55,81 @@ public void skipIfNotXPack() {
50
55
}
51
56
52
57
/**
53
- * Test a basic feature (SQL) which doesn't require any trial license.
54
- * Note that the test methods on this class can run in any order so we
55
- * <strong>might</strong> have already installed a trial license.
58
+ * Tests that xpack is able to work itself into a sane state during the
59
+ * upgrade by testing that it is able to create all of the templates that
60
+ * it needs. This isn't a very strong assertion of sanity, but it is better
61
+ * than nothing and should catch a few sad cases.
62
+ * <p>
63
+ * The trouble is that when xpack isn't able to create the templates that
64
+ * it needs it retries over and over and over again. This can
65
+ * <strong>really</strong> slow things down. This test asserts that xpack
66
+ * was able to create the templates so it <strong>shouldn't</strong> be
67
+ * spinning trying to create things and slowing down the rest of the
68
+ * system.
69
+ */
70
+ public void testIndexTemplatesCreated () throws Exception {
71
+ Version upgradeFromVersion =
72
+ Version .fromString (System .getProperty ("tests.upgrade_from_version" ));
73
+ boolean upgradeFromVersionHasXPack = upgradeFromVersion .onOrAfter (Version .V_6_3_0 );
74
+ assumeFalse ("this test doesn't really prove anything if the starting version has xpack and it is *much* more complex to maintain" ,
75
+ upgradeFromVersionHasXPack );
76
+ assumeFalse ("since we're upgrading from a version without x-pack it won't have any templates" ,
77
+ CLUSTER_TYPE == ClusterType .OLD );
78
+
79
+ List <String > expectedTemplates = new ArrayList <>();
80
+ // Watcher creates its templates as soon as the first watcher node connects
81
+ expectedTemplates .add (".triggered_watches" );
82
+ expectedTemplates .add (".watch-history-8" );
83
+ expectedTemplates .add (".watches" );
84
+ if (masterIsNewVersion ()) {
85
+ // Everything else waits until the master is upgraded to create its templates
86
+ expectedTemplates .add (".ml-anomalies-" );
87
+ expectedTemplates .add (".ml-meta" );
88
+ expectedTemplates .add (".ml-notifications" );
89
+ expectedTemplates .add (".ml-state" );
90
+ expectedTemplates .add ("logstash-index-template" );
91
+ expectedTemplates .add ("security-index-template" );
92
+ expectedTemplates .add ("security_audit_log" );
93
+ }
94
+ Collections .sort (expectedTemplates );
95
+
96
+ /*
97
+ * The index templates are created asynchronously after startup and
98
+ * while this is usually fast we use assertBusy here just in case
99
+ * they aren't created by the time this test is run.
100
+ */
101
+ assertBusy (() -> {
102
+ List <String > actualTemplates ;
103
+ try (XContentParser parser = JsonXContent .jsonXContent .createParser (
104
+ NamedXContentRegistry .EMPTY ,
105
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
106
+ client ().performRequest (new Request ("GET" , "/_template" )).getEntity ().getContent ())) {
107
+ actualTemplates = new ArrayList <>(parser .map ().keySet ());
108
+ }
109
+ Collections .sort (actualTemplates );
110
+ /*
111
+ * This test asserts that the templates match *exactly* to force
112
+ * us to keep the list of templates up to date. Most templates
113
+ * aren't likely to cause a problem on upgrade but it is better
114
+ * to be safe and make sure they are all created than to be sorry
115
+ * and miss a bug that causes one to be missed on upgrade.
116
+ *
117
+ * We sort the templates so the error message is easy to read.
118
+ */
119
+ assertEquals (expectedTemplates , actualTemplates );
120
+ });
121
+ }
122
+
123
+ /**
124
+ * Test a basic feature (SQL) after the upgrade which only requires the
125
+ * "default" basic license. Note that the test methods on this class can
126
+ * run in any order so we <strong>might</strong> have already installed a
127
+ * trial license.
56
128
*/
57
- public void testBasicFeature () throws IOException {
129
+ public void testBasicFeatureAfterUpgrade () throws IOException {
130
+ assumeThat ("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway" ,
131
+ CLUSTER_TYPE , equalTo (ClusterType .UPGRADED ));
132
+
58
133
Request bulk = new Request ("POST" , "/sql_test/doc/_bulk" );
59
134
bulk .setJsonEntity (
60
135
"{\" index\" :{}}\n "
@@ -71,16 +146,20 @@ public void testBasicFeature() throws IOException {
71
146
}
72
147
73
148
/**
74
- * Test creating a trial license and using it. This is interesting because
75
- * our other tests test cover starting a new cluster with the default
76
- * distribution and enabling the trial license but this test is the only
77
- * one that can upgrade from the oss distribution to the default
78
- * distribution with xpack and the create a trial license. We don't
79
- * <strong>do</strong> a lot with the trial license because for the most
80
- * part those things are tested elsewhere, off in xpack. But we do use the
81
- * trial license a little bit to make sure that it works.
149
+ * Test creating a trial license after the upgrade and a feature (ML) that
150
+ * requires the license. Our other tests test cover starting a new cluster
151
+ * with the default distribution and enabling the trial license but this
152
+ * test is the only one tests the rolling upgrade from the oss distribution
153
+ * to the default distribution with xpack and then creating of a trial
154
+ * license. We don't <strong>do</strong> a lot with the trial license
155
+ * because for the most part those things are tested elsewhere, off in
156
+ * xpack. But we do use the trial license a little bit to make sure that
157
+ * creating it worked properly.
82
158
*/
83
159
public void testTrialLicense () throws IOException {
160
+ assumeThat ("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway" ,
161
+ CLUSTER_TYPE , equalTo (ClusterType .UPGRADED ));
162
+
84
163
Request startTrial = new Request ("POST" , "/_xpack/license/start_trial" );
85
164
startTrial .addParameter ("acknowledge" , "true" );
86
165
client ().performRequest (startTrial );
@@ -108,4 +187,22 @@ public void testTrialLicense() throws IOException {
108
187
+ "}\n " );
109
188
client ().performRequest (createJob );
110
189
}
190
+
191
+ /**
192
+ * Has the master been upgraded to the new version?
193
+ */
194
+ private boolean masterIsNewVersion () throws IOException {
195
+ Map <?, ?> map ;
196
+ try (XContentParser parser = JsonXContent .jsonXContent .createParser (
197
+ NamedXContentRegistry .EMPTY ,
198
+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION ,
199
+ client ().performRequest (new Request ("GET" , "/_nodes/_master" )).getEntity ().getContent ())) {
200
+ map = parser .map ();
201
+ }
202
+ map = (Map <?, ?>) map .get ("nodes" );
203
+ assertThat (map .values (), hasSize (1 ));
204
+ map = (Map <?, ?>) map .values ().iterator ().next ();
205
+ Version masterVersion = Version .fromString (map .get ("version" ).toString ());
206
+ return Version .CURRENT .equals (masterVersion );
207
+ }
111
208
}
0 commit comments