def test_get_network_interface_speed(self, mock_ioctl, mock_unpack): """ The link speed is reported as unpacked from the ioctl() call. """ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) # ioctl always succeeds mock_unpack.return_value = (100, False) result = get_network_interface_speed(sock, b"eth0") mock_ioctl.assert_called_with(ANY, ANY, ANY) mock_unpack.assert_called_with("12xHB28x", ANY) self.assertEqual((100, False), result)
def test_unplugged(self, mock_ioctl, mock_unpack): """ The link speed for an unplugged interface is reported as 0. """ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) # ioctl always succeeds mock_unpack.return_value = (65535, False) result = get_network_interface_speed(sock, b"eth0") mock_ioctl.assert_called_with(ANY, ANY, ANY) mock_unpack.assert_called_with("12xHB28x", ANY) self.assertEqual((0, False), result) sock.close()
def test_get_network_interface_speed_not_permitted(self, mock_ioctl): """ In some cases (lucid seems to be affected), the ioctl() call is not allowed for non-root users. In that case we intercept the error and not report the network speed. """ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) theerror = IOError() theerror.errno = 1 theerror.message = "Operation not permitted" # ioctl always raises mock_ioctl.side_effect = theerror result = get_network_interface_speed(sock, b"eth0") mock_ioctl.assert_called_with(ANY, ANY, ANY) self.assertEqual((-1, False), result)
def test_get_network_interface_speed_not_supported(self, mock_ioctl): """ Some drivers do not report the needed interface speed. In this case an C{IOError} with errno 95 is raised ("Operation not supported"). If such an error is rasied, report the speed as -1. """ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) theerror = IOError() theerror.errno = 95 theerror.message = "Operation not supported" # ioctl always raises mock_ioctl.side_effect = theerror result = get_network_interface_speed(sock, b"eth0") mock_ioctl.assert_called_with(ANY, ANY, ANY) self.assertEqual((-1, False), result)