def test_packing_errors_last_arg(self):
     # Assert we check for NULLs in all but the "last" argument,
     # where last depends on the cmd_type.
     protocol.pack_binary_command(
         cmd_type=protocol.GEARMAN_COMMAND_SUBMIT_JOB,
         cmd_args={u"task": b"function", u"data": b"ab\x00cd", u"unique": b"12345"}
     )
Example #2
0
    def test_packing_errors(self):
        # Assert we get an unknown command
        cmd_type = 1234
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get a fake command
        cmd_type = protocol.GEARMAN_COMMAND_TEXT_COMMAND
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get arg mismatch, got 1, expecting 0
        cmd_type = protocol.GEARMAN_COMMAND_GRAB_JOB
        cmd_args = dict(extra='arguments')
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get arg mismatch, got 0, expecting 1
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get arg mismatch (name), got 1, expecting 1
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(extra='arguments')
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get a non-string argument
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(job_handle=12345)
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we get a non-string argument (expecting BYTES)
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(job_handle=str(12345))
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='funct\x00ion', data='abcd', unique='12345')
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', data='ab\x00cd', unique='12345')
        protocol.pack_binary_command(
            cmd_type, cmd_args)  # Should not raise, 'data' is last.

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', data='abcd', unique='123\x0045')
        self.assertRaises(ProtocolError, protocol.pack_binary_command,
                          cmd_type, cmd_args)
    def test_packing_errors(self):
        # Assert we get an unknown command
        cmd_type = 1234
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we get a fake command
        cmd_type = protocol.GEARMAN_COMMAND_TEXT_COMMAND
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we get arg mismatch, got 1, expecting 0
        cmd_type = protocol.GEARMAN_COMMAND_GRAB_JOB
        cmd_args = dict(extra='arguments')
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we get arg mismatch, got 0, expecting 1
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict()
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we get arg mismatch (name), got 1, expecting 1
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(extra='arguments')
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we get a non-string argument
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(job_handle=12345)
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we get a non-string argument (expecting BYTES)
        cmd_type = protocol.GEARMAN_COMMAND_JOB_CREATED
        cmd_args = dict(job_handle=unicode(12345))
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='funct\x00ion', data='abcd', unique='12345')
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', data='ab\x00cd', unique='12345')
        protocol.pack_binary_command(cmd_type, cmd_args) # Should not raise, 'data' is last.

        # Assert we check for NULLs in all but the "last" argument, where last depends on the cmd_type.
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', data='abcd', unique='123\x0045')
        self.assertRaises(ProtocolError, protocol.pack_binary_command, cmd_type, cmd_args)
    def test_packing_no_arg(self):
        cmd_type = protocol.GEARMAN_COMMAND_NOOP
        cmd_args = dict()

        expected_command_buffer = struct.pack('!4sII', protocol.MAGIC_REQ_STRING, cmd_type, 0)
        packed_command_buffer = protocol.pack_binary_command(cmd_type, cmd_args)
        self.assertEquals(packed_command_buffer, expected_command_buffer)
    def test_packing_response(self):
        # Test packing a response for a job (server side packing)
        cmd_type = protocol.GEARMAN_COMMAND_NO_JOB
        cmd_args = dict()

        expected_command_buffer = struct.pack('!4sII', protocol.MAGIC_RES_STRING, cmd_type, 0)
        packed_command_buffer = protocol.pack_binary_command(cmd_type, cmd_args, is_response=True)
        self.assertEquals(packed_command_buffer, expected_command_buffer)
    def test_packing_single_arg(self):
        cmd_type = protocol.GEARMAN_COMMAND_ECHO_REQ
        cmd_args = dict(data='abcde')

        expected_payload_size = len(cmd_args['data'])
        expected_format = '!4sII%ds' % expected_payload_size

        expected_command_buffer = struct.pack(expected_format, protocol.MAGIC_REQ_STRING, cmd_type, expected_payload_size, cmd_args['data'])
        packed_command_buffer = protocol.pack_binary_command(cmd_type, cmd_args)
        self.assertEquals(packed_command_buffer, expected_command_buffer)
    def test_packing_multiple_args(self):
        cmd_type = protocol.GEARMAN_COMMAND_SUBMIT_JOB
        cmd_args = dict(task='function', unique='12345', data='abcd')

        ordered_parameters = [cmd_args['task'], cmd_args['unique'], cmd_args['data']]

        expected_payload = protocol.NULL_CHAR.join(ordered_parameters)
        expected_payload_size = len(expected_payload)
        expected_format = '!4sII%ds' % expected_payload_size
        expected_command_buffer = struct.pack(expected_format, protocol.MAGIC_REQ_STRING, cmd_type, expected_payload_size, expected_payload)

        packed_command_buffer = protocol.pack_binary_command(cmd_type, cmd_args)
        self.assertEquals(packed_command_buffer, expected_command_buffer)
