Пример #1
0
    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)
Пример #2
0
    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', )
Пример #3
0
    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)
Пример #4
0
    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',)
Пример #5
0
    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), )
Пример #6
0
    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?',)
Пример #7
0
    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),
        )
Пример #8
0
    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?', )
Пример #9
0
    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
Пример #10
0
    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
Пример #11
0
    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
Пример #12
0
    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
Пример #13
0
    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
Пример #14
0
    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
Пример #15
0
    def test_load_config(self):
        controller = Controller(config_file=self.config_file.name)

        assert controller.sock_file == '/tmp/mcrunner.sock'