/core/src/test/java/org/elasticsearch/cluster/node/DiscoveryNodeFiltersTests.java

http://github.com/elasticsearch/elasticsearch · Java · 260 lines · 196 code · 44 blank · 20 comment · 1 complexity · 8fb2670b327286fd2d6f0b4b7a87f2ac MD5 · raw file

  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. package org.elasticsearch.cluster.node;
  20. import org.elasticsearch.Version;
  21. import org.elasticsearch.common.settings.Settings;
  22. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  23. import org.elasticsearch.common.transport.LocalTransportAddress;
  24. import org.elasticsearch.test.ESTestCase;
  25. import org.junit.AfterClass;
  26. import org.junit.BeforeClass;
  27. import java.net.InetAddress;
  28. import java.net.UnknownHostException;
  29. import java.util.ArrayList;
  30. import java.util.Collections;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import static java.util.Collections.emptyMap;
  35. import static java.util.Collections.emptySet;
  36. import static java.util.Collections.singletonMap;
  37. import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.AND;
  38. import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.OR;
  39. import static org.hamcrest.Matchers.equalTo;
  40. /**
  41. */
  42. public class DiscoveryNodeFiltersTests extends ESTestCase {
  43. private static InetSocketTransportAddress localAddress;
  44. @BeforeClass
  45. public static void createLocalAddress() throws UnknownHostException {
  46. localAddress = new InetSocketTransportAddress(InetAddress.getByName("192.1.1.54"), 9999);
  47. }
  48. @AfterClass
  49. public static void releaseLocalAddress() {
  50. localAddress = null;
  51. }
  52. public void testNameMatch() {
  53. Settings settings = Settings.builder()
  54. .put("xxx.name", "name1")
  55. .build();
  56. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  57. DiscoveryNode node = new DiscoveryNode("name1", "id1", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(),
  58. Version.CURRENT);
  59. assertThat(filters.match(node), equalTo(true));
  60. node = new DiscoveryNode("name2", "id2", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(), Version.CURRENT);
  61. assertThat(filters.match(node), equalTo(false));
  62. }
  63. public void testIdMatch() {
  64. Settings settings = Settings.builder()
  65. .put("xxx._id", "id1")
  66. .build();
  67. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  68. DiscoveryNode node = new DiscoveryNode("name1", "id1", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(),
  69. Version.CURRENT);
  70. assertThat(filters.match(node), equalTo(true));
  71. node = new DiscoveryNode("name2", "id2", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(), Version.CURRENT);
  72. assertThat(filters.match(node), equalTo(false));
  73. }
  74. public void testIdOrNameMatch() {
  75. Settings settings = shuffleSettings(Settings.builder()
  76. .put("xxx._id", "id1,blah")
  77. .put("xxx.name", "blah,name2")
  78. .build());
  79. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  80. final Version version = Version.CURRENT;
  81. DiscoveryNode node = new DiscoveryNode("name1", "id1", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(), version);
  82. assertThat(filters.match(node), equalTo(true));
  83. node = new DiscoveryNode("name2", "id2", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(), version);
  84. assertThat(filters.match(node), equalTo(true));
  85. node = new DiscoveryNode("name3", "id3", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(), version);
  86. assertThat(filters.match(node), equalTo(false));
  87. }
  88. public void testTagAndGroupMatch() {
  89. Settings settings = shuffleSettings(Settings.builder()
  90. .put("xxx.tag", "A")
  91. .put("xxx.group", "B")
  92. .build());
  93. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(AND, "xxx.", settings);
  94. Map<String, String> attributes = new HashMap<>();
  95. attributes.put("tag", "A");
  96. attributes.put("group", "B");
  97. DiscoveryNode node = new DiscoveryNode("name1", "id1", LocalTransportAddress.buildUnique(),
  98. attributes, emptySet(), Version.CURRENT);
  99. assertThat(filters.match(node), equalTo(true));
  100. attributes = new HashMap<>();
  101. attributes.put("tag", "A");
  102. attributes.put("group", "B");
  103. attributes.put("name", "X");
  104. node = new DiscoveryNode("name2", "id2", LocalTransportAddress.buildUnique(),
  105. attributes, emptySet(), Version.CURRENT);
  106. assertThat(filters.match(node), equalTo(true));
  107. attributes = new HashMap<>();
  108. attributes.put("tag", "A");
  109. attributes.put("group", "F");
  110. attributes.put("name", "X");
  111. node = new DiscoveryNode("name3", "id3", LocalTransportAddress.buildUnique(),
  112. attributes, emptySet(), Version.CURRENT);
  113. assertThat(filters.match(node), equalTo(false));
  114. node = new DiscoveryNode("name4", "id4", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(), Version.CURRENT);
  115. assertThat(filters.match(node), equalTo(false));
  116. }
  117. public void testStarMatch() {
  118. Settings settings = Settings.builder()
  119. .put("xxx.name", "*")
  120. .build();
  121. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  122. DiscoveryNode node = new DiscoveryNode("name1", "id1", LocalTransportAddress.buildUnique(), emptyMap(), emptySet(),
  123. Version.CURRENT);
  124. assertThat(filters.match(node), equalTo(true));
  125. }
  126. public void testIpBindFilteringMatchingAnd() {
  127. Settings settings = shuffleSettings(Settings.builder()
  128. .put("xxx.tag", "A")
  129. .put("xxx." + randomFrom("_ip", "_host_ip", "_publish_ip"), "192.1.1.54")
  130. .build());
  131. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(AND, "xxx.", settings);
  132. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  133. assertThat(filters.match(node), equalTo(true));
  134. }
  135. public void testIpBindFilteringNotMatching() {
  136. Settings settings = shuffleSettings(Settings.builder()
  137. .put("xxx.tag", "B")
  138. .put("xxx." + randomFrom("_ip", "_host_ip", "_publish_ip"), "192.1.1.54")
  139. .build());
  140. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(AND, "xxx.", settings);
  141. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  142. assertThat(filters.match(node), equalTo(false));
  143. }
  144. public void testIpBindFilteringNotMatchingAnd() {
  145. Settings settings = shuffleSettings(Settings.builder()
  146. .put("xxx.tag", "A")
  147. .put("xxx." + randomFrom("_ip", "_host_ip", "_publish_ip"), "8.8.8.8")
  148. .build());
  149. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(AND, "xxx.", settings);
  150. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  151. assertThat(filters.match(node), equalTo(false));
  152. }
  153. public void testIpBindFilteringMatchingOr() {
  154. Settings settings = shuffleSettings(Settings.builder()
  155. .put("xxx." + randomFrom("_ip", "_host_ip", "_publish_ip"), "192.1.1.54")
  156. .put("xxx.tag", "A")
  157. .build());
  158. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  159. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  160. assertThat(filters.match(node), equalTo(true));
  161. }
  162. public void testIpBindFilteringNotMatchingOr() {
  163. Settings settings = shuffleSettings(Settings.builder()
  164. .put("xxx.tag", "A")
  165. .put("xxx." + randomFrom("_ip", "_host_ip", "_publish_ip"), "8.8.8.8")
  166. .build());
  167. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  168. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  169. assertThat(filters.match(node), equalTo(true));
  170. }
  171. public void testIpPublishFilteringMatchingAnd() {
  172. Settings settings = shuffleSettings(Settings.builder()
  173. .put("xxx.tag", "A")
  174. .put("xxx._publish_ip", "192.1.1.54")
  175. .build());
  176. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(AND, "xxx.", settings);
  177. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  178. assertThat(filters.match(node), equalTo(true));
  179. }
  180. public void testIpPublishFilteringNotMatchingAnd() {
  181. Settings settings = shuffleSettings(Settings.builder()
  182. .put("xxx.tag", "A")
  183. .put("xxx._publish_ip", "8.8.8.8")
  184. .build());
  185. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(AND, "xxx.", settings);
  186. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  187. assertThat(filters.match(node), equalTo(false));
  188. }
  189. public void testIpPublishFilteringMatchingOr() {
  190. Settings settings = shuffleSettings(Settings.builder()
  191. .put("xxx._publish_ip", "192.1.1.54")
  192. .put("xxx.tag", "A")
  193. .build());
  194. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  195. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  196. assertThat(filters.match(node), equalTo(true));
  197. }
  198. public void testIpPublishFilteringNotMatchingOr() {
  199. Settings settings = shuffleSettings(Settings.builder()
  200. .put("xxx.tag", "A")
  201. .put("xxx._publish_ip", "8.8.8.8")
  202. .build());
  203. DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
  204. DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "A"), emptySet(), null);
  205. assertThat(filters.match(node), equalTo(true));
  206. }
  207. private Settings shuffleSettings(Settings source) {
  208. Settings.Builder settings = Settings.builder();
  209. List<String> keys = new ArrayList<>(source.getAsMap().keySet());
  210. Collections.shuffle(keys, random());
  211. for (String o : keys) {
  212. settings.put(o, source.getAsMap().get(o));
  213. }
  214. return settings.build();
  215. }
  216. }