test_rabbitmq.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.rabbitmq as rabbitmq
  9. from salt.exceptions import CommandExecutionError
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class RabbitmqTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.modules.rabbitmq
  17. """
  18. def setup_loader_modules(self):
  19. return {
  20. rabbitmq: {"__context__": {"rabbitmqctl": None, "rabbitmq-plugins": None}}
  21. }
  22. # 'list_users_rabbitmq2' function tests: 1
  23. def test_list_users_rabbitmq2(self):
  24. """
  25. Test if it return a list of users based off of rabbitmqctl user_list.
  26. """
  27. mock_run = MagicMock(
  28. return_value={
  29. "retcode": 0,
  30. "stdout": "Listing users ...\nguest\t[administrator, user]\njustAnAdmin\t[administrator]\n",
  31. "stderr": "",
  32. }
  33. )
  34. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  35. self.assertDictEqual(
  36. rabbitmq.list_users(),
  37. {"guest": ["administrator", "user"], "justAnAdmin": ["administrator"]},
  38. )
  39. # 'list_users_rabbitmq3' function tests: 1
  40. def test_list_users_rabbitmq3(self):
  41. """
  42. Test if it return a list of users based off of rabbitmqctl user_list.
  43. """
  44. mock_run = MagicMock(
  45. return_value={
  46. "retcode": 0,
  47. "stdout": "guest\t[administrator user]\r\nother\t[a b]\r\n",
  48. "stderr": "",
  49. }
  50. )
  51. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  52. self.assertDictEqual(
  53. rabbitmq.list_users(),
  54. {"guest": ["administrator", "user"], "other": ["a", "b"]},
  55. )
  56. # 'list_users_with_warning_rabbitmq2' function tests: 1
  57. def test_list_users_with_warning_rabbitmq2(self):
  58. """
  59. Test if having a leading WARNING returns the user_list anyway.
  60. """
  61. rtn_stdout = "\n".join(
  62. [
  63. "WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- location has moved to /etc/rabbitmq/rabbitmq-env.conf",
  64. "Listing users ...",
  65. "guest\t[administrator, user]\n",
  66. ]
  67. )
  68. mock_run = MagicMock(
  69. return_value={"retcode": 0, "stdout": rtn_stdout, "stderr": ""}
  70. )
  71. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  72. self.assertDictEqual(
  73. rabbitmq.list_users(), {"guest": ["administrator", "user"]}
  74. )
  75. # 'list_users_with_warning_rabbitmq3' function tests: 1
  76. def test_list_users_with_warning_rabbitmq3(self):
  77. """
  78. Test if having a leading WARNING returns the user_list anyway.
  79. """
  80. rtn_stdout = "\n".join(
  81. [
  82. "WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- location has moved to /etc/rabbitmq/rabbitmq-env.conf",
  83. "Listing users ...",
  84. "guest\t[administrator user]\n",
  85. ]
  86. )
  87. mock_run = MagicMock(
  88. return_value={"retcode": 0, "stdout": rtn_stdout, "stderr": ""}
  89. )
  90. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  91. self.assertDictEqual(
  92. rabbitmq.list_users(), {"guest": ["administrator", "user"]}
  93. )
  94. # 'list_vhosts' function tests: 2
  95. def test_list_vhosts(self):
  96. """
  97. Test if it return a list of vhost based on rabbitmqctl list_vhosts.
  98. """
  99. mock_run = MagicMock(
  100. return_value={"retcode": 0, "stdout": "/\nsaltstack\n...", "stderr": ""}
  101. )
  102. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  103. self.assertListEqual(rabbitmq.list_vhosts(), ["/", "saltstack", "..."])
  104. def test_list_vhosts_with_warning(self):
  105. """
  106. Test if it return a list of vhost based on rabbitmqctl list_vhosts even with a leading WARNING.
  107. """
  108. rtn_stdout = "\n".join(
  109. [
  110. "WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- location has moved to /etc/rabbitmq/rabbitmq-env.conf",
  111. "Listing users ...",
  112. "/",
  113. "saltstack",
  114. "...\n",
  115. ]
  116. )
  117. mock_run = MagicMock(
  118. return_value={"retcode": 0, "stdout": rtn_stdout, "stderr": ""}
  119. )
  120. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  121. self.assertListEqual(rabbitmq.list_vhosts(), ["/", "saltstack", "..."])
  122. # 'user_exists' function tests: 2
  123. def test_user_exists(self):
  124. """
  125. Test whether a given rabbitmq-internal user exists based
  126. on rabbitmqctl list_users.
  127. """
  128. mock_run = MagicMock(
  129. return_value={
  130. "retcode": 0,
  131. "stdout": "Listing users ...\n" "saltstack\t[administrator]\n...done",
  132. "stderr": "",
  133. }
  134. )
  135. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  136. self.assertTrue(rabbitmq.user_exists("saltstack"))
  137. def test_user_exists_negative(self):
  138. """
  139. Negative test of whether rabbitmq-internal user exists based
  140. on rabbitmqctl list_users.
  141. """
  142. mock_run = MagicMock(
  143. return_value={
  144. "retcode": 0,
  145. "stdout": "Listing users ...\n" "saltstack\t[administrator]\n...done",
  146. "stderr": "",
  147. }
  148. )
  149. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  150. self.assertFalse(rabbitmq.user_exists("salt"))
  151. # 'vhost_exists' function tests: 2
  152. def test_vhost_exists(self):
  153. """
  154. Test if it return whether the vhost exists based
  155. on rabbitmqctl list_vhosts.
  156. """
  157. mock_run = MagicMock(
  158. return_value={
  159. "retcode": 0,
  160. "stdout": "Listing vhosts ...\nsaltstack",
  161. "stderr": "",
  162. }
  163. )
  164. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  165. self.assertTrue(rabbitmq.vhost_exists("saltstack"))
  166. def test_vhost_exists_negative(self):
  167. """
  168. Test if it return whether the vhost exists based
  169. on rabbitmqctl list_vhosts.
  170. """
  171. mock_run = MagicMock(
  172. return_value={
  173. "retcode": 0,
  174. "stdout": "Listing vhosts ...\nsaltstack",
  175. "stderr": "",
  176. }
  177. )
  178. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  179. self.assertFalse(rabbitmq.vhost_exists("salt"))
  180. # 'add_user' function tests: 1
  181. def test_add_user(self):
  182. """
  183. Test if it add a rabbitMQ user via rabbitmqctl
  184. user_add <user> <password>
  185. """
  186. mock_run = MagicMock(
  187. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  188. )
  189. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  190. self.assertDictEqual(rabbitmq.add_user("saltstack"), {"Added": "saltstack"})
  191. mock_run = MagicMock(return_value="Error")
  192. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  193. with patch.object(
  194. rabbitmq,
  195. "clear_password",
  196. return_value={"Error": "Error", "retcode": 1},
  197. ):
  198. self.assertRaises(CommandExecutionError, rabbitmq.add_user, "saltstack")
  199. # 'delete_user' function tests: 1
  200. def test_delete_user(self):
  201. """
  202. Test if it deletes a user via rabbitmqctl delete_user.
  203. """
  204. mock_run = MagicMock(
  205. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  206. )
  207. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  208. self.assertDictEqual(
  209. rabbitmq.delete_user("saltstack"), {"Deleted": "saltstack"}
  210. )
  211. # 'check_password' function tests: 2
  212. def test_check_password_lt_38(self):
  213. """
  214. Test if it checks a user's password for RabbitMQ less than v3.8.
  215. """
  216. mock_run = MagicMock(return_value='{rabbit,"RabbitMQ","3.5.7"}')
  217. mock_run2 = MagicMock(
  218. return_value={
  219. "retcode": 0,
  220. "stdout": 'Authenticating user "saltstack" ...\nSuccess',
  221. "stderr": "",
  222. }
  223. )
  224. with patch.dict(
  225. rabbitmq.__salt__, {"cmd.run": mock_run, "cmd.run_all": mock_run2}
  226. ):
  227. self.assertEqual(rabbitmq.check_password("saltstack", "salt@123"), True)
  228. def test_check_password_gt_38(self):
  229. """
  230. Test if it checks a user's password for RabbitMQ greater than v3.8.
  231. """
  232. mock_run = MagicMock(return_value="RabbitMQ version: 3.8.3")
  233. mock_run2 = MagicMock(
  234. return_value={
  235. "retcode": 0,
  236. "stdout": 'Authenticating user "saltstack" ...\nSuccess',
  237. "stderr": "",
  238. }
  239. )
  240. with patch.dict(
  241. rabbitmq.__salt__, {"cmd.run": mock_run, "cmd.run_all": mock_run2}
  242. ):
  243. self.assertEqual(rabbitmq.check_password("saltstack", "salt@123"), True)
  244. # 'change_password' function tests: 1
  245. def test_change_password(self):
  246. """
  247. Test if it changes a user's password.
  248. """
  249. mock_run = MagicMock(
  250. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  251. )
  252. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  253. self.assertDictEqual(
  254. rabbitmq.change_password("saltstack", "salt@123"),
  255. {"Password Changed": "saltstack"},
  256. )
  257. # 'clear_password' function tests: 1
  258. def test_clear_password(self):
  259. """
  260. Test if it removes a user's password.
  261. """
  262. mock_run = MagicMock(
  263. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  264. )
  265. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  266. self.assertDictEqual(
  267. rabbitmq.clear_password("saltstack"), {"Password Cleared": "saltstack"}
  268. )
  269. # 'add_vhost' function tests: 1
  270. def test_add_vhost(self):
  271. """
  272. Test if it adds a vhost via rabbitmqctl add_vhost.
  273. """
  274. mock_run = MagicMock(
  275. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  276. )
  277. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  278. self.assertDictEqual(
  279. rabbitmq.add_vhost("saltstack"), {"Added": "saltstack"}
  280. )
  281. # 'delete_vhost' function tests: 1
  282. def test_delete_vhost(self):
  283. """
  284. Test if it deletes a vhost rabbitmqctl delete_vhost.
  285. """
  286. mock_run = MagicMock(
  287. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  288. )
  289. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  290. self.assertDictEqual(
  291. rabbitmq.delete_vhost("saltstack"), {"Deleted": "saltstack"}
  292. )
  293. # 'set_permissions' function tests: 1
  294. def test_set_permissions(self):
  295. """
  296. Test if it sets permissions for vhost via rabbitmqctl set_permissions.
  297. """
  298. mock_run = MagicMock(
  299. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  300. )
  301. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  302. self.assertDictEqual(
  303. rabbitmq.set_permissions("myvhost", "myuser"),
  304. {"Permissions Set": "saltstack"},
  305. )
  306. # 'list_permissions' function tests: 1
  307. def test_list_permissions(self):
  308. """
  309. Test if it lists permissions for a vhost
  310. via rabbitmqctl list_permissions.
  311. """
  312. mock_run = MagicMock(
  313. return_value={
  314. "retcode": 0,
  315. "stdout": "Listing stuff ...\nsaltstack\tsaltstack\t.*\t1\nguest\t0\tone\n...done",
  316. "stderr": "",
  317. }
  318. )
  319. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  320. self.assertDictEqual(
  321. rabbitmq.list_user_permissions("myuser"),
  322. {"saltstack": ["saltstack", ".*", "1"], "guest": ["0", "one"]},
  323. )
  324. # 'list_user_permissions' function tests: 1
  325. def test_list_user_permissions(self):
  326. """
  327. Test if it list permissions for a user
  328. via rabbitmqctl list_user_permissions.
  329. """
  330. mock_run = MagicMock(
  331. return_value={
  332. "retcode": 0,
  333. "stdout": "Listing stuff ...\nsaltstack\tsaltstack\t0\t1\nguest\t0\tone\n...done",
  334. "stderr": "",
  335. }
  336. )
  337. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  338. self.assertDictEqual(
  339. rabbitmq.list_user_permissions("myuser"),
  340. {"saltstack": ["saltstack", "0", "1"], "guest": ["0", "one"]},
  341. )
  342. # 'set_user_tags' function tests: 1
  343. def test_set_user_tags(self):
  344. """
  345. Test if it add user tags via rabbitmqctl set_user_tags.
  346. """
  347. mock_run = MagicMock(
  348. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  349. )
  350. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  351. self.assertDictEqual(
  352. rabbitmq.set_user_tags("myadmin", "admin"), {"Tag(s) set": "saltstack"}
  353. )
  354. # 'status' function tests: 1
  355. def test_status(self):
  356. """
  357. Test if it return rabbitmq status.
  358. """
  359. mock_run = MagicMock(
  360. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  361. )
  362. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  363. self.assertEqual(rabbitmq.status(), "saltstack")
  364. # 'cluster_status' function tests: 1
  365. def test_cluster_status(self):
  366. """
  367. Test if it return rabbitmq cluster_status.
  368. """
  369. mock_run = MagicMock(
  370. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  371. )
  372. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  373. self.assertEqual(rabbitmq.cluster_status(), "saltstack")
  374. # 'join_cluster' function tests: 1
  375. def test_join_cluster(self):
  376. """
  377. Test if it join a rabbit cluster.
  378. """
  379. mock_run = MagicMock(
  380. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  381. )
  382. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  383. self.assertDictEqual(
  384. rabbitmq.join_cluster("rabbit.example.com"), {"Join": "saltstack"}
  385. )
  386. # 'stop_app' function tests: 1
  387. def test_stop_app(self):
  388. """
  389. Test if it stops the RabbitMQ application,
  390. leaving the Erlang node running.
  391. """
  392. mock_run = MagicMock(
  393. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  394. )
  395. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  396. self.assertEqual(rabbitmq.stop_app(), "saltstack")
  397. # 'start_app' function tests: 1
  398. def test_start_app(self):
  399. """
  400. Test if it start the RabbitMQ application.
  401. """
  402. mock_run = MagicMock(
  403. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  404. )
  405. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  406. self.assertEqual(rabbitmq.start_app(), "saltstack")
  407. # 'reset' function tests: 1
  408. def test_reset(self):
  409. """
  410. Test if it return a RabbitMQ node to its virgin state
  411. """
  412. mock_run = MagicMock(
  413. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  414. )
  415. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  416. self.assertEqual(rabbitmq.reset(), "saltstack")
  417. # 'force_reset' function tests: 1
  418. def test_force_reset(self):
  419. """
  420. Test if it forcefully Return a RabbitMQ node to its virgin state
  421. """
  422. mock_run = MagicMock(
  423. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  424. )
  425. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  426. self.assertEqual(rabbitmq.force_reset(), "saltstack")
  427. # 'list_queues' function tests: 1
  428. def test_list_queues(self):
  429. """
  430. Test if it returns queue details of the / virtual host
  431. """
  432. mock_run = MagicMock(
  433. return_value={
  434. "retcode": 0,
  435. "stdout": "saltstack\t0\nceleryev.234-234\t10",
  436. "stderr": "",
  437. }
  438. )
  439. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  440. self.assertDictEqual(
  441. rabbitmq.list_queues(), {"saltstack": ["0"], "celeryev.234-234": ["10"]}
  442. )
  443. # 'list_queues_vhost' function tests: 1
  444. def test_list_queues_vhost(self):
  445. """
  446. Test if it returns queue details of specified virtual host.
  447. """
  448. mock_run = MagicMock(
  449. return_value={
  450. "retcode": 0,
  451. "stdout": "saltstack\t0\nceleryev.234-234\t10",
  452. "stderr": "",
  453. }
  454. )
  455. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  456. self.assertDictEqual(
  457. rabbitmq.list_queues_vhost("consumers"),
  458. {"saltstack": ["0"], "celeryev.234-234": ["10"]},
  459. )
  460. # 'list_policies' function tests: 3
  461. def test_list_policies(self):
  462. """
  463. Test if it return a dictionary of policies nested by vhost
  464. and name based on the data returned from rabbitmqctl list_policies.
  465. """
  466. mock_run = MagicMock(
  467. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  468. )
  469. mock_pkg = MagicMock(return_value="3.7")
  470. with patch.dict(
  471. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  472. ), patch.dict(rabbitmq.__grains__, {"os_family": ""}):
  473. self.assertDictEqual(rabbitmq.list_policies(), {})
  474. def test_list_policies_freebsd(self):
  475. """
  476. Test if it return a dictionary of policies nested by vhost
  477. and name based on the data returned from rabbitmqctl list_policies.
  478. """
  479. mock_run = MagicMock(
  480. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  481. )
  482. mock_pkg = MagicMock(return_value="3.7")
  483. with patch.dict(
  484. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  485. ), patch.dict(rabbitmq.__grains__, {"os_family": "FreeBSD"}):
  486. self.assertDictEqual(rabbitmq.list_policies(), {})
  487. def test_list_policies_old_version(self):
  488. """
  489. Test if it return a dictionary of policies nested by vhost
  490. and name based on the data returned from rabbitmqctl list_policies.
  491. """
  492. mock_run = MagicMock(
  493. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  494. )
  495. mock_pkg = MagicMock(return_value="3.0")
  496. with patch.dict(
  497. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  498. ), patch.dict(rabbitmq.__grains__, {"os_family": ""}):
  499. self.assertDictEqual(rabbitmq.list_policies(), {})
  500. # 'set_policy' function tests: 1
  501. def test_set_policy(self):
  502. """
  503. Test if it set a policy based on rabbitmqctl set_policy.
  504. """
  505. mock_run = MagicMock(
  506. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  507. )
  508. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  509. self.assertDictEqual(
  510. rabbitmq.set_policy("/", "HA", ".*", '{"ha-mode": "all"}'),
  511. {"Set": "saltstack"},
  512. )
  513. # 'delete_policy' function tests: 1
  514. def test_delete_policy(self):
  515. """
  516. Test if it delete a policy based on rabbitmqctl clear_policy.
  517. """
  518. mock_run = MagicMock(
  519. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  520. )
  521. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  522. self.assertDictEqual(
  523. rabbitmq.delete_policy("/", "HA"), {"Deleted": "saltstack"}
  524. )
  525. # 'policy_exists' function tests: 1
  526. def test_policy_exists(self):
  527. """
  528. Test if it return whether the policy exists
  529. based on rabbitmqctl list_policies.
  530. """
  531. mock_run = MagicMock(
  532. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  533. )
  534. mock_pkg = MagicMock(return_value="3.0")
  535. with patch.dict(
  536. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  537. ), patch.dict(rabbitmq.__grains__, {"os_family": ""}):
  538. self.assertFalse(rabbitmq.policy_exists("/", "HA"))
  539. # 'list_available_plugins' function tests: 2
  540. def test_list_available_plugins(self):
  541. """
  542. Test if it returns a list of plugins.
  543. """
  544. mock_run = MagicMock(
  545. return_value={
  546. "retcode": 0,
  547. "stdout": "saltstack\nsalt\nother",
  548. "stderr": "",
  549. }
  550. )
  551. mock_pkg = MagicMock(return_value="")
  552. with patch.dict(
  553. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  554. ):
  555. self.assertListEqual(
  556. rabbitmq.list_available_plugins(), ["saltstack", "salt", "other"]
  557. )
  558. def test_list_available_plugins_space_delimited(self):
  559. """
  560. Test if it returns a list of plugins.
  561. """
  562. mock_run = MagicMock(
  563. return_value={"retcode": 0, "stdout": "saltstack salt other", "stderr": ""}
  564. )
  565. mock_pkg = MagicMock(return_value="")
  566. with patch.dict(
  567. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  568. ):
  569. self.assertListEqual(
  570. rabbitmq.list_available_plugins(), ["saltstack", "salt", "other"]
  571. )
  572. # 'list_enabled_plugins' function tests: 2
  573. def test_list_enabled_plugins(self):
  574. """
  575. Test if it returns a list of plugins.
  576. """
  577. mock_run = MagicMock(
  578. return_value={
  579. "retcode": 0,
  580. "stdout": "saltstack\nsalt\nother",
  581. "stderr": "",
  582. }
  583. )
  584. mock_pkg = MagicMock(return_value="")
  585. with patch.dict(
  586. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  587. ):
  588. self.assertListEqual(
  589. rabbitmq.list_enabled_plugins(), ["saltstack", "salt", "other"]
  590. )
  591. def test_list_enabled_plugins_space_delimited(self):
  592. """
  593. Test if it returns a list of plugins.
  594. """
  595. mock_run = MagicMock(
  596. return_value={"retcode": 0, "stdout": "saltstack salt other", "stderr": ""}
  597. )
  598. mock_pkg = MagicMock(return_value="")
  599. with patch.dict(
  600. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  601. ):
  602. self.assertListEqual(
  603. rabbitmq.list_enabled_plugins(), ["saltstack", "salt", "other"]
  604. )
  605. # 'plugin_is_enabled' function tests: 2
  606. def test_plugin_is_enabled(self):
  607. """
  608. Test if it returns true for an enabled plugin.
  609. """
  610. mock_run = MagicMock(
  611. return_value={
  612. "retcode": 0,
  613. "stdout": "saltstack\nsalt\nother",
  614. "stderr": "",
  615. }
  616. )
  617. mock_pkg = MagicMock(return_value="")
  618. with patch.dict(
  619. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  620. ):
  621. self.assertTrue(rabbitmq.plugin_is_enabled("saltstack"))
  622. self.assertTrue(rabbitmq.plugin_is_enabled("salt"))
  623. self.assertTrue(rabbitmq.plugin_is_enabled("other"))
  624. def test_plugin_is_enabled_negative(self):
  625. """
  626. Test if it returns false for a disabled plugin.
  627. """
  628. mock_run = MagicMock(
  629. return_value={"retcode": 0, "stdout": "saltstack\nother", "stderr": ""}
  630. )
  631. mock_pkg = MagicMock(return_value="")
  632. with patch.dict(
  633. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  634. ):
  635. self.assertFalse(rabbitmq.plugin_is_enabled("salt"))
  636. self.assertFalse(rabbitmq.plugin_is_enabled("stack"))
  637. self.assertFalse(rabbitmq.plugin_is_enabled("random"))
  638. # 'enable_plugin' function tests: 1
  639. def test_enable_plugin(self):
  640. """
  641. Test if it enable a RabbitMQ plugin via the rabbitmq-plugins command.
  642. """
  643. mock_run = MagicMock(
  644. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  645. )
  646. mock_pkg = MagicMock(return_value="")
  647. with patch.dict(
  648. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  649. ):
  650. self.assertDictEqual(
  651. rabbitmq.enable_plugin("salt"), {"Enabled": "saltstack"}
  652. )
  653. # 'disable_plugin' function tests: 1
  654. def test_disable_plugin(self):
  655. """
  656. Test if it disable a RabbitMQ plugin via the rabbitmq-plugins command.
  657. """
  658. mock_run = MagicMock(
  659. return_value={"retcode": 0, "stdout": "saltstack", "stderr": ""}
  660. )
  661. mock_pkg = MagicMock(return_value="")
  662. with patch.dict(
  663. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  664. ):
  665. self.assertDictEqual(
  666. rabbitmq.disable_plugin("salt"), {"Disabled": "saltstack"}
  667. )
  668. # 'list_upstreams' function tests: 1
  669. def test_list_upstreams(self):
  670. """
  671. Test if it returns a list of upstreams.
  672. """
  673. mock_run = MagicMock(
  674. return_value={
  675. "retcode": 0,
  676. "stdout": 'federation-upstream\tremote-name\t{"ack-mode":"on-confirm"'
  677. ',"max-hops":1,"trust-user-id":true,"uri":"amqp://username:'
  678. 'password@remote.fqdn"}',
  679. "stderr": "",
  680. }
  681. )
  682. mock_pkg = MagicMock(return_value="")
  683. with patch.dict(
  684. rabbitmq.__salt__, {"cmd.run_all": mock_run, "pkg.version": mock_pkg}
  685. ):
  686. self.assertDictEqual(
  687. rabbitmq.list_upstreams(),
  688. {
  689. "remote-name": (
  690. '{"ack-mode":"on-confirm","max-hops":1,'
  691. '"trust-user-id":true,"uri":"amqp://username:'
  692. 'password@remote.fqdn"}'
  693. )
  694. },
  695. )
  696. # 'upstream_exists' function tests: 2
  697. def test_upstream_exists(self):
  698. """
  699. Test whether a given rabbitmq-internal upstream exists based
  700. on rabbitmqctl list_upstream.
  701. """
  702. mock_run = MagicMock(
  703. return_value={
  704. "retcode": 0,
  705. "stdout": 'federation-upstream\tremote-name\t{"ack-mode":"on-confirm"'
  706. ',"max-hops":1,"trust-user-id":true,"uri":"amqp://username:'
  707. 'password@remote.fqdn"}',
  708. "stderr": "",
  709. }
  710. )
  711. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  712. self.assertTrue(rabbitmq.upstream_exists("remote-name"))
  713. def test_upstream_exists_negative(self):
  714. """
  715. Negative test of whether rabbitmq-internal upstream exists based
  716. on rabbitmqctl list_upstream.
  717. """
  718. mock_run = MagicMock(
  719. return_value={
  720. "retcode": 0,
  721. "stdout": 'federation-upstream\tremote-name\t{"ack-mode":"on-confirm"'
  722. ',"max-hops":1,"trust-user-id":true,"uri":"amqp://username:'
  723. 'password@remote.fqdn"}',
  724. "stderr": "",
  725. }
  726. )
  727. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  728. self.assertFalse(rabbitmq.upstream_exists("does-not-exist"))
  729. # 'add_upstream' function tests: 1
  730. def test_set_upstream(self):
  731. """
  732. Test if a rabbitMQ upstream gets configured properly.
  733. """
  734. mock_run = MagicMock(
  735. return_value={
  736. "retcode": 0,
  737. "stdout": (
  738. 'Setting runtime parameter "federation-upstream" for component '
  739. '"remote-name" to "{"trust-user-id": true, "uri": '
  740. '"amqp://username:password@remote.fqdn", "ack-mode": "on-confirm", '
  741. '"max-hops": 1}" in vhost "/" ...'
  742. ),
  743. "stderr": "",
  744. }
  745. )
  746. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  747. self.assertTrue(
  748. rabbitmq.set_upstream(
  749. "remote-name",
  750. "amqp://username:password@remote.fqdn",
  751. ack_mode="on-confirm",
  752. max_hops=1,
  753. trust_user_id=True,
  754. )
  755. )
  756. # 'delete_upstream' function tests: 2
  757. def test_delete_upstream(self):
  758. """
  759. Test if an upstream gets deleted properly using rabbitmqctl delete_upstream.
  760. """
  761. mock_run = MagicMock(
  762. return_value={
  763. "retcode": 0,
  764. "stdout": (
  765. 'Clearing runtime parameter "remote-name" for component '
  766. '"federation-upstream" on vhost "/" ...'
  767. ),
  768. "stderr": "",
  769. }
  770. )
  771. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  772. self.assertTrue(rabbitmq.delete_upstream("remote-name"))
  773. def test_delete_upstream_negative(self):
  774. """
  775. Negative test trying to delete a non-existant upstream.
  776. """
  777. mock_run = MagicMock(
  778. return_value={
  779. "retcode": 70,
  780. "stdout": (
  781. 'Clearing runtime parameter "remote-name" for component '
  782. '"federation-upstream" on vhost "/" ...'
  783. ),
  784. "stderr": "Error:\nParameter does not exist",
  785. }
  786. )
  787. with patch.dict(rabbitmq.__salt__, {"cmd.run_all": mock_run}):
  788. self.assertRaises(
  789. CommandExecutionError, rabbitmq.delete_upstream, "remote-name"
  790. )