test_zabbix.py 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Christian McHugh <christian.mchugh@gmail.com>`
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals
  7. import salt.modules.zabbix as zabbix
  8. from salt.exceptions import SaltException
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. CONN_ARGS = {}
  14. CONN_ARGS["url"] = "http://test.url"
  15. CONN_ARGS["auth"] = "1234"
  16. GETID_QUERY_RESULT_OK = [
  17. {"internal": "0", "flags": "0", "groupid": "11", "name": "Databases"}
  18. ]
  19. GETID_QUERY_RESULT_BAD = [
  20. {"internal": "0", "flags": "0", "groupid": "11", "name": "Databases"},
  21. {"another": "object"},
  22. ]
  23. DEFINED_PARAMS = {
  24. "name": "beta",
  25. "eventsource": 2,
  26. "status": 0,
  27. "filter": {
  28. "evaltype": 2,
  29. "conditions": [{"conditiontype": 24, "operator": 2, "value": "db"}],
  30. },
  31. "operations": [
  32. {"operationtype": 2},
  33. {
  34. "operationtype": 4,
  35. "opgroup": [
  36. {"groupid": {"query_object": "hostgroup", "query_name": "Databases"}}
  37. ],
  38. },
  39. ],
  40. "empty_list": [],
  41. }
  42. SUBSTITUTED_DEFINED_PARAMS = {
  43. "status": "0",
  44. "filter": {
  45. "evaltype": "2",
  46. "conditions": [{"operator": "2", "conditiontype": "24", "value": "db"}],
  47. },
  48. "eventsource": "2",
  49. "name": "beta",
  50. "operations": [
  51. {"operationtype": "2"},
  52. {"opgroup": [{"groupid": "11"}], "operationtype": "4"},
  53. ],
  54. "empty_list": [],
  55. }
  56. EXISTING_OBJECT_PARAMS = {
  57. "status": "0",
  58. "operations": [
  59. {
  60. "operationtype": "2",
  61. "esc_period": "0",
  62. "evaltype": "0",
  63. "opconditions": [],
  64. "esc_step_to": "1",
  65. "actionid": "23",
  66. "esc_step_from": "1",
  67. "operationid": "64",
  68. },
  69. {
  70. "operationtype": "4",
  71. "esc_period": "0",
  72. "evaltype": "0",
  73. "opconditions": [],
  74. "esc_step_to": "1",
  75. "actionid": "23",
  76. "esc_step_from": "1",
  77. "opgroup": [{"groupid": "11", "operationid": "65"}],
  78. "operationid": "65",
  79. },
  80. ],
  81. "def_shortdata": "",
  82. "name": "beta",
  83. "esc_period": "0",
  84. "def_longdata": "",
  85. "filter": {
  86. "formula": "",
  87. "evaltype": "2",
  88. "conditions": [
  89. {
  90. "operator": "2",
  91. "conditiontype": "24",
  92. "formulaid": "A",
  93. "value": "DIFFERENT VALUE HERE",
  94. }
  95. ],
  96. "eval_formula": "A",
  97. },
  98. "eventsource": "2",
  99. "actionid": "23",
  100. "r_shortdata": "",
  101. "r_longdata": "",
  102. "recovery_msg": "0",
  103. "empty_list": [{"dict_key": "dic_val"}],
  104. }
  105. DIFF_PARAMS_RESULT = {
  106. "filter": {
  107. "evaltype": "2",
  108. "conditions": [{"operator": "2", "conditiontype": "24", "value": "db"}],
  109. },
  110. "empty_list": [],
  111. }
  112. DIFF_PARAMS_RESULT_WITH_ROLLBACK = {
  113. "new": DIFF_PARAMS_RESULT,
  114. "old": {
  115. "filter": {
  116. "formula": "",
  117. "evaltype": "2",
  118. "conditions": [
  119. {
  120. "operator": "2",
  121. "conditiontype": "24",
  122. "formulaid": "A",
  123. "value": "DIFFERENT VALUE HERE",
  124. }
  125. ],
  126. "eval_formula": "A",
  127. },
  128. "empty_list": [{"dict_key": "dic_val"}],
  129. },
  130. }
  131. class ZabbixTestCase(TestCase, LoaderModuleMockMixin):
  132. """
  133. Test cases for salt.modules.zabbix
  134. """
  135. def setup_loader_modules(self):
  136. return {zabbix: {"__salt__": {"cmd.which_bin": lambda _: "zabbix_server"}}}
  137. def test_get_object_id_by_params(self):
  138. """
  139. Test get_object_id function with expected result from API call
  140. """
  141. with patch(
  142. "salt.modules.zabbix.run_query",
  143. MagicMock(return_value=GETID_QUERY_RESULT_OK),
  144. ):
  145. self.assertEqual(
  146. zabbix.get_object_id_by_params("hostgroup", "Databases"), "11"
  147. )
  148. def test_get_obj_id_by_params_fail(self):
  149. """
  150. Test get_object_id function with unexpected result from API call
  151. """
  152. with patch(
  153. "salt.modules.zabbix.run_query",
  154. MagicMock(return_value=GETID_QUERY_RESULT_BAD),
  155. ):
  156. self.assertRaises(
  157. SaltException, zabbix.get_object_id_by_params, "hostgroup", "Databases"
  158. )
  159. def test_substitute_params(self):
  160. """
  161. Test proper parameter substitution for defined input
  162. """
  163. with patch(
  164. "salt.modules.zabbix.get_object_id_by_params", MagicMock(return_value="11")
  165. ):
  166. self.assertEqual(
  167. zabbix.substitute_params(DEFINED_PARAMS), SUBSTITUTED_DEFINED_PARAMS
  168. )
  169. def test_substitute_params_fail(self):
  170. """
  171. Test proper parameter substitution if there is needed parameter missing
  172. """
  173. self.assertRaises(
  174. SaltException,
  175. zabbix.substitute_params,
  176. {"groupid": {"query_object": "hostgroup"}},
  177. )
  178. def test_compare_params(self):
  179. """
  180. Test result comparison of two params structures
  181. """
  182. self.assertEqual(
  183. zabbix.compare_params(SUBSTITUTED_DEFINED_PARAMS, EXISTING_OBJECT_PARAMS),
  184. DIFF_PARAMS_RESULT,
  185. )
  186. def test_compare_params_rollback(self):
  187. """
  188. Test result comparison of two params structures with rollback return value option
  189. """
  190. self.assertEqual(
  191. zabbix.compare_params(
  192. SUBSTITUTED_DEFINED_PARAMS, EXISTING_OBJECT_PARAMS, True
  193. ),
  194. DIFF_PARAMS_RESULT_WITH_ROLLBACK,
  195. )
  196. def test_compare_params_fail(self):
  197. """
  198. Test result comparison of two params structures where some data type mismatch exists
  199. """
  200. self.assertRaises(
  201. SaltException, zabbix.compare_params, {"dict": "val"}, {"dict": ["list"]}
  202. )
  203. def test_apiiinfo_version(self):
  204. """
  205. Test apiinfo_version
  206. """
  207. module_return = "3.4.5"
  208. query_return = {"jsonrpc": "2.0", "result": "3.4.5", "id": 1}
  209. with patch.object(zabbix, "_query", return_value=query_return):
  210. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  211. self.assertEqual(zabbix.apiinfo_version(**CONN_ARGS), module_return)
  212. def test_user_create(self):
  213. """
  214. query_submitted = {"params": {"passwd": "password007", "alias": "james",
  215. "name": "James Bond", "usrgrps": [{"usrgrpid": 7}, {"usrgrpid": 12}]},
  216. "jsonrpc": "2.0", "id": 0, "auth": "f016981c4f0d3f8b9682e34588fe8a33",
  217. "method": "user.create"}
  218. """
  219. module_return = ["3"]
  220. query_return = {"jsonrpc": "2.0", "result": {"userids": ["3"]}, "id": 0}
  221. with patch.object(zabbix, "_query", return_value=query_return):
  222. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  223. self.assertEqual(
  224. zabbix.user_create(
  225. "james",
  226. "password007",
  227. "[7, 12]",
  228. firstname="James Bond",
  229. **CONN_ARGS
  230. ),
  231. module_return,
  232. )
  233. def test_user_delete(self):
  234. """
  235. query_submitted = {"params": [3], "jsonrpc": "2.0", "id": 0,
  236. "auth": "68d38eace8e42a35c8d0c6a2ab0245a6", "method": "user.delete"}
  237. """
  238. module_return = ["3"]
  239. query_return = {"jsonrpc": "2.0", "result": {"userids": ["3"]}, "id": 0}
  240. with patch.object(zabbix, "_query", return_value=query_return):
  241. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  242. self.assertEqual(zabbix.user_delete(3, **CONN_ARGS), module_return)
  243. def test_user_exists(self):
  244. """
  245. query_submitted = {"params": {"filter": {"alias": "Admin"}, "output": "extend"},
  246. "jsonrpc": "2.0", "id": 0, "auth": "72435c7f754cb2adb4ecddc98216057f",
  247. "method": "user.get"}
  248. """
  249. module_return = True
  250. # pylint: disable=E8128
  251. query_return = {
  252. "jsonrpc": "2.0",
  253. "result": [
  254. {
  255. "userid": "1",
  256. "alias": "Admin",
  257. "name": "Zabbix",
  258. "surname": "Administrator",
  259. "url": "",
  260. "autologin": "1",
  261. "autologout": "0",
  262. "lang": "en_GB",
  263. "refresh": "30s",
  264. "type": "3",
  265. "theme": "default",
  266. "attempt_failed": "0",
  267. "attempt_ip": "10.0.2.2",
  268. "attempt_clock": "1515922072",
  269. "rows_per_page": "50",
  270. }
  271. ],
  272. "id": 0,
  273. }
  274. with patch.object(zabbix, "_query", return_value=query_return):
  275. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  276. self.assertEqual(
  277. zabbix.user_exists("Admin", **CONN_ARGS), module_return
  278. )
  279. def test_user_get(self):
  280. """
  281. query_submitted = {"params": {"filter": {"alias": "Admin"}, "output": "extend"},
  282. "jsonrpc": "2.0", "id": 0, "auth": "49ef327f205d9e9150d4651cb6adc2d5",
  283. "method": "user.get"}
  284. """
  285. module_return = [
  286. {
  287. "lang": "en_GB",
  288. "rows_per_page": "50",
  289. "surname": "Administrator",
  290. "name": "Zabbix",
  291. "url": "",
  292. "attempt_clock": "1515922072",
  293. "userid": "1",
  294. "autologin": "1",
  295. "refresh": "30s",
  296. "attempt_failed": "0",
  297. "alias": "Admin",
  298. "theme": "default",
  299. "autologout": "0",
  300. "attempt_ip": "10.0.2.2",
  301. "type": "3",
  302. }
  303. ]
  304. # pylint: disable=E8128
  305. query_return = {
  306. "jsonrpc": "2.0",
  307. "result": [
  308. {
  309. "userid": "1",
  310. "alias": "Admin",
  311. "name": "Zabbix",
  312. "surname": "Administrator",
  313. "url": "",
  314. "autologin": "1",
  315. "autologout": "0",
  316. "lang": "en_GB",
  317. "refresh": "30s",
  318. "type": "3",
  319. "theme": "default",
  320. "attempt_failed": "0",
  321. "attempt_ip": "10.0.2.2",
  322. "attempt_clock": "1515922072",
  323. "rows_per_page": "50",
  324. }
  325. ],
  326. "id": 0,
  327. }
  328. with patch.object(zabbix, "_query", return_value=query_return):
  329. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  330. self.assertEqual(zabbix.user_get("Admin", **CONN_ARGS), module_return)
  331. self.assertEqual(
  332. zabbix.user_get(userids="1", **CONN_ARGS), module_return
  333. )
  334. def test_user_update(self):
  335. """
  336. query_submitted = {"params": {"userid": 3, "name": "James Brown"}, "jsonrpc": "2.0",
  337. "id": 0, "auth": "cdf2ee35e3bc47560585e9c457cbc398", "method": "user.update"}
  338. """
  339. module_return = ["3"]
  340. query_return = {"jsonrpc": "2.0", "result": {"userids": ["3"]}, "id": 0}
  341. with patch.object(zabbix, "_query", return_value=query_return):
  342. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  343. self.assertEqual(
  344. zabbix.user_update("3", visible_name="James Brown", **CONN_ARGS),
  345. module_return,
  346. )
  347. def test_user_getmedia(self):
  348. """
  349. query_submitted = {"params": {"userids": 3}, "jsonrpc": "2.0", "id": 0,
  350. "auth": "d4de741ea7cdd434b3ba7b56efa4efaf", "method": "usermedia.get"}
  351. """
  352. module_return = [
  353. {
  354. "mediatypeid": "1",
  355. "mediaid": "1",
  356. "severity": "63",
  357. "userid": "3",
  358. "period": "1-7,00:00-24:00",
  359. "sendto": "email@example.com",
  360. "active": "0",
  361. }
  362. ]
  363. # pylint: disable=E8128
  364. query_return = {
  365. "jsonrpc": "2.0",
  366. "result": [
  367. {
  368. "mediaid": "1",
  369. "userid": "3",
  370. "mediatypeid": "1",
  371. "sendto": "email@example.com",
  372. "active": "0",
  373. "severity": "63",
  374. "period": "1-7,00:00-24:00",
  375. }
  376. ],
  377. "id": 0,
  378. }
  379. with patch.object(zabbix, "_query", return_value=query_return):
  380. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  381. self.assertEqual(zabbix.user_getmedia("3", **CONN_ARGS), module_return)
  382. def test_user_addmedia(self):
  383. """
  384. query_submitted = {"params": {"medias": [{"active": 0, "mediatypeid": 1,
  385. "period": "1-7,00:00-24:00", "severity": 63, "sendto": "support2@example.com"}],
  386. "users": [{"userid": 1}]}, "jsonrpc": "2.0", "id": 0, "auth": "b347fc1bf1f5617b93755619a037c19e",
  387. "method": "user.addmedia"}
  388. """
  389. module_return = ["2"]
  390. query_return = {"jsonrpc": "2.0", "result": {"mediaids": ["2"]}, "id": 0}
  391. with patch.object(zabbix, "_query", return_value=query_return):
  392. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  393. self.assertEqual(
  394. zabbix.user_addmedia(
  395. "1",
  396. active="0",
  397. mediatypeid="1",
  398. period="1-7,00:00-24:00",
  399. sendto="support2@example.com",
  400. severity="63",
  401. **CONN_ARGS
  402. ),
  403. module_return,
  404. )
  405. def test_user_deletemedia(self):
  406. """
  407. query_submitted = {"params": [1], "jsonrpc": "2.0", "id": 0, "auth": "9fb226c759a320de0de3b7a141404506",
  408. "method": "user.deletemedia"}
  409. """
  410. module_return = [1]
  411. query_return = {"jsonrpc": "2.0", "result": {"mediaids": [1]}, "id": 0}
  412. with patch.object(zabbix, "_query", return_value=query_return):
  413. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  414. self.assertEqual(
  415. zabbix.user_deletemedia("1", **CONN_ARGS), module_return
  416. )
  417. def test_user_list(self):
  418. """
  419. query_submitted = {"params": {"output": "extend"}, "jsonrpc": "2.0", "id": 0,
  420. "auth": "54d67b63c37e690cf06972678f1e9720", "method": "user.get"}
  421. """
  422. module_return = [
  423. {
  424. "lang": "en_GB",
  425. "rows_per_page": "50",
  426. "surname": "Administrator",
  427. "name": "Zabbix",
  428. "url": "",
  429. "attempt_clock": "1515922072",
  430. "userid": "1",
  431. "autologin": "1",
  432. "refresh": "30s",
  433. "attempt_failed": "0",
  434. "alias": "Admin",
  435. "theme": "default",
  436. "autologout": "0",
  437. "attempt_ip": "10.0.2.2",
  438. "type": "3",
  439. },
  440. {
  441. "lang": "en_GB",
  442. "rows_per_page": "50",
  443. "surname": "",
  444. "name": "",
  445. "url": "",
  446. "attempt_clock": "0",
  447. "userid": "2",
  448. "autologin": "0",
  449. "refresh": "30s",
  450. "attempt_failed": "0",
  451. "alias": "guest",
  452. "theme": "default",
  453. "autologout": "15m",
  454. "attempt_ip": "",
  455. "type": "1",
  456. },
  457. {
  458. "lang": "en_GB",
  459. "rows_per_page": "50",
  460. "surname": "",
  461. "name": "James Brown",
  462. "url": "",
  463. "attempt_clock": "0",
  464. "userid": "5",
  465. "autologin": "0",
  466. "refresh": "30s",
  467. "attempt_failed": "0",
  468. "alias": "james",
  469. "theme": "default",
  470. "autologout": "15m",
  471. "attempt_ip": "",
  472. "type": "1",
  473. },
  474. ]
  475. # pylint: disable=E8128
  476. query_return = {
  477. "jsonrpc": "2.0",
  478. "result": [
  479. {
  480. "userid": "1",
  481. "alias": "Admin",
  482. "name": "Zabbix",
  483. "surname": "Administrator",
  484. "url": "",
  485. "autologin": "1",
  486. "autologout": "0",
  487. "lang": "en_GB",
  488. "refresh": "30s",
  489. "type": "3",
  490. "theme": "default",
  491. "attempt_failed": "0",
  492. "attempt_ip": "10.0.2.2",
  493. "attempt_clock": "1515922072",
  494. "rows_per_page": "50",
  495. },
  496. {
  497. "userid": "2",
  498. "alias": "guest",
  499. "name": "",
  500. "surname": "",
  501. "url": "",
  502. "autologin": "0",
  503. "autologout": "15m",
  504. "lang": "en_GB",
  505. "refresh": "30s",
  506. "type": "1",
  507. "theme": "default",
  508. "attempt_failed": "0",
  509. "attempt_ip": "",
  510. "attempt_clock": "0",
  511. "rows_per_page": "50",
  512. },
  513. {
  514. "userid": "5",
  515. "alias": "james",
  516. "name": "James Brown",
  517. "surname": "",
  518. "url": "",
  519. "autologin": "0",
  520. "autologout": "15m",
  521. "lang": "en_GB",
  522. "refresh": "30s",
  523. "type": "1",
  524. "theme": "default",
  525. "attempt_failed": "0",
  526. "attempt_ip": "",
  527. "attempt_clock": "0",
  528. "rows_per_page": "50",
  529. },
  530. ],
  531. "id": 0,
  532. }
  533. with patch.object(zabbix, "_query", return_value=query_return):
  534. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  535. self.assertEqual(zabbix.user_list(**CONN_ARGS), module_return)
  536. def test_usergroup_create(self):
  537. """
  538. query_submitted = {"params": {"name": "testgroup"}, "jsonrpc": "2.0", "id": 0,
  539. "auth": "7f3ac5e90201e5de4eb19e5322606575", "method": "usergroup.create"}
  540. """
  541. module_return = ["13"]
  542. query_return = {"jsonrpc": "2.0", "result": {"usrgrpids": ["13"]}, "id": 0}
  543. with patch.object(zabbix, "_query", return_value=query_return):
  544. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  545. self.assertEqual(
  546. zabbix.usergroup_create("testgroup", **CONN_ARGS), module_return
  547. )
  548. def test_usergroup_delete(self):
  549. """
  550. query_submitted = {"params": [13], "jsonrpc": "2.0", "id": 0,
  551. "auth": "9bad39de2a5a9211da588dd06dad8773", "method": "usergroup.delete"}
  552. """
  553. module_return = ["13"]
  554. query_return = {"jsonrpc": "2.0", "result": {"usrgrpids": ["13"]}, "id": 0}
  555. with patch.object(zabbix, "_query", return_value=query_return):
  556. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  557. self.assertEqual(
  558. zabbix.usergroup_delete("13", **CONN_ARGS), module_return
  559. )
  560. def test_usergroup_exists(self):
  561. """
  562. query_submitted = {"params": {"filter": {"name": "testgroup"}, "output": "extend",
  563. "selectRights": "extend"}, "jsonrpc": "2.0", "id": 0, "auth": "e62424cd7aa71f6748e1d69c190ac852",
  564. "method": "usergroup.get"}
  565. """
  566. module_return = True
  567. query_return = {
  568. "jsonrpc": "2.0",
  569. "result": [
  570. {
  571. "usrgrpid": "13",
  572. "name": "testgroup",
  573. "gui_access": "0",
  574. "users_status": "0",
  575. "debug_mode": "0",
  576. "rights": [],
  577. }
  578. ],
  579. "id": 0,
  580. }
  581. with patch.object(zabbix, "apiinfo_version", return_value="3.2"):
  582. with patch.object(zabbix, "_query", return_value=query_return):
  583. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  584. self.assertEqual(
  585. zabbix.usergroup_exists("testgroup", **CONN_ARGS), module_return
  586. )
  587. def test_usergroup_get(self):
  588. """
  589. query_submitted = {"params": {"filter": {"name": "testgroup"}, "output": "extend",
  590. "selectRights": "extend"}, "jsonrpc": "2.0", "id": 0, "auth": "739cf358050f2a2d33162fdcfa714a3c",
  591. "method": "usergroup.get"}
  592. """
  593. module_return = [
  594. {
  595. "name": "testgroup",
  596. "rights": [],
  597. "users_status": "0",
  598. "gui_access": "0",
  599. "debug_mode": "0",
  600. "usrgrpid": "13",
  601. }
  602. ]
  603. query_return = {
  604. "jsonrpc": "2.0",
  605. "result": [
  606. {
  607. "usrgrpid": "13",
  608. "name": "testgroup",
  609. "gui_access": "0",
  610. "users_status": "0",
  611. "debug_mode": "0",
  612. "rights": [],
  613. }
  614. ],
  615. "id": 0,
  616. }
  617. with patch.object(zabbix, "apiinfo_version", return_value="3.2"):
  618. with patch.object(zabbix, "_query", return_value=query_return):
  619. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  620. self.assertEqual(
  621. zabbix.usergroup_get("testgroup", **CONN_ARGS), module_return
  622. )
  623. def test_usergroup_update(self):
  624. """
  625. query_submitted = {"params": {"usrgrpid": 13, "users_status": 1}, "jsonrpc": "2.0",
  626. "id": 0, "auth": "ef772237245f59f655871bc8fbbcd67c", "method": "usergroup.update"}
  627. """
  628. module_return = ["13"]
  629. query_return = {"jsonrpc": "2.0", "result": {"usrgrpids": ["13"]}, "id": 0}
  630. with patch.object(zabbix, "_query", return_value=query_return):
  631. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  632. self.assertEqual(
  633. zabbix.usergroup_update("13", users_status="1", **CONN_ARGS),
  634. module_return,
  635. )
  636. def test_usergroup_list(self):
  637. """
  638. query_submitted = {"params": {"output": "extend"}, "jsonrpc": "2.0", "id": 0,
  639. "auth": "4bc366bc7803c07e80f15b1bc14dc61f", "method": "usergroup.get"}
  640. """
  641. module_return = [
  642. {
  643. "usrgrpid": "7",
  644. "gui_access": "0",
  645. "debug_mode": "0",
  646. "name": "Zabbix administrators",
  647. "users_status": "0",
  648. },
  649. {
  650. "usrgrpid": "8",
  651. "gui_access": "0",
  652. "debug_mode": "0",
  653. "name": "Guests",
  654. "users_status": "0",
  655. },
  656. {
  657. "usrgrpid": "9",
  658. "gui_access": "0",
  659. "debug_mode": "0",
  660. "name": "Disabled",
  661. "users_status": "1",
  662. },
  663. {
  664. "usrgrpid": "11",
  665. "gui_access": "0",
  666. "debug_mode": "1",
  667. "name": "Enabled debug mode",
  668. "users_status": "0",
  669. },
  670. {
  671. "usrgrpid": "12",
  672. "gui_access": "2",
  673. "debug_mode": "0",
  674. "name": "No access to the frontend",
  675. "users_status": "0",
  676. },
  677. {
  678. "usrgrpid": "13",
  679. "gui_access": "0",
  680. "debug_mode": "0",
  681. "name": "testgroup",
  682. "users_status": "0",
  683. },
  684. ]
  685. # pylint: disable=E8128
  686. query_return = {
  687. "jsonrpc": "2.0",
  688. "result": [
  689. {
  690. "usrgrpid": "7",
  691. "name": "Zabbix administrators",
  692. "gui_access": "0",
  693. "users_status": "0",
  694. "debug_mode": "0",
  695. },
  696. {
  697. "usrgrpid": "8",
  698. "name": "Guests",
  699. "gui_access": "0",
  700. "users_status": "0",
  701. "debug_mode": "0",
  702. },
  703. {
  704. "usrgrpid": "9",
  705. "name": "Disabled",
  706. "gui_access": "0",
  707. "users_status": "1",
  708. "debug_mode": "0",
  709. },
  710. {
  711. "usrgrpid": "11",
  712. "name": "Enabled debug mode",
  713. "gui_access": "0",
  714. "users_status": "0",
  715. "debug_mode": "1",
  716. },
  717. {
  718. "usrgrpid": "12",
  719. "name": "No access to the frontend",
  720. "gui_access": "2",
  721. "users_status": "0",
  722. "debug_mode": "0",
  723. },
  724. {
  725. "usrgrpid": "13",
  726. "name": "testgroup",
  727. "gui_access": "0",
  728. "users_status": "0",
  729. "debug_mode": "0",
  730. },
  731. ],
  732. "id": 0,
  733. }
  734. with patch.object(zabbix, "_query", return_value=query_return):
  735. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  736. self.assertEqual(zabbix.usergroup_list(**CONN_ARGS), module_return)
  737. def test_host_inventory_get(self):
  738. """
  739. test host_inventory_get
  740. """
  741. module_return = {
  742. "poc_2_email": "",
  743. "poc_2_phone_b": "",
  744. "site_country": "",
  745. "poc_2_screen": "",
  746. "poc_2_notes": "",
  747. "poc_1_screen": "",
  748. "hardware": "",
  749. "software_app_a": "",
  750. "software_app_b": "",
  751. "software_app_c": "",
  752. "software_app_d": "",
  753. "os_short": "",
  754. "site_zip": "",
  755. "poc_2_name": "",
  756. "os_full": "",
  757. "host_netmask": "",
  758. "host_router": "",
  759. "url_c": "",
  760. "date_hw_install": "",
  761. "poc_1_phone_b": "",
  762. "poc_1_phone_a": "",
  763. "poc_1_cell": "",
  764. "type_full": "",
  765. "location_lat": "",
  766. "vendor": "",
  767. "contact": "",
  768. "site_rack": "",
  769. "location": "",
  770. "poc_2_cell": "",
  771. "date_hw_expiry": "",
  772. "installer_name": "",
  773. "type": "",
  774. "contract_number": "",
  775. "deployment_status": "",
  776. "site_notes": "",
  777. "inventory_mode": "0",
  778. "oob_ip": "",
  779. "host_networks": "",
  780. "hardware_full": "",
  781. "poc_2_phone_a": "",
  782. "poc_1_name": "",
  783. "site_state": "",
  784. "chassis": "",
  785. "software_app_e": "",
  786. "site_address_b": "",
  787. "site_address_a": "",
  788. "date_hw_decomm": "",
  789. "date_hw_purchase": "",
  790. "location_lon": "",
  791. "hw_arch": "",
  792. "software_full": "",
  793. "asset_tag": "",
  794. "oob_router": "",
  795. "hostid": "10258",
  796. "poc_1_email": "",
  797. "name": "",
  798. "poc_1_notes": "",
  799. "serialno_b": "",
  800. "notes": "",
  801. "oob_netmask": "",
  802. "alias": "other thing",
  803. "tag": "",
  804. "macaddress_b": "",
  805. "macaddress_a": "",
  806. "site_city": "",
  807. "site_address_c": "",
  808. "model": "",
  809. "serialno_a": "",
  810. "os": "some",
  811. "url_b": "",
  812. "url_a": "",
  813. "software": "",
  814. }
  815. query_return = {
  816. "jsonrpc": "2.0",
  817. "result": [
  818. {
  819. "hostid": "10258",
  820. "proxy_hostid": "0",
  821. "host": "master",
  822. "status": "0",
  823. "disable_until": "1517766661",
  824. "error": "Get value from agent failed: cannot connect to [[10.0.2.15]:10050]: [111] Connection refused",
  825. "available": "2",
  826. "errors_from": "1516087871",
  827. "lastaccess": "0",
  828. "ipmi_authtype": "-1",
  829. "ipmi_privilege": "2",
  830. "ipmi_username": "",
  831. "ipmi_password": "",
  832. "ipmi_disable_until": "0",
  833. "ipmi_available": "0",
  834. "snmp_disable_until": "0",
  835. "snmp_available": "0",
  836. "maintenanceid": "0",
  837. "maintenance_status": "0",
  838. "maintenance_type": "0",
  839. "maintenance_from": "0",
  840. "ipmi_errors_from": "0",
  841. "snmp_errors_from": "0",
  842. "ipmi_error": "",
  843. "snmp_error": "",
  844. "jmx_disable_until": "0",
  845. "jmx_available": "0",
  846. "jmx_errors_from": "0",
  847. "jmx_error": "",
  848. "name": "master",
  849. "flags": "0",
  850. "templateid": "0",
  851. "description": "",
  852. "tls_connect": "1",
  853. "tls_accept": "1",
  854. "tls_issuer": "",
  855. "tls_subject": "",
  856. "tls_psk_identity": "",
  857. "tls_psk": "",
  858. "inventory": {
  859. "hostid": "10258",
  860. "inventory_mode": "0",
  861. "type": "",
  862. "type_full": "",
  863. "name": "",
  864. "alias": "other thing",
  865. "os": "some",
  866. "os_full": "",
  867. "os_short": "",
  868. "serialno_a": "",
  869. "serialno_b": "",
  870. "tag": "",
  871. "asset_tag": "",
  872. "macaddress_a": "",
  873. "macaddress_b": "",
  874. "hardware": "",
  875. "hardware_full": "",
  876. "software": "",
  877. "software_full": "",
  878. "software_app_a": "",
  879. "software_app_b": "",
  880. "software_app_c": "",
  881. "software_app_d": "",
  882. "software_app_e": "",
  883. "contact": "",
  884. "location": "",
  885. "location_lat": "",
  886. "location_lon": "",
  887. "notes": "",
  888. "chassis": "",
  889. "model": "",
  890. "hw_arch": "",
  891. "vendor": "",
  892. "contract_number": "",
  893. "installer_name": "",
  894. "deployment_status": "",
  895. "url_a": "",
  896. "url_b": "",
  897. "url_c": "",
  898. "host_networks": "",
  899. "host_netmask": "",
  900. "host_router": "",
  901. "oob_ip": "",
  902. "oob_netmask": "",
  903. "oob_router": "",
  904. "date_hw_purchase": "",
  905. "date_hw_install": "",
  906. "date_hw_expiry": "",
  907. "date_hw_decomm": "",
  908. "site_address_a": "",
  909. "site_address_b": "",
  910. "site_address_c": "",
  911. "site_city": "",
  912. "site_state": "",
  913. "site_country": "",
  914. "site_zip": "",
  915. "site_rack": "",
  916. "site_notes": "",
  917. "poc_1_name": "",
  918. "poc_1_email": "",
  919. "poc_1_phone_a": "",
  920. "poc_1_phone_b": "",
  921. "poc_1_cell": "",
  922. "poc_1_screen": "",
  923. "poc_1_notes": "",
  924. "poc_2_name": "",
  925. "poc_2_email": "",
  926. "poc_2_phone_a": "",
  927. "poc_2_phone_b": "",
  928. "poc_2_cell": "",
  929. "poc_2_screen": "",
  930. "poc_2_notes": "",
  931. },
  932. }
  933. ],
  934. "id": 1,
  935. }
  936. with patch.object(zabbix, "_query", return_value=query_return):
  937. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  938. self.assertEqual(
  939. zabbix.host_inventory_get("12345", **CONN_ARGS), module_return
  940. )
  941. def test_host_inventory_set(self):
  942. """
  943. query_submitted = {"params": {"hostid": 10258, "inventory_mode": "0", "inventory":
  944. {"asset_tag": "jml3322", "type": "Xen"}}, "jsonrpc": "2.0", "id": 0,
  945. "auth": "a50d2c3030b9b73d7c28b5ebd89c044c", "method": "host.update"}
  946. """
  947. module_return = {"hostids": [10258]}
  948. query_return = {"jsonrpc": "2.0", "result": {"hostids": [10258]}, "id": 0}
  949. with patch.object(zabbix, "_query", return_value=query_return):
  950. with patch.object(zabbix, "_login", return_value=CONN_ARGS):
  951. self.assertEqual(
  952. zabbix.host_inventory_set(
  953. 10258, asset_tag="jml3322", type="Xen", **CONN_ARGS
  954. ),
  955. module_return,
  956. )