test_elasticsearch.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Lukas Raska <lukas@raska.me>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.utils.dictdiffer as dictdiffer
  9. from salt.exceptions import CommandExecutionError
  10. from salt.states import elasticsearch
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.unit import TestCase
  15. class ElasticsearchTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.states.elasticsearch
  18. """
  19. def setup_loader_modules(self):
  20. return {
  21. elasticsearch: {
  22. "__opts__": {"test": False},
  23. "__utils__": {"dictdiffer.deep_diff": dictdiffer.deep_diff},
  24. }
  25. }
  26. # 'index_absent' function tests: 1
  27. def test_index_absent(self):
  28. """
  29. Test to manage a elasticsearch index.
  30. """
  31. name = "foo"
  32. ret = {
  33. "name": name,
  34. "result": True,
  35. "comment": "Index foo is already absent",
  36. "changes": {},
  37. }
  38. mock_get = MagicMock(
  39. side_effect=[
  40. None,
  41. {name: {"test": "key"}},
  42. {name: {}},
  43. {name: {"test": "key"}},
  44. CommandExecutionError,
  45. {name: {"test": "key"}},
  46. ]
  47. )
  48. mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
  49. with patch.dict(
  50. elasticsearch.__salt__,
  51. {
  52. "elasticsearch.index_get": mock_get,
  53. "elasticsearch.index_delete": mock_delete,
  54. },
  55. ):
  56. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  57. ret.update(
  58. {
  59. "comment": "Successfully removed index foo",
  60. "changes": {"old": {"test": "key"}},
  61. }
  62. )
  63. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  64. ret.update(
  65. {
  66. "comment": "Failed to remove index foo for unknown reasons",
  67. "result": False,
  68. "changes": {},
  69. }
  70. )
  71. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  72. with patch.dict(elasticsearch.__opts__, {"test": True}):
  73. ret.update(
  74. {
  75. "comment": "Index foo will be removed",
  76. "result": None,
  77. "changes": {"old": {"test": "key"}},
  78. }
  79. )
  80. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  81. ret.update({"comment": "", "result": False, "changes": {}})
  82. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  83. ret.update({"comment": "", "result": False, "changes": {}})
  84. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  85. # 'index_present' function tests: 1
  86. def test_index_present(self):
  87. """
  88. Test to manage a elasticsearch index.
  89. """
  90. name = "foo"
  91. ret = {
  92. "name": name,
  93. "result": True,
  94. "comment": "Index foo is already present",
  95. "changes": {},
  96. }
  97. mock_exists = MagicMock(
  98. side_effect=[True, False, False, False, CommandExecutionError, False, False]
  99. )
  100. mock_get = MagicMock(
  101. side_effect=[{name: {"test": "key"}}, CommandExecutionError]
  102. )
  103. mock_create = MagicMock(side_effect=[True, False, CommandExecutionError, True])
  104. with patch.dict(
  105. elasticsearch.__salt__,
  106. {
  107. "elasticsearch.index_get": mock_get,
  108. "elasticsearch.index_exists": mock_exists,
  109. "elasticsearch.index_create": mock_create,
  110. },
  111. ):
  112. self.assertDictEqual(elasticsearch.index_present(name), ret)
  113. ret.update(
  114. {
  115. "comment": "Successfully created index foo",
  116. "changes": {"new": {"test": "key"}},
  117. }
  118. )
  119. self.assertDictEqual(elasticsearch.index_present(name), ret)
  120. ret.update(
  121. {
  122. "comment": "Cannot create index foo, False",
  123. "result": False,
  124. "changes": {},
  125. }
  126. )
  127. self.assertDictEqual(elasticsearch.index_present(name), ret)
  128. with patch.dict(elasticsearch.__opts__, {"test": True}):
  129. ret.update(
  130. {
  131. "comment": "Index foo does not exist and will be created",
  132. "result": None,
  133. "changes": {"new": {"test2": "key"}},
  134. }
  135. )
  136. self.assertDictEqual(
  137. elasticsearch.index_present(name, {"test2": "key"}), ret
  138. )
  139. ret.update({"comment": "", "result": False, "changes": {}})
  140. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  141. ret.update({"comment": "", "result": False, "changes": {}})
  142. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  143. ret.update({"comment": "", "result": False, "changes": {}})
  144. self.assertDictEqual(elasticsearch.index_absent(name), ret)
  145. # 'alias_absent' function tests: 1
  146. def test_alias_absent(self):
  147. """
  148. Test to manage a elasticsearch alias.
  149. """
  150. name = "foo"
  151. index = "bar"
  152. alias = {index: {"aliases": {name: {"test": "key"}}}}
  153. ret = {
  154. "name": name,
  155. "result": True,
  156. "comment": "Alias foo for index bar is already absent",
  157. "changes": {},
  158. }
  159. mock_get = MagicMock(
  160. side_effect=[
  161. None,
  162. {"foo2": {}},
  163. alias,
  164. alias,
  165. alias,
  166. CommandExecutionError,
  167. alias,
  168. ]
  169. )
  170. mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
  171. with patch.dict(
  172. elasticsearch.__salt__,
  173. {
  174. "elasticsearch.alias_get": mock_get,
  175. "elasticsearch.alias_delete": mock_delete,
  176. },
  177. ):
  178. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  179. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  180. ret.update(
  181. {
  182. "comment": "Successfully removed alias foo for index bar",
  183. "changes": {"old": {"test": "key"}},
  184. }
  185. )
  186. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  187. ret.update(
  188. {
  189. "comment": "Failed to remove alias foo for index bar for unknown reasons",
  190. "result": False,
  191. "changes": {},
  192. }
  193. )
  194. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  195. with patch.dict(elasticsearch.__opts__, {"test": True}):
  196. ret.update(
  197. {
  198. "comment": "Alias foo for index bar will be removed",
  199. "result": None,
  200. "changes": {"old": {"test": "key"}},
  201. }
  202. )
  203. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  204. ret.update({"comment": "", "result": False, "changes": {}})
  205. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  206. ret.update({"comment": "", "result": False, "changes": {}})
  207. self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
  208. # 'alias_present' function tests: 1
  209. def test_alias_present(self):
  210. """
  211. Test to manage a elasticsearch alias.
  212. """
  213. name = "foo"
  214. index = "bar"
  215. alias = {index: {"aliases": {name: {"test": "key"}}}}
  216. ret = {
  217. "name": name,
  218. "result": True,
  219. "comment": "Alias foo for index bar is already present",
  220. "changes": {},
  221. }
  222. mock_get = MagicMock(
  223. side_effect=[
  224. alias,
  225. alias,
  226. None,
  227. None,
  228. None,
  229. alias,
  230. CommandExecutionError,
  231. None,
  232. ]
  233. )
  234. mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])
  235. with patch.dict(
  236. elasticsearch.__salt__,
  237. {
  238. "elasticsearch.alias_get": mock_get,
  239. "elasticsearch.alias_create": mock_create,
  240. },
  241. ):
  242. self.assertDictEqual(
  243. elasticsearch.alias_present(name, index, {"test": "key"}), ret
  244. )
  245. ret.update(
  246. {
  247. "comment": "Successfully replaced alias foo for index bar",
  248. "changes": {"old": {"test": "key"}, "new": {"test2": "key"}},
  249. }
  250. )
  251. self.assertDictEqual(
  252. elasticsearch.alias_present(name, index, {"test2": "key"}), ret
  253. )
  254. ret.update(
  255. {
  256. "comment": "Successfully created alias foo for index bar",
  257. "changes": {"new": {"test2": "key"}},
  258. }
  259. )
  260. self.assertDictEqual(
  261. elasticsearch.alias_present(name, index, {"test2": "key"}), ret
  262. )
  263. ret.update(
  264. {
  265. "comment": "Cannot create alias foo for index bar, False",
  266. "result": False,
  267. }
  268. )
  269. self.assertDictEqual(
  270. elasticsearch.alias_present(name, index, {"test2": "key"}), ret
  271. )
  272. with patch.dict(elasticsearch.__opts__, {"test": True}):
  273. ret.update(
  274. {
  275. "comment": "Alias foo for index bar does not exist and will be created",
  276. "result": None,
  277. "changes": {"new": {"test2": "key"}},
  278. }
  279. )
  280. self.assertDictEqual(
  281. elasticsearch.alias_present(name, index, {"test2": "key"}), ret
  282. )
  283. ret.update(
  284. {
  285. "comment": "Alias foo for index bar exists with wrong configuration and will be overridden",
  286. "result": None,
  287. "changes": {"old": {"test": "key"}, "new": {"test2": "key"}},
  288. }
  289. )
  290. self.assertDictEqual(
  291. elasticsearch.alias_present(name, index, {"test2": "key"}), ret
  292. )
  293. ret.update({"comment": "", "result": False, "changes": {}})
  294. self.assertDictEqual(elasticsearch.alias_present(name, index), ret)
  295. ret.update({"comment": "", "result": False, "changes": {}})
  296. self.assertDictEqual(elasticsearch.alias_present(name, index), ret)
  297. # 'index_template_absent' function tests: 1
  298. def test_index_template_absent(self):
  299. """
  300. Test to manage a elasticsearch index template.
  301. """
  302. name = "foo"
  303. index_template = {name: {"test": "key"}}
  304. ret = {
  305. "name": name,
  306. "result": True,
  307. "comment": "Index template foo is already absent",
  308. "changes": {},
  309. }
  310. mock_get = MagicMock(
  311. side_effect=[
  312. None,
  313. {"bar": {}},
  314. index_template,
  315. index_template,
  316. index_template,
  317. CommandExecutionError,
  318. index_template,
  319. ]
  320. )
  321. mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
  322. with patch.dict(
  323. elasticsearch.__salt__,
  324. {
  325. "elasticsearch.index_template_get": mock_get,
  326. "elasticsearch.index_template_delete": mock_delete,
  327. },
  328. ):
  329. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  330. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  331. ret.update(
  332. {
  333. "comment": "Successfully removed index template foo",
  334. "changes": {"old": {"test": "key"}},
  335. }
  336. )
  337. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  338. ret.update(
  339. {
  340. "comment": "Failed to remove index template foo for unknown reasons",
  341. "result": False,
  342. "changes": {},
  343. }
  344. )
  345. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  346. with patch.dict(elasticsearch.__opts__, {"test": True}):
  347. ret.update(
  348. {
  349. "comment": "Index template foo will be removed",
  350. "result": None,
  351. "changes": {"old": {"test": "key"}},
  352. }
  353. )
  354. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  355. ret.update({"comment": "", "result": False, "changes": {}})
  356. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  357. ret.update({"comment": "", "result": False, "changes": {}})
  358. self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
  359. # 'index_template_present' function tests: 1
  360. def test_index_template_present(self):
  361. """
  362. Test to manage a elasticsearch index template.
  363. """
  364. name = "foo"
  365. index_template = {name: {"test": "key"}}
  366. ret = {
  367. "name": name,
  368. "result": True,
  369. "comment": "Index template foo is already present",
  370. "changes": {},
  371. }
  372. mock_exists = MagicMock(
  373. side_effect=[True, False, False, False, CommandExecutionError, False, False]
  374. )
  375. mock_create = MagicMock(side_effect=[True, False, CommandExecutionError, True])
  376. mock_get = MagicMock(side_effect=[index_template, CommandExecutionError])
  377. with patch.dict(
  378. elasticsearch.__salt__,
  379. {
  380. "elasticsearch.index_template_get": mock_get,
  381. "elasticsearch.index_template_create": mock_create,
  382. "elasticsearch.index_template_exists": mock_exists,
  383. },
  384. ):
  385. self.assertDictEqual(
  386. elasticsearch.index_template_present(name, {"test2": "key"}), ret
  387. )
  388. ret.update(
  389. {
  390. "comment": "Successfully created index template foo",
  391. "changes": {"new": {"test": "key"}},
  392. }
  393. )
  394. self.assertDictEqual(
  395. elasticsearch.index_template_present(name, {"test2": "key"}), ret
  396. )
  397. ret.update(
  398. {
  399. "comment": "Cannot create index template foo, False",
  400. "result": False,
  401. "changes": {},
  402. }
  403. )
  404. self.assertDictEqual(
  405. elasticsearch.index_template_present(name, {"test2": "key"}), ret
  406. )
  407. with patch.dict(elasticsearch.__opts__, {"test": True}):
  408. ret.update(
  409. {
  410. "comment": "Index template foo does not exist and will be created",
  411. "result": None,
  412. "changes": {"new": {"test2": "key"}},
  413. }
  414. )
  415. self.assertDictEqual(
  416. elasticsearch.index_template_present(name, {"test2": "key"}), ret
  417. )
  418. ret.update({"comment": "", "result": False, "changes": {}})
  419. self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret)
  420. ret.update({"comment": "", "result": False, "changes": {}})
  421. self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret)
  422. ret.update({"comment": "", "result": False, "changes": {}})
  423. self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret)
  424. def test_index_template_present_check_definition(self):
  425. """
  426. Test to manage a elasticsearch index template.
  427. with check_definition set
  428. """
  429. name = "foo"
  430. index_template = {
  431. name: {"test2": "key", "aliases": {}, "mappings": {}, "settings": {}}
  432. }
  433. expected = {
  434. "name": name,
  435. "result": True,
  436. "comment": "Index template foo is already present and up to date",
  437. "changes": {},
  438. }
  439. mock_exists = MagicMock(side_effect=[True])
  440. mock_create = MagicMock(side_effect=[True])
  441. mock_get = MagicMock(side_effect=[index_template])
  442. with patch.dict(
  443. elasticsearch.__salt__,
  444. {
  445. "elasticsearch.index_template_get": mock_get,
  446. "elasticsearch.index_template_create": mock_create,
  447. "elasticsearch.index_template_exists": mock_exists,
  448. },
  449. ):
  450. ret = elasticsearch.index_template_present(
  451. name, {"test2": "key", "aliases": {}}, check_definition=True
  452. )
  453. self.assertDictEqual(expected, ret)
  454. def test_index_template_present_check_definition_alias_not_empty(self):
  455. """
  456. Test to manage a elasticsearch index template.
  457. with check_definition set and alias is not empty
  458. """
  459. name = "foo"
  460. index_template = {
  461. name: {"test2": "key", "aliases": {}, "mappings": {}, "settings": {}}
  462. }
  463. expected = {
  464. "name": name,
  465. "result": True,
  466. "comment": "Successfully updated index template foo",
  467. "changes": {"new": {"aliases": {"alias1": {}}}, "old": {"aliases": {}}},
  468. }
  469. mock_exists = MagicMock(side_effect=[True])
  470. mock_create = MagicMock(side_effect=[True])
  471. mock_get = MagicMock(side_effect=[index_template])
  472. with patch.dict(
  473. elasticsearch.__salt__,
  474. {
  475. "elasticsearch.index_template_get": mock_get,
  476. "elasticsearch.index_template_create": mock_create,
  477. "elasticsearch.index_template_exists": mock_exists,
  478. },
  479. ):
  480. ret = elasticsearch.index_template_present(
  481. name, {"test2": "key", "aliases": {"alias1": {}}}, check_definition=True
  482. )
  483. self.assertDictEqual(expected, ret)
  484. # 'pipeline_absent' function tests: 1
  485. def test_pipeline_absent(self):
  486. """
  487. Test to manage a elasticsearch pipeline.
  488. """
  489. name = "foo"
  490. pipeline = {name: {"test": "key"}}
  491. ret = {
  492. "name": name,
  493. "result": True,
  494. "comment": "Pipeline foo is already absent",
  495. "changes": {},
  496. }
  497. mock_get = MagicMock(
  498. side_effect=[
  499. None,
  500. {"foo2": {}},
  501. pipeline,
  502. pipeline,
  503. pipeline,
  504. CommandExecutionError,
  505. pipeline,
  506. ]
  507. )
  508. mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
  509. with patch.dict(
  510. elasticsearch.__salt__,
  511. {
  512. "elasticsearch.pipeline_get": mock_get,
  513. "elasticsearch.pipeline_delete": mock_delete,
  514. },
  515. ):
  516. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  517. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  518. ret.update(
  519. {
  520. "comment": "Successfully removed pipeline foo",
  521. "changes": {"old": {"test": "key"}},
  522. }
  523. )
  524. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  525. ret.update(
  526. {
  527. "comment": "Failed to remove pipeline foo for unknown reasons",
  528. "result": False,
  529. "changes": {},
  530. }
  531. )
  532. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  533. with patch.dict(elasticsearch.__opts__, {"test": True}):
  534. ret.update(
  535. {
  536. "comment": "Pipeline foo will be removed",
  537. "result": None,
  538. "changes": {"old": {"test": "key"}},
  539. }
  540. )
  541. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  542. ret.update({"comment": "", "result": False, "changes": {}})
  543. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  544. ret.update({"comment": "", "result": False, "changes": {}})
  545. self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
  546. # 'pipeline_present' function tests: 1
  547. def test_pipeline_present(self):
  548. """
  549. Test to manage a elasticsearch pipeline.
  550. """
  551. name = "foo"
  552. pipeline = {name: {"test": "key"}}
  553. ret = {
  554. "name": name,
  555. "result": True,
  556. "comment": "Pipeline foo is already present",
  557. "changes": {},
  558. }
  559. mock_get = MagicMock(
  560. side_effect=[
  561. pipeline,
  562. pipeline,
  563. None,
  564. None,
  565. None,
  566. pipeline,
  567. CommandExecutionError,
  568. None,
  569. ]
  570. )
  571. mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])
  572. with patch.dict(
  573. elasticsearch.__salt__,
  574. {
  575. "elasticsearch.pipeline_get": mock_get,
  576. "elasticsearch.pipeline_create": mock_create,
  577. },
  578. ):
  579. self.assertDictEqual(
  580. elasticsearch.pipeline_present(name, {"test": "key"}), ret
  581. )
  582. ret.update(
  583. {
  584. "comment": "Successfully replaced pipeline foo",
  585. "changes": {"old": {"test": "key"}, "new": {"test2": "key"}},
  586. }
  587. )
  588. self.assertDictEqual(
  589. elasticsearch.pipeline_present(name, {"test2": "key"}), ret
  590. )
  591. ret.update(
  592. {
  593. "comment": "Successfully created pipeline foo",
  594. "changes": {"new": {"test2": "key"}},
  595. }
  596. )
  597. self.assertDictEqual(
  598. elasticsearch.pipeline_present(name, {"test2": "key"}), ret
  599. )
  600. ret.update(
  601. {"comment": "Cannot create pipeline foo, False", "result": False}
  602. )
  603. self.assertDictEqual(
  604. elasticsearch.pipeline_present(name, {"test2": "key"}), ret
  605. )
  606. with patch.dict(elasticsearch.__opts__, {"test": True}):
  607. ret.update(
  608. {
  609. "comment": "Pipeline foo does not exist and will be created",
  610. "result": None,
  611. "changes": {"new": {"test2": "key"}},
  612. }
  613. )
  614. self.assertDictEqual(
  615. elasticsearch.pipeline_present(name, {"test2": "key"}), ret
  616. )
  617. ret.update(
  618. {
  619. "comment": "Pipeline foo exists with wrong configuration and will be overridden",
  620. "result": None,
  621. "changes": {"old": {"test": "key"}, "new": {"test2": "key"}},
  622. }
  623. )
  624. self.assertDictEqual(
  625. elasticsearch.pipeline_present(name, {"test2": "key"}), ret
  626. )
  627. ret.update({"comment": "", "result": False, "changes": {}})
  628. self.assertDictEqual(elasticsearch.pipeline_present(name, {}), ret)
  629. ret.update({"comment": "", "result": False, "changes": {}})
  630. self.assertDictEqual(elasticsearch.pipeline_present(name, {}), ret)
  631. # 'search_template_absent' function tests: 1
  632. def test_search_template_absent(self):
  633. """
  634. Test to manage a elasticsearch search template.
  635. """
  636. name = "foo"
  637. template = {"template": '{"test": "key"}'}
  638. ret = {
  639. "name": name,
  640. "result": True,
  641. "comment": "Search template foo is already absent",
  642. "changes": {},
  643. }
  644. mock_get = MagicMock(
  645. side_effect=[
  646. None,
  647. template,
  648. template,
  649. template,
  650. CommandExecutionError,
  651. template,
  652. ]
  653. )
  654. mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
  655. with patch.dict(
  656. elasticsearch.__salt__,
  657. {
  658. "elasticsearch.search_template_get": mock_get,
  659. "elasticsearch.search_template_delete": mock_delete,
  660. },
  661. ):
  662. self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
  663. ret.update(
  664. {
  665. "comment": "Successfully removed search template foo",
  666. "changes": {"old": {"test": "key"}},
  667. }
  668. )
  669. self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
  670. ret.update(
  671. {
  672. "comment": "Failed to remove search template foo for unknown reasons",
  673. "result": False,
  674. "changes": {},
  675. }
  676. )
  677. self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
  678. with patch.dict(elasticsearch.__opts__, {"test": True}):
  679. ret.update(
  680. {
  681. "comment": "Search template foo will be removed",
  682. "result": None,
  683. "changes": {"old": {"test": "key"}},
  684. }
  685. )
  686. self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
  687. ret.update({"comment": "", "result": False, "changes": {}})
  688. self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
  689. ret.update({"comment": "", "result": False, "changes": {}})
  690. self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
  691. # 'pipeline_present' function tests: 1
  692. def test_search_template_present(self):
  693. """
  694. Test to manage a elasticsearch search template.
  695. """
  696. name = "foo"
  697. template = {"template": '{"test": "key"}'}
  698. ret = {
  699. "name": name,
  700. "result": True,
  701. "comment": "Search template foo is already present",
  702. "changes": {},
  703. }
  704. mock_get = MagicMock(
  705. side_effect=[
  706. template,
  707. template,
  708. None,
  709. None,
  710. None,
  711. template,
  712. CommandExecutionError,
  713. None,
  714. ]
  715. )
  716. mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])
  717. with patch.dict(
  718. elasticsearch.__salt__,
  719. {
  720. "elasticsearch.search_template_get": mock_get,
  721. "elasticsearch.search_template_create": mock_create,
  722. },
  723. ):
  724. self.assertDictEqual(
  725. elasticsearch.search_template_present(name, {"test": "key"}), ret
  726. )
  727. ret.update(
  728. {
  729. "comment": "Successfully replaced search template foo",
  730. "changes": {"old": {"test": "key"}, "new": {"test2": "key"}},
  731. }
  732. )
  733. self.assertDictEqual(
  734. elasticsearch.search_template_present(name, {"test2": "key"}), ret
  735. )
  736. ret.update(
  737. {
  738. "comment": "Successfully created search template foo",
  739. "changes": {"new": {"test2": "key"}},
  740. }
  741. )
  742. self.assertDictEqual(
  743. elasticsearch.search_template_present(name, {"test2": "key"}), ret
  744. )
  745. ret.update(
  746. {"comment": "Cannot create search template foo, False", "result": False}
  747. )
  748. self.assertDictEqual(
  749. elasticsearch.search_template_present(name, {"test2": "key"}), ret
  750. )
  751. with patch.dict(elasticsearch.__opts__, {"test": True}):
  752. ret.update(
  753. {
  754. "comment": "Search template foo does not exist and will be created",
  755. "result": None,
  756. "changes": {"new": {"test2": "key"}},
  757. }
  758. )
  759. self.assertDictEqual(
  760. elasticsearch.search_template_present(name, {"test2": "key"}), ret
  761. )
  762. ret.update(
  763. {
  764. "comment": "Search template foo exists with wrong configuration and will be overridden",
  765. "result": None,
  766. "changes": {"old": {"test": "key"}, "new": {"test2": "key"}},
  767. }
  768. )
  769. self.assertDictEqual(
  770. elasticsearch.search_template_present(name, {"test2": "key"}), ret
  771. )
  772. ret.update({"comment": "", "result": False, "changes": {}})
  773. self.assertDictEqual(elasticsearch.search_template_present(name, {}), ret)
  774. ret.update({"comment": "", "result": False, "changes": {}})
  775. self.assertDictEqual(elasticsearch.search_template_present(name, {}), ret)