def test_raw_promiscuous_sockets(self, sock_mock, fcntl_mock): interfaces = ['eth0', 'ens9f1'] protocol = 3 sock1 = mock.Mock() sock2 = mock.Mock() sock_mock.side_effect = [sock1, sock2] with netutils.RawPromiscuousSockets(interfaces, protocol) as sockets: # 2 interfaces, 1 get, 1 set call each self.assertEqual(4, fcntl_mock.call_count) self.assertEqual([('eth0', sock1), ('ens9f1', sock2)], sockets) sock1.bind.assert_called_once_with(('eth0', protocol)) sock2.bind.assert_called_once_with(('ens9f1', protocol)) self.assertEqual(6, fcntl_mock.call_count) sock1.close.assert_called_once_with() sock2.close.assert_called_once_with()
def test_raw_promiscuous_sockets_bind_fail(self, sock_mock, fcntl_mock): interfaces = ['eth0', 'ens9f1'] protocol = 3 sock1 = mock.Mock() sock2 = mock.Mock() sock_mock.side_effect = [sock1, sock2] sock_mock.bind.side_effects = [None, Exception] with netutils.RawPromiscuousSockets(interfaces, protocol) as sockets: # Ensure this isn't run self.assertEqual([], sockets) sock1.bind.assert_called_once_with(('eth0', protocol)) sock2.bind.assert_called_once_with(('ens9f1', protocol)) self.assertEqual(6, fcntl_mock.call_count) sock1.close.assert_called_once_with() sock2.close.assert_called_once_with()
def _run_with_exception(): with netutils.RawPromiscuousSockets(interfaces, protocol): raise RuntimeError()
def _run_with_bind_fail(): with netutils.RawPromiscuousSockets(interfaces, protocol): self.fail('Unreachable code')