def test_send_command_kill(self, mock_create_connection, mock_socket_send, mock_socket_recv): # This test just makes sure we don't infinitely loop reading from socket waiting for END # Needs rewriting once we add methods for killing clients. # Example output from management interface: # client-kill 1 # SUCCESS: client-kill command succeeded # kill 1.2.3.4:12345 # SUCCESS: 1 client(s) at address 1.2.3.4:12345 killed vpn = VPN(host="localhost", port=1234) vpn.connect() mock_create_connection.assert_called_once_with(("localhost", 1234), timeout=ANY) mock_socket_recv.assert_called_once() mock_socket_recv.reset_mock() mock_socket_recv.return_value = "SUCCESS: 1 client(s) at address 1.2.3.4:12345 killed" vpn.send_command("kill 1.2.3.4:12345") mock_socket_send.assert_called_once_with("kill 1.2.3.4:12345\n") mock_socket_recv.assert_called_once() mock_socket_send.reset_mock() mock_socket_recv.reset_mock() mock_socket_recv.return_value = "SUCCESS: client-kill command succeeded" vpn.send_command("client-kill 1") mock_socket_send.assert_called_once_with("client-kill 1\n") mock_socket_recv.assert_called_once()
def test_connect_ip_failure(self, mock_create_connection): vpn = VPN(host="localhost", port=1234) mock_create_connection.side_effect = socket.error() with self.assertRaises(errors.ConnectError): vpn.connect() mock_create_connection.side_effect = socket.timeout() with self.assertRaises(errors.ConnectError): vpn.connect()
def test_send_sigterm(self, mock_create_connection, mock_socket_send, mock_socket_recv): vpn = VPN(host="localhost", port=1234) vpn.connect() mock_create_connection.assert_called_once_with(("localhost", 1234), timeout=ANY) mock_socket_recv.assert_called_once() mock_socket_recv.reset_mock() mock_socket_recv.return_value = "SUCCESS: signal SIGTERM thrown" vpn.send_sigterm() mock_socket_send.assert_called_once_with("signal SIGTERM\n") mock_socket_recv.assert_called_once()
def test_send_command(self, mock_create_connection, mock_socket_send, mock_socket_recv): vpn = VPN(host="localhost", port=1234) vpn.connect() mock_create_connection.assert_called_once_with(("localhost", 1234), timeout=ANY) mock_socket_recv.assert_called_once() mock_socket_recv.reset_mock() vals = gen_mock_values(["asd\n", "END\n"]) mock_socket_recv.side_effect = lambda: next(vals) a = vpn.send_command("help") mock_socket_send.assert_called_once_with("help\n") self.assertEqual(2, mock_socket_recv.call_count) self.assertEqual(a, "asd\nEND\n")
def test_send_command_kill(self, mock_create_connection, mock_socket_send, mock_socket_recv): vpn = VPN(host="localhost", port=1234) vpn.connect() mock_create_connection.assert_called_once_with(("localhost", 1234), timeout=ANY) mock_socket_recv.assert_called_once() mock_socket_recv.reset_mock() self.assertIsNone(vpn.send_command("kill")) mock_socket_send.assert_called_once_with("kill\n") mock_socket_recv.assert_not_called() mock_socket_send.reset_mock() self.assertIsNone(vpn.send_command("client-kill")) mock_socket_send.assert_called_once_with("client-kill\n") mock_socket_recv.assert_not_called()