def test_socket_client(self): controller = Controller(config_file=self.config_file.name) with mock.patch('socket.socket') as MockSocket: sock = controller.socket_client() assert sock == MockSocket.return_value assert MockSocket.call_args[0] == (socket.AF_UNIX, socket.SOCK_STREAM)
def test_send_mcrunnerd_package_with_socket_error(self): controller = Controller(config_file=self.config_file.name) controller.socket_client = mock.MagicMock(side_effect=socket.error) with mock.patch('mcrunner.mcrunner._output') as mock_print: controller.send_mcrunnerd_package('some_package') assert mock_print.call_count == 1 assert mock_print.call_args[0] == ('Could not connect to socket - is mcrunnerd running?',)
def test_send_mcrunnerd_package_with_socket_error(self): controller = Controller(config_file=self.config_file.name) controller.socket_client = mock.MagicMock(side_effect=socket.error) with mock.patch('mcrunner.mcrunner._output') as mock_print: controller.send_mcrunnerd_package('some_package') assert mock_print.call_count == 1 assert mock_print.call_args[0] == ( 'Could not connect to socket - is mcrunnerd running?', )
def test_send_mcrunnerd_package_with_sendall_error(self): controller = Controller(config_file=self.config_file.name) socket_error = socket.error('bad send') mock_sock = mock.MagicMock() controller.socket_client = mock.MagicMock(return_value=mock_sock) mock_sock.sendall = mock.MagicMock(side_effect=socket_error) with mock.patch('mcrunner.mcrunner._output') as mock_print: controller.send_mcrunnerd_package('some_package') assert mock_sock.sendall.call_count == 1 assert mock_sock.sendall.call_args[0] == ('some_package',) assert mock_print.call_count == 1 assert mock_print.call_args[0] == ('Error sending mcrunnerd package: %s' % str(socket_error),) assert mock_sock.close.call_count == 1
def test_send_mcrunnerd_package_with_no_message(self): controller = Controller(config_file=self.config_file.name) mock_sock = mock.MagicMock() controller.socket_client = mock.MagicMock(return_value=mock_sock) mock_sock.recv = mock.MagicMock(side_effect=[ COMMAND_RESPONSE_STATUSES['DONE'] ]) with mock.patch('mcrunner.mcrunner._output') as mock_print: controller.send_mcrunnerd_package('some_package') assert mock_sock.sendall.call_count == 1 assert mock_sock.sendall.call_args[0] == ('some_package',) assert mock_sock.recv.call_count == 1 assert mock_sock.recv.call_args[0] == (1,) assert mock_print.call_count == 0 assert mock_sock.close.call_count == 1
def test_send_mcrunnerd_package(self): controller = Controller(config_file=self.config_file.name) controller.socket_client = mock.MagicMock() mock_connection = mock.MagicMock() mock_connection.receive_message = mock.MagicMock(side_effect=[ 'some sample response message', None # socket closed ]) with mock.patch('mcrunner.mcrunner._output') as mock_print: with mock.patch('mcrunner.mcrunner.ClientSocketConnection', return_value=mock_connection): controller.send_mcrunnerd_package('some_package') assert mock_connection.send_message.call_count == 1 assert mock_connection.send_message.call_args[0] == ('some_package',) assert mock_print.call_count == 1 assert mock_print.call_args[0] == ('some sample response message',) assert mock_connection.close.call_count == 1
def test_send_mcrunnerd_package_with_sendall_error(self): controller = Controller(config_file=self.config_file.name) controller.socket_client = mock.MagicMock() socket_error = socket.error('bad send') mock_connection = mock.MagicMock() mock_connection.send_message = mock.MagicMock(side_effect=socket_error) with mock.patch('mcrunner.mcrunner._output') as mock_print: with mock.patch('mcrunner.mcrunner.ClientSocketConnection', return_value=mock_connection): controller.send_mcrunnerd_package('some_package') assert mock_connection.send_message.call_count == 1 assert mock_connection.send_message.call_args[0] == ('some_package', ) assert mock_print.call_count == 1 assert mock_print.call_args[0] == ( 'Error sending mcrunnerd package: %s' % str(socket_error), ) assert mock_connection.close.call_count == 1
def test_send_mcrunnerd_package(self): controller = Controller(config_file=self.config_file.name) controller.socket_client = mock.MagicMock() mock_connection = mock.MagicMock() mock_connection.receive_message = mock.MagicMock(side_effect=[ 'some sample response message', None # socket closed ]) with mock.patch('mcrunner.mcrunner._output') as mock_print: with mock.patch('mcrunner.mcrunner.ClientSocketConnection', return_value=mock_connection): controller.send_mcrunnerd_package('some_package') assert mock_connection.send_message.call_count == 1 assert mock_connection.send_message.call_args[0] == ('some_package', ) assert mock_print.call_count == 1 assert mock_print.call_args[0] == ('some sample response message', ) assert mock_connection.close.call_count == 1