1
0

test_win_ip.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import salt.modules.win_ip as win_ip
  7. from salt.exceptions import CommandExecutionError, SaltInvocationError
  8. from tests.support.helpers import slowTest
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, call, patch
  11. from tests.support.unit import TestCase
  12. ETHERNET_CONFIG = (
  13. 'Configuration for interface "Ethernet"\n'
  14. "DHCP enabled: Yes\n"
  15. "IP Address: 1.2.3.74\n"
  16. "Subnet Prefix: 1.2.3.0/24 (mask 255.255.255.0)\n"
  17. "Default Gateway: 1.2.3.1\n"
  18. "Gateway Metric: 0\n"
  19. "InterfaceMetric: 20\n"
  20. "DNS servers configured through DHCP: 1.2.3.4\n"
  21. "Register with which suffix: Primary only\n"
  22. "WINS servers configured through DHCP: None\n"
  23. )
  24. ETHERNET_ENABLE = (
  25. "Ethernet\n"
  26. "Type: Dedicated\n"
  27. "Administrative state: Enabled\n"
  28. "Connect state: Connected"
  29. )
  30. class WinShadowTestCase(TestCase, LoaderModuleMockMixin):
  31. """
  32. Test cases for salt.modules.win_ip
  33. """
  34. def setup_loader_modules(self):
  35. return {win_ip: {}}
  36. # 'raw_interface_configs' function tests: 1
  37. def test_raw_interface_configs(self):
  38. """
  39. Test if it return raw configs for all interfaces.
  40. """
  41. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  42. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  43. self.assertEqual(win_ip.raw_interface_configs(), ETHERNET_CONFIG)
  44. # 'get_all_interfaces' function tests: 1
  45. def test_get_all_interfaces(self):
  46. """
  47. Test if it return configs for all interfaces.
  48. """
  49. ret = {
  50. "Ethernet": {
  51. "DHCP enabled": "Yes",
  52. "DNS servers configured through DHCP": ["1.2.3.4"],
  53. "Default Gateway": "1.2.3.1",
  54. "Gateway Metric": "0",
  55. "InterfaceMetric": "20",
  56. "Register with which suffix": "Primary only",
  57. "WINS servers configured through DHCP": ["None"],
  58. "ip_addrs": [
  59. {
  60. "IP Address": "1.2.3.74",
  61. "Netmask": "255.255.255.0",
  62. "Subnet": "1.2.3.0/24",
  63. }
  64. ],
  65. }
  66. }
  67. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  68. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  69. self.assertDictEqual(win_ip.get_all_interfaces(), ret)
  70. # 'get_interface' function tests: 1
  71. def test_get_interface(self):
  72. """
  73. Test if it return the configuration of a network interface.
  74. """
  75. ret = {
  76. "DHCP enabled": "Yes",
  77. "DNS servers configured through DHCP": ["1.2.3.4"],
  78. "Default Gateway": "1.2.3.1",
  79. "Gateway Metric": "0",
  80. "InterfaceMetric": "20",
  81. "Register with which suffix": "Primary only",
  82. "WINS servers configured through DHCP": ["None"],
  83. "ip_addrs": [
  84. {
  85. "IP Address": "1.2.3.74",
  86. "Netmask": "255.255.255.0",
  87. "Subnet": "1.2.3.0/24",
  88. }
  89. ],
  90. }
  91. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  92. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  93. self.assertDictEqual(win_ip.get_interface("Ethernet"), ret)
  94. # 'is_enabled' function tests: 1
  95. def test_is_enabled(self):
  96. """
  97. Test if it returns `True` if interface is enabled, otherwise `False`.
  98. """
  99. mock_cmd = MagicMock(side_effect=[ETHERNET_ENABLE, ""])
  100. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  101. self.assertTrue(win_ip.is_enabled("Ethernet"))
  102. self.assertRaises(CommandExecutionError, win_ip.is_enabled, "Ethernet")
  103. # 'is_disabled' function tests: 1
  104. def test_is_disabled(self):
  105. """
  106. Test if it returns `True` if interface is disabled, otherwise `False`.
  107. """
  108. mock_cmd = MagicMock(return_value=ETHERNET_ENABLE)
  109. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  110. self.assertFalse(win_ip.is_disabled("Ethernet"))
  111. # 'enable' function tests: 1
  112. def test_enable(self):
  113. """
  114. Test if it enable an interface.
  115. """
  116. # Test with enabled interface
  117. with patch.object(win_ip, "is_enabled", return_value=True):
  118. self.assertTrue(win_ip.enable("Ethernet"))
  119. mock_cmd = MagicMock()
  120. with patch.object(win_ip, "is_enabled", side_effect=[False, True]), patch.dict(
  121. win_ip.__salt__, {"cmd.run": mock_cmd}
  122. ):
  123. self.assertTrue(win_ip.enable("Ethernet"))
  124. mock_cmd.called_once_with(
  125. [
  126. "netsh",
  127. "interface",
  128. "set",
  129. "interface",
  130. "name=Ethernet",
  131. "admin=ENABLED",
  132. ],
  133. python_shell=False,
  134. )
  135. # 'disable' function tests: 1
  136. def test_disable(self):
  137. """
  138. Test if it disable an interface.
  139. """
  140. with patch.object(win_ip, "is_disabled", return_value=True):
  141. self.assertTrue(win_ip.disable("Ethernet"))
  142. mock_cmd = MagicMock()
  143. with patch.object(win_ip, "is_disabled", side_effect=[False, True]), patch.dict(
  144. win_ip.__salt__, {"cmd.run": mock_cmd}
  145. ):
  146. self.assertTrue(win_ip.disable("Ethernet"))
  147. mock_cmd.called_once_with(
  148. [
  149. "netsh",
  150. "interface",
  151. "set",
  152. "interface",
  153. "name=Ethernet",
  154. "admin=DISABLED",
  155. ],
  156. python_shell=False,
  157. )
  158. # 'get_subnet_length' function tests: 1
  159. def test_get_subnet_length(self):
  160. """
  161. Test if it disable an interface.
  162. """
  163. self.assertEqual(win_ip.get_subnet_length("255.255.255.0"), 24)
  164. self.assertRaises(SaltInvocationError, win_ip.get_subnet_length, "255.255.0")
  165. # 'set_static_ip' function tests: 1
  166. @slowTest
  167. def test_set_static_ip(self):
  168. """
  169. Test if it set static IP configuration on a Windows NIC.
  170. """
  171. self.assertRaises(
  172. SaltInvocationError,
  173. win_ip.set_static_ip,
  174. "Local Area Connection",
  175. "10.1.2/24",
  176. )
  177. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  178. mock_all = MagicMock(return_value={"retcode": 1, "stderr": "Error"})
  179. with patch.dict(
  180. win_ip.__salt__, {"cmd.run": mock_cmd, "cmd.run_all": mock_all}
  181. ):
  182. self.assertRaises(
  183. CommandExecutionError,
  184. win_ip.set_static_ip,
  185. "Ethernet",
  186. "1.2.3.74/24",
  187. append=True,
  188. )
  189. self.assertRaises(
  190. CommandExecutionError, win_ip.set_static_ip, "Ethernet", "1.2.3.74/24"
  191. )
  192. mock_all = MagicMock(return_value={"retcode": 0})
  193. with patch.dict(
  194. win_ip.__salt__, {"cmd.run": mock_cmd, "cmd.run_all": mock_all}
  195. ):
  196. self.assertDictEqual(
  197. win_ip.set_static_ip("Local Area Connection", "1.2.3.74/24"), {}
  198. )
  199. self.assertDictEqual(
  200. win_ip.set_static_ip("Ethernet", "1.2.3.74/24"),
  201. {
  202. "Address Info": {
  203. "IP Address": "1.2.3.74",
  204. "Netmask": "255.255.255.0",
  205. "Subnet": "1.2.3.0/24",
  206. }
  207. },
  208. )
  209. # 'set_dhcp_ip' function tests: 1
  210. def test_set_dhcp_ip(self):
  211. """
  212. Test if it set Windows NIC to get IP from DHCP.
  213. """
  214. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  215. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  216. self.assertDictEqual(
  217. win_ip.set_dhcp_ip("Ethernet"),
  218. {"DHCP enabled": "Yes", "Interface": "Ethernet"},
  219. )
  220. # 'set_static_dns' function tests: 1
  221. def test_set_static_dns(self):
  222. """
  223. Test if it set static DNS configuration on a Windows NIC.
  224. """
  225. mock_cmd = MagicMock()
  226. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  227. self.assertDictEqual(
  228. win_ip.set_static_dns("Ethernet", "192.168.1.252", "192.168.1.253"),
  229. {
  230. "DNS Server": ("192.168.1.252", "192.168.1.253"),
  231. "Interface": "Ethernet",
  232. },
  233. )
  234. mock_cmd.assert_has_calls(
  235. [
  236. call(
  237. [
  238. "netsh",
  239. "interface",
  240. "ip",
  241. "set",
  242. "dns",
  243. "name=Ethernet",
  244. "source=static",
  245. "address=192.168.1.252",
  246. "register=primary",
  247. ],
  248. python_shell=False,
  249. ),
  250. call(
  251. [
  252. "netsh",
  253. "interface",
  254. "ip",
  255. "add",
  256. "dns",
  257. "name=Ethernet",
  258. "address=192.168.1.253",
  259. "index=2",
  260. ],
  261. python_shell=False,
  262. ),
  263. ]
  264. )
  265. def test_set_static_dns_clear(self):
  266. """
  267. Test if it set static DNS configuration on a Windows NIC.
  268. """
  269. mock_cmd = MagicMock()
  270. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  271. self.assertDictEqual(
  272. win_ip.set_static_dns("Ethernet", []),
  273. {"DNS Server": [], "Interface": "Ethernet"},
  274. )
  275. mock_cmd.assert_called_once_with(
  276. [
  277. "netsh",
  278. "interface",
  279. "ip",
  280. "set",
  281. "dns",
  282. "name=Ethernet",
  283. "source=static",
  284. "address=none",
  285. ],
  286. python_shell=False,
  287. )
  288. def test_set_static_dns_no_action(self):
  289. """
  290. Test if it set static DNS configuration on a Windows NIC.
  291. """
  292. # Test passing nothing
  293. self.assertDictEqual(
  294. win_ip.set_static_dns("Ethernet"),
  295. {"DNS Server": "No Changes", "Interface": "Ethernet"},
  296. )
  297. # Test passing None
  298. self.assertDictEqual(
  299. win_ip.set_static_dns("Ethernet", None),
  300. {"DNS Server": "No Changes", "Interface": "Ethernet"},
  301. )
  302. # Test passing string None
  303. self.assertDictEqual(
  304. win_ip.set_static_dns("Ethernet", "None"),
  305. {"DNS Server": "No Changes", "Interface": "Ethernet"},
  306. )
  307. # 'set_dhcp_dns' function tests: 1
  308. def test_set_dhcp_dns(self):
  309. """
  310. Test if it set DNS source to DHCP on Windows.
  311. """
  312. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  313. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  314. self.assertDictEqual(
  315. win_ip.set_dhcp_dns("Ethernet"),
  316. {"DNS Server": "DHCP", "Interface": "Ethernet"},
  317. )
  318. # 'set_dhcp_all' function tests: 1
  319. def test_set_dhcp_all(self):
  320. """
  321. Test if it set both IP Address and DNS to DHCP.
  322. """
  323. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  324. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  325. self.assertDictEqual(
  326. win_ip.set_dhcp_all("Ethernet"),
  327. {"Interface": "Ethernet", "DNS Server": "DHCP", "DHCP enabled": "Yes"},
  328. )
  329. # 'get_default_gateway' function tests: 1
  330. def test_get_default_gateway(self):
  331. """
  332. Test if it set DNS source to DHCP on Windows.
  333. """
  334. mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
  335. with patch.dict(win_ip.__salt__, {"cmd.run": mock_cmd}):
  336. self.assertEqual(win_ip.get_default_gateway(), "1.2.3.1")