test_win_network.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.mock import patch, MagicMock
  6. from tests.support.unit import TestCase, skipIf
  7. # Import Salt Libs
  8. import salt.utils.platform
  9. import salt.utils.win_network as win_network
  10. mock_ip_base = MagicMock(return_value={
  11. 'dns_enabled': False,
  12. 'dns_suffix': '',
  13. 'dynamic_dns_enabled': False,
  14. })
  15. mock_unicast = MagicMock(return_value={
  16. 'ip_addresses': [{
  17. 'address': '172.18.87.49',
  18. 'broadcast': '172.18.87.63',
  19. 'loopback': '127.0.0.1',
  20. 'netmask': '255.255.255.240',
  21. 'prefix_length': 28,
  22. 'prefix_origin': 'Manual',
  23. 'suffix_origin': 'Manual'}],
  24. 'ipv6_addresses': [{
  25. 'address': 'fe80::e8a4:1224:5548:2b81',
  26. 'interface_index': 32,
  27. 'prefix_length': 64,
  28. 'prefix_origin': 'WellKnown',
  29. 'suffix_origin': 'Router'}],
  30. })
  31. mock_gateway = MagicMock(return_value={
  32. 'ip_gateways': ['192.168.0.1'],
  33. 'ipv6_gateways': ['fe80::208:a2ff:fe0b:de70']
  34. })
  35. mock_dns = MagicMock(return_value={
  36. 'ip_dns': ['10.4.0.1', '10.1.0.1', '8.8.8.8'],
  37. 'ipv6_dns': ['2600:740a:1:304::1']
  38. })
  39. mock_multicast = MagicMock(return_value={
  40. u'ip_multicast': ['224.0.0.1',
  41. '224.0.0.251',
  42. '224.0.0.252',
  43. '230.230.230.230',
  44. '239.0.0.250',
  45. '239.255.255.250'],
  46. 'ipv6_multicast': ['ff01::1',
  47. 'ff02::1',
  48. 'ff02::c',
  49. 'ff02::fb',
  50. 'ff02::1:3',
  51. 'ff02::1:ff0f:4c48',
  52. 'ff02::1:ffa6:f6e6'],
  53. })
  54. mock_anycast = MagicMock(return_value={'ip_anycast': [],
  55. 'ipv6_anycast': []})
  56. mock_wins = MagicMock(return_value={'ip_wins': []})
  57. class PhysicalAddress(object):
  58. def __init__(self, address):
  59. self.address = address
  60. def ToString(self):
  61. return str(self.address)
  62. class Interface(object):
  63. '''
  64. Mocked interface object
  65. '''
  66. def __init__(self,
  67. i_address='02D5F1DD31E0',
  68. i_description='Dell GigabitEthernet',
  69. i_id='{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}',
  70. i_name='Ethernet',
  71. i_receive_only=False,
  72. i_status=1,
  73. i_type=6):
  74. self.PhysicalAddress = PhysicalAddress(i_address)
  75. self.Description = i_description
  76. self.Id = i_id
  77. self.Name = i_name
  78. self.NetworkInterfaceType = i_type
  79. self.IsReceiveOnly = i_receive_only
  80. self.OperationalStatus = i_status
  81. def GetPhysicalAddress(self):
  82. return self.PhysicalAddress
  83. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  84. class WinNetworkTestCase(TestCase):
  85. def test_get_interface_info_dot_net(self):
  86. expected = {
  87. 'Ethernet': {
  88. 'alias': 'Ethernet',
  89. 'description': 'Dell GigabitEthernet',
  90. 'dns_enabled': False,
  91. 'dns_suffix': '',
  92. 'dynamic_dns_enabled': False,
  93. 'id': '{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}',
  94. 'ip_addresses': [{'address': u'172.18.87.49',
  95. 'broadcast': u'172.18.87.63',
  96. 'loopback': u'127.0.0.1',
  97. 'netmask': u'255.255.255.240',
  98. 'prefix_length': 28,
  99. 'prefix_origin': u'Manual',
  100. 'suffix_origin': u'Manual'}],
  101. 'ip_anycast': [],
  102. 'ip_dns': ['10.4.0.1', '10.1.0.1', '8.8.8.8'],
  103. 'ip_gateways': ['192.168.0.1'],
  104. 'ip_multicast': ['224.0.0.1',
  105. '224.0.0.251',
  106. '224.0.0.252',
  107. '230.230.230.230',
  108. '239.0.0.250',
  109. '239.255.255.250'],
  110. 'ip_wins': [],
  111. 'ipv6_addresses': [{'address': u'fe80::e8a4:1224:5548:2b81',
  112. 'interface_index': 32,
  113. 'prefix_length': 64,
  114. 'prefix_origin': u'WellKnown',
  115. 'suffix_origin': u'Router'}],
  116. 'ipv6_anycast': [],
  117. 'ipv6_dns': ['2600:740a:1:304::1'],
  118. 'ipv6_gateways': ['fe80::208:a2ff:fe0b:de70'],
  119. 'ipv6_multicast': ['ff01::1',
  120. 'ff02::1',
  121. 'ff02::c',
  122. 'ff02::fb',
  123. 'ff02::1:3',
  124. 'ff02::1:ff0f:4c48',
  125. 'ff02::1:ffa6:f6e6'],
  126. 'physical_address': '02:D5:F1:DD:31:E0',
  127. 'receive_only': False,
  128. 'status': 'Up',
  129. 'type': 'Ethernet'}}
  130. mock_int = MagicMock(return_value=[Interface()])
  131. with patch.object(win_network, '_get_network_interfaces', mock_int), \
  132. patch.object(win_network, '_get_ip_base_properties', mock_ip_base), \
  133. patch.object(win_network, '_get_ip_unicast_info', mock_unicast), \
  134. patch.object(win_network, '_get_ip_gateway_info', mock_gateway), \
  135. patch.object(win_network, '_get_ip_dns_info', mock_dns), \
  136. patch.object(win_network, '_get_ip_multicast_info', mock_multicast), \
  137. patch.object(win_network, '_get_ip_anycast_info', mock_anycast), \
  138. patch.object(win_network, '_get_ip_wins_info', mock_wins):
  139. # ret = win_network._get_base_properties()
  140. results = win_network.get_interface_info_dot_net()
  141. self.assertDictEqual(expected, results)
  142. def test_get_network_info(self):
  143. expected = {
  144. 'Dell GigabitEthernet': {
  145. 'hwaddr': '02:D5:F1:DD:31:E0',
  146. 'inet': [{'address': '172.18.87.49',
  147. 'broadcast': '172.18.87.63',
  148. 'gateway': '192.168.0.1',
  149. 'label': 'Dell GigabitEthernet',
  150. 'netmask': '255.255.255.240'}],
  151. 'inet6': [{'address': 'fe80::e8a4:1224:5548:2b81',
  152. 'gateway': 'fe80::208:a2ff:fe0b:de70'}],
  153. 'up': True}}
  154. mock_int = MagicMock(return_value=[Interface()])
  155. with patch.object(win_network, '_get_network_interfaces', mock_int), \
  156. patch.object(win_network, '_get_ip_base_properties', mock_ip_base), \
  157. patch.object(win_network, '_get_ip_unicast_info', mock_unicast), \
  158. patch.object(win_network, '_get_ip_gateway_info', mock_gateway), \
  159. patch.object(win_network, '_get_ip_dns_info', mock_dns), \
  160. patch.object(win_network, '_get_ip_multicast_info', mock_multicast), \
  161. patch.object(win_network, '_get_ip_anycast_info', mock_anycast), \
  162. patch.object(win_network, '_get_ip_wins_info', mock_wins):
  163. # ret = win_network._get_base_properties()
  164. results = win_network.get_interface_info()
  165. self.assertDictEqual(expected, results)
  166. def test__get_base_properties_tap_adapter(self):
  167. '''
  168. Adapter Type 53 is apparently an undocumented type corresponding to
  169. OpenVPN TAP Adapters and possibly other TAP Adapters. This test makes
  170. sure the win_network util will catch that.
  171. https://github.com/saltstack/salt/issues/56196
  172. https://github.com/saltstack/salt/issues/56275
  173. '''
  174. i_face = Interface(
  175. i_address='03DE4D0713FA',
  176. i_description='Windows TAP Adapter',
  177. i_id='{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}',
  178. i_name='Windows TAP Adapter',
  179. i_receive_only=False,
  180. i_status=1,
  181. i_type=53)
  182. expected = {
  183. 'alias': 'Windows TAP Adapter',
  184. 'description': 'Windows TAP Adapter',
  185. 'id': '{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}',
  186. 'receive_only': False,
  187. 'physical_address': '03:DE:4D:07:13:FA',
  188. 'status': 'Up',
  189. 'type': 'TAPAdapter'}
  190. results = win_network._get_base_properties(i_face=i_face)
  191. self.assertDictEqual(expected, results)
  192. def test__get_base_properties_undefined_adapter(self):
  193. '''
  194. The Adapter Type 53 may be an arbitrary number assigned by OpenVPN.
  195. This will test the ability to avoid stack tracing on an undefined
  196. adapter type. If one is encountered, just use the description.
  197. '''
  198. i_face = Interface(
  199. i_address='03DE4D0713FA',
  200. i_description='Undefined Adapter',
  201. i_id='{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}',
  202. i_name='Undefined',
  203. i_receive_only=False,
  204. i_status=1,
  205. i_type=50)
  206. expected = {
  207. 'alias': 'Undefined',
  208. 'description': 'Undefined Adapter',
  209. 'id': '{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}',
  210. 'receive_only': False,
  211. 'physical_address': '03:DE:4D:07:13:FA',
  212. 'status': 'Up',
  213. 'type': 'Undefined Adapter'}
  214. results = win_network._get_base_properties(i_face=i_face)
  215. self.assertDictEqual(expected, results)