2222import org .apache .http .entity .ContentType ;
2323import org .apache .http .nio .entity .NStringEntity ;
2424import 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 ;
2529import org .junit .Before ;
30+ import org .elasticsearch .Version ;
2631
2732import java .io .IOException ;
33+ import java .util .ArrayList ;
34+ import java .util .Collections ;
35+ import java .util .List ;
36+ import java .util .Map ;
2837
2938import static org .hamcrest .Matchers .equalTo ;
39+ import static org .hamcrest .Matchers .hasSize ;
3040import static org .junit .Assume .assumeThat ;
3141import static java .util .Collections .emptyMap ;
3242import static java .util .Collections .singletonMap ;
3747 */
3848public 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