test_network.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. import logging
  5. import socket
  6. import textwrap
  7. # Import Salt Testing libs
  8. from tests.support.unit import TestCase
  9. from tests.support.mock import (
  10. MagicMock,
  11. mock_open,
  12. create_autospec,
  13. patch,
  14. )
  15. # Import salt libs
  16. import salt.utils.network as network
  17. import salt.exceptions
  18. from salt._compat import ipaddress
  19. log = logging.getLogger(__name__)
  20. LINUX = '''\
  21. eth0 Link encap:Ethernet HWaddr e0:3f:49:85:6a:af
  22. inet addr:10.10.10.56 Bcast:10.10.10.255 Mask:255.255.252.0
  23. inet6 addr: fe80::e23f:49ff:fe85:6aaf/64 Scope:Link
  24. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  25. RX packets:643363 errors:0 dropped:0 overruns:0 frame:0
  26. TX packets:196539 errors:0 dropped:0 overruns:0 carrier:0
  27. collisions:0 txqueuelen:1000
  28. RX bytes:386388355 (368.4 MiB) TX bytes:25600939 (24.4 MiB)
  29. lo Link encap:Local Loopback
  30. inet addr:127.0.0.1 Mask:255.0.0.0
  31. inet6 addr: ::1/128 Scope:Host
  32. UP LOOPBACK RUNNING MTU:65536 Metric:1
  33. RX packets:548901 errors:0 dropped:0 overruns:0 frame:0
  34. TX packets:548901 errors:0 dropped:0 overruns:0 carrier:0
  35. collisions:0 txqueuelen:0
  36. RX bytes:613479895 (585.0 MiB) TX bytes:613479895 (585.0 MiB)
  37. '''
  38. FREEBSD = '''
  39. em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
  40. options=4219b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,WOL_MAGIC,VLAN_HWTSO>
  41. ether 00:30:48:ff:ff:ff
  42. inet 10.10.10.250 netmask 0xffffffe0 broadcast 10.10.10.255
  43. inet 10.10.10.56 netmask 0xffffffc0 broadcast 10.10.10.63
  44. media: Ethernet autoselect (1000baseT <full-duplex>)
  45. status: active
  46. em1: flags=8c02<BROADCAST,OACTIVE,SIMPLEX,MULTICAST> metric 0 mtu 1500
  47. options=4219b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,WOL_MAGIC,VLAN_HWTSO>
  48. ether 00:30:48:aa:aa:aa
  49. media: Ethernet autoselect
  50. status: no carrier
  51. plip0: flags=8810<POINTOPOINT,SIMPLEX,MULTICAST> metric 0 mtu 1500
  52. lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
  53. options=3<RXCSUM,TXCSUM>
  54. inet6 fe80::1%lo0 prefixlen 64 scopeid 0x8
  55. inet6 ::1 prefixlen 128
  56. inet 127.0.0.1 netmask 0xff000000
  57. nd6 options=3<PERFORMNUD,ACCEPT_RTADV>
  58. tun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> metric 0 mtu 1500
  59. options=80000<LINKSTATE>
  60. inet 10.12.0.1 --> 10.12.0.2 netmask 0xffffffff
  61. Opened by PID 1964
  62. '''
  63. SOLARIS = '''\
  64. lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
  65. inet 127.0.0.1 netmask ff000000
  66. net0: flags=100001100943<UP,BROADCAST,RUNNING,PROMISC,MULTICAST,ROUTER,IPv4,PHYSRUNNING> mtu 1500 index 2
  67. inet 10.10.10.38 netmask ffffffe0 broadcast 10.10.10.63
  68. ilbint0: flags=110001100843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4,VRRP,PHYSRUNNING> mtu 1500 index 3
  69. inet 10.6.0.11 netmask ffffff00 broadcast 10.6.0.255
  70. ilbext0: flags=110001100843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4,VRRP,PHYSRUNNING> mtu 1500 index 4
  71. inet 10.10.11.11 netmask ffffffe0 broadcast 10.10.11.31
  72. ilbext0:1: flags=110001100843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4,VRRP,PHYSRUNNING> mtu 1500 index 4
  73. inet 10.10.11.12 netmask ffffffe0 broadcast 10.10.11.31
  74. vpn0: flags=1000011008d1<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST,ROUTER,IPv4,PHYSRUNNING> mtu 1480 index 5
  75. inet tunnel src 10.10.11.12 tunnel dst 10.10.5.5
  76. tunnel hop limit 64
  77. inet 10.6.0.14 --> 10.6.0.15 netmask ff000000
  78. lo0: flags=2002000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv6,VIRTUAL> mtu 8252 index 1
  79. inet6 ::1/128
  80. net0: flags=120002004941<UP,RUNNING,PROMISC,MULTICAST,DHCP,IPv6,PHYSRUNNING> mtu 1500 index 2
  81. inet6 fe80::221:9bff:fefd:2a22/10
  82. ilbint0: flags=120002000840<RUNNING,MULTICAST,IPv6,PHYSRUNNING> mtu 1500 index 3
  83. inet6 ::/0
  84. ilbext0: flags=120002000840<RUNNING,MULTICAST,IPv6,PHYSRUNNING> mtu 1500 index 4
  85. inet6 ::/0
  86. vpn0: flags=120002200850<POINTOPOINT,RUNNING,MULTICAST,NONUD,IPv6,PHYSRUNNING> mtu 1480 index 5
  87. inet tunnel src 10.10.11.12 tunnel dst 10.10.5.5
  88. tunnel hop limit 64
  89. inet6 ::/0 --> fe80::b2d6:7c10
  90. '''
  91. NETBSD = '''\
  92. vioif0: flags=0x8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
  93. ec_capabilities=1<VLAN_MTU>
  94. ec_enabled=0
  95. address: 00:a0:98:e6:83:18
  96. inet 192.168.1.80/24 broadcast 192.168.1.255 flags 0x0
  97. inet6 fe80::2a0:98ff:fee6:8318%vioif0/64 flags 0x0 scopeid 0x1
  98. lo0: flags=0x8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
  99. inet 127.0.0.1/8 flags 0x0
  100. inet6 ::1/128 flags 0x20<NODAD>
  101. inet6 fe80::1%lo0/64 flags 0x0 scopeid 0x2
  102. '''
  103. FREEBSD_SOCKSTAT = '''\
  104. USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
  105. root python2.7 1294 41 tcp4 127.0.0.1:61115 127.0.0.1:4506
  106. '''
  107. FREEBSD_SOCKSTAT_WITH_FAT_PID = '''\
  108. USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
  109. salt-master python2.781106 35 tcp4 127.0.0.1:61115 127.0.0.1:4506
  110. '''
  111. NETLINK_SS = '''
  112. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  113. ESTAB 0 0 127.0.0.1:56726 127.0.0.1:4505
  114. ESTAB 0 0 ::ffff:1.2.3.4:5678 ::ffff:1.2.3.4:4505
  115. '''
  116. IPV4_SUBNETS = {True: ('10.10.0.0/24',),
  117. False: ('10.10.0.0', '10.10.0.0/33', 'FOO', 9, '0.9.800.1000/24')}
  118. IPV6_SUBNETS = {True: ('::1/128',),
  119. False: ('::1', '::1/129', 'FOO', 9, 'aj01::feac/64')}
  120. class NetworkTestCase(TestCase):
  121. def test_sanitize_host(self):
  122. ret = network.sanitize_host('10.1./2.$3')
  123. self.assertEqual(ret, '10.1.2.3')
  124. def test_host_to_ips(self):
  125. '''
  126. NOTE: When this test fails it's usually because the IP address has
  127. changed. In these cases, we just need to update the IP address in the
  128. assertion.
  129. '''
  130. def _side_effect(host, *args):
  131. try:
  132. return {
  133. 'github.com': [
  134. (2, 1, 6, '', ('192.30.255.112', 0)),
  135. (2, 1, 6, '', ('192.30.255.113', 0)),
  136. ],
  137. 'ipv6host.foo': [
  138. (socket.AF_INET6, 1, 6, '', ('2001:a71::1', 0, 0, 0)),
  139. ],
  140. }[host]
  141. except KeyError:
  142. raise socket.gaierror(-2, 'Name or service not known')
  143. getaddrinfo_mock = MagicMock(side_effect=_side_effect)
  144. with patch.object(socket, 'getaddrinfo', getaddrinfo_mock):
  145. # Test host that can be resolved
  146. ret = network.host_to_ips('github.com')
  147. self.assertEqual(ret, ['192.30.255.112', '192.30.255.113'])
  148. # Test ipv6
  149. ret = network.host_to_ips('ipv6host.foo')
  150. self.assertEqual(ret, ['2001:a71::1'])
  151. # Test host that can't be resolved
  152. ret = network.host_to_ips('someothersite.com')
  153. self.assertEqual(ret, None)
  154. def test_generate_minion_id(self):
  155. self.assertTrue(network.generate_minion_id())
  156. def test__generate_minion_id_with_unicode_in_etc_hosts(self):
  157. '''
  158. Test that unicode in /etc/hosts doesn't raise an error when
  159. _generate_minion_id() helper is called to gather the hosts.
  160. '''
  161. content = textwrap.dedent('''\
  162. # 以下为主机名解析
  163. ## ccc
  164. 127.0.0.1 localhost thisismyhostname # 本机
  165. ''')
  166. fopen_mock = mock_open(read_data={'/etc/hosts': content})
  167. with patch('salt.utils.files.fopen', fopen_mock):
  168. assert 'thisismyhostname' in network._generate_minion_id()
  169. def test_is_ip(self):
  170. self.assertTrue(network.is_ip('10.10.0.3'))
  171. self.assertFalse(network.is_ip('0.9.800.1000'))
  172. # Check 16-char-long unicode string
  173. # https://github.com/saltstack/salt/issues/51258
  174. self.assertFalse(network.is_ipv6('sixteen-char-str'))
  175. def test_is_ipv4(self):
  176. self.assertTrue(network.is_ipv4('10.10.0.3'))
  177. self.assertFalse(network.is_ipv4('10.100.1'))
  178. self.assertFalse(network.is_ipv4('2001:db8:0:1:1:1:1:1'))
  179. # Check 16-char-long unicode string
  180. # https://github.com/saltstack/salt/issues/51258
  181. self.assertFalse(network.is_ipv4('sixteen-char-str'))
  182. def test_is_ipv6(self):
  183. self.assertTrue(network.is_ipv6('2001:db8:0:1:1:1:1:1'))
  184. self.assertTrue(network.is_ipv6('0:0:0:0:0:0:0:1'))
  185. self.assertTrue(network.is_ipv6('::1'))
  186. self.assertTrue(network.is_ipv6('::'))
  187. self.assertTrue(network.is_ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))
  188. self.assertTrue(network.is_ipv6('2001:0db8:85a3::8a2e:0370:7334'))
  189. self.assertFalse(network.is_ipv6('2001:0db8:0370:7334'))
  190. self.assertFalse(network.is_ipv6('2001:0db8:::0370:7334'))
  191. self.assertFalse(network.is_ipv6('10.0.1.2'))
  192. self.assertFalse(network.is_ipv6('2001.0db8.85a3.0000.0000.8a2e.0370.7334'))
  193. # Check 16-char-long unicode string
  194. # https://github.com/saltstack/salt/issues/51258
  195. self.assertFalse(network.is_ipv6('sixteen-char-str'))
  196. def test_parse_host_port(self):
  197. _ip = ipaddress.ip_address
  198. good_host_ports = {
  199. '10.10.0.3': (_ip('10.10.0.3').compressed, None),
  200. '10.10.0.3:1234': (_ip('10.10.0.3').compressed, 1234),
  201. '2001:0db8:85a3::8a2e:0370:7334': (_ip('2001:0db8:85a3::8a2e:0370:7334').compressed, None),
  202. '[2001:0db8:85a3::8a2e:0370:7334]:1234': (_ip('2001:0db8:85a3::8a2e:0370:7334').compressed, 1234),
  203. '2001:0db8:85a3::7334': (_ip('2001:0db8:85a3::7334').compressed, None),
  204. '[2001:0db8:85a3::7334]:1234': (_ip('2001:0db8:85a3::7334').compressed, 1234)
  205. }
  206. bad_host_ports = [
  207. '10.10.0.3/24',
  208. '10.10.0.3::1234',
  209. '2001:0db8:0370:7334',
  210. '2001:0db8:0370::7334]:1234',
  211. '2001:0db8:0370:0:a:b:c:d:1234'
  212. ]
  213. for host_port, assertion_value in good_host_ports.items():
  214. host = port = None
  215. host, port = network.parse_host_port(host_port)
  216. self.assertEqual((host, port), assertion_value)
  217. for host_port in bad_host_ports:
  218. try:
  219. self.assertRaises(ValueError, network.parse_host_port, host_port)
  220. except AssertionError as _e_:
  221. log.error('bad host_port value: "%s" failed to trigger ValueError exception', host_port)
  222. raise _e_
  223. def test_dns_check(self):
  224. hosts = [
  225. {'host': '10.10.0.3',
  226. 'port': '',
  227. 'mocked': [(2, 1, 6, '', ('10.10.0.3', 0))],
  228. 'ret': '10.10.0.3'},
  229. {'host': '10.10.0.3',
  230. 'port': '1234',
  231. 'mocked': [(2, 1, 6, '', ('10.10.0.3', 0))],
  232. 'ret': '10.10.0.3'},
  233. {'host': '2001:0db8:85a3::8a2e:0370:7334',
  234. 'port': '',
  235. 'mocked': [(10, 1, 6, '', ('2001:db8:85a3::8a2e:370:7334', 0, 0, 0))],
  236. 'ret': '[2001:db8:85a3::8a2e:370:7334]'},
  237. {'host': '2001:0db8:85a3::8a2e:370:7334',
  238. 'port': '1234',
  239. 'mocked': [(10, 1, 6, '', ('2001:db8:85a3::8a2e:370:7334', 0, 0, 0))],
  240. 'ret': '[2001:db8:85a3::8a2e:370:7334]'},
  241. {'host': 'salt-master',
  242. 'port': '1234',
  243. 'mocked': [(2, 1, 6, '', ('127.0.0.1', 0))],
  244. 'ret': '127.0.0.1'},
  245. ]
  246. for host in hosts:
  247. with patch.object(socket, 'getaddrinfo', create_autospec(socket.getaddrinfo, return_value=host['mocked'])):
  248. with patch('socket.socket', create_autospec(socket.socket)):
  249. ret = network.dns_check(host['host'], host['port'])
  250. self.assertEqual(ret, host['ret'])
  251. def test_dns_check_ipv6_filter(self):
  252. # raise exception to skip everything after the getaddrinfo call
  253. with patch.object(socket, 'getaddrinfo',
  254. create_autospec(socket.getaddrinfo, side_effect=Exception)) as getaddrinfo:
  255. for ipv6, param in [
  256. (None, socket.AF_UNSPEC),
  257. (True, socket.AF_INET6),
  258. (False, socket.AF_INET),
  259. ]:
  260. with self.assertRaises(Exception):
  261. network.dns_check('foo', '1', ipv6=ipv6)
  262. getaddrinfo.assert_called_with('foo', '1', param, socket.SOCK_STREAM)
  263. def test_dns_check_errors(self):
  264. with patch.object(socket, 'getaddrinfo', create_autospec(socket.getaddrinfo, return_value=[])):
  265. with self.assertRaisesRegex(salt.exceptions.SaltSystemExit,
  266. "DNS lookup or connection check of 'foo' failed"):
  267. network.dns_check('foo', '1')
  268. with patch.object(socket, 'getaddrinfo', create_autospec(socket.getaddrinfo, side_effect=TypeError)):
  269. with self.assertRaisesRegex(salt.exceptions.SaltSystemExit,
  270. "Invalid or unresolveable address"):
  271. network.dns_check('foo', '1')
  272. def test_test_addrs(self):
  273. # subset of real data from getaddrinfo against saltstack.com
  274. addrinfo = [(30, 2, 17, '', ('2600:9000:21eb:a800:8:1031:abc0:93a1', 0, 0, 0)),
  275. (30, 1, 6, '', ('2600:9000:21eb:a800:8:1031:abc0:93a1', 0, 0, 0)),
  276. (30, 2, 17, '', ('2600:9000:21eb:b400:8:1031:abc0:93a1', 0, 0, 0)),
  277. (30, 1, 6, '', ('2600:9000:21eb:b400:8:1031:abc0:93a1', 0, 0, 0)),
  278. (2, 1, 6, '', ('13.35.99.52', 0)), (2, 2, 17, '', ('13.35.99.85', 0)),
  279. (2, 1, 6, '', ('13.35.99.85', 0)), (2, 2, 17, '', ('13.35.99.122', 0))]
  280. with patch('socket.socket', create_autospec(socket.socket)) as s:
  281. # we connect to the first address
  282. addrs = network._test_addrs(addrinfo, 80)
  283. self.assertTrue(len(addrs) == 1)
  284. self.assertTrue(addrs[0] == addrinfo[0][4][0])
  285. # the first lookup fails, succeeds on next check
  286. s.side_effect = [socket.error, MagicMock()]
  287. addrs = network._test_addrs(addrinfo, 80)
  288. self.assertTrue(len(addrs) == 1)
  289. self.assertTrue(addrs[0] == addrinfo[2][4][0])
  290. # nothing can connect, but we've eliminated duplicates
  291. s.side_effect = socket.error
  292. addrs = network._test_addrs(addrinfo, 80)
  293. self.assertTrue(len(addrs) == 5)
  294. def test_is_subnet(self):
  295. for subnet_data in (IPV4_SUBNETS, IPV6_SUBNETS):
  296. for item in subnet_data[True]:
  297. log.debug('Testing that %s is a valid subnet', item)
  298. self.assertTrue(network.is_subnet(item))
  299. for item in subnet_data[False]:
  300. log.debug('Testing that %s is not a valid subnet', item)
  301. self.assertFalse(network.is_subnet(item))
  302. def test_is_ipv4_subnet(self):
  303. for item in IPV4_SUBNETS[True]:
  304. log.debug('Testing that %s is a valid subnet', item)
  305. self.assertTrue(network.is_ipv4_subnet(item))
  306. for item in IPV4_SUBNETS[False]:
  307. log.debug('Testing that %s is not a valid subnet', item)
  308. self.assertFalse(network.is_ipv4_subnet(item))
  309. def test_is_ipv6_subnet(self):
  310. for item in IPV6_SUBNETS[True]:
  311. log.debug('Testing that %s is a valid subnet', item)
  312. self.assertTrue(network.is_ipv6_subnet(item))
  313. for item in IPV6_SUBNETS[False]:
  314. log.debug('Testing that %s is not a valid subnet', item)
  315. self.assertFalse(network.is_ipv6_subnet(item))
  316. def test_cidr_to_ipv4_netmask(self):
  317. self.assertEqual(network.cidr_to_ipv4_netmask(24), '255.255.255.0')
  318. self.assertEqual(network.cidr_to_ipv4_netmask(21), '255.255.248.0')
  319. self.assertEqual(network.cidr_to_ipv4_netmask(17), '255.255.128.0')
  320. self.assertEqual(network.cidr_to_ipv4_netmask(9), '255.128.0.0')
  321. self.assertEqual(network.cidr_to_ipv4_netmask(36), '')
  322. self.assertEqual(network.cidr_to_ipv4_netmask('lol'), '')
  323. def test_number_of_set_bits_to_ipv4_netmask(self):
  324. set_bits_to_netmask = network._number_of_set_bits_to_ipv4_netmask(0xffffff00)
  325. self.assertEqual(set_bits_to_netmask, '255.255.255.0')
  326. set_bits_to_netmask = network._number_of_set_bits_to_ipv4_netmask(0xffff6400)
  327. def test_hex2ip(self):
  328. self.assertEqual(network.hex2ip('0x4A7D2B63'), '74.125.43.99')
  329. self.assertEqual(network.hex2ip('0x4A7D2B63', invert=True), '99.43.125.74')
  330. self.assertEqual(network.hex2ip('00000000000000000000FFFF7F000001'), '127.0.0.1')
  331. self.assertEqual(network.hex2ip('0000000000000000FFFF00000100007F', invert=True), '127.0.0.1')
  332. self.assertEqual(network.hex2ip('20010DB8000000000000000000000000'), '2001:db8::')
  333. self.assertEqual(network.hex2ip('B80D0120000000000000000000000000', invert=True), '2001:db8::')
  334. def test_interfaces_ifconfig_linux(self):
  335. interfaces = network._interfaces_ifconfig(LINUX)
  336. self.assertEqual(interfaces,
  337. {'eth0': {'hwaddr': 'e0:3f:49:85:6a:af',
  338. 'inet': [{'address': '10.10.10.56',
  339. 'broadcast': '10.10.10.255',
  340. 'netmask': '255.255.252.0'}],
  341. 'inet6': [{'address': 'fe80::e23f:49ff:fe85:6aaf',
  342. 'prefixlen': '64',
  343. 'scope': 'link'}],
  344. 'up': True},
  345. 'lo': {'inet': [{'address': '127.0.0.1',
  346. 'netmask': '255.0.0.0'}],
  347. 'inet6': [{'address': '::1',
  348. 'prefixlen': '128',
  349. 'scope': 'host'}],
  350. 'up': True}}
  351. )
  352. def test_interfaces_ifconfig_freebsd(self):
  353. interfaces = network._interfaces_ifconfig(FREEBSD)
  354. self.assertEqual(interfaces,
  355. {'': {'up': False},
  356. 'em0': {'hwaddr': '00:30:48:ff:ff:ff',
  357. 'inet': [{'address': '10.10.10.250',
  358. 'broadcast': '10.10.10.255',
  359. 'netmask': '255.255.255.224'},
  360. {'address': '10.10.10.56',
  361. 'broadcast': '10.10.10.63',
  362. 'netmask': '255.255.255.192'}],
  363. 'up': True},
  364. 'em1': {'hwaddr': '00:30:48:aa:aa:aa',
  365. 'up': False},
  366. 'lo0': {'inet': [{'address': '127.0.0.1',
  367. 'netmask': '255.0.0.0'}],
  368. 'inet6': [{'address': 'fe80::1',
  369. 'prefixlen': '64',
  370. 'scope': '0x8'},
  371. {'address': '::1',
  372. 'prefixlen': '128',
  373. 'scope': None}],
  374. 'up': True},
  375. 'plip0': {'up': False},
  376. 'tun0': {'inet': [{'address': '10.12.0.1',
  377. 'netmask': '255.255.255.255'}],
  378. 'up': True}}
  379. )
  380. def test_interfaces_ifconfig_solaris(self):
  381. with patch('salt.utils.platform.is_sunos', lambda: True):
  382. interfaces = network._interfaces_ifconfig(SOLARIS)
  383. expected_interfaces = {'ilbint0':
  384. {'inet6': [],
  385. 'inet': [{'broadcast': '10.6.0.255',
  386. 'netmask': '255.255.255.0',
  387. 'address': '10.6.0.11'}],
  388. 'up': True},
  389. 'lo0':
  390. {'inet6': [{'prefixlen': '128',
  391. 'address': '::1'}],
  392. 'inet': [{'netmask': '255.0.0.0',
  393. 'address': '127.0.0.1'}],
  394. 'up': True},
  395. 'ilbext0': {'inet6': [],
  396. 'inet': [{'broadcast': '10.10.11.31',
  397. 'netmask': '255.255.255.224',
  398. 'address': '10.10.11.11'},
  399. {'broadcast': '10.10.11.31',
  400. 'netmask': '255.255.255.224',
  401. 'address': '10.10.11.12'}],
  402. 'up': True},
  403. 'vpn0': {'inet6': [],
  404. 'inet': [{'netmask': '255.0.0.0',
  405. 'address': '10.6.0.14'}],
  406. 'up': True},
  407. 'net0': {'inet6': [{'prefixlen': '10',
  408. 'address': 'fe80::221:9bff:fefd:2a22'}],
  409. 'inet': [{'broadcast': '10.10.10.63',
  410. 'netmask': '255.255.255.224',
  411. 'address': '10.10.10.38'}],
  412. 'up': True}}
  413. self.assertEqual(interfaces, expected_interfaces)
  414. def test_interfaces_ifconfig_netbsd(self):
  415. interfaces = network._netbsd_interfaces_ifconfig(NETBSD)
  416. self.assertEqual(interfaces,
  417. {'lo0': {'inet': [{'address': '127.0.0.1', 'netmask': '255.0.0.0'}],
  418. 'inet6': [{'address': 'fe80::1',
  419. 'prefixlen': '64',
  420. 'scope': 'lo0'}],
  421. 'up': True},
  422. 'vioif0': {'hwaddr': '00:a0:98:e6:83:18',
  423. 'inet': [{'address': '192.168.1.80',
  424. 'broadcast': '192.168.1.255',
  425. 'netmask': '255.255.255.0'}],
  426. 'inet6': [{'address': 'fe80::2a0:98ff:fee6:8318',
  427. 'prefixlen': '64',
  428. 'scope': 'vioif0'}],
  429. 'up': True}}
  430. )
  431. def test_freebsd_remotes_on(self):
  432. with patch('salt.utils.platform.is_sunos', lambda: False):
  433. with patch('salt.utils.platform.is_freebsd', lambda: True):
  434. with patch('subprocess.check_output',
  435. return_value=FREEBSD_SOCKSTAT):
  436. remotes = network._freebsd_remotes_on('4506', 'remote')
  437. self.assertEqual(remotes, set(['127.0.0.1']))
  438. def test_freebsd_remotes_on_with_fat_pid(self):
  439. with patch('salt.utils.platform.is_sunos', lambda: False):
  440. with patch('salt.utils.platform.is_freebsd', lambda: True):
  441. with patch('subprocess.check_output',
  442. return_value=FREEBSD_SOCKSTAT_WITH_FAT_PID):
  443. remotes = network._freebsd_remotes_on('4506', 'remote')
  444. self.assertEqual(remotes, set(['127.0.0.1']))
  445. def test_generate_minion_id_distinct(self):
  446. '''
  447. Test if minion IDs are distinct in the pool.
  448. :return:
  449. '''
  450. with patch('platform.node', MagicMock(return_value='nodename')), \
  451. patch('socket.gethostname', MagicMock(return_value='hostname')), \
  452. patch('socket.getfqdn', MagicMock(return_value='hostname.domainname.blank')), \
  453. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'attrname', ('127.0.1.1', 0))])), \
  454. patch('salt.utils.files.fopen', mock_open()), \
  455. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '5.6.7.8'])):
  456. self.assertEqual(network._generate_minion_id(),
  457. ['hostname.domainname.blank', 'nodename', 'hostname', '1.2.3.4', '5.6.7.8'])
  458. def test_generate_minion_id_127_name(self):
  459. '''
  460. Test if minion IDs can be named 127.foo
  461. :return:
  462. '''
  463. with patch('platform.node', MagicMock(return_value='127')), \
  464. patch('socket.gethostname', MagicMock(return_value='127')), \
  465. patch('socket.getfqdn', MagicMock(return_value='127.domainname.blank')), \
  466. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'attrname', ('127.0.1.1', 0))])), \
  467. patch('salt.utils.files.fopen', mock_open()), \
  468. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '5.6.7.8'])):
  469. self.assertEqual(network._generate_minion_id(),
  470. ['127.domainname.blank', '127', '1.2.3.4', '5.6.7.8'])
  471. def test_generate_minion_id_127_name_startswith(self):
  472. '''
  473. Test if minion IDs can be named starting from "127"
  474. :return:
  475. '''
  476. with patch('platform.node', MagicMock(return_value='127890')), \
  477. patch('socket.gethostname', MagicMock(return_value='127890')), \
  478. patch('socket.getfqdn', MagicMock(return_value='127890.domainname.blank')), \
  479. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'attrname', ('127.0.1.1', 0))])), \
  480. patch('salt.utils.files.fopen', mock_open()), \
  481. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '5.6.7.8'])):
  482. self.assertEqual(network._generate_minion_id(),
  483. ['127890.domainname.blank', '127890', '1.2.3.4', '5.6.7.8'])
  484. def test_generate_minion_id_duplicate(self):
  485. '''
  486. Test if IP addresses in the minion IDs are distinct in the pool
  487. :return:
  488. '''
  489. with patch('platform.node', MagicMock(return_value='hostname')), \
  490. patch('socket.gethostname', MagicMock(return_value='hostname')), \
  491. patch('socket.getfqdn', MagicMock(return_value='hostname')), \
  492. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'hostname', ('127.0.1.1', 0))])), \
  493. patch('salt.utils.files.fopen', mock_open()), \
  494. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '1.2.3.4', '1.2.3.4'])):
  495. self.assertEqual(network._generate_minion_id(), ['hostname', '1.2.3.4'])
  496. def test_generate_minion_id_platform_used(self):
  497. '''
  498. Test if platform.node is used for the first occurrence.
  499. The platform.node is most common hostname resolver before anything else.
  500. :return:
  501. '''
  502. with patch('platform.node', MagicMock(return_value='very.long.and.complex.domain.name')), \
  503. patch('socket.gethostname', MagicMock(return_value='hostname')), \
  504. patch('socket.getfqdn', MagicMock(return_value='')), \
  505. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'hostname', ('127.0.1.1', 0))])), \
  506. patch('salt.utils.files.fopen', mock_open()), \
  507. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '1.2.3.4', '1.2.3.4'])):
  508. self.assertEqual(network.generate_minion_id(), 'very.long.and.complex.domain.name')
  509. def test_generate_minion_id_platform_localhost_filtered(self):
  510. '''
  511. Test if localhost is filtered from the first occurrence.
  512. :return:
  513. '''
  514. with patch('platform.node', MagicMock(return_value='localhost')), \
  515. patch('socket.gethostname', MagicMock(return_value='pick.me')), \
  516. patch('socket.getfqdn', MagicMock(return_value='hostname.domainname.blank')), \
  517. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'hostname', ('127.0.1.1', 0))])), \
  518. patch('salt.utils.files.fopen', mock_open()), \
  519. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '1.2.3.4', '1.2.3.4'])):
  520. self.assertEqual(network.generate_minion_id(), 'hostname.domainname.blank')
  521. def test_generate_minion_id_platform_localhost_filtered_all(self):
  522. '''
  523. Test if any of the localhost is filtered from everywhere.
  524. :return:
  525. '''
  526. with patch('platform.node', MagicMock(return_value='localhost')), \
  527. patch('socket.gethostname', MagicMock(return_value='ip6-loopback')), \
  528. patch('socket.getfqdn', MagicMock(return_value='ip6-localhost')), \
  529. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'localhost', ('127.0.1.1', 0))])), \
  530. patch('salt.utils.files.fopen', mock_open()), \
  531. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['127.0.0.1', '::1', 'fe00::0', 'fe02::1', '1.2.3.4'])):
  532. self.assertEqual(network.generate_minion_id(), '1.2.3.4')
  533. def test_generate_minion_id_platform_localhost_only(self):
  534. '''
  535. Test if there is no other choice but localhost.
  536. :return:
  537. '''
  538. with patch('platform.node', MagicMock(return_value='localhost')), \
  539. patch('socket.gethostname', MagicMock(return_value='ip6-loopback')), \
  540. patch('socket.getfqdn', MagicMock(return_value='ip6-localhost')), \
  541. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'localhost', ('127.0.1.1', 0))])), \
  542. patch('salt.utils.files.fopen', mock_open()), \
  543. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['127.0.0.1', '::1', 'fe00::0', 'fe02::1'])):
  544. self.assertEqual(network.generate_minion_id(), 'localhost')
  545. def test_generate_minion_id_platform_fqdn(self):
  546. '''
  547. Test if fqdn is picked up.
  548. :return:
  549. '''
  550. with patch('platform.node', MagicMock(return_value='localhost')), \
  551. patch('socket.gethostname', MagicMock(return_value='ip6-loopback')), \
  552. patch('socket.getfqdn', MagicMock(return_value='pick.me')), \
  553. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'localhost', ('127.0.1.1', 0))])), \
  554. patch('salt.utils.files.fopen', mock_open()), \
  555. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['127.0.0.1', '::1', 'fe00::0', 'fe02::1'])):
  556. self.assertEqual(network.generate_minion_id(), 'pick.me')
  557. def test_generate_minion_id_platform_localhost_addrinfo(self):
  558. '''
  559. Test if addinfo is picked up.
  560. :return:
  561. '''
  562. with patch('platform.node', MagicMock(return_value='localhost')), \
  563. patch('socket.gethostname', MagicMock(return_value='ip6-loopback')), \
  564. patch('socket.getfqdn', MagicMock(return_value='ip6-localhost')), \
  565. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'pick.me', ('127.0.1.1', 0))])), \
  566. patch('salt.utils.files.fopen', mock_open()), \
  567. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['127.0.0.1', '::1', 'fe00::0', 'fe02::1'])):
  568. self.assertEqual(network.generate_minion_id(), 'pick.me')
  569. def test_generate_minion_id_platform_ip_addr_only(self):
  570. '''
  571. Test if IP address is the only what is used as a Minion ID in case no DNS name.
  572. :return:
  573. '''
  574. with patch('platform.node', MagicMock(return_value='localhost')), \
  575. patch('socket.gethostname', MagicMock(return_value='ip6-loopback')), \
  576. patch('socket.getfqdn', MagicMock(return_value='ip6-localhost')), \
  577. patch('socket.getaddrinfo', MagicMock(return_value=[(2, 3, 0, 'localhost', ('127.0.1.1', 0))])), \
  578. patch('salt.utils.files.fopen', mock_open()), \
  579. patch('salt.utils.network.ip_addrs', MagicMock(return_value=['127.0.0.1', '::1', 'fe00::0', 'fe02::1', '1.2.3.4'])):
  580. self.assertEqual(network.generate_minion_id(), '1.2.3.4')
  581. def test_gen_mac(self):
  582. with patch('random.randint', return_value=1) as random_mock:
  583. self.assertEqual(random_mock.return_value, 1)
  584. ret = network.gen_mac('00:16:3E')
  585. expected_mac = '00:16:3E:01:01:01'
  586. self.assertEqual(ret, expected_mac)
  587. def test_mac_str_to_bytes(self):
  588. self.assertRaises(ValueError, network.mac_str_to_bytes, '31337')
  589. self.assertRaises(ValueError, network.mac_str_to_bytes, '0001020304056')
  590. self.assertRaises(ValueError, network.mac_str_to_bytes, '00:01:02:03:04:056')
  591. self.assertRaises(ValueError, network.mac_str_to_bytes, 'a0:b0:c0:d0:e0:fg')
  592. self.assertEqual(b'\x10\x08\x06\x04\x02\x00', network.mac_str_to_bytes('100806040200'))
  593. self.assertEqual(b'\xf8\xe7\xd6\xc5\xb4\xa3', network.mac_str_to_bytes('f8e7d6c5b4a3'))
  594. def test_generate_minion_id_with_long_hostname(self):
  595. '''
  596. Validate the fix for:
  597. https://github.com/saltstack/salt/issues/51160
  598. '''
  599. long_name = 'localhost-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz'
  600. with patch('socket.gethostname', MagicMock(return_value=long_name)):
  601. # An exception is raised if unicode is passed to socket.getfqdn
  602. minion_id = network.generate_minion_id()
  603. assert minion_id != '', minion_id
  604. def test_netlink_tool_remote_on(self):
  605. with patch('subprocess.check_output', return_value=NETLINK_SS):
  606. remotes = network._netlink_tool_remote_on('4505', 'remote')
  607. self.assertEqual(remotes, set(['127.0.0.1', '::ffff:1.2.3.4']))