test_win_network.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.modules.win_network as win_network
  8. # Import Salt Libs
  9. import salt.utils.network
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, Mock, patch
  13. from tests.support.unit import TestCase, skipIf
  14. try:
  15. import wmi
  16. HAS_WMI = True
  17. except ImportError:
  18. HAS_WMI = False
  19. class Mockwmi(object):
  20. """
  21. Mock wmi class
  22. """
  23. NetConnectionID = "Ethernet"
  24. def __init__(self):
  25. pass
  26. class Mockwinapi(object):
  27. """
  28. Mock winapi class
  29. """
  30. def __init__(self):
  31. pass
  32. class winapi(object):
  33. """
  34. Mock winapi class
  35. """
  36. def __init__(self):
  37. pass
  38. class Com(object):
  39. """
  40. Mock Com method
  41. """
  42. def __enter__(self):
  43. return self
  44. def __exit__(self, *exc_info):
  45. return False
  46. class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
  47. """
  48. Test cases for salt.modules.win_network
  49. """
  50. def setup_loader_modules(self):
  51. self.WMI = Mock()
  52. self.addCleanup(delattr, self, "WMI")
  53. return {win_network: {}}
  54. # 'ping' function tests: 1
  55. def test_ping(self):
  56. """
  57. Test if it performs a ping to a host.
  58. """
  59. mock = MagicMock(return_value=True)
  60. with patch.dict(win_network.__salt__, {"cmd.run": mock}):
  61. self.assertTrue(win_network.ping("127.0.0.1"))
  62. # 'netstat' function tests: 1
  63. def test_netstat(self):
  64. """
  65. Test if it return information on open ports and states
  66. """
  67. ret = (
  68. " Proto Local Address Foreign Address State PID\n"
  69. " TCP 127.0.0.1:1434 0.0.0.0:0 LISTENING 1728\n"
  70. " UDP 127.0.0.1:1900 *:* 4240"
  71. )
  72. mock = MagicMock(return_value=ret)
  73. with patch.dict(win_network.__salt__, {"cmd.run": mock}):
  74. self.assertListEqual(
  75. win_network.netstat(),
  76. [
  77. {
  78. "local-address": "127.0.0.1:1434",
  79. "program": "1728",
  80. "proto": "TCP",
  81. "remote-address": "0.0.0.0:0",
  82. "state": "LISTENING",
  83. },
  84. {
  85. "local-address": "127.0.0.1:1900",
  86. "program": "4240",
  87. "proto": "UDP",
  88. "remote-address": "*:*",
  89. "state": None,
  90. },
  91. ],
  92. )
  93. # 'traceroute' function tests: 1
  94. def test_traceroute(self):
  95. """
  96. Test if it performs a traceroute to a 3rd party host
  97. """
  98. ret = (
  99. " 1 1 ms <1 ms <1 ms 172.27.104.1\n"
  100. " 2 1 ms <1 ms 1 ms 121.242.35.1.s[121.242.35.1]\n"
  101. " 3 3 ms 2 ms 2 ms 121.242.4.53.s[121.242.4.53]\n"
  102. )
  103. mock = MagicMock(return_value=ret)
  104. with patch.dict(win_network.__salt__, {"cmd.run": mock}):
  105. self.assertListEqual(
  106. win_network.traceroute("google.com"),
  107. [
  108. {
  109. "count": "1",
  110. "hostname": None,
  111. "ip": "172.27.104.1",
  112. "ms1": "1",
  113. "ms2": "<1",
  114. "ms3": "<1",
  115. },
  116. {
  117. "count": "2",
  118. "hostname": None,
  119. "ip": "121.242.35.1.s[121.242.35.1]",
  120. "ms1": "1",
  121. "ms2": "<1",
  122. "ms3": "1",
  123. },
  124. {
  125. "count": "3",
  126. "hostname": None,
  127. "ip": "121.242.4.53.s[121.242.4.53]",
  128. "ms1": "3",
  129. "ms2": "2",
  130. "ms3": "2",
  131. },
  132. ],
  133. )
  134. # 'nslookup' function tests: 1
  135. def test_nslookup(self):
  136. """
  137. Test if it query DNS for information about a domain or ip address
  138. """
  139. ret = (
  140. "Server: ct-dc-3-2.cybage.com\n"
  141. "Address: 172.27.172.12\n"
  142. "Non-authoritative answer:\n"
  143. "Name: google.com\n"
  144. "Addresses: 2404:6800:4007:806::200e\n"
  145. "216.58.196.110\n"
  146. )
  147. mock = MagicMock(return_value=ret)
  148. with patch.dict(win_network.__salt__, {"cmd.run": mock}):
  149. self.assertListEqual(
  150. win_network.nslookup("google.com"),
  151. [
  152. {"Server": "ct-dc-3-2.cybage.com"},
  153. {"Address": "172.27.172.12"},
  154. {"Name": "google.com"},
  155. {"Addresses": ["2404:6800:4007:806::200e", "216.58.196.110"]},
  156. ],
  157. )
  158. # 'dig' function tests: 1
  159. def test_dig(self):
  160. """
  161. Test if it performs a DNS lookup with dig
  162. """
  163. mock = MagicMock(return_value=True)
  164. with patch.dict(win_network.__salt__, {"cmd.run": mock}):
  165. self.assertTrue(win_network.dig("google.com"))
  166. # 'interfaces_names' function tests: 1
  167. @skipIf(not HAS_WMI, "WMI only available on Windows")
  168. def test_interfaces_names(self):
  169. """
  170. Test if it return a list of all the interfaces names
  171. """
  172. self.WMI.Win32_NetworkAdapter = MagicMock(return_value=Mockwmi)
  173. with patch("salt.utils.winapi.Com", MagicMock()), patch.object(
  174. self.WMI, "Win32_NetworkAdapter", return_value=[Mockwmi()]
  175. ), patch("salt.utils", Mockwinapi), patch.object(
  176. wmi, "WMI", Mock(return_value=self.WMI)
  177. ):
  178. self.assertListEqual(win_network.interfaces_names(), ["Ethernet"])
  179. # 'interfaces' function tests: 1
  180. def test_interfaces(self):
  181. """
  182. Test if it return information about all the interfaces on the minion
  183. """
  184. with patch.object(
  185. salt.utils.network, "win_interfaces", MagicMock(return_value=True)
  186. ):
  187. self.assertTrue(win_network.interfaces())
  188. # 'hw_addr' function tests: 1
  189. def test_hw_addr(self):
  190. """
  191. Test if it return the hardware address (a.k.a. MAC address)
  192. for a given interface
  193. """
  194. with patch.object(
  195. salt.utils.network, "hw_addr", MagicMock(return_value="Ethernet")
  196. ):
  197. self.assertEqual(win_network.hw_addr("Ethernet"), "Ethernet")
  198. # 'subnets' function tests: 1
  199. def test_subnets(self):
  200. """
  201. Test if it returns a list of subnets to which the host belongs
  202. """
  203. with patch.object(
  204. salt.utils.network, "subnets", MagicMock(return_value="10.1.1.0/24")
  205. ):
  206. self.assertEqual(win_network.subnets(), "10.1.1.0/24")
  207. # 'in_subnet' function tests: 1
  208. def test_in_subnet(self):
  209. """
  210. Test if it returns True if host is within specified subnet,
  211. otherwise False
  212. """
  213. with patch.object(
  214. salt.utils.network, "in_subnet", MagicMock(return_value=True)
  215. ):
  216. self.assertTrue(win_network.in_subnet("10.1.1.0/16"))
  217. # 'get_route' function tests: 1
  218. def test_get_route(self):
  219. """
  220. Test if it return information on open ports and states
  221. """
  222. ret = (
  223. "\n\n"
  224. "IPAddress : 10.0.0.15\n"
  225. "InterfaceIndex : 3\n"
  226. "InterfaceAlias : Wi-Fi\n"
  227. "AddressFamily : IPv4\n"
  228. "Type : Unicast\n"
  229. "PrefixLength : 24\n"
  230. "PrefixOrigin : Dhcp\n"
  231. "SuffixOrigin : Dhcp\n"
  232. "AddressState : Preferred\n"
  233. "ValidLifetime : 6.17:52:39\n"
  234. "PreferredLifetime : 6.17:52:39\n"
  235. "SkipAsSource : False\n"
  236. "PolicyStore : ActiveStore\n"
  237. "\n\n"
  238. "Caption :\n"
  239. "Description :\n"
  240. "ElementName :\n"
  241. "InstanceID : :8:8:8:9:55=55;:8;8;:8;55;\n"
  242. "AdminDistance :\n"
  243. "DestinationAddress :\n"
  244. "IsStatic :\n"
  245. "RouteMetric : 0\n"
  246. "TypeOfRoute : 3\n"
  247. "AddressFamily : IPv4\n"
  248. "CompartmentId : 1\n"
  249. "DestinationPrefix : 0.0.0.0/0\n"
  250. "InterfaceAlias : Wi-Fi\n"
  251. "InterfaceIndex : 3\n"
  252. "NextHop : 10.0.0.1\n"
  253. "PreferredLifetime : 6.23:14:43\n"
  254. "Protocol : NetMgmt\n"
  255. "Publish : No\n"
  256. "Store : ActiveStore\n"
  257. "ValidLifetime : 6.23:14:43\n"
  258. "PSComputerName :\n"
  259. "ifIndex : 3"
  260. )
  261. mock = MagicMock(return_value=ret)
  262. with patch.dict(win_network.__salt__, {"cmd.run": mock}):
  263. self.assertDictEqual(
  264. win_network.get_route("192.0.0.8"),
  265. {
  266. "destination": "192.0.0.8",
  267. "gateway": "10.0.0.1",
  268. "interface": "Wi-Fi",
  269. "source": "10.0.0.15",
  270. },
  271. )