Example #8
0
    def _pack_command(self, cmd_type, cmd_args):
        """Converts a command to its raw binary format"""
        if cmd_type not in protocol.GEARMAN_PARAMS_FOR_COMMAND:
            raise ProtocolError('Unknown command: %r' % 
                                protocol.get_command_name(cmd_type))

        log.msg("%s - send - %s - %r" % (
            id(self), protocol.get_command_name(cmd_type), cmd_args), 
            logging.DEBUG)

        if cmd_type == protocol.GEARMAN_COMMAND_TEXT_COMMAND:
            return protocol.pack_text_command(cmd_type, cmd_args)
        else:
            return protocol.pack_binary_command(cmd_type, cmd_args)
Example #9
0
    def _pack_command(self, cmd_type, cmd_args):
        """Converts a command to its raw binary format"""
        if cmd_type not in GEARMAN_PARAMS_FOR_COMMAND:
            raise ProtocolError('Unknown command: %r' % get_command_name(cmd_type))

        if _DEBUG_MODE_:
            gearman_logger.debug('%s - Send - %s - %r', hex(id(self)), get_command_name(cmd_type), cmd_args)

        if cmd_type == GEARMAN_COMMAND_TEXT_COMMAND:
            return pack_text_command(cmd_type, cmd_args)
        else:
            # We'll be sending a response if we know we're a server side command
            is_response = bool(self._is_server_side)
            return pack_binary_command(cmd_type, cmd_args, is_response)
    def test_packing_single_arg(self):
        cmd_type = protocol.GEARMAN_COMMAND_ECHO_REQ
        cmd_args = {u"data": b"abcde"}

        expected_payload_size = len(cmd_args['data'])
        expected_format = '!4sII%ds' % expected_payload_size

        expected_command_buffer = struct.pack(
            expected_format,
            protocol.MAGIC_REQ_STRING,
            cmd_type,
            expected_payload_size,
            cmd_args['data']
        )
        packed_command_buffer = protocol.pack_binary_command(cmd_type, cmd_args)
        assert packed_command_buffer == expected_command_buffer
 def test_packing_errors(self, cmd_type, cmd_args):
     with pytest.raises(ProtocolError):
         protocol.pack_binary_command(cmd_type=cmd_type, cmd_args=cmd_args)
 def no_job(self):
     """Job Server -> Worker"""
     data = protocol.pack_binary_command(protocol.GEARMAN_COMMAND_NO_JOB, {}, True)
     self._stream.write(data)
 def work_fail(self, job_handle):
     """Job Server -> Client"""
     buf = protocol.pack_binary_command(protocol.GEARMAN_COMMAND_WORK_FAIL, {'job_handle':job_handle}, True)
     self._stream.write(buf)
 def work_complete(self, job_handle, data):
     """Job Server -> Client"""
     buf = protocol.pack_binary_command(protocol.GEARMAN_COMMAND_WORK_COMPLETE, {'job_handle':job_handle, 'data':data}, True)
     self._stream.write(buf)
 def job_created(self, job_handle):
     """Job Server -> Client"""
     data = protocol.pack_binary_command(protocol.GEARMAN_COMMAND_JOB_CREATED, {'job_handle':job_handle}, True)
     self._stream.write(data)
 def job_assign_uniq(self, job_handle, task, unique, data):
     """Job Server -> Worker"""
     buf = protocol.pack_binary_command(protocol.GEARMAN_COMMAND_JOB_ASSIGN_UNIQ, {'job_handle':job_handle, 'task':task, 'unique':unique, 'data':data}, True)
     self._stream.write(buf)
 def noop(self, req):
     """Job Server -> Worker"""
     data = protocol.pack_binary_command(protocol.GEARMAN_COMMAND_NOOP, {}, True)
     self._stream.write(data)
     self.request = req