PageRenderTime 4103ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/cloudinit/sources/helpers/tests/test_netlink.py

https://gitlab.com/bertjwregeer/cloud-init
Python | 373 lines | 287 code | 33 blank | 53 comment | 13 complexity | f2b99239db4f2bc072cd189a868daad6 MD5 | raw file
  1. # Author: Tamilmani Manoharan <tamanoha@microsoft.com>
  2. #
  3. # This file is part of cloud-init. See LICENSE file for license information.
  4. from cloudinit.tests.helpers import CiTestCase, mock
  5. import socket
  6. import struct
  7. import codecs
  8. from cloudinit.sources.helpers.netlink import (
  9. NetlinkCreateSocketError, create_bound_netlink_socket, read_netlink_socket,
  10. read_rta_oper_state, unpack_rta_attr, wait_for_media_disconnect_connect,
  11. OPER_DOWN, OPER_UP, OPER_DORMANT, OPER_LOWERLAYERDOWN, OPER_NOTPRESENT,
  12. OPER_TESTING, OPER_UNKNOWN, RTATTR_START_OFFSET, RTM_NEWLINK, RTM_SETLINK,
  13. RTM_GETLINK, MAX_SIZE)
  14. def int_to_bytes(i):
  15. '''convert integer to binary: eg: 1 to \x01'''
  16. hex_value = '{0:x}'.format(i)
  17. hex_value = '0' * (len(hex_value) % 2) + hex_value
  18. return codecs.decode(hex_value, 'hex_codec')
  19. class TestCreateBoundNetlinkSocket(CiTestCase):
  20. @mock.patch('cloudinit.sources.helpers.netlink.socket.socket')
  21. def test_socket_error_on_create(self, m_socket):
  22. '''create_bound_netlink_socket catches socket creation exception'''
  23. """NetlinkCreateSocketError is raised when socket creation errors."""
  24. m_socket.side_effect = socket.error("Fake socket failure")
  25. with self.assertRaises(NetlinkCreateSocketError) as ctx_mgr:
  26. create_bound_netlink_socket()
  27. self.assertEqual(
  28. 'Exception during netlink socket create: Fake socket failure',
  29. str(ctx_mgr.exception))
  30. class TestReadNetlinkSocket(CiTestCase):
  31. @mock.patch('cloudinit.sources.helpers.netlink.socket.socket')
  32. @mock.patch('cloudinit.sources.helpers.netlink.select.select')
  33. def test_read_netlink_socket(self, m_select, m_socket):
  34. '''read_netlink_socket able to receive data'''
  35. data = 'netlinktest'
  36. m_select.return_value = [m_socket], None, None
  37. m_socket.recv.return_value = data
  38. recv_data = read_netlink_socket(m_socket, 2)
  39. m_select.assert_called_with([m_socket], [], [], 2)
  40. m_socket.recv.assert_called_with(MAX_SIZE)
  41. self.assertIsNotNone(recv_data)
  42. self.assertEqual(recv_data, data)
  43. @mock.patch('cloudinit.sources.helpers.netlink.socket.socket')
  44. @mock.patch('cloudinit.sources.helpers.netlink.select.select')
  45. def test_netlink_read_timeout(self, m_select, m_socket):
  46. '''read_netlink_socket should timeout if nothing to read'''
  47. m_select.return_value = [], None, None
  48. data = read_netlink_socket(m_socket, 1)
  49. m_select.assert_called_with([m_socket], [], [], 1)
  50. self.assertEqual(m_socket.recv.call_count, 0)
  51. self.assertIsNone(data)
  52. def test_read_invalid_socket(self):
  53. '''read_netlink_socket raises assert error if socket is invalid'''
  54. socket = None
  55. with self.assertRaises(AssertionError) as context:
  56. read_netlink_socket(socket, 1)
  57. self.assertTrue('netlink socket is none' in str(context.exception))
  58. class TestParseNetlinkMessage(CiTestCase):
  59. def test_read_rta_oper_state(self):
  60. '''read_rta_oper_state could parse netlink message and extract data'''
  61. ifname = "eth0"
  62. bytes = ifname.encode("utf-8")
  63. buf = bytearray(48)
  64. struct.pack_into("HH4sHHc", buf, RTATTR_START_OFFSET, 8, 3, bytes, 5,
  65. 16, int_to_bytes(OPER_DOWN))
  66. interface_state = read_rta_oper_state(buf)
  67. self.assertEqual(interface_state.ifname, ifname)
  68. self.assertEqual(interface_state.operstate, OPER_DOWN)
  69. def test_read_none_data(self):
  70. '''read_rta_oper_state raises assert error if data is none'''
  71. data = None
  72. with self.assertRaises(AssertionError) as context:
  73. read_rta_oper_state(data)
  74. self.assertTrue('data is none', str(context.exception))
  75. def test_read_invalid_rta_operstate_none(self):
  76. '''read_rta_oper_state returns none if operstate is none'''
  77. ifname = "eth0"
  78. buf = bytearray(40)
  79. bytes = ifname.encode("utf-8")
  80. struct.pack_into("HH4s", buf, RTATTR_START_OFFSET, 8, 3, bytes)
  81. interface_state = read_rta_oper_state(buf)
  82. self.assertIsNone(interface_state)
  83. def test_read_invalid_rta_ifname_none(self):
  84. '''read_rta_oper_state returns none if ifname is none'''
  85. buf = bytearray(40)
  86. struct.pack_into("HHc", buf, RTATTR_START_OFFSET, 5, 16,
  87. int_to_bytes(OPER_DOWN))
  88. interface_state = read_rta_oper_state(buf)
  89. self.assertIsNone(interface_state)
  90. def test_read_invalid_data_len(self):
  91. '''raise assert error if data size is smaller than required size'''
  92. buf = bytearray(32)
  93. with self.assertRaises(AssertionError) as context:
  94. read_rta_oper_state(buf)
  95. self.assertTrue('length of data is smaller than RTATTR_START_OFFSET' in
  96. str(context.exception))
  97. def test_unpack_rta_attr_none_data(self):
  98. '''unpack_rta_attr raises assert error if data is none'''
  99. data = None
  100. with self.assertRaises(AssertionError) as context:
  101. unpack_rta_attr(data, RTATTR_START_OFFSET)
  102. self.assertTrue('data is none' in str(context.exception))
  103. def test_unpack_rta_attr_invalid_offset(self):
  104. '''unpack_rta_attr raises assert error if offset is invalid'''
  105. data = bytearray(48)
  106. with self.assertRaises(AssertionError) as context:
  107. unpack_rta_attr(data, "offset")
  108. self.assertTrue('offset is not integer' in str(context.exception))
  109. with self.assertRaises(AssertionError) as context:
  110. unpack_rta_attr(data, 31)
  111. self.assertTrue('rta offset is less than expected length' in
  112. str(context.exception))
  113. @mock.patch('cloudinit.sources.helpers.netlink.socket.socket')
  114. @mock.patch('cloudinit.sources.helpers.netlink.read_netlink_socket')
  115. class TestWaitForMediaDisconnectConnect(CiTestCase):
  116. with_logs = True
  117. def _media_switch_data(self, ifname, msg_type, operstate):
  118. '''construct netlink data with specified fields'''
  119. if ifname and operstate is not None:
  120. data = bytearray(48)
  121. bytes = ifname.encode("utf-8")
  122. struct.pack_into("HH4sHHc", data, RTATTR_START_OFFSET, 8, 3,
  123. bytes, 5, 16, int_to_bytes(operstate))
  124. elif ifname:
  125. data = bytearray(40)
  126. bytes = ifname.encode("utf-8")
  127. struct.pack_into("HH4s", data, RTATTR_START_OFFSET, 8, 3, bytes)
  128. elif operstate:
  129. data = bytearray(40)
  130. struct.pack_into("HHc", data, RTATTR_START_OFFSET, 5, 16,
  131. int_to_bytes(operstate))
  132. struct.pack_into("=LHHLL", data, 0, len(data), msg_type, 0, 0, 0)
  133. return data
  134. def test_media_down_up_scenario(self, m_read_netlink_socket,
  135. m_socket):
  136. '''Test for media down up sequence for required interface name'''
  137. ifname = "eth0"
  138. # construct data for Oper State down
  139. data_op_down = self._media_switch_data(ifname, RTM_NEWLINK, OPER_DOWN)
  140. # construct data for Oper State up
  141. data_op_up = self._media_switch_data(ifname, RTM_NEWLINK, OPER_UP)
  142. m_read_netlink_socket.side_effect = [data_op_down, data_op_up]
  143. wait_for_media_disconnect_connect(m_socket, ifname)
  144. self.assertEqual(m_read_netlink_socket.call_count, 2)
  145. def test_wait_for_media_switch_diff_interface(self, m_read_netlink_socket,
  146. m_socket):
  147. '''wait_for_media_disconnect_connect ignores unexpected interfaces.
  148. The first two messages are for other interfaces and last two are for
  149. expected interface. So the function exit only after receiving last
  150. 2 messages and therefore the call count for m_read_netlink_socket
  151. has to be 4
  152. '''
  153. other_ifname = "eth1"
  154. expected_ifname = "eth0"
  155. data_op_down_eth1 = self._media_switch_data(
  156. other_ifname, RTM_NEWLINK, OPER_DOWN)
  157. data_op_up_eth1 = self._media_switch_data(
  158. other_ifname, RTM_NEWLINK, OPER_UP)
  159. data_op_down_eth0 = self._media_switch_data(
  160. expected_ifname, RTM_NEWLINK, OPER_DOWN)
  161. data_op_up_eth0 = self._media_switch_data(
  162. expected_ifname, RTM_NEWLINK, OPER_UP)
  163. m_read_netlink_socket.side_effect = [data_op_down_eth1,
  164. data_op_up_eth1,
  165. data_op_down_eth0,
  166. data_op_up_eth0]
  167. wait_for_media_disconnect_connect(m_socket, expected_ifname)
  168. self.assertIn('Ignored netlink event on interface %s' % other_ifname,
  169. self.logs.getvalue())
  170. self.assertEqual(m_read_netlink_socket.call_count, 4)
  171. def test_invalid_msgtype_getlink(self, m_read_netlink_socket, m_socket):
  172. '''wait_for_media_disconnect_connect ignores GETLINK events.
  173. The first two messages are for oper down and up for RTM_GETLINK type
  174. which netlink module will ignore. The last 2 messages are RTM_NEWLINK
  175. with oper state down and up messages. Therefore the call count for
  176. m_read_netlink_socket has to be 4 ignoring first 2 messages
  177. of RTM_GETLINK
  178. '''
  179. ifname = "eth0"
  180. data_getlink_down = self._media_switch_data(
  181. ifname, RTM_GETLINK, OPER_DOWN)
  182. data_getlink_up = self._media_switch_data(
  183. ifname, RTM_GETLINK, OPER_UP)
  184. data_newlink_down = self._media_switch_data(
  185. ifname, RTM_NEWLINK, OPER_DOWN)
  186. data_newlink_up = self._media_switch_data(
  187. ifname, RTM_NEWLINK, OPER_UP)
  188. m_read_netlink_socket.side_effect = [data_getlink_down,
  189. data_getlink_up,
  190. data_newlink_down,
  191. data_newlink_up]
  192. wait_for_media_disconnect_connect(m_socket, ifname)
  193. self.assertEqual(m_read_netlink_socket.call_count, 4)
  194. def test_invalid_msgtype_setlink(self, m_read_netlink_socket, m_socket):
  195. '''wait_for_media_disconnect_connect ignores SETLINK events.
  196. The first two messages are for oper down and up for RTM_GETLINK type
  197. which it will ignore. 3rd and 4th messages are RTM_NEWLINK with down
  198. and up messages. This function should exit after 4th messages since it
  199. sees down->up scenario. So the call count for m_read_netlink_socket
  200. has to be 4 ignoring first 2 messages of RTM_GETLINK and
  201. last 2 messages of RTM_NEWLINK
  202. '''
  203. ifname = "eth0"
  204. data_setlink_down = self._media_switch_data(
  205. ifname, RTM_SETLINK, OPER_DOWN)
  206. data_setlink_up = self._media_switch_data(
  207. ifname, RTM_SETLINK, OPER_UP)
  208. data_newlink_down = self._media_switch_data(
  209. ifname, RTM_NEWLINK, OPER_DOWN)
  210. data_newlink_up = self._media_switch_data(
  211. ifname, RTM_NEWLINK, OPER_UP)
  212. m_read_netlink_socket.side_effect = [data_setlink_down,
  213. data_setlink_up,
  214. data_newlink_down,
  215. data_newlink_up,
  216. data_newlink_down,
  217. data_newlink_up]
  218. wait_for_media_disconnect_connect(m_socket, ifname)
  219. self.assertEqual(m_read_netlink_socket.call_count, 4)
  220. def test_netlink_invalid_switch_scenario(self, m_read_netlink_socket,
  221. m_socket):
  222. '''returns only if it receives UP event after a DOWN event'''
  223. ifname = "eth0"
  224. data_op_down = self._media_switch_data(ifname, RTM_NEWLINK, OPER_DOWN)
  225. data_op_up = self._media_switch_data(ifname, RTM_NEWLINK, OPER_UP)
  226. data_op_dormant = self._media_switch_data(ifname, RTM_NEWLINK,
  227. OPER_DORMANT)
  228. data_op_notpresent = self._media_switch_data(ifname, RTM_NEWLINK,
  229. OPER_NOTPRESENT)
  230. data_op_lowerdown = self._media_switch_data(ifname, RTM_NEWLINK,
  231. OPER_LOWERLAYERDOWN)
  232. data_op_testing = self._media_switch_data(ifname, RTM_NEWLINK,
  233. OPER_TESTING)
  234. data_op_unknown = self._media_switch_data(ifname, RTM_NEWLINK,
  235. OPER_UNKNOWN)
  236. m_read_netlink_socket.side_effect = [data_op_up, data_op_up,
  237. data_op_dormant, data_op_up,
  238. data_op_notpresent, data_op_up,
  239. data_op_lowerdown, data_op_up,
  240. data_op_testing, data_op_up,
  241. data_op_unknown, data_op_up,
  242. data_op_down, data_op_up]
  243. wait_for_media_disconnect_connect(m_socket, ifname)
  244. self.assertEqual(m_read_netlink_socket.call_count, 14)
  245. def test_netlink_valid_inbetween_transitions(self, m_read_netlink_socket,
  246. m_socket):
  247. '''wait_for_media_disconnect_connect handles in between transitions'''
  248. ifname = "eth0"
  249. data_op_down = self._media_switch_data(ifname, RTM_NEWLINK, OPER_DOWN)
  250. data_op_up = self._media_switch_data(ifname, RTM_NEWLINK, OPER_UP)
  251. data_op_dormant = self._media_switch_data(ifname, RTM_NEWLINK,
  252. OPER_DORMANT)
  253. data_op_unknown = self._media_switch_data(ifname, RTM_NEWLINK,
  254. OPER_UNKNOWN)
  255. m_read_netlink_socket.side_effect = [data_op_down, data_op_dormant,
  256. data_op_unknown, data_op_up]
  257. wait_for_media_disconnect_connect(m_socket, ifname)
  258. self.assertEqual(m_read_netlink_socket.call_count, 4)
  259. def test_netlink_invalid_operstate(self, m_read_netlink_socket, m_socket):
  260. '''wait_for_media_disconnect_connect should handle invalid operstates.
  261. The function should not fail and return even if it receives invalid
  262. operstates. It always should wait for down up sequence.
  263. '''
  264. ifname = "eth0"
  265. data_op_down = self._media_switch_data(ifname, RTM_NEWLINK, OPER_DOWN)
  266. data_op_up = self._media_switch_data(ifname, RTM_NEWLINK, OPER_UP)
  267. data_op_invalid = self._media_switch_data(ifname, RTM_NEWLINK, 7)
  268. m_read_netlink_socket.side_effect = [data_op_invalid, data_op_up,
  269. data_op_down, data_op_invalid,
  270. data_op_up]
  271. wait_for_media_disconnect_connect(m_socket, ifname)
  272. self.assertEqual(m_read_netlink_socket.call_count, 5)
  273. def test_wait_invalid_socket(self, m_read_netlink_socket, m_socket):
  274. '''wait_for_media_disconnect_connect handle none netlink socket.'''
  275. socket = None
  276. ifname = "eth0"
  277. with self.assertRaises(AssertionError) as context:
  278. wait_for_media_disconnect_connect(socket, ifname)
  279. self.assertTrue('netlink socket is none' in str(context.exception))
  280. def test_wait_invalid_ifname(self, m_read_netlink_socket, m_socket):
  281. '''wait_for_media_disconnect_connect handle none interface name'''
  282. ifname = None
  283. with self.assertRaises(AssertionError) as context:
  284. wait_for_media_disconnect_connect(m_socket, ifname)
  285. self.assertTrue('interface name is none' in str(context.exception))
  286. ifname = ""
  287. with self.assertRaises(AssertionError) as context:
  288. wait_for_media_disconnect_connect(m_socket, ifname)
  289. self.assertTrue('interface name cannot be empty' in
  290. str(context.exception))
  291. def test_wait_invalid_rta_attr(self, m_read_netlink_socket, m_socket):
  292. ''' wait_for_media_disconnect_connect handles invalid rta data'''
  293. ifname = "eth0"
  294. data_invalid1 = self._media_switch_data(None, RTM_NEWLINK, OPER_DOWN)
  295. data_invalid2 = self._media_switch_data(ifname, RTM_NEWLINK, None)
  296. data_op_down = self._media_switch_data(ifname, RTM_NEWLINK, OPER_DOWN)
  297. data_op_up = self._media_switch_data(ifname, RTM_NEWLINK, OPER_UP)
  298. m_read_netlink_socket.side_effect = [data_invalid1, data_invalid2,
  299. data_op_down, data_op_up]
  300. wait_for_media_disconnect_connect(m_socket, ifname)
  301. self.assertEqual(m_read_netlink_socket.call_count, 4)
  302. def test_read_multiple_netlink_msgs(self, m_read_netlink_socket, m_socket):
  303. '''Read multiple messages in single receive call'''
  304. ifname = "eth0"
  305. bytes = ifname.encode("utf-8")
  306. data = bytearray(96)
  307. struct.pack_into("=LHHLL", data, 0, 48, RTM_NEWLINK, 0, 0, 0)
  308. struct.pack_into("HH4sHHc", data, RTATTR_START_OFFSET, 8, 3,
  309. bytes, 5, 16, int_to_bytes(OPER_DOWN))
  310. struct.pack_into("=LHHLL", data, 48, 48, RTM_NEWLINK, 0, 0, 0)
  311. struct.pack_into("HH4sHHc", data, 48 + RTATTR_START_OFFSET, 8,
  312. 3, bytes, 5, 16, int_to_bytes(OPER_UP))
  313. m_read_netlink_socket.return_value = data
  314. wait_for_media_disconnect_connect(m_socket, ifname)
  315. self.assertEqual(m_read_netlink_socket.call_count, 1)
  316. def test_read_partial_netlink_msgs(self, m_read_netlink_socket, m_socket):
  317. '''Read partial messages in receive call'''
  318. ifname = "eth0"
  319. bytes = ifname.encode("utf-8")
  320. data1 = bytearray(112)
  321. data2 = bytearray(32)
  322. struct.pack_into("=LHHLL", data1, 0, 48, RTM_NEWLINK, 0, 0, 0)
  323. struct.pack_into("HH4sHHc", data1, RTATTR_START_OFFSET, 8, 3,
  324. bytes, 5, 16, int_to_bytes(OPER_DOWN))
  325. struct.pack_into("=LHHLL", data1, 48, 48, RTM_NEWLINK, 0, 0, 0)
  326. struct.pack_into("HH4sHHc", data1, 80, 8, 3, bytes, 5, 16,
  327. int_to_bytes(OPER_DOWN))
  328. struct.pack_into("=LHHLL", data1, 96, 48, RTM_NEWLINK, 0, 0, 0)
  329. struct.pack_into("HH4sHHc", data2, 16, 8, 3, bytes, 5, 16,
  330. int_to_bytes(OPER_UP))
  331. m_read_netlink_socket.side_effect = [data1, data2]
  332. wait_for_media_disconnect_connect(m_socket, ifname)
  333. self.assertEqual(m_read_netlink_socket.call_count, 2)