test_nxos.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Mike Wiebe <@mikewiebe>
  4. """
  5. # Copyright (c) 2019 Cisco and/or its affiliates.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # Import Python libs
  19. from __future__ import absolute_import, print_function, unicode_literals
  20. # Import Salt Libs
  21. import salt.modules.cp as cp_module
  22. import salt.modules.file as file_module
  23. import salt.modules.nxos as nxos_module
  24. import salt.utils.nxos as nxos_utils
  25. import salt.utils.pycrypto
  26. from salt.exceptions import CommandExecutionError, SaltInvocationError
  27. # Import Salt Testing Libs
  28. from tests.support.mixins import LoaderModuleMockMixin
  29. from tests.support.mock import MagicMock, create_autospec, patch
  30. from tests.support.unit import TestCase, skipIf
  31. from tests.unit.modules.nxos.nxos_config import (
  32. config_input_file,
  33. config_result,
  34. config_result_file,
  35. initial_config,
  36. initial_config_file,
  37. modified_config,
  38. modified_config_file,
  39. save_running_config,
  40. set_role,
  41. template_engine_file_str,
  42. template_engine_file_str_file,
  43. unset_role,
  44. )
  45. from tests.unit.modules.nxos.nxos_grains import n9k_grains
  46. from tests.unit.modules.nxos.nxos_show_cmd_output import (
  47. n9k_get_user_output,
  48. n9k_show_user_account,
  49. n9k_show_user_account_list,
  50. n9k_show_ver,
  51. n9k_show_ver_int_list,
  52. n9k_show_ver_int_list_structured,
  53. n9k_show_ver_list,
  54. )
  55. from tests.unit.modules.nxos.nxos_show_run import (
  56. n9k_running_config,
  57. n9k_show_running_config_list,
  58. n9k_show_running_inc_username_list,
  59. )
  60. # pylint: disable-msg=C0103
  61. # pylint: disable-msg=C0301
  62. # pylint: disable-msg=E1101
  63. # pylint: disable-msg=R0904
  64. class NxosTestCase(TestCase, LoaderModuleMockMixin):
  65. """ Test cases for salt.modules.nxos """
  66. COPY_RS = "copy running-config startup-config"
  67. def setup_loader_modules(self):
  68. sendline = create_autospec(
  69. nxos_module.sendline, autospec=True, return_value={"command": "fake_output"}
  70. )
  71. return {nxos_module: {"__proxy__": {"nxos.sendline": sendline}}}
  72. def tearDown(self):
  73. pass
  74. @staticmethod
  75. def test_check_virtual():
  76. """ UT: nxos module:check_virtual method - return value """
  77. result = nxos_module.__virtual__()
  78. assert "nxos" in result
  79. def test_ping_proxy(self):
  80. """ UT: nxos module:ping method - proxy """
  81. with patch("salt.utils.platform.is_proxy", return_value=True, autospec=True):
  82. with patch.dict(
  83. nxos_module.__proxy__, {"nxos.ping": MagicMock(return_value=True)}
  84. ):
  85. result = nxos_module.ping()
  86. self.assertTrue(result)
  87. def test_ping_native_minion(self):
  88. """ UT: nxos module:ping method - proxy """
  89. with patch("salt.utils.platform.is_proxy", return_value=False, autospec=True):
  90. with patch.dict(
  91. nxos_module.__utils__, {"nxos.ping": MagicMock(return_value=True)}
  92. ):
  93. result = nxos_module.ping()
  94. self.assertTrue(result)
  95. def test_check_password_return_none(self):
  96. """ UT: nxos module:check_password method - return None """
  97. username = "admin"
  98. password = "foo"
  99. with patch("salt.modules.nxos.get_user", return_value=None, autospec=True):
  100. result = nxos_module.check_password(username, password, encrypted=False)
  101. self.assertIsNone(result)
  102. def test_check_password_password_nxos_comment(self):
  103. """ UT: nxos module:check_password method - password_line has '!' """
  104. username = "admin"
  105. password = "foo"
  106. with patch("salt.modules.nxos.get_user", return_value="!", autospec=True):
  107. result = nxos_module.check_password(username, password, encrypted=False)
  108. self.assertFalse(result)
  109. @skipIf(
  110. "sha256" not in salt.utils.pycrypto.methods,
  111. "compatible crypt method for fake data not available",
  112. )
  113. def test_check_password_password_encrypted_false(self):
  114. """ UT: nxos module:check_password method - password is not encrypted """
  115. username = "salt_test"
  116. password = "foobar123&"
  117. with patch(
  118. "salt.modules.nxos.get_user",
  119. return_value=n9k_get_user_output,
  120. autospec=True,
  121. ):
  122. result = nxos_module.check_password(username, password, encrypted=False)
  123. self.assertTrue(result)
  124. def test_check_password_password_encrypted_true(self):
  125. """ UT: nxos module:check_password method - password is encrypted """
  126. username = "salt_test"
  127. password = "$5$mkXh6O4T$YUVtA89HbXCnue63kgghPlaqPHyaXhdtxPBbPEHhbRC"
  128. with patch(
  129. "salt.modules.nxos.get_user",
  130. return_value=n9k_get_user_output,
  131. autospec=True,
  132. ):
  133. result = nxos_module.check_password(username, password, encrypted=True)
  134. self.assertTrue(result)
  135. def test_check_password_password_encrypted_true_negative(self):
  136. """ UT: nxos module:check_password method - password is not encrypted """
  137. username = "salt_test"
  138. password = "foobar123&"
  139. with patch(
  140. "salt.modules.nxos.get_user", return_value=n9k_running_config, autospec=True
  141. ):
  142. result = nxos_module.check_password(username, password, encrypted=True)
  143. self.assertFalse(result)
  144. def test_check_role_true(self):
  145. """ UT: nxos module:check_role method - Role configured """
  146. username = "salt_test"
  147. roles = ["network-admin", "dev-ops"]
  148. with patch("salt.modules.nxos.get_roles", return_value=roles, autospec=True):
  149. result = nxos_module.check_role(username, "dev-ops")
  150. self.assertTrue(result)
  151. def test_check_role_false(self):
  152. """ UT: nxos module:check_role method - Role not configured """
  153. username = "salt_test"
  154. roles = ["network-admin", "dev-ops"]
  155. with patch("salt.modules.nxos.get_roles", return_value=roles, autospec=True):
  156. result = nxos_module.check_role(username, "network-operator")
  157. self.assertFalse(result)
  158. def test_cmd_any_function(self):
  159. """ UT: nxos module:cmd method - check_role function """
  160. with patch.dict(
  161. nxos_module.__salt__,
  162. {
  163. "nxos.check_role": create_autospec(
  164. nxos_module.check_role, return_value=True
  165. )
  166. },
  167. ):
  168. result = nxos_module.cmd(
  169. "check_role",
  170. "salt_test",
  171. "network-admin",
  172. encrypted=True,
  173. __pub_fun="nxos.cmd",
  174. )
  175. self.assertTrue(result)
  176. def test_cmd_function_absent(self):
  177. """ UT: nxos module:cmd method - non existent function """
  178. result = nxos_module.cmd(
  179. "cool_new_function", "salt_test", "network-admin", encrypted=True
  180. )
  181. self.assertFalse(result)
  182. def test_find_single_match(self):
  183. """ UT: nxos module:test_find method - Find single match in running config """
  184. find_pattern = "^vrf context testing$"
  185. find_string = "vrf context testing"
  186. with patch(
  187. "salt.modules.nxos.show_run", return_value=n9k_running_config, autospec=True
  188. ):
  189. result = nxos_module.find(find_pattern)
  190. self.assertIn(find_string, result)
  191. def test_find_multiple_matches(self):
  192. """ UT: nxos module:test_find method - Find multiple matches in running config """
  193. find_pattern = "^no logging.*$"
  194. find_string = "no logging event link-status enable"
  195. with patch(
  196. "salt.modules.nxos.show_run", return_value=n9k_running_config, autospec=True
  197. ):
  198. result = nxos_module.find(find_pattern)
  199. self.assertIn(find_string, result)
  200. self.assertEqual(len(result), 7)
  201. def test_get_roles_user_not_configured(self):
  202. """ UT: nxos module:get_roles method - User not configured """
  203. username = "salt_does_not_exist"
  204. user_info = ""
  205. with patch("salt.modules.nxos.get_user", return_value=user_info, autospec=True):
  206. result = nxos_module.get_roles(username)
  207. self.assertEqual(result, [])
  208. def test_get_roles_user_configured(self):
  209. """ UT: nxos module:get_roles method - User configured """
  210. username = "salt_test"
  211. expected_result = ["network-operator", "network-admin", "dev-ops"]
  212. with patch("salt.modules.nxos.get_user", return_value=username, autospec=True):
  213. for rv in [n9k_show_user_account, n9k_show_user_account_list]:
  214. with patch("salt.modules.nxos.show", return_value=rv, autospec=True):
  215. result = nxos_module.get_roles(username)
  216. self.assertEqual(result.sort(), expected_result.sort())
  217. def test_get_roles_user_configured_no_role(self):
  218. """ UT: nxos module:get_roles method - User configured no roles"""
  219. username = "salt_test"
  220. with patch("salt.modules.nxos.get_user", return_value=username, autospec=True):
  221. with patch("salt.modules.nxos.show", return_value="", autospec=True):
  222. result = nxos_module.get_roles(username)
  223. self.assertEqual(result, [])
  224. def test_get_user_configured(self):
  225. """ UT: nxos module:get_user method - User configured """
  226. username = "salt_test"
  227. expected_output = n9k_show_running_inc_username_list[0]
  228. for rv in [
  229. n9k_show_running_inc_username_list[0],
  230. n9k_show_running_inc_username_list,
  231. ]:
  232. with patch("salt.modules.nxos.show", return_value=rv, autospec=True):
  233. result = nxos_module.get_user(username)
  234. self.assertEqual(result, expected_output)
  235. def test_grains(self):
  236. """ UT: nxos module:grains method """
  237. nxos_module.DEVICE_DETAILS["grains_cache"] = {}
  238. expected_grains = {
  239. "software": {
  240. "BIOS": "version 08.36",
  241. "NXOS": "version 9.2(1)",
  242. "BIOS compile time": "06/07/2019",
  243. "NXOS image file is": "bootflash:///nxos.9.2.1.bin",
  244. "NXOS compile time": "7/17/2018 16:00:00 [07/18/2018 00:21:19]",
  245. },
  246. "hardware": {"Device name": "n9k-device", "bootflash": "53298520 kB"},
  247. "plugins": ["Core Plugin", "Ethernet Plugin"],
  248. }
  249. with patch.dict(
  250. nxos_module.__salt__,
  251. {
  252. "utils.nxos.system_info": create_autospec(
  253. nxos_utils.system_info, return_value=n9k_grains
  254. )
  255. },
  256. ):
  257. with patch(
  258. "salt.modules.nxos.show_ver", return_value=n9k_show_ver, autospec=True
  259. ):
  260. result = nxos_module.grains()
  261. self.assertEqual(result, expected_grains)
  262. def test_grains_get_cache(self):
  263. """ UT: nxos module:grains method """
  264. expected_grains = {
  265. "software": {
  266. "BIOS": "version 08.36",
  267. "NXOS": "version 9.2(1)",
  268. "BIOS compile time": "06/07/2019",
  269. "NXOS image file is": "bootflash:///nxos.9.2.1.bin",
  270. "NXOS compile time": "7/17/2018 16:00:00 [07/18/2018 00:21:19]",
  271. },
  272. "hardware": {"Device name": "n9k-device", "bootflash": "53298520 kB"},
  273. "plugins": ["Core Plugin", "Ethernet Plugin"],
  274. }
  275. nxos_module.DEVICE_DETAILS["grains_cache"] = expected_grains
  276. with patch.dict(
  277. nxos_module.__salt__,
  278. {
  279. "utils.nxos.system_info": create_autospec(
  280. nxos_utils.system_info, return_value=n9k_grains
  281. )
  282. },
  283. ):
  284. with patch(
  285. "salt.modules.nxos.show_ver", return_value=n9k_show_ver, autospec=True
  286. ):
  287. result = nxos_module.grains()
  288. self.assertEqual(result, expected_grains)
  289. def test_grains_refresh(self):
  290. """ UT: nxos module:grains_refresh method """
  291. expected_grains = {
  292. "software": {
  293. "BIOS": "version 08.36",
  294. "NXOS": "version 9.2(1)",
  295. "BIOS compile time": "06/07/2019",
  296. "NXOS image file is": "bootflash:///nxos.9.2.1.bin",
  297. "NXOS compile time": "7/17/2018 16:00:00 [07/18/2018 00:21:19]",
  298. },
  299. "hardware": {"Device name": "n9k-device", "bootflash": "53298520 kB"},
  300. "plugins": ["Core Plugin", "Ethernet Plugin"],
  301. }
  302. with patch(
  303. "salt.modules.nxos.grains", return_value=expected_grains, autospec=True
  304. ):
  305. result = nxos_module.grains_refresh()
  306. self.assertEqual(result, expected_grains)
  307. def test_system_info(self):
  308. """ UT: nxos module:system_info method """
  309. expected_grains = {
  310. "software": {
  311. "BIOS": "version 08.36",
  312. "NXOS": "version 9.2(1)",
  313. "BIOS compile time": "06/07/2019",
  314. "NXOS image file is": "bootflash:///nxos.9.2.1.bin",
  315. "NXOS compile time": "7/17/2018 16:00:00 [07/18/2018 00:21:19]",
  316. },
  317. "hardware": {"Device name": "n9k-device", "bootflash": "53298520 kB"},
  318. "plugins": ["Core Plugin", "Ethernet Plugin"],
  319. }
  320. with patch.dict(
  321. nxos_module.__salt__,
  322. {
  323. "utils.nxos.system_info": create_autospec(
  324. nxos_utils.system_info, return_value=n9k_grains
  325. )
  326. },
  327. ):
  328. with patch(
  329. "salt.modules.nxos.show_ver", return_value=n9k_show_ver, autospec=True
  330. ):
  331. result = nxos_module.system_info()
  332. self.assertEqual(result, expected_grains)
  333. def test_sendline_invalid_method(self):
  334. """ UT: nxos module:sendline method - invalid method """
  335. command = "show version"
  336. method = "invalid"
  337. # Execute the function under test
  338. result = nxos_module.sendline(command, method)
  339. self.assertIn("INPUT ERROR", result)
  340. def test_sendline_valid_method_proxy(self):
  341. """ UT: nxos module:sendline method - valid method over proxy """
  342. command = "show version"
  343. method = "cli_show_ascii"
  344. with patch("salt.utils.platform.is_proxy", return_value=True, autospec=True):
  345. nxos_module.__proxy__["nxos.sendline"].return_value = n9k_show_ver
  346. result = nxos_module.sendline(command, method)
  347. self.assertIn(n9k_show_ver, result)
  348. def test_sendline_valid_method_nxapi_uds(self):
  349. """ UT: nxos module:sendline method - valid method over nxapi uds """
  350. command = "show version"
  351. method = "cli_show_ascii"
  352. with patch("salt.utils.platform.is_proxy", MagicMock(return_value=False)):
  353. with patch(
  354. "salt.modules.nxos._nxapi_request",
  355. return_value=n9k_show_ver,
  356. autospec=True,
  357. ):
  358. result = nxos_module.sendline(command, method)
  359. self.assertIn(n9k_show_ver, result)
  360. def test_show_raw_text_invalid(self):
  361. """ UT: nxos module:show method - invalid argument """
  362. command = "show version"
  363. raw_text = "invalid"
  364. result = nxos_module.show(command, raw_text)
  365. self.assertIn("INPUT ERROR", result)
  366. def test_show_raw_text_true(self):
  367. """ UT: nxos module:show method - raw_test true """
  368. command = "show version"
  369. raw_text = True
  370. with patch(
  371. "salt.modules.nxos.sendline", autospec=True, return_value=n9k_show_ver
  372. ):
  373. result = nxos_module.show(command, raw_text)
  374. self.assertEqual(result, n9k_show_ver)
  375. def test_show_raw_text_true_multiple_commands(self):
  376. """ UT: nxos module:show method - raw_test true multiple commands """
  377. command = "show bgp sessions ; show processes"
  378. raw_text = True
  379. data = ["bgp_session_data", "process_data"]
  380. with patch("salt.modules.nxos.sendline", autospec=True, return_value=data):
  381. result = nxos_module.show(command, raw_text)
  382. self.assertEqual(result, data)
  383. def test_show_nxapi(self):
  384. """ UT: nxos module:show method - nxapi returns info as list """
  385. command = "show version; show interface eth1/1"
  386. raw_text = True
  387. with patch(
  388. "salt.modules.nxos.sendline",
  389. autospec=True,
  390. return_value=n9k_show_ver_int_list,
  391. ):
  392. result = nxos_module.show(command, raw_text)
  393. self.assertEqual(result[0], n9k_show_ver_int_list[0])
  394. self.assertEqual(result[1], n9k_show_ver_int_list[1])
  395. def test_show_nxapi_structured(self):
  396. """ UT: nxos module:show method - nxapi returns info as list """
  397. command = "show version; show interface eth1/1"
  398. raw_text = False
  399. with patch(
  400. "salt.modules.nxos.sendline",
  401. autospec=True,
  402. return_value=n9k_show_ver_int_list_structured,
  403. ):
  404. result = nxos_module.show(command, raw_text)
  405. self.assertEqual(result[0], n9k_show_ver_int_list_structured[0])
  406. self.assertEqual(result[1], n9k_show_ver_int_list_structured[1])
  407. def test_show_run(self):
  408. """ UT: nxos module:show_run method """
  409. expected_output = n9k_show_running_config_list[0]
  410. for rv in [n9k_show_running_config_list[0], n9k_show_running_config_list]:
  411. with patch("salt.modules.nxos.show", autospec=True, return_value=rv):
  412. result = nxos_module.show_run()
  413. self.assertEqual(result, expected_output)
  414. def test_show_ver(self):
  415. """ UT: nxos module:show_ver method """
  416. expected_output = n9k_show_ver_list[0]
  417. for rv in [n9k_show_ver_list[0], n9k_show_ver_list]:
  418. with patch("salt.modules.nxos.show", autospec=True, return_value=rv):
  419. result = nxos_module.show_ver()
  420. self.assertEqual(result, expected_output)
  421. def test_add_config(self):
  422. """ UT: nxos module:add_config method """
  423. expected_output = "COMMAND_LIST: feature bgp"
  424. with patch(
  425. "salt.modules.nxos.config", autospec=True, return_value=expected_output
  426. ):
  427. result = nxos_module.add_config("feature bgp")
  428. self.assertEqual(result, expected_output)
  429. def test_config_commands(self):
  430. """ UT: nxos module:config method - Using commands arg"""
  431. commands = ["no feature ospf", ["no feature ospf"]]
  432. expected_output = "COMMAND_LIST: no feature ospf\n\n--- \n+++ \n@@ -19,7 +19,6 @@\n feature bash-shell\n cfs eth distribute\n feature ngmvpn\n-feature ospf\n feature pim\n feature lldp\n \n"
  433. for cmd_set in commands:
  434. with patch(
  435. "salt.modules.nxos.show",
  436. autospec=True,
  437. side_effect=[initial_config, modified_config],
  438. ):
  439. mock_cmd = create_autospec(
  440. file_module.apply_template_on_contents,
  441. return_value=template_engine_file_str,
  442. )
  443. with patch.dict(
  444. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  445. ):
  446. with patch(
  447. "salt.modules.nxos._configure_device",
  448. autospec=True,
  449. return_value=config_result,
  450. ):
  451. result = nxos_module.config(cmd_set, save_config=False)
  452. self.assertEqual(result, expected_output)
  453. def test_config_commands_template_none(self):
  454. """ UT: nxos module:config method - Template engine is None"""
  455. commands = ["no feature ospf", ["no feature ospf"]]
  456. expected_output = "COMMAND_LIST: no feature ospf\n\n--- \n+++ \n@@ -19,7 +19,6 @@\n feature bash-shell\n cfs eth distribute\n feature ngmvpn\n-feature ospf\n feature pim\n feature lldp\n \n"
  457. for cmd_set in commands:
  458. with patch(
  459. "salt.modules.nxos.show",
  460. autospec=True,
  461. side_effect=[initial_config, modified_config],
  462. ):
  463. mock_cmd = create_autospec(
  464. file_module.apply_template_on_contents,
  465. return_value=template_engine_file_str,
  466. )
  467. with patch.dict(
  468. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  469. ):
  470. with patch(
  471. "salt.modules.nxos._configure_device",
  472. autospec=True,
  473. return_value=config_result,
  474. ):
  475. result = nxos_module.config(cmd_set, template_engine=None)
  476. self.assertEqual(result, expected_output)
  477. def test_config_commands_string(self):
  478. """ UT: nxos module:config method - Using commands arg and output is string"""
  479. commands = "no feature ospf"
  480. expected_output = "COMMAND_LIST: no feature ospf\n\n--- \n+++ \n@@ -19,7 +19,6 @@\n feature bash-shell\n cfs eth distribute\n feature ngmvpn\n-feature ospf\n feature pim\n feature lldp\n \n"
  481. with patch(
  482. "salt.modules.nxos.show",
  483. autospec=True,
  484. side_effect=[initial_config[0], modified_config[0]],
  485. ):
  486. mock_cmd = create_autospec(
  487. file_module.apply_template_on_contents,
  488. return_value=template_engine_file_str,
  489. )
  490. with patch.dict(
  491. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  492. ):
  493. with patch(
  494. "salt.modules.nxos._configure_device",
  495. autospec=True,
  496. return_value=config_result,
  497. ):
  498. result = nxos_module.config(commands)
  499. self.assertEqual(result, expected_output)
  500. def test_config_file(self):
  501. """ UT: nxos module:config method - Using config_file arg"""
  502. config_file = "salt://bgp_config.txt"
  503. expected_output = "COMMAND_LIST: feature bgp ; ! ; router bgp 55 ; address-family ipv4 unicast ; no client-to-client reflection ; additional-paths send\n\n--- \n+++ \n@@ -19,6 +19,7 @@\n feature bash-shell\n cfs eth distribute\n feature ngmvpn\n+feature bgp\n feature pim\n feature lldp\n \n@@ -233,6 +234,10 @@\n line console\n line vty\n boot nxos bootflash:/nxos.9.2.4.bin \n+router bgp 55\n+ address-family ipv4 unicast\n+ no client-to-client reflection\n+ additional-paths send\n \n no logging logfile\n no logging monitor\n"
  504. with patch(
  505. "salt.modules.nxos.show",
  506. autospec=True,
  507. side_effect=[initial_config_file, modified_config_file],
  508. ):
  509. mock_cmd = create_autospec(
  510. cp_module.get_file_str, return_value=config_input_file
  511. )
  512. with patch.dict(nxos_module.__salt__, {"cp.get_file_str": mock_cmd}):
  513. mock_cmd = create_autospec(
  514. file_module.apply_template_on_contents,
  515. return_value=template_engine_file_str_file,
  516. )
  517. with patch.dict(
  518. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  519. ):
  520. with patch(
  521. "salt.modules.nxos._configure_device",
  522. autospec=True,
  523. return_value=config_result_file,
  524. ):
  525. result = nxos_module.config(config_file=config_file)
  526. self.assertEqual(result, expected_output)
  527. def test_config_file_error1(self):
  528. """ UT: nxos module:config method - Error file not found """
  529. config_file = "salt://bgp_config.txt"
  530. with patch(
  531. "salt.modules.nxos.show",
  532. autospec=True,
  533. side_effect=[initial_config_file, modified_config_file],
  534. ):
  535. mock_cmd = create_autospec(cp_module.get_file_str, return_value=False)
  536. with patch.dict(nxos_module.__salt__, {"cp.get_file_str": mock_cmd}):
  537. mock_cmd = create_autospec(
  538. file_module.apply_template_on_contents,
  539. return_value=template_engine_file_str_file,
  540. )
  541. with patch.dict(
  542. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  543. ):
  544. with patch(
  545. "salt.modules.nxos._configure_device",
  546. autospec=True,
  547. return_value=config_result_file,
  548. ):
  549. with self.assertRaises(CommandExecutionError):
  550. nxos_module.config(config_file=config_file)
  551. def test_config_nxos_error_ssh(self):
  552. """ UT: nxos module:config method - nxos device error over ssh transport """
  553. commands = ["feature bgp", "router bgp 57"]
  554. config_result = [
  555. ["feature bgp", "router bgp 57"],
  556. "bgp instance is already running; Tag is 55",
  557. ]
  558. expected_output = "COMMAND_LIST: feature bgp ; router bgp 57\nbgp instance is already running; Tag is 55\n--- \n+++ \n@@ -19,7 +19,6 @@\n feature bash-shell\n cfs eth distribute\n feature ngmvpn\n-feature ospf\n feature pim\n feature lldp\n \n"
  559. with patch(
  560. "salt.modules.nxos.show",
  561. autospec=True,
  562. side_effect=[initial_config[0], modified_config[0]],
  563. ):
  564. mock_cmd = create_autospec(
  565. file_module.apply_template_on_contents,
  566. return_value=template_engine_file_str,
  567. )
  568. with patch.dict(
  569. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  570. ):
  571. with patch(
  572. "salt.modules.nxos._configure_device",
  573. autospec=True,
  574. return_value=config_result,
  575. ):
  576. result = nxos_module.config(commands)
  577. self.assertEqual(result, expected_output)
  578. def test_commands_error(self):
  579. """ UT: nxos module:config method - Mandatory arg commands not specified """
  580. commands = None
  581. with patch(
  582. "salt.modules.nxos.show",
  583. autospec=True,
  584. side_effect=[initial_config_file, modified_config_file],
  585. ):
  586. mock_cmd = create_autospec(cp_module.get_file_str, return_value=False)
  587. with patch.dict(nxos_module.__salt__, {"cp.get_file_str": mock_cmd}):
  588. mock_cmd = create_autospec(
  589. file_module.apply_template_on_contents,
  590. return_value=template_engine_file_str_file,
  591. )
  592. with patch.dict(
  593. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  594. ):
  595. with patch(
  596. "salt.modules.nxos._configure_device",
  597. autospec=True,
  598. return_value=config_result_file,
  599. ):
  600. with self.assertRaises(CommandExecutionError):
  601. nxos_module.config(commands=commands)
  602. def test_config_file_error2(self):
  603. """ UT: nxos module:config method - Mandatory arg config_file not specified """
  604. config_file = None
  605. with patch(
  606. "salt.modules.nxos.show",
  607. autospec=True,
  608. side_effect=[initial_config_file, modified_config_file],
  609. ):
  610. mock_cmd = create_autospec(cp_module.get_file_str, return_value=False)
  611. with patch.dict(nxos_module.__salt__, {"cp.get_file_str": mock_cmd}):
  612. mock_cmd = create_autospec(
  613. file_module.apply_template_on_contents,
  614. return_value=template_engine_file_str_file,
  615. )
  616. with patch.dict(
  617. nxos_module.__salt__, {"file.apply_template_on_contents": mock_cmd}
  618. ):
  619. with patch(
  620. "salt.modules.nxos._configure_device",
  621. autospec=True,
  622. return_value=config_result_file,
  623. ):
  624. with self.assertRaises(CommandExecutionError):
  625. nxos_module.config(config_file=config_file)
  626. def test_delete_config(self):
  627. """ UT: nxos module:delete_config method """
  628. for lines in ["feature bgp", ["feature bgp"]]:
  629. with patch("salt.modules.nxos.config", autospec=True):
  630. result = nxos_module.delete_config(lines)
  631. nxos_module.config.assert_called_with(["no feature bgp"])
  632. self.assertEqual(result, nxos_module.config.return_value)
  633. def test_remove_user(self):
  634. """ UT: nxos module:remove_user method """
  635. with patch("salt.modules.nxos.config", autospec=True):
  636. result = nxos_module.remove_user("salt_test")
  637. nxos_module.config.assert_called_with("no username salt_test")
  638. self.assertEqual(result, nxos_module.config.return_value)
  639. def test_replace(self):
  640. """ UT: nxos module:replace method """
  641. old_value = "feature bgp"
  642. new_value = "feature ospf"
  643. with patch(
  644. "salt.modules.nxos.show_run",
  645. autospec=True,
  646. return_value=n9k_show_running_config_list[0],
  647. ):
  648. with patch(
  649. "salt.modules.nxos.delete_config", autospec=True, return_value=None
  650. ):
  651. with patch(
  652. "salt.modules.nxos.add_config", autospec=True, return_value=None
  653. ):
  654. result = nxos_module.replace(old_value, new_value)
  655. self.assertEqual(result["old"], ["feature bgp"])
  656. self.assertEqual(result["new"], ["feature ospf"])
  657. def test_replace_full_match_true(self):
  658. """ UT: nxos module:replace method - full match true"""
  659. old_value = "feature bgp"
  660. new_value = "feature ospf"
  661. with patch(
  662. "salt.modules.nxos.show_run",
  663. autospec=True,
  664. return_value=n9k_show_running_config_list[0],
  665. ):
  666. with patch(
  667. "salt.modules.nxos.delete_config", autospec=True, return_value=None
  668. ):
  669. with patch(
  670. "salt.modules.nxos.add_config", autospec=True, return_value=None
  671. ):
  672. result = nxos_module.replace(old_value, new_value, full_match=True)
  673. self.assertEqual(result["old"], ["feature bgp"])
  674. self.assertEqual(result["new"], ["feature ospf"])
  675. def test_replace_no_match(self):
  676. """ UT: nxos module:replace method - no match """
  677. old_value = "feature does_not_exist"
  678. new_value = "feature ospf"
  679. with patch(
  680. "salt.modules.nxos.show_run",
  681. autospec=True,
  682. return_value=n9k_show_running_config_list[0],
  683. ):
  684. with patch(
  685. "salt.modules.nxos.delete_config", autospec=True, return_value=None
  686. ):
  687. with patch(
  688. "salt.modules.nxos.add_config", autospec=True, return_value=None
  689. ):
  690. result = nxos_module.replace(old_value, new_value)
  691. self.assertEqual(result["old"], [])
  692. self.assertEqual(result["new"], [])
  693. def test_save_running_config(self):
  694. """ UT: nxos module:save_running_config method """
  695. with patch(
  696. "salt.modules.nxos.config", autospec=True, return_value=save_running_config
  697. ):
  698. result = nxos_module.save_running_config()
  699. self.assertEqual(result, save_running_config)
  700. def test_set_password_enc_false_cs_none(self):
  701. """ UT: nxos module:set_password method - encrypted False, crypt_salt None """
  702. username = "devops"
  703. password = "test123TMM^&"
  704. hashed_pass = "$5$ZcZqm15X$exHN2m6yrPKpYhGArK3Vml3ZjNbJaJYdzWyf0fp1Up2"
  705. config_line = "username devops password 5 $5$ZcZqm15X$exHN2m6yrPKpYhGArK3Vml3ZjNbJaJYdzWyf0fp1Up2"
  706. with patch("salt.modules.nxos.get_user", autospec=True):
  707. with patch(
  708. "salt.modules.nxos.gen_hash", autospec=True, return_value=hashed_pass
  709. ):
  710. with patch(
  711. "salt.modules.nxos.config",
  712. autospec=True,
  713. return_value="password_set",
  714. ) as config:
  715. result = nxos_module.set_password(username, password)
  716. config.assert_called_with(config_line)
  717. self.assertEqual("password_set", result)
  718. def test_set_password_enc_false_cs_set(self):
  719. """ UT: nxos module:set_password method - encrypted False, crypt_salt set """
  720. username = "devops"
  721. password = "test123TMM^&"
  722. crypt_salt = "ZcZqm15X"
  723. hashed_pass = "$5$ZcZqm15X$exHN2m6yrPKpYhGArK3Vml3ZjNbJaJYdzWyf0fp1Up2"
  724. config_line = "username devops password 5 $5$ZcZqm15X$exHN2m6yrPKpYhGArK3Vml3ZjNbJaJYdzWyf0fp1Up2"
  725. with patch("salt.modules.nxos.get_user", autospec=True):
  726. with patch(
  727. "salt.modules.nxos.gen_hash", autospec=True, return_value=hashed_pass
  728. ):
  729. with patch(
  730. "salt.modules.nxos.config",
  731. autospec=True,
  732. return_value="password_set",
  733. ) as config:
  734. result = nxos_module.set_password(
  735. username, password, crypt_salt=crypt_salt
  736. )
  737. config.assert_called_with(config_line)
  738. self.assertEqual("password_set", result)
  739. def test_set_password_enc_true(self):
  740. """ UT: nxos module:set_password method - encrypted True """
  741. username = "devops"
  742. password = "test123TMM^&"
  743. hashed_pass = "$5$ZcZqm15X$exHN2m6yrPKpYhGArK3Vml3ZjNbJaJYdzWyf0fp1Up2"
  744. config_line = "username devops password 5 test123TMM^&"
  745. with patch("salt.modules.nxos.get_user", autospec=True):
  746. with patch(
  747. "salt.modules.nxos.gen_hash", autospec=True, return_value=hashed_pass
  748. ):
  749. with patch(
  750. "salt.modules.nxos.config",
  751. autospec=True,
  752. return_value="password_set",
  753. ) as config:
  754. result = nxos_module.set_password(
  755. username, password, encrypted=True
  756. )
  757. config.assert_called_with(config_line)
  758. self.assertEqual("password_set", result)
  759. def test_set_password_role_none(self):
  760. """ UT: nxos module:set_password method - role none """
  761. username = "devops"
  762. password = "test123TMM^&"
  763. hashed_pass = "$5$ZcZqm15X$exHN2m6yrPKpYhGArK3Vml3ZjNbJaJYdzWyf0fp1Up2"
  764. config_line = "username devops password 5 test123TMM^& role devops"
  765. with patch("salt.modules.nxos.get_user", autospec=True):
  766. with patch(
  767. "salt.modules.nxos.gen_hash", autospec=True, return_value=hashed_pass
  768. ):
  769. with patch(
  770. "salt.modules.nxos.config",
  771. autospec=True,
  772. return_value="password_set",
  773. ) as config:
  774. # Execute the function under test
  775. result = nxos_module.set_password(
  776. username, password, encrypted=True, role="devops"
  777. )
  778. config.assert_called_with(config_line)
  779. self.assertEqual("password_set", result)
  780. def test_set_password_blowfish_crypt(self):
  781. """ UT: nxos module:set_password method - role none """
  782. with self.assertRaises(SaltInvocationError):
  783. nxos_module.set_password(
  784. "username", "password", encrypted=True, algorithm="blowfish"
  785. )
  786. def test_set_role(self):
  787. """ UT: nxos module:save_running_config method """
  788. username = "salt_test"
  789. role = "vdc-admin"
  790. with patch("salt.modules.nxos.config", autospec=True, return_value=set_role):
  791. result = nxos_module.set_role(username, role)
  792. self.assertEqual(result, set_role)
  793. def test_unset_role(self):
  794. """ UT: nxos module:save_running_config method """
  795. username = "salt_test"
  796. role = "vdc-admin"
  797. with patch("salt.modules.nxos.config", autospec=True, return_value=unset_role):
  798. result = nxos_module.unset_role(username, role)
  799. self.assertEqual(result, unset_role)
  800. def test_configure_device(self):
  801. """ UT: nxos module:_configure_device method """
  802. with patch("salt.utils.platform.is_proxy", autospec=True, return_value=True):
  803. with patch.dict(
  804. nxos_module.__proxy__,
  805. {"nxos.proxy_config": MagicMock(return_value="configured")},
  806. ):
  807. result = nxos_module._configure_device("feature bgp")
  808. self.assertEqual(result, "configured")
  809. with patch("salt.utils.platform.is_proxy", autospec=True, return_value=False):
  810. with patch.object(
  811. nxos_module, "_nxapi_config", MagicMock(return_value="configured")
  812. ):
  813. nxos_module._configure_device("feature bgp")
  814. self.assertEqual(result, "configured")
  815. def test_nxapi_config(self):
  816. """ UT: nxos module:_nxapi_config method """
  817. mock_cmd = MagicMock(return_value={"nxos": {"save_config": False}})
  818. with patch.dict(nxos_module.__salt__, {"config.get": mock_cmd}):
  819. with patch(
  820. "salt.modules.nxos._nxapi_request",
  821. return_value="router_data",
  822. autospec=True,
  823. ):
  824. result = nxos_module._nxapi_config("show version")
  825. self.assertEqual(result, [["show version"], "router_data"])
  826. def test_nxapi_config_failure(self):
  827. """ UT: nxos module:_nxapi_config method """
  828. side_effect = ["Failure", "saved_data"]
  829. mock_cmd = MagicMock(return_value={"nxos": {"save_config": True}})
  830. with patch.dict(nxos_module.__salt__, {"config.get": mock_cmd}):
  831. with patch(
  832. "salt.modules.nxos._nxapi_request",
  833. side_effect=side_effect,
  834. autospec=True,
  835. ):
  836. result = nxos_module._nxapi_config("show bad_command")
  837. self.assertEqual(result, [["show bad_command"], "Failure"])
  838. def test_nxapi_request_proxy(self):
  839. """ UT: nxos module:_nxapi_request method - proxy"""
  840. with patch("salt.utils.platform.is_proxy", autospec=True, return_value=True):
  841. mock_request = create_autospec(
  842. nxos_utils.nxapi_request, return_value="router_data"
  843. )
  844. with patch.dict(
  845. nxos_module.__proxy__, {"nxos._nxapi_request": mock_request}
  846. ):
  847. result = nxos_module._nxapi_request("show version")
  848. self.assertEqual(result, "router_data")
  849. def test_nxapi_request_no_proxy(self):
  850. """ UT: nxos module:_nxapi_request method - no proxy"""
  851. with patch("salt.utils.platform.is_proxy", autospec=True, return_value=False):
  852. mock_cmd = MagicMock(return_value={"nxos": {"save_config": False}})
  853. with patch.dict(nxos_module.__salt__, {"config.get": mock_cmd}):
  854. mock_request = create_autospec(nxos_utils.nxapi_request)
  855. with patch.dict(
  856. nxos_module.__utils__, {"nxos.nxapi_request": mock_request}
  857. ):
  858. result = nxos_module._nxapi_request("show version")
  859. self.assertEqual(result, mock_request.return_value)
  860. mock_request.assert_called_with(
  861. "show version", "cli_conf", **mock_cmd.return_value
  862. )