|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.tribe; |
| 21 | + |
| 22 | +import org.elasticsearch.client.Client; |
| 23 | +import org.elasticsearch.cluster.ClusterState; |
| 24 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 25 | +import org.elasticsearch.common.settings.ImmutableSettings; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.node.Node; |
| 28 | +import org.elasticsearch.node.NodeBuilder; |
| 29 | +import org.elasticsearch.test.ElasticsearchTestCase; |
| 30 | +import org.elasticsearch.test.InternalTestCluster; |
| 31 | +import org.junit.AfterClass; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.nio.file.Paths; |
| 37 | + |
| 38 | +import static org.hamcrest.CoreMatchers.either; |
| 39 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 40 | + |
| 41 | +/** |
| 42 | + * This test doesn't extend {@link org.elasticsearch.test.ElasticsearchIntegrationTest} as the internal cluster ignores system properties |
| 43 | + * all the time, while we need to make the tribe node accept them in this case, so that we can verify that they are not read again as part |
| 44 | + * of the tribe client nodes initialization. Note that the started nodes will obey to the 'node.mode' settings as the internal cluster does. |
| 45 | + */ |
| 46 | +public class TribeUnitTests extends ElasticsearchTestCase { |
| 47 | + |
| 48 | + private static Node tribe1; |
| 49 | + private static Node tribe2; |
| 50 | + |
| 51 | + private static final String NODE_MODE = InternalTestCluster.nodeMode(); |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void createTribes() { |
| 55 | + tribe1 = NodeBuilder.nodeBuilder().settings(ImmutableSettings.builder().put("config.ignore_system_properties", true).put("http.enabled", false) |
| 56 | + .put("node.mode", NODE_MODE).put("cluster.name", "tribe1").put("node.name", "tribe1_node")).node(); |
| 57 | + tribe2 = NodeBuilder.nodeBuilder().settings(ImmutableSettings.builder().put("config.ignore_system_properties", true).put("http.enabled", false) |
| 58 | + .put("node.mode", NODE_MODE).put("cluster.name", "tribe2").put("node.name", "tribe2_node")).node(); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @AfterClass |
| 63 | + public static void closeTribes() { |
| 64 | + tribe1.close(); |
| 65 | + tribe1 = null; |
| 66 | + tribe2.close(); |
| 67 | + tribe2 = null; |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testThatTribeClientsIgnoreGlobalSysProps() throws Exception { |
| 72 | + System.setProperty("es.cluster.name", "tribe_node_cluster"); |
| 73 | + System.setProperty("es.tribe.t1.cluster.name", "tribe1"); |
| 74 | + System.setProperty("es.tribe.t2.cluster.name", "tribe2"); |
| 75 | + |
| 76 | + try { |
| 77 | + assertTribeNodeSuccesfullyCreated(ImmutableSettings.EMPTY); |
| 78 | + } finally { |
| 79 | + System.clearProperty("es.cluster.name"); |
| 80 | + System.clearProperty("es.tribe.t1.cluster.name"); |
| 81 | + System.clearProperty("es.tribe.t2.cluster.name"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testThatTribeClientsIgnoreGlobalConfig() throws Exception { |
| 87 | + Path pathConf = Paths.get(TribeUnitTests.class.getResource("elasticsearch.yml").toURI()).getParent(); |
| 88 | + Settings settings = ImmutableSettings.builder().put("config.ignore_system_properties", true).put("path.conf", pathConf).build(); |
| 89 | + assertTribeNodeSuccesfullyCreated(settings); |
| 90 | + } |
| 91 | + |
| 92 | + private static void assertTribeNodeSuccesfullyCreated(Settings extraSettings) throws Exception { |
| 93 | + //tribe node doesn't need the node.mode setting, as it's forced local internally anyways. The tribe clients do need it to make sure |
| 94 | + //they can find their corresponding tribes using the proper transport |
| 95 | + Settings settings = ImmutableSettings.builder().put("http.enabled", false).put("node.name", "tribe_node") |
| 96 | + .put("tribe.t1.node.mode", NODE_MODE).put("tribe.t2.node.mode", NODE_MODE).put(extraSettings).build(); |
| 97 | + |
| 98 | + try (Node node = NodeBuilder.nodeBuilder().settings(settings).node()) { |
| 99 | + try (Client client = node.client()) { |
| 100 | + assertBusy(new Runnable() { |
| 101 | + @Override |
| 102 | + public void run() { |
| 103 | + ClusterState state = client.admin().cluster().prepareState().clear().setNodes(true).get().getState(); |
| 104 | + assertThat(state.getClusterName().value(), equalTo("tribe_node_cluster")); |
| 105 | + assertThat(state.getNodes().getSize(), equalTo(5)); |
| 106 | + for (DiscoveryNode discoveryNode : state.getNodes()) { |
| 107 | + assertThat(discoveryNode.getName(), either(equalTo("tribe1_node")).or(equalTo("tribe2_node")).or(equalTo("tribe_node")) |
| 108 | + .or(equalTo("tribe_node/t1")).or(equalTo("tribe_node/t2"))); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments