/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfigTests.java

https://github.com/elasticsearch/elasticsearch · Java · 415 lines · 368 code · 41 blank · 6 comment · 2 complexity · 1a8f8ecc19006e0560c50ffd359ed770 MD5 · raw file

  1. /*
  2. * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
  3. * or more contributor license agreements. Licensed under the Elastic License
  4. * 2.0; you may not use this file except in compliance with the Elastic License
  5. * 2.0.
  6. */
  7. package org.elasticsearch.xpack.core.rollup.job;
  8. import org.elasticsearch.common.io.stream.Writeable;
  9. import org.elasticsearch.test.AbstractSerializingTestCase;
  10. import org.elasticsearch.xcontent.XContentParser;
  11. import org.junit.Before;
  12. import java.io.IOException;
  13. import static java.util.Collections.emptyList;
  14. import static org.elasticsearch.xpack.core.rollup.ConfigTestHelpers.randomRollupJobConfig;
  15. import static org.hamcrest.Matchers.arrayContaining;
  16. import static org.hamcrest.Matchers.equalTo;
  17. public class RollupJobConfigTests extends AbstractSerializingTestCase<RollupJobConfig> {
  18. private String jobId;
  19. @Before
  20. public void setUpOptionalId() {
  21. jobId = randomAlphaOfLengthBetween(1, 10);
  22. }
  23. @Override
  24. protected RollupJobConfig createTestInstance() {
  25. return randomRollupJobConfig(random(), jobId);
  26. }
  27. @Override
  28. protected Writeable.Reader<RollupJobConfig> instanceReader() {
  29. return RollupJobConfig::new;
  30. }
  31. @Override
  32. protected RollupJobConfig doParseInstance(final XContentParser parser) throws IOException {
  33. if (randomBoolean()) {
  34. return RollupJobConfig.fromXContent(parser, jobId);
  35. } else {
  36. return RollupJobConfig.fromXContent(parser, null);
  37. }
  38. }
  39. public void testEmptyIndexPattern() {
  40. final RollupJobConfig sample = randomRollupJobConfig(random());
  41. IllegalArgumentException e = expectThrows(
  42. IllegalArgumentException.class,
  43. () -> new RollupJobConfig(
  44. sample.getId(),
  45. null,
  46. sample.getRollupIndex(),
  47. sample.getCron(),
  48. sample.getPageSize(),
  49. sample.getGroupConfig(),
  50. sample.getMetricsConfig(),
  51. sample.getTimeout()
  52. )
  53. );
  54. assertThat(e.getMessage(), equalTo("Index pattern must be a non-null, non-empty string"));
  55. e = expectThrows(
  56. IllegalArgumentException.class,
  57. () -> new RollupJobConfig(
  58. sample.getId(),
  59. "",
  60. sample.getRollupIndex(),
  61. sample.getCron(),
  62. sample.getPageSize(),
  63. sample.getGroupConfig(),
  64. sample.getMetricsConfig(),
  65. sample.getTimeout()
  66. )
  67. );
  68. assertThat(e.getMessage(), equalTo("Index pattern must be a non-null, non-empty string"));
  69. }
  70. public void testEmptyCron() {
  71. final RollupJobConfig sample = randomRollupJobConfig(random());
  72. IllegalArgumentException e = expectThrows(
  73. IllegalArgumentException.class,
  74. () -> new RollupJobConfig(
  75. sample.getId(),
  76. sample.getIndexPattern(),
  77. sample.getRollupIndex(),
  78. null,
  79. sample.getPageSize(),
  80. sample.getGroupConfig(),
  81. sample.getMetricsConfig(),
  82. sample.getTimeout()
  83. )
  84. );
  85. assertThat(e.getMessage(), equalTo("Cron schedule must be a non-null, non-empty string"));
  86. e = expectThrows(
  87. IllegalArgumentException.class,
  88. () -> new RollupJobConfig(
  89. sample.getId(),
  90. sample.getIndexPattern(),
  91. sample.getRollupIndex(),
  92. "",
  93. sample.getPageSize(),
  94. sample.getGroupConfig(),
  95. sample.getMetricsConfig(),
  96. sample.getTimeout()
  97. )
  98. );
  99. assertThat(e.getMessage(), equalTo("Cron schedule must be a non-null, non-empty string"));
  100. }
  101. public void testEmptyID() {
  102. final RollupJobConfig sample = randomRollupJobConfig(random());
  103. IllegalArgumentException e = expectThrows(
  104. IllegalArgumentException.class,
  105. () -> new RollupJobConfig(
  106. null,
  107. sample.getIndexPattern(),
  108. sample.getRollupIndex(),
  109. sample.getCron(),
  110. sample.getPageSize(),
  111. sample.getGroupConfig(),
  112. sample.getMetricsConfig(),
  113. sample.getTimeout()
  114. )
  115. );
  116. assertThat(e.getMessage(), equalTo("Id must be a non-null, non-empty string"));
  117. e = expectThrows(
  118. IllegalArgumentException.class,
  119. () -> new RollupJobConfig(
  120. "",
  121. sample.getIndexPattern(),
  122. sample.getRollupIndex(),
  123. sample.getCron(),
  124. sample.getPageSize(),
  125. sample.getGroupConfig(),
  126. sample.getMetricsConfig(),
  127. sample.getTimeout()
  128. )
  129. );
  130. assertThat(e.getMessage(), equalTo("Id must be a non-null, non-empty string"));
  131. }
  132. public void testMatchAllIndexPattern() {
  133. final RollupJobConfig sample = randomRollupJobConfig(random());
  134. Exception e = expectThrows(
  135. IllegalArgumentException.class,
  136. () -> new RollupJobConfig(
  137. sample.getId(),
  138. "*",
  139. sample.getRollupIndex(),
  140. sample.getCron(),
  141. sample.getPageSize(),
  142. sample.getGroupConfig(),
  143. sample.getMetricsConfig(),
  144. sample.getTimeout()
  145. )
  146. );
  147. assertThat(e.getMessage(), equalTo("Index pattern must not match all indices (as it would match it's own rollup index"));
  148. e = expectThrows(
  149. IllegalArgumentException.class,
  150. () -> new RollupJobConfig(
  151. sample.getId(),
  152. "test,*",
  153. sample.getRollupIndex(),
  154. sample.getCron(),
  155. sample.getPageSize(),
  156. sample.getGroupConfig(),
  157. sample.getMetricsConfig(),
  158. sample.getTimeout()
  159. )
  160. );
  161. assertThat(e.getMessage(), equalTo("Index pattern must not match all indices (as it would match it's own rollup index"));
  162. }
  163. public void testMatchOwnRollupPatternPrefix() {
  164. final RollupJobConfig sample = randomRollupJobConfig(random());
  165. Exception e = expectThrows(
  166. IllegalArgumentException.class,
  167. () -> new RollupJobConfig(
  168. sample.getId(),
  169. "foo-*",
  170. "foo-rollup",
  171. sample.getCron(),
  172. sample.getPageSize(),
  173. sample.getGroupConfig(),
  174. sample.getMetricsConfig(),
  175. sample.getTimeout()
  176. )
  177. );
  178. assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed"));
  179. e = expectThrows(
  180. IllegalArgumentException.class,
  181. () -> new RollupJobConfig(
  182. sample.getId(),
  183. "test,foo-*",
  184. "foo-rollup",
  185. sample.getCron(),
  186. sample.getPageSize(),
  187. sample.getGroupConfig(),
  188. sample.getMetricsConfig(),
  189. sample.getTimeout()
  190. )
  191. );
  192. assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed"));
  193. }
  194. public void testMatchOwnRollupPatternSuffix() {
  195. final RollupJobConfig sample = randomRollupJobConfig(random());
  196. Exception e = expectThrows(
  197. IllegalArgumentException.class,
  198. () -> new RollupJobConfig(
  199. sample.getId(),
  200. "*-rollup",
  201. "foo-rollup",
  202. sample.getCron(),
  203. sample.getPageSize(),
  204. sample.getGroupConfig(),
  205. sample.getMetricsConfig(),
  206. sample.getTimeout()
  207. )
  208. );
  209. assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed"));
  210. e = expectThrows(
  211. IllegalArgumentException.class,
  212. () -> new RollupJobConfig(
  213. sample.getId(),
  214. "test,*-rollup",
  215. "foo-rollup",
  216. sample.getCron(),
  217. sample.getPageSize(),
  218. sample.getGroupConfig(),
  219. sample.getMetricsConfig(),
  220. sample.getTimeout()
  221. )
  222. );
  223. assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed"));
  224. }
  225. public void testIndexPatternIdenticalToRollup() {
  226. final RollupJobConfig sample = randomRollupJobConfig(random());
  227. Exception e = expectThrows(
  228. IllegalArgumentException.class,
  229. () -> new RollupJobConfig(
  230. sample.getId(),
  231. "foo",
  232. "foo",
  233. sample.getCron(),
  234. sample.getPageSize(),
  235. sample.getGroupConfig(),
  236. sample.getMetricsConfig(),
  237. sample.getTimeout()
  238. )
  239. );
  240. assertThat(e.getMessage(), equalTo("Rollup index may not be the same as the index pattern"));
  241. e = expectThrows(
  242. IllegalArgumentException.class,
  243. () -> new RollupJobConfig(
  244. sample.getId(),
  245. "test,foo",
  246. "foo",
  247. sample.getCron(),
  248. sample.getPageSize(),
  249. sample.getGroupConfig(),
  250. sample.getMetricsConfig(),
  251. sample.getTimeout()
  252. )
  253. );
  254. assertThat(e.getMessage(), equalTo("Rollup index may not be the same as the index pattern"));
  255. }
  256. public void testEmptyRollupIndex() {
  257. final RollupJobConfig sample = randomRollupJobConfig(random());
  258. Exception e = expectThrows(
  259. IllegalArgumentException.class,
  260. () -> new RollupJobConfig(
  261. sample.getId(),
  262. sample.getIndexPattern(),
  263. "",
  264. sample.getCron(),
  265. sample.getPageSize(),
  266. sample.getGroupConfig(),
  267. sample.getMetricsConfig(),
  268. sample.getTimeout()
  269. )
  270. );
  271. assertThat(e.getMessage(), equalTo("Rollup index must be a non-null, non-empty string"));
  272. e = expectThrows(
  273. IllegalArgumentException.class,
  274. () -> new RollupJobConfig(
  275. sample.getId(),
  276. sample.getIndexPattern(),
  277. null,
  278. sample.getCron(),
  279. sample.getPageSize(),
  280. sample.getGroupConfig(),
  281. sample.getMetricsConfig(),
  282. sample.getTimeout()
  283. )
  284. );
  285. assertThat(e.getMessage(), equalTo("Rollup index must be a non-null, non-empty string"));
  286. }
  287. public void testBadSize() {
  288. final RollupJobConfig sample = randomRollupJobConfig(random());
  289. Exception e = expectThrows(
  290. IllegalArgumentException.class,
  291. () -> new RollupJobConfig(
  292. sample.getId(),
  293. sample.getIndexPattern(),
  294. sample.getRollupIndex(),
  295. sample.getCron(),
  296. -1,
  297. sample.getGroupConfig(),
  298. sample.getMetricsConfig(),
  299. sample.getTimeout()
  300. )
  301. );
  302. assertThat(e.getMessage(), equalTo("Page size is mandatory and must be a positive long"));
  303. e = expectThrows(
  304. IllegalArgumentException.class,
  305. () -> new RollupJobConfig(
  306. sample.getId(),
  307. sample.getIndexPattern(),
  308. sample.getRollupIndex(),
  309. sample.getCron(),
  310. 0,
  311. sample.getGroupConfig(),
  312. sample.getMetricsConfig(),
  313. sample.getTimeout()
  314. )
  315. );
  316. assertThat(e.getMessage(), equalTo("Page size is mandatory and must be a positive long"));
  317. }
  318. public void testEmptyGroupAndMetrics() {
  319. final RollupJobConfig sample = randomRollupJobConfig(random());
  320. Exception e = expectThrows(
  321. IllegalArgumentException.class,
  322. () -> new RollupJobConfig(
  323. sample.getId(),
  324. sample.getIndexPattern(),
  325. sample.getRollupIndex(),
  326. sample.getCron(),
  327. sample.getPageSize(),
  328. null,
  329. null,
  330. sample.getTimeout()
  331. )
  332. );
  333. assertThat(e.getMessage(), equalTo("At least one grouping or metric must be configured"));
  334. e = expectThrows(
  335. IllegalArgumentException.class,
  336. () -> new RollupJobConfig(
  337. sample.getId(),
  338. sample.getIndexPattern(),
  339. sample.getRollupIndex(),
  340. sample.getCron(),
  341. sample.getPageSize(),
  342. null,
  343. emptyList(),
  344. sample.getTimeout()
  345. )
  346. );
  347. assertThat(e.getMessage(), equalTo("At least one grouping or metric must be configured"));
  348. }
  349. public void testIndices() {
  350. final RollupJobConfig sample = randomRollupJobConfig(random());
  351. RollupJobConfig config = new RollupJobConfig(
  352. sample.getId(),
  353. "foo",
  354. sample.getRollupIndex(),
  355. sample.getCron(),
  356. sample.getPageSize(),
  357. sample.getGroupConfig(),
  358. sample.getMetricsConfig(),
  359. sample.getTimeout()
  360. );
  361. assertThat(config.indices(), arrayContaining("foo"));
  362. config = new RollupJobConfig(
  363. sample.getId(),
  364. "foo,bar",
  365. sample.getRollupIndex(),
  366. sample.getCron(),
  367. sample.getPageSize(),
  368. sample.getGroupConfig(),
  369. sample.getMetricsConfig(),
  370. sample.getTimeout()
  371. );
  372. assertThat(config.indices(), arrayContaining("foo", "bar"));
  373. }
  374. }