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_handle_mcrunnerd_action(self): controller = Controller(config_file=self.config_file.name) controller.send_mcrunnerd_package = mock.MagicMock() controller.handle_mcrunnerd_action('package') assert controller.send_mcrunnerd_package.call_count == 1 assert controller.send_mcrunnerd_package.call_args[0] == ('package', )
def test_handle_mcrunnerd_action(self): controller = Controller(config_file=self.config_file.name) controller.send_mcrunnerd_package = mock.MagicMock() controller.handle_mcrunnerd_action('package') assert controller.send_mcrunnerd_package.call_count == 1 assert controller.send_mcrunnerd_package.call_args[0] == ('package',)
def test_handle_server_action(self): controller = Controller(config_file=self.config_file.name) controller.send_mcrunnerd_package = mock.MagicMock() controller.handle_server_action('action', 'server_1') assert controller.send_mcrunnerd_package.call_count == 1 assert controller.send_mcrunnerd_package.call_args[0] == ( 'action{delim}server_1'.format( delim=MCRUNNERD_COMMAND_DELIMITER), )
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_handle_server_action_with_command(self): controller = Controller(config_file=self.config_file.name) controller.send_mcrunnerd_package = mock.MagicMock() controller.handle_server_action('action', 'server_1', command='some command') assert controller.send_mcrunnerd_package.call_count == 1 assert controller.send_mcrunnerd_package.call_args[0] == ( 'action{delim}server_1{delim}some command'.format(delim=MCRUNNERD_COMMAND_DELIMITER), )
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_load_empty_config(self): self.config_file.close() self.config_file = tempfile.NamedTemporaryFile() controller = Controller(config_file=self.config_file.name) assert controller.sock_file == None
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
def test_load_config(self): controller = Controller(config_file=self.config_file.name) assert controller.sock_file == '/tmp/mcrunner.sock'