test_elasticsearch.py 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  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 Testing Libs
  8. from tests.support.unit import TestCase, skipIf
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch,
  12. )
  13. # Import Salt Libs
  14. from salt.modules import elasticsearch
  15. from salt.exceptions import CommandExecutionError, SaltInvocationError
  16. # Import elasticsearch exceptions
  17. NO_ELASTIC = False
  18. try:
  19. from elasticsearch import TransportError, NotFoundError
  20. except Exception:
  21. NO_ELASTIC = True
  22. @skipIf(NO_ELASTIC, 'Install elasticsearch-py before running Elasticsearch unit tests.')
  23. class ElasticsearchTestCase(TestCase):
  24. '''
  25. Test cases for salt.modules.elasticsearch
  26. '''
  27. @staticmethod
  28. def es_return_true(hosts=None, profile=None):
  29. return True
  30. @staticmethod
  31. def es_raise_command_execution_error(hosts=None, profile=None):
  32. raise CommandExecutionError("custom message")
  33. # 'ping' function tests: 2
  34. def test_ping(self):
  35. '''
  36. Test if ping succeeds
  37. '''
  38. with patch.object(elasticsearch, '_get_instance', self.es_return_true):
  39. self.assertTrue(elasticsearch.ping())
  40. def test_ping_failure(self):
  41. '''
  42. Test if ping fails
  43. '''
  44. with patch.object(elasticsearch, '_get_instance', self.es_raise_command_execution_error):
  45. self.assertFalse(elasticsearch.ping())
  46. # 'info' function tests: 2
  47. def test_info(self):
  48. '''
  49. Test if status fetch succeeds
  50. '''
  51. class MockElastic(object):
  52. '''
  53. Mock of Elasticsearch client
  54. '''
  55. def info(self):
  56. '''
  57. Mock of info method
  58. '''
  59. return [{"test": "key"}]
  60. with patch.object(elasticsearch, '_get_instance',
  61. MagicMock(return_value=MockElastic())):
  62. self.assertListEqual(elasticsearch.info(), [{"test": "key"}])
  63. def test_info_failure(self):
  64. '''
  65. Test if status fetch fails with CommandExecutionError
  66. '''
  67. class MockElastic(object):
  68. '''
  69. Mock of Elasticsearch client
  70. '''
  71. def info(self):
  72. '''
  73. Mock of info method
  74. '''
  75. raise TransportError("custom error", 123)
  76. with patch.object(elasticsearch, '_get_instance',
  77. MagicMock(return_value=MockElastic())):
  78. self.assertRaises(CommandExecutionError, elasticsearch.info)
  79. # 'node_info' function tests: 2
  80. def test_node_info(self):
  81. '''
  82. Test if node status fetch succeeds
  83. '''
  84. class MockElasticNodes(object):
  85. '''
  86. Mock of Elasticsearch NodesClient
  87. '''
  88. def info(self, node_id=None, flat_settings=None):
  89. '''
  90. Mock of info method
  91. '''
  92. return [{"test": "key"}]
  93. class MockElastic(object):
  94. '''
  95. Mock of Elasticsearch client
  96. '''
  97. nodes = MockElasticNodes()
  98. with patch.object(elasticsearch, '_get_instance',
  99. MagicMock(return_value=MockElastic())):
  100. self.assertListEqual(elasticsearch.node_info(), [{"test": "key"}])
  101. def test_node_info_failure(self):
  102. '''
  103. Test if node status fetch fails with CommandExecutionError
  104. '''
  105. class MockElasticNodes(object):
  106. '''
  107. Mock of Elasticsearch NodesClient
  108. '''
  109. def info(self, node_id=None, flat_settings=None):
  110. '''
  111. Mock of info method
  112. '''
  113. raise TransportError("custom error", 123)
  114. class MockElastic(object):
  115. '''
  116. Mock of Elasticsearch client
  117. '''
  118. nodes = MockElasticNodes()
  119. with patch.object(elasticsearch, '_get_instance',
  120. MagicMock(return_value=MockElastic())):
  121. self.assertRaises(CommandExecutionError, elasticsearch.node_info)
  122. # 'cluster_health' function tests: 2
  123. def test_cluster_health(self):
  124. '''
  125. Test if cluster status health fetch succeeds
  126. '''
  127. class MockElasticCluster(object):
  128. '''
  129. Mock of Elasticsearch ClusterClient
  130. '''
  131. def health(self, index=None, level=None, local=None):
  132. '''
  133. Mock of health method
  134. '''
  135. return [{"test": "key"}]
  136. class MockElastic(object):
  137. '''
  138. Mock of Elasticsearch client
  139. '''
  140. cluster = MockElasticCluster()
  141. with patch.object(elasticsearch, '_get_instance',
  142. MagicMock(return_value=MockElastic())):
  143. self.assertListEqual(elasticsearch.cluster_health(), [{"test": "key"}])
  144. def test_cluster_health_failure(self):
  145. '''
  146. Test if cluster status health fetch fails with CommandExecutionError
  147. '''
  148. class MockElasticCluster(object):
  149. '''
  150. Mock of Elasticsearch ClusterClient
  151. '''
  152. def health(self, index=None, level=None, local=None):
  153. '''
  154. Mock of health method
  155. '''
  156. raise TransportError("custom error", 123)
  157. class MockElastic(object):
  158. '''
  159. Mock of Elasticsearch client
  160. '''
  161. cluster = MockElasticCluster()
  162. with patch.object(elasticsearch, '_get_instance',
  163. MagicMock(return_value=MockElastic())):
  164. self.assertRaises(CommandExecutionError, elasticsearch.cluster_health)
  165. # 'cluster_stats' function tests: 2
  166. def test_cluster_stats(self):
  167. '''
  168. Test if cluster stats fetch succeeds
  169. '''
  170. class MockElasticCluster(object):
  171. '''
  172. Mock of Elasticsearch ClusterClient
  173. '''
  174. def stats(self, node_id=None):
  175. '''
  176. Mock of health method
  177. '''
  178. return [{"test": "key"}]
  179. class MockElastic(object):
  180. '''
  181. Mock of Elasticsearch client
  182. '''
  183. cluster = MockElasticCluster()
  184. with patch.object(elasticsearch, '_get_instance',
  185. MagicMock(return_value=MockElastic())):
  186. self.assertListEqual(elasticsearch.cluster_stats(), [{"test": "key"}])
  187. def test_cluster_stats_failure(self):
  188. '''
  189. Test if cluster stats fetch fails with CommandExecutionError
  190. '''
  191. class MockElasticCluster(object):
  192. '''
  193. Mock of Elasticsearch ClusterClient
  194. '''
  195. def stats(self, node_id=None):
  196. '''
  197. Mock of health method
  198. '''
  199. raise TransportError("custom error", 123)
  200. class MockElastic(object):
  201. '''
  202. Mock of Elasticsearch client
  203. '''
  204. cluster = MockElasticCluster()
  205. with patch.object(elasticsearch, '_get_instance',
  206. MagicMock(return_value=MockElastic())):
  207. self.assertRaises(CommandExecutionError, elasticsearch.cluster_stats)
  208. # 'alias_create' function tests: 3
  209. def test_alias_create(self):
  210. '''
  211. Test if alias is created
  212. '''
  213. class MockElasticIndices(object):
  214. '''
  215. Mock of Elasticsearch IndicesClient
  216. '''
  217. def put_alias(self, index=None, name=None, body=None):
  218. '''
  219. Mock of put_alias method
  220. '''
  221. return {"acknowledged": True}
  222. class MockElastic(object):
  223. '''
  224. Mock of Elasticsearch client
  225. '''
  226. indices = MockElasticIndices()
  227. with patch.object(elasticsearch, '_get_instance',
  228. MagicMock(return_value=MockElastic())):
  229. self.assertTrue(elasticsearch.alias_create("foo", "bar", body="baz"))
  230. def test_alias_create_unack(self):
  231. '''
  232. Test if alias creation is not acked
  233. '''
  234. class MockElasticIndices(object):
  235. '''
  236. Mock of Elasticsearch IndicesClient
  237. '''
  238. def put_alias(self, index=None, name=None, body=None):
  239. '''
  240. Mock of put_alias method
  241. '''
  242. return {"acknowledged": False}
  243. class MockElastic(object):
  244. '''
  245. Mock of Elasticsearch client
  246. '''
  247. indices = MockElasticIndices()
  248. with patch.object(elasticsearch, '_get_instance',
  249. MagicMock(return_value=MockElastic())):
  250. self.assertFalse(elasticsearch.alias_create("foo", "bar", body="baz"))
  251. def test_alias_create_failure(self):
  252. '''
  253. Test if alias creation fails with CommandExecutionError
  254. '''
  255. class MockElasticIndices(object):
  256. '''
  257. Mock of Elasticsearch IndicesClient
  258. '''
  259. def put_alias(self, index=None, name=None, body=None):
  260. '''
  261. Mock of put_alias method
  262. '''
  263. raise TransportError("custom message", 123)
  264. class MockElastic(object):
  265. '''
  266. Mock of Elasticsearch client
  267. '''
  268. indices = MockElasticIndices()
  269. with patch.object(elasticsearch, '_get_instance',
  270. MagicMock(return_value=MockElastic())):
  271. self.assertRaises(CommandExecutionError, elasticsearch.alias_create, "foo", "bar", body="baz")
  272. # 'alias_delete' function tests: 3
  273. def test_alias_delete(self):
  274. '''
  275. Test if alias is deleted
  276. '''
  277. class MockElasticIndices(object):
  278. '''
  279. Mock of Elasticsearch IndicesClient
  280. '''
  281. def delete_alias(self, index=None, name=None):
  282. '''
  283. Mock of delete_alias method
  284. '''
  285. return {"acknowledged": True}
  286. class MockElastic(object):
  287. '''
  288. Mock of Elasticsearch client
  289. '''
  290. indices = MockElasticIndices()
  291. with patch.object(elasticsearch, '_get_instance',
  292. MagicMock(return_value=MockElastic())):
  293. self.assertTrue(elasticsearch.alias_delete("foo", "bar"))
  294. def test_alias_delete_unack(self):
  295. '''
  296. Test if alias deletion is not acked
  297. '''
  298. class MockElasticIndices(object):
  299. '''
  300. Mock of Elasticsearch IndicesClient
  301. '''
  302. def delete_alias(self, index=None, name=None):
  303. '''
  304. Mock of delete_alias method
  305. '''
  306. return {"acknowledged": False}
  307. class MockElastic(object):
  308. '''
  309. Mock of Elasticsearch client
  310. '''
  311. indices = MockElasticIndices()
  312. with patch.object(elasticsearch, '_get_instance',
  313. MagicMock(return_value=MockElastic())):
  314. self.assertFalse(elasticsearch.alias_delete("foo", "bar"))
  315. def test_alias_delete_failure(self):
  316. '''
  317. Test if alias deletion fails with CommandExecutionError
  318. '''
  319. class MockElasticIndices(object):
  320. '''
  321. Mock of Elasticsearch IndicesClient
  322. '''
  323. def delete_alias(self, index=None, name=None):
  324. '''
  325. Mock of delete_alias method
  326. '''
  327. raise TransportError("custom message", 123)
  328. class MockElastic(object):
  329. '''
  330. Mock of Elasticsearch client
  331. '''
  332. indices = MockElasticIndices()
  333. with patch.object(elasticsearch, '_get_instance',
  334. MagicMock(return_value=MockElastic())):
  335. self.assertRaises(CommandExecutionError, elasticsearch.alias_delete, "foo", "bar")
  336. # 'alias_exists' function tests: 3
  337. def test_alias_exists(self):
  338. '''
  339. Test if alias exists
  340. '''
  341. class MockElasticIndices(object):
  342. '''
  343. Mock of Elasticsearch IndicesClient
  344. '''
  345. def exists_alias(self, index=None, name=None):
  346. '''
  347. Mock of exists_alias method
  348. '''
  349. return {"acknowledged": True}
  350. class MockElastic(object):
  351. '''
  352. Mock of Elasticsearch client
  353. '''
  354. indices = MockElasticIndices()
  355. with patch.object(elasticsearch, '_get_instance',
  356. MagicMock(return_value=MockElastic())):
  357. self.assertTrue(elasticsearch.alias_exists("foo", "bar"))
  358. def test_alias_exists_not(self):
  359. '''
  360. Test if alias doesn't exist
  361. '''
  362. class MockElasticIndices(object):
  363. '''
  364. Mock of Elasticsearch IndicesClient
  365. '''
  366. def exists_alias(self, index=None, name=None):
  367. '''
  368. Mock of exists_alias method
  369. '''
  370. raise NotFoundError
  371. class MockElastic(object):
  372. '''
  373. Mock of Elasticsearch client
  374. '''
  375. indices = MockElasticIndices()
  376. with patch.object(elasticsearch, '_get_instance',
  377. MagicMock(return_value=MockElastic())):
  378. self.assertFalse(elasticsearch.alias_exists("foo", "bar"))
  379. def test_alias_exists_failure(self):
  380. '''
  381. Test if alias status obtain fails with CommandExecutionError
  382. '''
  383. class MockElasticIndices(object):
  384. '''
  385. Mock of Elasticsearch IndicesClient
  386. '''
  387. def exists_alias(self, index=None, name=None):
  388. '''
  389. Mock of exists_alias method
  390. '''
  391. raise TransportError("custom message", 123)
  392. class MockElastic(object):
  393. '''
  394. Mock of Elasticsearch client
  395. '''
  396. indices = MockElasticIndices()
  397. with patch.object(elasticsearch, '_get_instance',
  398. MagicMock(return_value=MockElastic())):
  399. self.assertRaises(CommandExecutionError, elasticsearch.alias_exists, "foo", "bar")
  400. # 'alias_get' function tests: 3
  401. def test_alias_get(self):
  402. '''
  403. Test if alias can be obtained
  404. '''
  405. class MockElasticIndices(object):
  406. '''
  407. Mock of Elasticsearch IndicesClient
  408. '''
  409. def get_alias(self, index=None, name=None):
  410. '''
  411. Mock of get_alias method
  412. '''
  413. return {"test": "key"}
  414. class MockElastic(object):
  415. '''
  416. Mock of Elasticsearch client
  417. '''
  418. indices = MockElasticIndices()
  419. with patch.object(elasticsearch, '_get_instance',
  420. MagicMock(return_value=MockElastic())):
  421. self.assertDictEqual(elasticsearch.alias_get("foo", "bar"), {"test": "key"})
  422. def test_alias_get_not(self):
  423. '''
  424. Test if alias doesn't exist
  425. '''
  426. class MockElasticIndices(object):
  427. '''
  428. Mock of Elasticsearch IndicesClient
  429. '''
  430. def get_alias(self, index=None, name=None):
  431. '''
  432. Mock of get_alias method
  433. '''
  434. raise NotFoundError
  435. class MockElastic(object):
  436. '''
  437. Mock of Elasticsearch client
  438. '''
  439. indices = MockElasticIndices()
  440. with patch.object(elasticsearch, '_get_instance',
  441. MagicMock(return_value=MockElastic())):
  442. self.assertIs(elasticsearch.alias_get("foo", "bar"), None)
  443. def test_alias_get_failure(self):
  444. '''
  445. Test if alias obtain fails with CommandExecutionError
  446. '''
  447. class MockElasticIndices(object):
  448. '''
  449. Mock of Elasticsearch IndicesClient
  450. '''
  451. def get_alias(self, index=None, name=None):
  452. '''
  453. Mock of get_alias method
  454. '''
  455. raise TransportError("custom message", 123)
  456. class MockElastic(object):
  457. '''
  458. Mock of Elasticsearch client
  459. '''
  460. indices = MockElasticIndices()
  461. with patch.object(elasticsearch, '_get_instance',
  462. MagicMock(return_value=MockElastic())):
  463. self.assertRaises(CommandExecutionError, elasticsearch.alias_get, "foo", "bar")
  464. # 'document_create' function tests: 2
  465. def test_document_create(self):
  466. '''
  467. Test if document can be created
  468. '''
  469. class MockElastic(object):
  470. '''
  471. Mock of Elasticsearch client
  472. '''
  473. def index(self, index=None, doc_type=None, body=None, id=None):
  474. '''
  475. Mock of index method
  476. '''
  477. return {"test": "key"}
  478. with patch.object(elasticsearch, '_get_instance',
  479. MagicMock(return_value=MockElastic())):
  480. self.assertDictEqual(elasticsearch.document_create("foo", "bar"), {"test": "key"})
  481. def test_document_create_failure(self):
  482. '''
  483. Test if document creation fails with CommandExecutionError
  484. '''
  485. class MockElastic(object):
  486. '''
  487. Mock of Elasticsearch client
  488. '''
  489. def index(self, index=None, doc_type=None, body=None, id=None):
  490. '''
  491. Mock of index method
  492. '''
  493. raise TransportError("custom message", 123)
  494. with patch.object(elasticsearch, '_get_instance',
  495. MagicMock(return_value=MockElastic())):
  496. self.assertRaises(CommandExecutionError, elasticsearch.document_create, "foo", "bar")
  497. # 'document_delete' function tests: 2
  498. def test_document_delete(self):
  499. '''
  500. Test if document can be deleted
  501. '''
  502. class MockElastic(object):
  503. '''
  504. Mock of Elasticsearch client
  505. '''
  506. def delete(self, index=None, doc_type=None, id=None):
  507. '''
  508. Mock of index method
  509. '''
  510. return {"test": "key"}
  511. with patch.object(elasticsearch, '_get_instance',
  512. MagicMock(return_value=MockElastic())):
  513. self.assertDictEqual(elasticsearch.document_delete("foo", "bar", "baz"), {"test": "key"})
  514. def test_document_delete_failure(self):
  515. '''
  516. Test if document deletion fails with CommandExecutionError
  517. '''
  518. class MockElastic(object):
  519. '''
  520. Mock of Elasticsearch client
  521. '''
  522. def delete(self, index=None, doc_type=None, id=None):
  523. '''
  524. Mock of index method
  525. '''
  526. raise TransportError("custom message", 123)
  527. with patch.object(elasticsearch, '_get_instance',
  528. MagicMock(return_value=MockElastic())):
  529. self.assertRaises(CommandExecutionError, elasticsearch.document_delete, "foo", "bar", "baz")
  530. # 'document_exists' function tests: 3
  531. def test_document_exists(self):
  532. '''
  533. Test if document status can be obtained
  534. '''
  535. class MockElastic(object):
  536. '''
  537. Mock of Elasticsearch client
  538. '''
  539. def exists(self, index=None, doc_type=None, id=None):
  540. '''
  541. Mock of index method
  542. '''
  543. return True
  544. with patch.object(elasticsearch, '_get_instance',
  545. MagicMock(return_value=MockElastic())):
  546. self.assertTrue(elasticsearch.document_exists("foo", "bar"))
  547. def test_document_exists_not(self):
  548. '''
  549. Test if document doesn't exist
  550. '''
  551. class MockElastic(object):
  552. '''
  553. Mock of Elasticsearch client
  554. '''
  555. def exists(self, index=None, doc_type=None, id=None):
  556. '''
  557. Mock of index method
  558. '''
  559. return False
  560. with patch.object(elasticsearch, '_get_instance',
  561. MagicMock(return_value=MockElastic())):
  562. self.assertFalse(elasticsearch.document_exists("foo", "bar"))
  563. def test_document_exists_failure(self):
  564. '''
  565. Test if document exist state fails with CommandExecutionError
  566. '''
  567. class MockElastic(object):
  568. '''
  569. Mock of Elasticsearch client
  570. '''
  571. def exists(self, index=None, doc_type=None, id=None):
  572. '''
  573. Mock of index method
  574. '''
  575. raise TransportError("custom message", 123)
  576. with patch.object(elasticsearch, '_get_instance',
  577. MagicMock(return_value=MockElastic())):
  578. self.assertRaises(CommandExecutionError, elasticsearch.document_exists, "foo", "bar")
  579. # 'document_get' function tests: 3
  580. def test_document_get(self):
  581. '''
  582. Test if document exists
  583. '''
  584. class MockElastic(object):
  585. '''
  586. Mock of Elasticsearch client
  587. '''
  588. def get(self, index=None, doc_type=None, id=None):
  589. '''
  590. Mock of index method
  591. '''
  592. return {"test": "key"}
  593. with patch.object(elasticsearch, '_get_instance',
  594. MagicMock(return_value=MockElastic())):
  595. self.assertDictEqual(elasticsearch.document_get("foo", "bar"), {"test": "key"})
  596. def test_document_get_not(self):
  597. '''
  598. Test if document doesn't exit
  599. '''
  600. class MockElastic(object):
  601. '''
  602. Mock of Elasticsearch client
  603. '''
  604. def get(self, index=None, doc_type=None, id=None):
  605. '''
  606. Mock of index method
  607. '''
  608. raise NotFoundError
  609. with patch.object(elasticsearch, '_get_instance',
  610. MagicMock(return_value=MockElastic())):
  611. self.assertIs(elasticsearch.document_get("foo", "bar"), None)
  612. def test_document_get_failure(self):
  613. '''
  614. Test if document obtain fails with CommandExecutionError
  615. '''
  616. class MockElastic(object):
  617. '''
  618. Mock of Elasticsearch client
  619. '''
  620. def get(self, index=None, doc_type=None, id=None):
  621. '''
  622. Mock of index method
  623. '''
  624. raise TransportError("custom message", 123)
  625. with patch.object(elasticsearch, '_get_instance',
  626. MagicMock(return_value=MockElastic())):
  627. self.assertRaises(CommandExecutionError, elasticsearch.document_get, "foo", "bar")
  628. # 'index_create' function tests: 5
  629. def test_index_create(self):
  630. '''
  631. Test if index can be created
  632. '''
  633. class MockElasticIndices(object):
  634. '''
  635. Mock of Elasticsearch IndicesClient
  636. '''
  637. def create(self, index=None, body=None):
  638. '''
  639. Mock of index method
  640. '''
  641. return {"acknowledged": True, "shards_acknowledged": True}
  642. class MockElastic(object):
  643. '''
  644. Mock of Elasticsearch client
  645. '''
  646. indices = MockElasticIndices()
  647. with patch.object(elasticsearch, '_get_instance',
  648. MagicMock(return_value=MockElastic())):
  649. self.assertTrue(elasticsearch.index_create("foo", "bar"))
  650. def test_index_create_no_shards(self):
  651. '''
  652. Test if index is created and no shards info is returned
  653. '''
  654. class MockElasticIndices(object):
  655. '''
  656. Mock of Elasticsearch IndicesClient
  657. '''
  658. def create(self, index=None, body=None):
  659. '''
  660. Mock of index method
  661. '''
  662. return {"acknowledged": True}
  663. class MockElastic(object):
  664. '''
  665. Mock of Elasticsearch client
  666. '''
  667. indices = MockElasticIndices()
  668. with patch.object(elasticsearch, '_get_instance',
  669. MagicMock(return_value=MockElastic())):
  670. self.assertTrue(elasticsearch.index_create("foo", "bar"))
  671. def test_index_create_not_shards(self):
  672. '''
  673. Test if index is created and shards didn't acked
  674. '''
  675. class MockElasticIndices(object):
  676. '''
  677. Mock of Elasticsearch IndicesClient
  678. '''
  679. def create(self, index=None, body=None):
  680. '''
  681. Mock of index method
  682. '''
  683. return {"acknowledged": True, "shards_acknowledged": False}
  684. class MockElastic(object):
  685. '''
  686. Mock of Elasticsearch client
  687. '''
  688. indices = MockElasticIndices()
  689. with patch.object(elasticsearch, '_get_instance',
  690. MagicMock(return_value=MockElastic())):
  691. self.assertFalse(elasticsearch.index_create("foo", "bar"))
  692. def test_index_create_not(self):
  693. '''
  694. Test if index is created and shards didn't acked
  695. '''
  696. class MockElasticIndices(object):
  697. '''
  698. Mock of Elasticsearch IndicesClient
  699. '''
  700. def create(self, index=None, body=None):
  701. '''
  702. Mock of index method
  703. '''
  704. return {"acknowledged": False, "shards_acknowledged": False}
  705. class MockElastic(object):
  706. '''
  707. Mock of Elasticsearch client
  708. '''
  709. indices = MockElasticIndices()
  710. with patch.object(elasticsearch, '_get_instance',
  711. MagicMock(return_value=MockElastic())):
  712. self.assertFalse(elasticsearch.index_create("foo", "bar"))
  713. def test_index_create_failure(self):
  714. '''
  715. Test if index creation fails with CommandExecutionError
  716. '''
  717. class MockElasticIndices(object):
  718. '''
  719. Mock of Elasticsearch IndicesClient
  720. '''
  721. def create(self, index=None, body=None):
  722. '''
  723. Mock of index method
  724. '''
  725. raise TransportError("custom message", 123)
  726. class MockElastic(object):
  727. '''
  728. Mock of Elasticsearch client
  729. '''
  730. indices = MockElasticIndices()
  731. with patch.object(elasticsearch, '_get_instance',
  732. MagicMock(return_value=MockElastic())):
  733. self.assertRaises(CommandExecutionError, elasticsearch.index_create, "foo", "bar")
  734. # 'index_delete' function tests: 3
  735. def test_index_delete(self):
  736. '''
  737. Test if index can be deleted
  738. '''
  739. class MockElasticIndices(object):
  740. '''
  741. Mock of Elasticsearch IndicesClient
  742. '''
  743. def delete(self, index=None):
  744. '''
  745. Mock of index method
  746. '''
  747. return {"acknowledged": True}
  748. class MockElastic(object):
  749. '''
  750. Mock of Elasticsearch client
  751. '''
  752. indices = MockElasticIndices()
  753. with patch.object(elasticsearch, '_get_instance',
  754. MagicMock(return_value=MockElastic())):
  755. self.assertTrue(elasticsearch.index_delete("foo", "bar"))
  756. def test_index_delete_not(self):
  757. '''
  758. Test if index is deleted and shards didn't acked
  759. '''
  760. class MockElasticIndices(object):
  761. '''
  762. Mock of Elasticsearch IndicesClient
  763. '''
  764. def delete(self, index=None):
  765. '''
  766. Mock of index method
  767. '''
  768. return {"acknowledged": False}
  769. class MockElastic(object):
  770. '''
  771. Mock of Elasticsearch client
  772. '''
  773. indices = MockElasticIndices()
  774. with patch.object(elasticsearch, '_get_instance',
  775. MagicMock(return_value=MockElastic())):
  776. self.assertFalse(elasticsearch.index_delete("foo", "bar"))
  777. def test_index_delete_failure(self):
  778. '''
  779. Test if index deletion fails with CommandExecutionError
  780. '''
  781. class MockElasticIndices(object):
  782. '''
  783. Mock of Elasticsearch IndicesClient
  784. '''
  785. def delete(self, index=None):
  786. '''
  787. Mock of index method
  788. '''
  789. raise TransportError("custom message", 123)
  790. class MockElastic(object):
  791. '''
  792. Mock of Elasticsearch client
  793. '''
  794. indices = MockElasticIndices()
  795. with patch.object(elasticsearch, '_get_instance',
  796. MagicMock(return_value=MockElastic())):
  797. self.assertRaises(CommandExecutionError, elasticsearch.index_delete, "foo", "bar")
  798. # 'index_exists' function tests: 3
  799. def test_index_exists(self):
  800. '''
  801. Test if index exists
  802. '''
  803. class MockElasticIndices(object):
  804. '''
  805. Mock of Elasticsearch IndicesClient
  806. '''
  807. def exists(self, index=None):
  808. '''
  809. Mock of index method
  810. '''
  811. return True
  812. class MockElastic(object):
  813. '''
  814. Mock of Elasticsearch client
  815. '''
  816. indices = MockElasticIndices()
  817. with patch.object(elasticsearch, '_get_instance',
  818. MagicMock(return_value=MockElastic())):
  819. self.assertTrue(elasticsearch.index_exists("foo", "bar"))
  820. def test_index_exists_not(self):
  821. '''
  822. Test if index doesn't exist
  823. '''
  824. class MockElasticIndices(object):
  825. '''
  826. Mock of Elasticsearch IndicesClient
  827. '''
  828. def exists(self, index=None):
  829. '''
  830. Mock of index method
  831. '''
  832. raise NotFoundError
  833. class MockElastic(object):
  834. '''
  835. Mock of Elasticsearch client
  836. '''
  837. indices = MockElasticIndices()
  838. with patch.object(elasticsearch, '_get_instance',
  839. MagicMock(return_value=MockElastic())):
  840. self.assertFalse(elasticsearch.index_exists("foo", "bar"))
  841. def test_index_exists_failure(self):
  842. '''
  843. Test if alias exist state fails with CommandExecutionError
  844. '''
  845. class MockElasticIndices(object):
  846. '''
  847. Mock of Elasticsearch IndicesClient
  848. '''
  849. def exists(self, index=None):
  850. '''
  851. Mock of index method
  852. '''
  853. raise TransportError("custom message", 123)
  854. class MockElastic(object):
  855. '''
  856. Mock of Elasticsearch client
  857. '''
  858. indices = MockElasticIndices()
  859. with patch.object(elasticsearch, '_get_instance',
  860. MagicMock(return_value=MockElastic())):
  861. self.assertRaises(CommandExecutionError, elasticsearch.index_exists, "foo", "bar")
  862. # 'index_get' function tests: 3
  863. def test_index_get(self):
  864. '''
  865. Test if index can be obtained
  866. '''
  867. class MockElasticIndices(object):
  868. '''
  869. Mock of Elasticsearch IndicesClient
  870. '''
  871. def get(self, index=None):
  872. '''
  873. Mock of index method
  874. '''
  875. return {"test": "key"}
  876. class MockElastic(object):
  877. '''
  878. Mock of Elasticsearch client
  879. '''
  880. indices = MockElasticIndices()
  881. with patch.object(elasticsearch, '_get_instance',
  882. MagicMock(return_value=MockElastic())):
  883. self.assertDictEqual(elasticsearch.index_get("foo", "bar"), {"test": "key"})
  884. def test_index_get_not(self):
  885. '''
  886. Test if index doesn't exist
  887. '''
  888. class MockElasticIndices(object):
  889. '''
  890. Mock of Elasticsearch IndicesClient
  891. '''
  892. def get(self, index=None):
  893. '''
  894. Mock of index method
  895. '''
  896. raise NotFoundError
  897. class MockElastic(object):
  898. '''
  899. Mock of Elasticsearch client
  900. '''
  901. indices = MockElasticIndices()
  902. with patch.object(elasticsearch, '_get_instance',
  903. MagicMock(return_value=MockElastic())):
  904. self.assertIs(elasticsearch.index_get("foo", "bar"), None)
  905. def test_index_get_failure(self):
  906. '''
  907. Test if index obtain fails with CommandExecutionError
  908. '''
  909. class MockElasticIndices(object):
  910. '''
  911. Mock of Elasticsearch IndicesClient
  912. '''
  913. def get(self, index=None):
  914. '''
  915. Mock of index method
  916. '''
  917. raise TransportError("custom message", 123)
  918. class MockElastic(object):
  919. '''
  920. Mock of Elasticsearch client
  921. '''
  922. indices = MockElasticIndices()
  923. with patch.object(elasticsearch, '_get_instance',
  924. MagicMock(return_value=MockElastic())):
  925. self.assertRaises(CommandExecutionError, elasticsearch.index_get, "foo", "bar")
  926. # 'index_open' function tests: 3
  927. def test_index_open(self):
  928. '''
  929. Test if index can be opened
  930. '''
  931. class MockElasticIndices(object):
  932. '''
  933. Mock of Elasticsearch IndicesClient
  934. '''
  935. def open(self, index=None, allow_no_indices=None, expand_wildcards=None, ignore_unavailable=None):
  936. '''
  937. Mock of index method
  938. '''
  939. return {"acknowledged": True}
  940. class MockElastic(object):
  941. '''
  942. Mock of Elasticsearch client
  943. '''
  944. indices = MockElasticIndices()
  945. with patch.object(elasticsearch, '_get_instance',
  946. MagicMock(return_value=MockElastic())):
  947. self.assertTrue(elasticsearch.index_open("foo", "bar"))
  948. def test_index_open_not(self):
  949. '''
  950. Test if index open isn't acked
  951. '''
  952. class MockElasticIndices(object):
  953. '''
  954. Mock of Elasticsearch IndicesClient
  955. '''
  956. def open(self, index=None, allow_no_indices=None, expand_wildcards=None, ignore_unavailable=None):
  957. '''
  958. Mock of index method
  959. '''
  960. return {"acknowledged": False}
  961. class MockElastic(object):
  962. '''
  963. Mock of Elasticsearch client
  964. '''
  965. indices = MockElasticIndices()
  966. with patch.object(elasticsearch, '_get_instance',
  967. MagicMock(return_value=MockElastic())):
  968. self.assertFalse(elasticsearch.index_open("foo", "bar"))
  969. def test_index_open_failure(self):
  970. '''
  971. Test if alias opening fails with CommandExecutionError
  972. '''
  973. class MockElasticIndices(object):
  974. '''
  975. Mock of Elasticsearch IndicesClient
  976. '''
  977. def open(self, index=None, allow_no_indices=None, expand_wildcards=None, ignore_unavailable=None):
  978. '''
  979. Mock of index method
  980. '''
  981. raise TransportError("custom message", 123)
  982. class MockElastic(object):
  983. '''
  984. Mock of Elasticsearch client
  985. '''
  986. indices = MockElasticIndices()
  987. with patch.object(elasticsearch, '_get_instance',
  988. MagicMock(return_value=MockElastic())):
  989. self.assertRaises(CommandExecutionError, elasticsearch.index_open, "foo", "bar")
  990. # 'index_close' function tests: 3
  991. def test_index_close(self):
  992. '''
  993. Test if index can be closed
  994. '''
  995. class MockElasticIndices(object):
  996. '''
  997. Mock of Elasticsearch IndicesClient
  998. '''
  999. def close(self, index=None, allow_no_indices=None, expand_wildcards=None, ignore_unavailable=None):
  1000. '''
  1001. Mock of index method
  1002. '''
  1003. return {"acknowledged": True}
  1004. class MockElastic(object):
  1005. '''
  1006. Mock of Elasticsearch client
  1007. '''
  1008. indices = MockElasticIndices()
  1009. with patch.object(elasticsearch, '_get_instance',
  1010. MagicMock(return_value=MockElastic())):
  1011. self.assertTrue(elasticsearch.index_close("foo", "bar"))
  1012. def test_index_close_not(self):
  1013. '''
  1014. Test if index close isn't acked
  1015. '''
  1016. class MockElasticIndices(object):
  1017. '''
  1018. Mock of Elasticsearch IndicesClient
  1019. '''
  1020. def close(self, index=None, allow_no_indices=None, expand_wildcards=None, ignore_unavailable=None):
  1021. '''
  1022. Mock of index method
  1023. '''
  1024. return {"acknowledged": False}
  1025. class MockElastic(object):
  1026. '''
  1027. Mock of Elasticsearch client
  1028. '''
  1029. indices = MockElasticIndices()
  1030. with patch.object(elasticsearch, '_get_instance',
  1031. MagicMock(return_value=MockElastic())):
  1032. self.assertFalse(elasticsearch.index_close("foo", "bar"))
  1033. def test_index_close_failure(self):
  1034. '''
  1035. Test if index closing fails with CommandExecutionError
  1036. '''
  1037. class MockElasticIndices(object):
  1038. '''
  1039. Mock of Elasticsearch IndicesClient
  1040. '''
  1041. def close(self, index=None, allow_no_indices=None, expand_wildcards=None, ignore_unavailable=None):
  1042. '''
  1043. Mock of index method
  1044. '''
  1045. raise TransportError("custom message", 123)
  1046. class MockElastic(object):
  1047. '''
  1048. Mock of Elasticsearch client
  1049. '''
  1050. indices = MockElasticIndices()
  1051. with patch.object(elasticsearch, '_get_instance',
  1052. MagicMock(return_value=MockElastic())):
  1053. self.assertRaises(CommandExecutionError, elasticsearch.index_close, "foo", "bar")
  1054. # 'mapping_create' function tests: 3
  1055. def test_mapping_create(self):
  1056. '''
  1057. Test if mapping can be created
  1058. '''
  1059. class MockElasticIndices(object):
  1060. '''
  1061. Mock of Elasticsearch IndicesClient
  1062. '''
  1063. def put_mapping(self, index=None, doc_type=None, body=None):
  1064. '''
  1065. Mock of put_mapping method
  1066. '''
  1067. return {"acknowledged": True}
  1068. class MockElastic(object):
  1069. '''
  1070. Mock of Elasticsearch client
  1071. '''
  1072. indices = MockElasticIndices()
  1073. with patch.object(elasticsearch, '_get_instance',
  1074. MagicMock(return_value=MockElastic())):
  1075. self.assertTrue(elasticsearch.mapping_create("foo", "bar", "baz"))
  1076. def test_mapping_create_not(self):
  1077. '''
  1078. Test if mapping creation didn't ack
  1079. '''
  1080. class MockElasticIndices(object):
  1081. '''
  1082. Mock of Elasticsearch IndicesClient
  1083. '''
  1084. def put_mapping(self, index=None, doc_type=None, body=None):
  1085. '''
  1086. Mock of put_mapping method
  1087. '''
  1088. return {"acknowledged": False}
  1089. class MockElastic(object):
  1090. '''
  1091. Mock of Elasticsearch client
  1092. '''
  1093. indices = MockElasticIndices()
  1094. with patch.object(elasticsearch, '_get_instance',
  1095. MagicMock(return_value=MockElastic())):
  1096. self.assertFalse(elasticsearch.mapping_create("foo", "bar", "baz"))
  1097. def test_mapping_create_failure(self):
  1098. '''
  1099. Test if mapping creation fails
  1100. '''
  1101. class MockElasticIndices(object):
  1102. '''
  1103. Mock of Elasticsearch IndicesClient
  1104. '''
  1105. def put_mapping(self, index=None, doc_type=None, body=None):
  1106. '''
  1107. Mock of put_mapping method
  1108. '''
  1109. raise TransportError("custom message", 123)
  1110. class MockElastic(object):
  1111. '''
  1112. Mock of Elasticsearch client
  1113. '''
  1114. indices = MockElasticIndices()
  1115. with patch.object(elasticsearch, '_get_instance',
  1116. MagicMock(return_value=MockElastic())):
  1117. self.assertRaises(CommandExecutionError, elasticsearch.mapping_create, "foo", "bar", "baz")
  1118. # 'mapping_delete' function tests: 3
  1119. def test_mapping_delete(self):
  1120. '''
  1121. Test if mapping can be created
  1122. '''
  1123. class MockElasticIndices(object):
  1124. '''
  1125. Mock of Elasticsearch IndicesClient
  1126. '''
  1127. def delete_mapping(self, index=None, doc_type=None):
  1128. '''
  1129. Mock of put_mapping method
  1130. '''
  1131. return {"acknowledged": True}
  1132. class MockElastic(object):
  1133. '''
  1134. Mock of Elasticsearch client
  1135. '''
  1136. indices = MockElasticIndices()
  1137. with patch.object(elasticsearch, '_get_instance',
  1138. MagicMock(return_value=MockElastic())):
  1139. self.assertTrue(elasticsearch.mapping_delete("foo", "bar", "baz"))
  1140. def test_mapping_delete_not(self):
  1141. '''
  1142. Test if mapping creation didn't ack
  1143. '''
  1144. class MockElasticIndices(object):
  1145. '''
  1146. Mock of Elasticsearch IndicesClient
  1147. '''
  1148. def delete_mapping(self, index=None, doc_type=None):
  1149. '''
  1150. Mock of put_mapping method
  1151. '''
  1152. return {"acknowledged": False}
  1153. class MockElastic(object):
  1154. '''
  1155. Mock of Elasticsearch client
  1156. '''
  1157. indices = MockElasticIndices()
  1158. with patch.object(elasticsearch, '_get_instance',
  1159. MagicMock(return_value=MockElastic())):
  1160. self.assertFalse(elasticsearch.mapping_delete("foo", "bar", "baz"))
  1161. def test_mapping_delete_failure(self):
  1162. '''
  1163. Test if mapping creation fails
  1164. '''
  1165. class MockElasticIndices(object):
  1166. '''
  1167. Mock of Elasticsearch IndicesClient
  1168. '''
  1169. def delete_mapping(self, index=None, doc_type=None):
  1170. '''
  1171. Mock of put_mapping method
  1172. '''
  1173. raise TransportError("custom message", 123)
  1174. class MockElastic(object):
  1175. '''
  1176. Mock of Elasticsearch client
  1177. '''
  1178. indices = MockElasticIndices()
  1179. with patch.object(elasticsearch, '_get_instance',
  1180. MagicMock(return_value=MockElastic())):
  1181. self.assertRaises(CommandExecutionError, elasticsearch.mapping_delete, "foo", "bar", "baz")
  1182. # 'mapping_get' function tests: 3
  1183. def test_mapping_get(self):
  1184. '''
  1185. Test if mapping can be created
  1186. '''
  1187. class MockElasticIndices(object):
  1188. '''
  1189. Mock of Elasticsearch IndicesClient
  1190. '''
  1191. def get_mapping(self, index=None, doc_type=None):
  1192. '''
  1193. Mock of get_mapping method
  1194. '''
  1195. return {"test": "key"}
  1196. class MockElastic(object):
  1197. '''
  1198. Mock of Elasticsearch client
  1199. '''
  1200. indices = MockElasticIndices()
  1201. with patch.object(elasticsearch, '_get_instance',
  1202. MagicMock(return_value=MockElastic())):
  1203. self.assertDictEqual(elasticsearch.mapping_get("foo", "bar", "baz"), {"test": "key"})
  1204. def test_mapping_get_not(self):
  1205. '''
  1206. Test if mapping creation didn't ack
  1207. '''
  1208. class MockElasticIndices(object):
  1209. '''
  1210. Mock of Elasticsearch IndicesClient
  1211. '''
  1212. def get_mapping(self, index=None, doc_type=None):
  1213. '''
  1214. Mock of get_mapping method
  1215. '''
  1216. raise NotFoundError
  1217. class MockElastic(object):
  1218. '''
  1219. Mock of Elasticsearch client
  1220. '''
  1221. indices = MockElasticIndices()
  1222. with patch.object(elasticsearch, '_get_instance',
  1223. MagicMock(return_value=MockElastic())):
  1224. self.assertIs(elasticsearch.mapping_get("foo", "bar", "baz"), None)
  1225. def test_mapping_get_failure(self):
  1226. '''
  1227. Test if mapping creation fails
  1228. '''
  1229. class MockElasticIndices(object):
  1230. '''
  1231. Mock of Elasticsearch IndicesClient
  1232. '''
  1233. def get_mapping(self, index=None, doc_type=None):
  1234. '''
  1235. Mock of get_mapping method
  1236. '''
  1237. raise TransportError("custom message", 123)
  1238. class MockElastic(object):
  1239. '''
  1240. Mock of Elasticsearch client
  1241. '''
  1242. indices = MockElasticIndices()
  1243. with patch.object(elasticsearch, '_get_instance',
  1244. MagicMock(return_value=MockElastic())):
  1245. self.assertRaises(CommandExecutionError, elasticsearch.mapping_get, "foo", "bar", "baz")
  1246. # 'index_template_create' function tests: 3
  1247. def test_index_template_create(self):
  1248. '''
  1249. Test if mapping can be created
  1250. '''
  1251. class MockElasticIndices(object):
  1252. '''
  1253. Mock of Elasticsearch IndicesClient
  1254. '''
  1255. def put_template(self, name=None, body=None):
  1256. '''
  1257. Mock of put_template method
  1258. '''
  1259. return {"acknowledged": True}
  1260. class MockElastic(object):
  1261. '''
  1262. Mock of Elasticsearch client
  1263. '''
  1264. indices = MockElasticIndices()
  1265. with patch.object(elasticsearch, '_get_instance',
  1266. MagicMock(return_value=MockElastic())):
  1267. self.assertTrue(elasticsearch.index_template_create("foo", "bar"))
  1268. def test_index_template_create_not(self):
  1269. '''
  1270. Test if mapping creation didn't ack
  1271. '''
  1272. class MockElasticIndices(object):
  1273. '''
  1274. Mock of Elasticsearch IndicesClient
  1275. '''
  1276. def put_template(self, name=None, body=None):
  1277. '''
  1278. Mock of put_template method
  1279. '''
  1280. return {"acknowledged": False}
  1281. class MockElastic(object):
  1282. '''
  1283. Mock of Elasticsearch client
  1284. '''
  1285. indices = MockElasticIndices()
  1286. with patch.object(elasticsearch, '_get_instance',
  1287. MagicMock(return_value=MockElastic())):
  1288. self.assertFalse(elasticsearch.index_template_create("foo", "bar"))
  1289. def test_index_template_create_failure(self):
  1290. '''
  1291. Test if mapping creation fails
  1292. '''
  1293. class MockElasticIndices(object):
  1294. '''
  1295. Mock of Elasticsearch IndicesClient
  1296. '''
  1297. def put_template(self, name=None, body=None):
  1298. '''
  1299. Mock of put_template method
  1300. '''
  1301. raise TransportError("custom message", 123)
  1302. class MockElastic(object):
  1303. '''
  1304. Mock of Elasticsearch client
  1305. '''
  1306. indices = MockElasticIndices()
  1307. with patch.object(elasticsearch, '_get_instance',
  1308. MagicMock(return_value=MockElastic())):
  1309. self.assertRaises(CommandExecutionError, elasticsearch.index_template_create, "foo", "bar")
  1310. # 'index_template_delete' function tests: 3
  1311. def test_index_template_delete(self):
  1312. '''
  1313. Test if mapping can be created
  1314. '''
  1315. class MockElasticIndices(object):
  1316. '''
  1317. Mock of Elasticsearch IndicesClient
  1318. '''
  1319. def delete_template(self, name=None):
  1320. '''
  1321. Mock of delete_template method
  1322. '''
  1323. return {"acknowledged": True}
  1324. class MockElastic(object):
  1325. '''
  1326. Mock of Elasticsearch client
  1327. '''
  1328. indices = MockElasticIndices()
  1329. with patch.object(elasticsearch, '_get_instance',
  1330. MagicMock(return_value=MockElastic())):
  1331. self.assertTrue(elasticsearch.index_template_delete("foo"))
  1332. def test_index_template_delete_not(self):
  1333. '''
  1334. Test if mapping creation didn't ack
  1335. '''
  1336. class MockElasticIndices(object):
  1337. '''
  1338. Mock of Elasticsearch IndicesClient
  1339. '''
  1340. def delete_template(self, name=None):
  1341. '''
  1342. Mock of delete_template method
  1343. '''
  1344. return {"acknowledged": False}
  1345. class MockElastic(object):
  1346. '''
  1347. Mock of Elasticsearch client
  1348. '''
  1349. indices = MockElasticIndices()
  1350. with patch.object(elasticsearch, '_get_instance',
  1351. MagicMock(return_value=MockElastic())):
  1352. self.assertFalse(elasticsearch.index_template_delete("foo"))
  1353. def test_index_template_delete_failure(self):
  1354. '''
  1355. Test if mapping creation fails
  1356. '''
  1357. class MockElasticIndices(object):
  1358. '''
  1359. Mock of Elasticsearch IndicesClient
  1360. '''
  1361. def delete_template(self, name=None):
  1362. '''
  1363. Mock of delete_template method
  1364. '''
  1365. raise TransportError("custom message", 123)
  1366. class MockElastic(object):
  1367. '''
  1368. Mock of Elasticsearch client
  1369. '''
  1370. indices = MockElasticIndices()
  1371. with patch.object(elasticsearch, '_get_instance',
  1372. MagicMock(return_value=MockElastic())):
  1373. self.assertRaises(CommandExecutionError, elasticsearch.index_template_delete, "foo")
  1374. # 'index_template_exists' function tests: 3
  1375. def test_index_template_exists(self):
  1376. '''
  1377. Test if mapping can be created
  1378. '''
  1379. class MockElasticIndices(object):
  1380. '''
  1381. Mock of Elasticsearch IndicesClient
  1382. '''
  1383. def exists_template(self, name=None):
  1384. '''
  1385. Mock of exists_template method
  1386. '''
  1387. return True
  1388. class MockElastic(object):
  1389. '''
  1390. Mock of Elasticsearch client
  1391. '''
  1392. indices = MockElasticIndices()
  1393. with patch.object(elasticsearch, '_get_instance',
  1394. MagicMock(return_value=MockElastic())):
  1395. self.assertTrue(elasticsearch.index_template_exists("foo"))
  1396. def test_index_template_exists_not(self):
  1397. '''
  1398. Test if mapping creation didn't ack
  1399. '''
  1400. class MockElasticIndices(object):
  1401. '''
  1402. Mock of Elasticsearch IndicesClient
  1403. '''
  1404. def exists_template(self, name=None):
  1405. '''
  1406. Mock of exists_template method
  1407. '''
  1408. return False
  1409. class MockElastic(object):
  1410. '''
  1411. Mock of Elasticsearch client
  1412. '''
  1413. indices = MockElasticIndices()
  1414. with patch.object(elasticsearch, '_get_instance',
  1415. MagicMock(return_value=MockElastic())):
  1416. self.assertFalse(elasticsearch.index_template_exists("foo"))
  1417. def test_index_template_exists_failure(self):
  1418. '''
  1419. Test if mapping creation fails
  1420. '''
  1421. class MockElasticIndices(object):
  1422. '''
  1423. Mock of Elasticsearch IndicesClient
  1424. '''
  1425. def exists_template(self, name=None):
  1426. '''
  1427. Mock of exists_template method
  1428. '''
  1429. raise TransportError("custom message", 123)
  1430. class MockElastic(object):
  1431. '''
  1432. Mock of Elasticsearch client
  1433. '''
  1434. indices = MockElasticIndices()
  1435. with patch.object(elasticsearch, '_get_instance',
  1436. MagicMock(return_value=MockElastic())):
  1437. self.assertRaises(CommandExecutionError, elasticsearch.index_template_exists, "foo")
  1438. # 'index_template_get' function tests: 3
  1439. def test_index_template_get(self):
  1440. '''
  1441. Test if mapping can be created
  1442. '''
  1443. class MockElasticIndices(object):
  1444. '''
  1445. Mock of Elasticsearch IndicesClient
  1446. '''
  1447. def get_template(self, name=None):
  1448. '''
  1449. Mock of get_template method
  1450. '''
  1451. return {"test": "key"}
  1452. class MockElastic(object):
  1453. '''
  1454. Mock of Elasticsearch client
  1455. '''
  1456. indices = MockElasticIndices()
  1457. with patch.object(elasticsearch, '_get_instance',
  1458. MagicMock(return_value=MockElastic())):
  1459. self.assertDictEqual(elasticsearch.index_template_get("foo"), {"test": "key"})
  1460. def test_index_template_get_not(self):
  1461. '''
  1462. Test if mapping creation didn't ack
  1463. '''
  1464. class MockElasticIndices(object):
  1465. '''
  1466. Mock of Elasticsearch IndicesClient
  1467. '''
  1468. def get_template(self, name=None):
  1469. '''
  1470. Mock of get_template method
  1471. '''
  1472. raise NotFoundError
  1473. class MockElastic(object):
  1474. '''
  1475. Mock of Elasticsearch client
  1476. '''
  1477. indices = MockElasticIndices()
  1478. with patch.object(elasticsearch, '_get_instance',
  1479. MagicMock(return_value=MockElastic())):
  1480. self.assertIs(elasticsearch.index_template_get("foo"), None)
  1481. def test_index_template_get_failure(self):
  1482. '''
  1483. Test if mapping creation fails
  1484. '''
  1485. class MockElasticIndices(object):
  1486. '''
  1487. Mock of Elasticsearch IndicesClient
  1488. '''
  1489. def get_template(self, name=None):
  1490. '''
  1491. Mock of get_template method
  1492. '''
  1493. raise TransportError("custom message", 123)
  1494. class MockElastic(object):
  1495. '''
  1496. Mock of Elasticsearch client
  1497. '''
  1498. indices = MockElasticIndices()
  1499. with patch.object(elasticsearch, '_get_instance',
  1500. MagicMock(return_value=MockElastic())):
  1501. self.assertRaises(CommandExecutionError, elasticsearch.index_template_get, "foo")
  1502. # 'pipeline_get' function tests: 4
  1503. def test_pipeline_get(self):
  1504. '''
  1505. Test if mapping can be created
  1506. '''
  1507. class MockElasticIngest(object):
  1508. '''
  1509. Mock of Elasticsearch IndicesClient
  1510. '''
  1511. def get_pipeline(self, id=None):
  1512. '''
  1513. Mock of get_pipeline method
  1514. '''
  1515. return {"test": "key"}
  1516. class MockElastic(object):
  1517. '''
  1518. Mock of Elasticsearch client
  1519. '''
  1520. ingest = MockElasticIngest()
  1521. with patch.object(elasticsearch, '_get_instance',
  1522. MagicMock(return_value=MockElastic())):
  1523. self.assertDictEqual(elasticsearch.pipeline_get("foo"), {"test": "key"})
  1524. def test_pipeline_get_not(self):
  1525. '''
  1526. Test if mapping creation didn't ack
  1527. '''
  1528. class MockElasticIngest(object):
  1529. '''
  1530. Mock of Elasticsearch IndicesClient
  1531. '''
  1532. def get_pipeline(self, id=None):
  1533. '''
  1534. Mock of get_pipeline method
  1535. '''
  1536. raise NotFoundError
  1537. class MockElastic(object):
  1538. '''
  1539. Mock of Elasticsearch client
  1540. '''
  1541. ingest = MockElasticIngest()
  1542. with patch.object(elasticsearch, '_get_instance',
  1543. MagicMock(return_value=MockElastic())):
  1544. self.assertIs(elasticsearch.pipeline_get("foo"), None)
  1545. def test_pipeline_get_failure(self):
  1546. '''
  1547. Test if mapping creation fails
  1548. '''
  1549. class MockElasticIngest(object):
  1550. '''
  1551. Mock of Elasticsearch IndicesClient
  1552. '''
  1553. def get_pipeline(self, id=None):
  1554. '''
  1555. Mock of get_pipeline method
  1556. '''
  1557. raise TransportError("custom message", 123)
  1558. class MockElastic(object):
  1559. '''
  1560. Mock of Elasticsearch client
  1561. '''
  1562. ingest = MockElasticIngest()
  1563. with patch.object(elasticsearch, '_get_instance',
  1564. MagicMock(return_value=MockElastic())):
  1565. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_get, "foo")
  1566. def test_pipeline_get_wrong_version(self):
  1567. '''
  1568. Test if mapping creation fails with CEE on invalid elasticsearch-py version
  1569. '''
  1570. class MockElasticIngest(object):
  1571. pass
  1572. class MockElastic(object):
  1573. '''
  1574. Mock of Elasticsearch client
  1575. '''
  1576. ingest = MockElasticIngest()
  1577. with patch.object(elasticsearch, '_get_instance',
  1578. MagicMock(return_value=MockElastic())):
  1579. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_get, "foo")
  1580. # 'pipeline_delete' function tests: 4
  1581. def test_pipeline_delete(self):
  1582. '''
  1583. Test if mapping can be created
  1584. '''
  1585. class MockElasticIngest(object):
  1586. '''
  1587. Mock of Elasticsearch IndicesClient
  1588. '''
  1589. def delete_pipeline(self, id=None):
  1590. '''
  1591. Mock of delete_pipeline method
  1592. '''
  1593. return {"acknowledged": True}
  1594. class MockElastic(object):
  1595. '''
  1596. Mock of Elasticsearch client
  1597. '''
  1598. ingest = MockElasticIngest()
  1599. with patch.object(elasticsearch, '_get_instance',
  1600. MagicMock(return_value=MockElastic())):
  1601. self.assertTrue(elasticsearch.pipeline_delete("foo"))
  1602. def test_pipeline_delete_not(self):
  1603. '''
  1604. Test if mapping creation didn't ack
  1605. '''
  1606. class MockElasticIngest(object):
  1607. '''
  1608. Mock of Elasticsearch IndicesClient
  1609. '''
  1610. def delete_pipeline(self, id=None):
  1611. '''
  1612. Mock of delete_pipeline method
  1613. '''
  1614. return {"acknowledged": False}
  1615. class MockElastic(object):
  1616. '''
  1617. Mock of Elasticsearch client
  1618. '''
  1619. ingest = MockElasticIngest()
  1620. with patch.object(elasticsearch, '_get_instance',
  1621. MagicMock(return_value=MockElastic())):
  1622. self.assertFalse(elasticsearch.pipeline_delete("foo"))
  1623. def test_pipeline_delete_failure(self):
  1624. '''
  1625. Test if mapping creation fails
  1626. '''
  1627. class MockElasticIngest(object):
  1628. '''
  1629. Mock of Elasticsearch IndicesClient
  1630. '''
  1631. def delete_pipeline(self, id=None):
  1632. '''
  1633. Mock of delete_pipeline method
  1634. '''
  1635. raise TransportError("custom message", 123)
  1636. class MockElastic(object):
  1637. '''
  1638. Mock of Elasticsearch client
  1639. '''
  1640. ingest = MockElasticIngest()
  1641. with patch.object(elasticsearch, '_get_instance',
  1642. MagicMock(return_value=MockElastic())):
  1643. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_delete, "foo")
  1644. def test_pipeline_delete_wrong_version(self):
  1645. '''
  1646. Test if mapping creation fails with CEE on invalid elasticsearch-py version
  1647. '''
  1648. class MockElasticIngest(object):
  1649. pass
  1650. class MockElastic(object):
  1651. '''
  1652. Mock of Elasticsearch client
  1653. '''
  1654. ingest = MockElasticIngest()
  1655. with patch.object(elasticsearch, '_get_instance',
  1656. MagicMock(return_value=MockElastic())):
  1657. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_delete, "foo")
  1658. # 'pipeline_create' function tests: 4
  1659. def test_pipeline_create(self):
  1660. '''
  1661. Test if mapping can be created
  1662. '''
  1663. class MockElasticIngest(object):
  1664. '''
  1665. Mock of Elasticsearch IndicesClient
  1666. '''
  1667. def put_pipeline(self, id=None, body=None):
  1668. '''
  1669. Mock of put_pipeline method
  1670. '''
  1671. return {"acknowledged": True}
  1672. class MockElastic(object):
  1673. '''
  1674. Mock of Elasticsearch client
  1675. '''
  1676. ingest = MockElasticIngest()
  1677. with patch.object(elasticsearch, '_get_instance',
  1678. MagicMock(return_value=MockElastic())):
  1679. self.assertTrue(elasticsearch.pipeline_create("foo", "bar"))
  1680. def test_pipeline_create_not(self):
  1681. '''
  1682. Test if mapping creation didn't ack
  1683. '''
  1684. class MockElasticIngest(object):
  1685. '''
  1686. Mock of Elasticsearch IndicesClient
  1687. '''
  1688. def put_pipeline(self, id=None, body=None):
  1689. '''
  1690. Mock of put_pipeline method
  1691. '''
  1692. return {"acknowledged": False}
  1693. class MockElastic(object):
  1694. '''
  1695. Mock of Elasticsearch client
  1696. '''
  1697. ingest = MockElasticIngest()
  1698. with patch.object(elasticsearch, '_get_instance',
  1699. MagicMock(return_value=MockElastic())):
  1700. self.assertFalse(elasticsearch.pipeline_create("foo", "bar"))
  1701. def test_pipeline_create_failure(self):
  1702. '''
  1703. Test if mapping creation fails
  1704. '''
  1705. class MockElasticIngest(object):
  1706. '''
  1707. Mock of Elasticsearch IndicesClient
  1708. '''
  1709. def put_pipeline(self, id=None, body=None):
  1710. '''
  1711. Mock of put_pipeline method
  1712. '''
  1713. raise TransportError("custom message", 123)
  1714. class MockElastic(object):
  1715. '''
  1716. Mock of Elasticsearch client
  1717. '''
  1718. ingest = MockElasticIngest()
  1719. with patch.object(elasticsearch, '_get_instance',
  1720. MagicMock(return_value=MockElastic())):
  1721. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_create, "foo", "bar")
  1722. def test_pipeline_create_wrong_version(self):
  1723. '''
  1724. Test if mapping creation fails with CEE on invalid elasticsearch-py version
  1725. '''
  1726. class MockElasticIngest(object):
  1727. pass
  1728. class MockElastic(object):
  1729. '''
  1730. Mock of Elasticsearch client
  1731. '''
  1732. ingest = MockElasticIngest()
  1733. with patch.object(elasticsearch, '_get_instance',
  1734. MagicMock(return_value=MockElastic())):
  1735. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_create, "foo", "bar")
  1736. # 'pipeline_simulate' function tests: 3
  1737. def test_pipeline_simulate(self):
  1738. '''
  1739. Test if mapping can be created
  1740. '''
  1741. class MockElasticIngest(object):
  1742. '''
  1743. Mock of Elasticsearch IndicesClient
  1744. '''
  1745. def simulate(self, id=None, body=None, verbose=None):
  1746. '''
  1747. Mock of simulate method
  1748. '''
  1749. return {"test": "key"}
  1750. class MockElastic(object):
  1751. '''
  1752. Mock of Elasticsearch client
  1753. '''
  1754. ingest = MockElasticIngest()
  1755. with patch.object(elasticsearch, '_get_instance',
  1756. MagicMock(return_value=MockElastic())):
  1757. self.assertDictEqual(elasticsearch.pipeline_simulate("foo", "bar"), {"test": "key"})
  1758. def test_pipeline_simulate_failure(self):
  1759. '''
  1760. Test if mapping creation fails
  1761. '''
  1762. class MockElasticIngest(object):
  1763. '''
  1764. Mock of Elasticsearch IndicesClient
  1765. '''
  1766. def simulate(self, id=None, body=None, verbose=None):
  1767. '''
  1768. Mock of simulate method
  1769. '''
  1770. raise TransportError("custom message", 123)
  1771. class MockElastic(object):
  1772. '''
  1773. Mock of Elasticsearch client
  1774. '''
  1775. ingest = MockElasticIngest()
  1776. with patch.object(elasticsearch, '_get_instance',
  1777. MagicMock(return_value=MockElastic())):
  1778. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_simulate, "foo", "bar")
  1779. def test_pipeline_simulate_wrong_version(self):
  1780. '''
  1781. Test if mapping creation fails with CEE on invalid elasticsearch-py version
  1782. '''
  1783. class MockElasticIngest(object):
  1784. pass
  1785. class MockElastic(object):
  1786. '''
  1787. Mock of Elasticsearch client
  1788. '''
  1789. ingest = MockElasticIngest()
  1790. with patch.object(elasticsearch, '_get_instance',
  1791. MagicMock(return_value=MockElastic())):
  1792. self.assertRaises(CommandExecutionError, elasticsearch.pipeline_simulate, "foo", "bar")
  1793. # 'search_template_get' function tests: 3
  1794. def test_search_template_get(self):
  1795. '''
  1796. Test if mapping can be created
  1797. '''
  1798. class MockElastic(object):
  1799. '''
  1800. Mock of Elasticsearch client
  1801. '''
  1802. def get_template(self, id=None):
  1803. '''
  1804. Mock of get_template method
  1805. '''
  1806. return {"test": "key"}
  1807. with patch.object(elasticsearch, '_get_instance',
  1808. MagicMock(return_value=MockElastic())):
  1809. self.assertDictEqual(elasticsearch.search_template_get("foo"), {"test": "key"})
  1810. def test_search_template_get_not(self):
  1811. '''
  1812. Test if mapping can be created
  1813. '''
  1814. class MockElastic(object):
  1815. '''
  1816. Mock of Elasticsearch client
  1817. '''
  1818. def get_template(self, id=None):
  1819. '''
  1820. Mock of get_template method
  1821. '''
  1822. raise NotFoundError
  1823. with patch.object(elasticsearch, '_get_instance',
  1824. MagicMock(return_value=MockElastic())):
  1825. self.assertIs(elasticsearch.search_template_get("foo"), None)
  1826. def test_search_template_get_failure(self):
  1827. '''
  1828. Test if mapping creation fails
  1829. '''
  1830. class MockElastic(object):
  1831. '''
  1832. Mock of Elasticsearch client
  1833. '''
  1834. def get_template(self, id=None):
  1835. '''
  1836. Mock of get_template method
  1837. '''
  1838. raise TransportError("custom message", 123)
  1839. with patch.object(elasticsearch, '_get_instance',
  1840. MagicMock(return_value=MockElastic())):
  1841. self.assertRaises(CommandExecutionError, elasticsearch.search_template_get, "foo")
  1842. # 'search_template_create' function tests: 3
  1843. def test_search_template_create(self):
  1844. '''
  1845. Test if mapping can be created
  1846. '''
  1847. class MockElastic(object):
  1848. '''
  1849. Mock of Elasticsearch client
  1850. '''
  1851. def put_template(self, id=None, body=None):
  1852. '''
  1853. Mock of put_template method
  1854. '''
  1855. return {"acknowledged": True}
  1856. with patch.object(elasticsearch, '_get_instance',
  1857. MagicMock(return_value=MockElastic())):
  1858. self.assertTrue(elasticsearch.search_template_create("foo", "bar"))
  1859. def test_search_template_create_not(self):
  1860. '''
  1861. Test if mapping can be created
  1862. '''
  1863. class MockElastic(object):
  1864. '''
  1865. Mock of Elasticsearch client
  1866. '''
  1867. def put_template(self, id=None, body=None):
  1868. '''
  1869. Mock of put_template method
  1870. '''
  1871. return {"acknowledged": False}
  1872. with patch.object(elasticsearch, '_get_instance',
  1873. MagicMock(return_value=MockElastic())):
  1874. self.assertFalse(elasticsearch.search_template_create("foo", "bar"))
  1875. def test_search_template_create_failure(self):
  1876. '''
  1877. Test if mapping creation fails
  1878. '''
  1879. class MockElastic(object):
  1880. '''
  1881. Mock of Elasticsearch client
  1882. '''
  1883. def put_template(self, id=None, body=None):
  1884. '''
  1885. Mock of put_template method
  1886. '''
  1887. raise TransportError("custom message", 123)
  1888. with patch.object(elasticsearch, '_get_instance',
  1889. MagicMock(return_value=MockElastic())):
  1890. self.assertRaises(CommandExecutionError, elasticsearch.search_template_create, "foo", "bar")
  1891. # 'search_template_delete' function tests: 4
  1892. def test_search_template_delete(self):
  1893. '''
  1894. Test if mapping can be deleted
  1895. '''
  1896. class MockElastic(object):
  1897. '''
  1898. Mock of Elasticsearch client
  1899. '''
  1900. def delete_template(self, id=None):
  1901. '''
  1902. Mock of delete_template method
  1903. '''
  1904. return {"acknowledged": True}
  1905. with patch.object(elasticsearch, '_get_instance',
  1906. MagicMock(return_value=MockElastic())):
  1907. self.assertTrue(elasticsearch.search_template_delete("foo"))
  1908. def test_search_template_delete_not(self):
  1909. '''
  1910. Test if mapping can be deleted but not acked
  1911. '''
  1912. class MockElastic(object):
  1913. '''
  1914. Mock of Elasticsearch client
  1915. '''
  1916. def delete_template(self, id=None):
  1917. '''
  1918. Mock of delete_template method
  1919. '''
  1920. return {"acknowledged": False}
  1921. with patch.object(elasticsearch, '_get_instance',
  1922. MagicMock(return_value=MockElastic())):
  1923. self.assertFalse(elasticsearch.search_template_delete("foo"))
  1924. def test_search_template_delete_not_exists(self):
  1925. '''
  1926. Test if deleting mapping doesn't exist
  1927. '''
  1928. class MockElastic(object):
  1929. '''
  1930. Mock of Elasticsearch client
  1931. '''
  1932. def delete_template(self, id=None):
  1933. '''
  1934. Mock of delete_template method
  1935. '''
  1936. raise NotFoundError
  1937. with patch.object(elasticsearch, '_get_instance',
  1938. MagicMock(return_value=MockElastic())):
  1939. self.assertTrue(elasticsearch.search_template_delete("foo"))
  1940. def test_search_template_delete_failure(self):
  1941. '''
  1942. Test if mapping deletion fails
  1943. '''
  1944. class MockElastic(object):
  1945. '''
  1946. Mock of Elasticsearch client
  1947. '''
  1948. def delete_template(self, id=None):
  1949. '''
  1950. Mock of delete_template method
  1951. '''
  1952. raise TransportError("custom message", 123)
  1953. with patch.object(elasticsearch, '_get_instance',
  1954. MagicMock(return_value=MockElastic())):
  1955. self.assertRaises(CommandExecutionError, elasticsearch.search_template_delete, "foo")
  1956. # Cluster settings tests below.
  1957. # We're assuming that _get_instance is properly tested
  1958. # These tests are very simple in nature, mostly checking default arguments.
  1959. def test_cluster_get_settings_succeess(self):
  1960. '''
  1961. Test if cluster get_settings fetch succeeds
  1962. '''
  1963. expected_settings = {"transient": {}, "persistent": {}}
  1964. fake_es = MagicMock()
  1965. fake_es.cluster = MagicMock()
  1966. fake_es.cluster.get_settings = MagicMock(return_value=expected_settings)
  1967. fake_instance = MagicMock(return_value=fake_es)
  1968. with patch.object(elasticsearch, '_get_instance', fake_instance):
  1969. actual_settings = elasticsearch.cluster_get_settings()
  1970. fake_es.cluster.get_settings.assert_called_with(flat_settings=False, include_defaults=False)
  1971. assert actual_settings == expected_settings
  1972. def test_cluster_get_settings_failure(self):
  1973. '''
  1974. Test if cluster get_settings fetch fails with CommandExecutionError
  1975. '''
  1976. fake_es = MagicMock()
  1977. fake_es.cluster = MagicMock()
  1978. fake_es.cluster.get_settings = MagicMock()
  1979. fake_es.cluster.get_settings.side_effect = TransportError("custom error", 123)
  1980. fake_instance = MagicMock(return_value=fake_es)
  1981. with patch.object(elasticsearch, '_get_instance', fake_instance):
  1982. self.assertRaises(CommandExecutionError, elasticsearch.cluster_get_settings)
  1983. def test_cluster_put_settings_succeess(self):
  1984. '''
  1985. Test if cluster put_settings succeeds
  1986. '''
  1987. expected_settings = {"acknowledged": True,
  1988. "transient": {},
  1989. "persistent": {"indices": {"recovery": {"max_bytes_per_sec": "50mb"}}}
  1990. }
  1991. body = {"transient": {}, "persistent": {"indices.recovery.max_bytes_per_sec": "50mb"}}
  1992. fake_es = MagicMock()
  1993. fake_es.cluster = MagicMock()
  1994. fake_es.cluster.put_settings = MagicMock(return_value=expected_settings)
  1995. fake_instance = MagicMock(return_value=fake_es)
  1996. with patch.object(elasticsearch, '_get_instance', fake_instance):
  1997. actual_settings = elasticsearch.cluster_put_settings(body=body)
  1998. fake_es.cluster.put_settings.assert_called_with(body=body, flat_settings=False)
  1999. assert actual_settings == expected_settings
  2000. def test_cluster_put_settings_failure(self):
  2001. '''
  2002. Test if cluster put_settings fails with CommandExecutionError
  2003. '''
  2004. body = {"transient": {}, "persistent": {"indices.recovery.max_bytes_per_sec": "50mb"}}
  2005. fake_es = MagicMock()
  2006. fake_es.cluster = MagicMock()
  2007. fake_es.cluster.put_settings = MagicMock()
  2008. fake_es.cluster.put_settings.side_effect = TransportError("custom error", 123)
  2009. fake_instance = MagicMock(return_value=fake_es)
  2010. with patch.object(elasticsearch, '_get_instance', fake_instance):
  2011. self.assertRaises(CommandExecutionError, elasticsearch.cluster_put_settings, body=body)
  2012. def test_cluster_put_settings_nobody(self):
  2013. '''
  2014. Test if cluster put_settings fails with SaltInvocationError
  2015. '''
  2016. self.assertRaises(SaltInvocationError, elasticsearch.cluster_put_settings)
  2017. # flush_synced tests below.
  2018. # We're assuming that _get_instance is properly tested
  2019. # These tests are very simple in nature, mostly checking default arguments.
  2020. def test_flush_synced_succeess(self):
  2021. '''
  2022. Test if flush_synced succeeds
  2023. '''
  2024. expected_return = {'_shards': {'failed': 0, 'successful': 0, 'total': 0}}
  2025. fake_es = MagicMock()
  2026. fake_es.indices = MagicMock()
  2027. fake_es.indices.flush_synced = MagicMock(return_value=expected_return)
  2028. fake_instance = MagicMock(return_value=fake_es)
  2029. with patch.object(elasticsearch, '_get_instance', fake_instance):
  2030. output = elasticsearch.flush_synced(index='_all', ignore_unavailable=True, allow_no_indices=True, expand_wildcards='all')
  2031. fake_es.indices.flush_synced.assert_called_with({'index': '_all', 'ignore_unavailable': True, 'allow_no_indices': True, 'expand_wildcards': 'all'})
  2032. assert output == expected_return
  2033. def test_flush_synced_failure(self):
  2034. '''
  2035. Test if flush_synced fails with CommandExecutionError
  2036. '''
  2037. fake_es = MagicMock()
  2038. fake_es.indices = MagicMock()
  2039. fake_es.indices.flush_synced = MagicMock()
  2040. fake_es.indices.flush_synced.side_effect = TransportError("custom error", 123)
  2041. fake_instance = MagicMock(return_value=fake_es)
  2042. with patch.object(elasticsearch, '_get_instance', fake_instance):
  2043. self.assertRaises(CommandExecutionError, elasticsearch.flush_synced)