test_win_network.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.win_network as win_network
  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. class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Validate the nftables state
  16. """
  17. def setup_loader_modules(self):
  18. return {win_network: {}}
  19. def test_managed_missing_parameters(self):
  20. """
  21. Test to ensure that the named interface is configured properly.
  22. """
  23. ret = {
  24. "name": "salt",
  25. "changes": {},
  26. "result": False,
  27. "comment": "dns_proto must be one of the following: static, dhcp\n"
  28. "ip_proto must be one of the following: static, dhcp",
  29. }
  30. self.assertDictEqual(win_network.managed("salt"), ret)
  31. def test_managed_static_enabled_false(self):
  32. ret = {
  33. "name": "salt",
  34. "changes": {},
  35. "result": True,
  36. "comment": "Interface 'salt' is up to date (already disabled)",
  37. }
  38. mock_false = MagicMock(return_value=False)
  39. with patch.dict(win_network.__salt__, {"ip.is_enabled": mock_false}):
  40. self.assertDictEqual(
  41. win_network.managed(
  42. "salt", dns_proto="static", ip_proto="static", enabled=False
  43. ),
  44. ret,
  45. )
  46. def test_managed_test_true(self):
  47. ret = {
  48. "name": "salt",
  49. "changes": {},
  50. "result": False,
  51. "comment": "Failed to enable interface 'salt' to make changes",
  52. }
  53. mock_false = MagicMock(return_value=False)
  54. with patch.dict(
  55. win_network.__salt__, {"ip.is_enabled": mock_false, "ip.enable": mock_false}
  56. ), patch.dict(win_network.__opts__, {"test": False}):
  57. self.assertDictEqual(
  58. win_network.managed("salt", dns_proto="static", ip_proto="static"), ret
  59. )
  60. def test_managed_validate_errors(self):
  61. ret = {
  62. "name": "salt",
  63. "changes": {},
  64. "result": False,
  65. "comment": "The following SLS configuration errors were "
  66. "detected:\n"
  67. "- First Error\n"
  68. "- Second Error",
  69. }
  70. mock_true = MagicMock(return_value=True)
  71. mock_validate = MagicMock(return_value=["First Error", "Second Error"])
  72. with patch.dict(
  73. win_network.__salt__, {"ip.is_enabled": mock_true}
  74. ), patch.object(win_network, "_validate", mock_validate):
  75. self.assertDictEqual(
  76. win_network.managed("salt", dns_proto="static", ip_proto="static"), ret
  77. )
  78. def test_managed_get_current_config_failed(self):
  79. ret = {
  80. "name": "salt",
  81. "changes": {},
  82. "result": False,
  83. "comment": "Unable to get current configuration for interface " "'salt'",
  84. }
  85. mock_true = MagicMock(return_value=True)
  86. mock_false = MagicMock(return_value=False)
  87. mock_validate = MagicMock(return_value=[])
  88. with patch.dict(
  89. win_network.__salt__,
  90. {"ip.is_enabled": mock_true, "ip.get_interface": mock_false},
  91. ), patch.object(win_network, "_validate", mock_validate):
  92. self.assertDictEqual(
  93. win_network.managed("salt", dns_proto="dhcp", ip_proto="dhcp"), ret
  94. )
  95. def test_managed_test_true_no_changes(self):
  96. ret = {
  97. "name": "salt",
  98. "changes": {},
  99. "result": True,
  100. "comment": "Interface 'salt' is up to date",
  101. }
  102. mock_true = MagicMock(return_value=True)
  103. mock_validate = MagicMock(return_value=[])
  104. mock_get_int = MagicMock(
  105. return_value={
  106. "DHCP enabled": "yes",
  107. "DNS servers configured through DHCP": "192.168.0.10",
  108. }
  109. )
  110. with patch.dict(
  111. win_network.__salt__,
  112. {"ip.is_enabled": mock_true, "ip.get_interface": mock_get_int},
  113. ), patch.dict(win_network.__opts__, {"test": True}), patch.object(
  114. win_network, "_validate", mock_validate
  115. ):
  116. self.assertDictEqual(
  117. win_network.managed("salt", dns_proto="dhcp", ip_proto="dhcp"), ret
  118. )
  119. def test_managed_test_true_changes(self):
  120. ret = {
  121. "name": "salt",
  122. "changes": {},
  123. "result": None,
  124. "comment": "The following changes will be made to interface "
  125. "'salt':\n"
  126. "- DNS protocol will be changed to: dhcp",
  127. }
  128. mock_true = MagicMock(return_value=True)
  129. mock_validate = MagicMock(return_value=[])
  130. mock_get_int = MagicMock(
  131. return_value={
  132. "DHCP enabled": "no",
  133. "Statically Configured DNS Servers": "192.168.0.10",
  134. }
  135. )
  136. with patch.dict(
  137. win_network.__salt__,
  138. {"ip.is_enabled": mock_true, "ip.get_interface": mock_get_int},
  139. ), patch.dict(win_network.__opts__, {"test": True}), patch.object(
  140. win_network, "_validate", mock_validate
  141. ):
  142. self.assertDictEqual(
  143. win_network.managed("salt", dns_proto="dhcp", ip_proto="dhcp"), ret
  144. )
  145. def test_managed_failed(self):
  146. ret = {
  147. "name": "salt",
  148. "changes": {},
  149. "result": False,
  150. "comment": "Failed to set desired configuration settings for "
  151. "interface 'salt'",
  152. }
  153. mock_true = MagicMock(return_value=True)
  154. mock_validate = MagicMock(return_value=[])
  155. mock_get_int = MagicMock(
  156. return_value={
  157. "DHCP enabled": "no",
  158. "Statically Configured DNS Servers": "192.168.0.10",
  159. }
  160. )
  161. with patch.dict(
  162. win_network.__salt__,
  163. {
  164. "ip.is_enabled": mock_true,
  165. "ip.get_interface": mock_get_int,
  166. "ip.set_dhcp_dns": mock_true,
  167. "ip.set_dhcp_ip": mock_true,
  168. },
  169. ), patch.dict(win_network.__opts__, {"test": False}), patch.object(
  170. win_network, "_validate", mock_validate
  171. ):
  172. self.assertDictEqual(
  173. win_network.managed("salt", dns_proto="dhcp", ip_proto="dhcp"), ret
  174. )
  175. def test_managed(self):
  176. ret = {
  177. "name": "salt",
  178. "changes": {
  179. "DHCP enabled": {"new": "yes", "old": "no"},
  180. "DNS servers configured through DHCP": {
  181. "new": "192.168.0.10",
  182. "old": "",
  183. },
  184. "Statically Configured DNS Servers": {"new": "", "old": "192.168.0.10"},
  185. },
  186. "result": True,
  187. "comment": "Successfully updated configuration for interface " "'salt'",
  188. }
  189. mock_true = MagicMock(return_value=True)
  190. mock_validate = MagicMock(return_value=[])
  191. mock_get_int = MagicMock(
  192. side_effect=[
  193. {
  194. "DHCP enabled": "no",
  195. "Statically Configured DNS Servers": "192.168.0.10",
  196. },
  197. {
  198. "DHCP enabled": "yes",
  199. "DNS servers configured through DHCP": "192.168.0.10",
  200. },
  201. ]
  202. )
  203. with patch.dict(
  204. win_network.__salt__,
  205. {
  206. "ip.is_enabled": mock_true,
  207. "ip.get_interface": mock_get_int,
  208. "ip.set_dhcp_dns": mock_true,
  209. "ip.set_dhcp_ip": mock_true,
  210. },
  211. ), patch.dict(win_network.__opts__, {"test": False}), patch.object(
  212. win_network, "_validate", mock_validate
  213. ):
  214. self.assertDictEqual(
  215. win_network.managed("salt", dns_proto="dhcp", ip_proto="dhcp"), ret
  216. )
  217. def test_managed_static_dns_clear(self):
  218. expected = {
  219. "name": "salt",
  220. "changes": {
  221. "Statically Configured DNS Servers": {
  222. "new": "None",
  223. "old": "192.168.0.10",
  224. }
  225. },
  226. "result": True,
  227. "comment": "Successfully updated configuration for " "interface 'salt'",
  228. }
  229. mock_true = MagicMock(return_value=True)
  230. mock_validate = MagicMock(return_value=[])
  231. mock_get_int = MagicMock(
  232. side_effect=[
  233. {
  234. "DHCP enabled": "no",
  235. "Statically Configured DNS Servers": "192.168.0.10",
  236. },
  237. {"DHCP enabled": "no", "Statically Configured DNS Servers": "None"},
  238. ]
  239. )
  240. with patch.dict(
  241. win_network.__salt__,
  242. {
  243. "ip.is_enabled": mock_true,
  244. "ip.get_interface": mock_get_int,
  245. "ip.set_static_dns": mock_true,
  246. },
  247. ), patch.dict(win_network.__opts__, {"test": False}), patch.object(
  248. win_network, "_validate", mock_validate
  249. ):
  250. ret = win_network.managed(
  251. "salt", dns_proto="static", dns_servers=[], ip_proto="dhcp"
  252. )
  253. self.assertDictEqual(ret, expected)
  254. def test_managed_static_dns(self):
  255. expected = {
  256. "name": "salt",
  257. "changes": {
  258. "Statically Configured DNS Servers": {
  259. "new": "192.168.0.10",
  260. "old": "None",
  261. }
  262. },
  263. "result": True,
  264. "comment": "Successfully updated configuration for " "interface 'salt'",
  265. }
  266. mock_true = MagicMock(return_value=True)
  267. mock_validate = MagicMock(return_value=[])
  268. mock_get_int = MagicMock(
  269. side_effect=[
  270. {"DHCP enabled": "no", "Statically Configured DNS Servers": "None"},
  271. {
  272. "DHCP enabled": "no",
  273. "Statically Configured DNS Servers": "192.168.0.10",
  274. },
  275. ]
  276. )
  277. with patch.dict(
  278. win_network.__salt__,
  279. {
  280. "ip.is_enabled": mock_true,
  281. "ip.get_interface": mock_get_int,
  282. "ip.set_static_dns": mock_true,
  283. },
  284. ), patch.dict(win_network.__opts__, {"test": False}), patch.object(
  285. win_network, "_validate", mock_validate
  286. ):
  287. ret = win_network.managed(
  288. "salt",
  289. dns_proto="static",
  290. dns_servers=["192.168.0.10"],
  291. ip_proto="dhcp",
  292. )
  293. self.assertDictEqual(ret, expected)
  294. def test_managed_static_dns_no_action(self):
  295. expected = {
  296. "name": "salt",
  297. "changes": {},
  298. "result": True,
  299. "comment": "Interface 'salt' is up to date",
  300. }
  301. mock_true = MagicMock(return_value=True)
  302. mock_validate = MagicMock(return_value=[])
  303. mock_get_int = MagicMock(
  304. return_value={
  305. "DHCP enabled": "no",
  306. "Statically Configured DNS Servers": "192.168.0.10",
  307. }
  308. )
  309. with patch.dict(
  310. win_network.__salt__,
  311. {
  312. "ip.is_enabled": mock_true,
  313. "ip.get_interface": mock_get_int,
  314. "ip.set_static_dns": mock_true,
  315. },
  316. ), patch.dict(win_network.__opts__, {"test": False}), patch.object(
  317. win_network, "_validate", mock_validate
  318. ):
  319. # Don't pass dns_servers
  320. ret = win_network.managed("salt", dns_proto="static", ip_proto="dhcp")
  321. self.assertDictEqual(ret, expected)
  322. # Pass dns_servers=None
  323. ret = win_network.managed(
  324. "salt", dns_proto="static", dns_servers=None, ip_proto="dhcp"
  325. )
  326. self.assertDictEqual(ret, expected)