1
0

test_network.py 34 KB

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