Esempio n. 1
0
    def test_multi_register(self):
        basic = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
        adv = util.parse_schema_from_string(data_gen.ADVANCED_SCHEMA)
        subject = 'test'
        client = self.client

        id1 = client.register(subject, basic)
        latest1 = client.get_latest_schema(subject)
        v1 = client.get_version(subject, basic)
        self.assertLatest(latest1, id1, basic, v1)

        id2 = client.register(subject, adv)
        latest2 = client.get_latest_schema(subject)
        v2 = client.get_version(subject, adv)
        self.assertLatest(latest2, id2, adv, v2)

        self.assertNotEqual(id1, id2)
        self.assertNotEqual(latest1, latest2)
        # ensure version is higher
        self.assertTrue(latest1[2] < latest2[2])

        client.register(subject, basic)
        latest3 = client.get_latest_schema(subject)
        # latest should not change with a re-reg
        self.assertEqual(latest2, latest3)
    def get_by_id(self, schema_id):
        """
        GET /schemas/ids/{int: id}
        Retrieve a parsed avro schema by id or None if not found
        @:param: schema_id: int value
        @:returns: Avro schema
        """
        if schema_id in self.id_to_schema:
            return self.id_to_schema[schema_id]
        # fetch from the registry
        url = '/'.join([self.url, 'schemas', 'ids', str(schema_id)])

        result, code = self._send_request(url)
        if code == 404:
            log.error("Schema not found:" + str(code))
            return None
        elif code != 200:
            log.error("Unable to get schema for the specific ID:" + str(code))
            return None
        else:
            # need to parse the schema
            schema_str = result.get("schema")
            try:
                result = util.parse_schema_from_string(schema_str)
                # cache it
                self._cache_schema(result, schema_id)
                return result
            except:
                # bad schema - should not happen
                log.error("Received bad schema from registry.")
                raise ClientError("Received bad schema from registry.")
Esempio n. 3
0
 def test_encode_record_with_schema(self):
     topic = 'test'
     basic = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
     subject = 'test-value'
     schema_id = self.client.register(subject, basic)
     records = data_gen.BASIC_ITEMS
     for record in records:
         message = self.ms.encode_record_with_schema(topic, basic, record)
         self.assertMessageIsSame(message, record, schema_id)
Esempio n. 4
0
    def test_multi_subject_register(self):
        parsed = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
        client = self.client
        schema_id = client.register('test', parsed)
        self.assertTrue(schema_id > 0)

        # register again under different subject
        dupe_id = client.register('other', parsed)
        self.assertEqual(schema_id, dupe_id)
        self.assertEqual(len(client.id_to_schema), 1)
Esempio n. 5
0
    def test_encode_with_schema_id(self):
        adv = util.parse_schema_from_string(data_gen.ADVANCED_SCHEMA)
        basic = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
        subject = 'test'
        schema_id = self.client.register(subject, basic)

        records = data_gen.BASIC_ITEMS
        for record in records:
            message = self.ms.encode_record_with_schema_id(schema_id, record)
            self.assertMessageIsSame(message, record, schema_id)

        subject = 'test_adv'
        adv_schema_id = self.client.register(subject, adv)
        self.assertNotEqual(adv_schema_id, schema_id)
        records = data_gen.ADVANCED_ITEMS
        for record in records:
            message = self.ms.encode_record_with_schema_id(
                adv_schema_id, record)
            self.assertMessageIsSame(message, record, adv_schema_id)
 def _get_schema_from_body(self, req):
     length = int(req.headers['content-length'])
     data = req.rfile.read(length)
     data = json.loads(data.decode("utf-8"))
     schema = data.get("schema", None)
     if not schema:
         return None
     try:
         avro_schema = util.parse_schema_from_string(schema)
         return self._get_identity_schema(avro_schema)
     except:
         return None
Esempio n. 7
0
    def test_dupe_register(self):
        parsed = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
        subject = 'test'
        client = self.client
        schema_id = client.register(subject, parsed)
        self.assertTrue(schema_id > 0)
        latest = client.get_latest_schema(subject)

        # register again under same subject
        dupe_id = client.register(subject, parsed)
        self.assertEqual(schema_id, dupe_id)
        dupe_latest = client.get_latest_schema(subject)
        self.assertEqual(latest, dupe_latest)
Esempio n. 8
0
    def test_getters(self):
        parsed = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
        client = self.client
        subject = 'test'
        version = client.get_version(subject, parsed)
        self.assertEqual(version, -1)
        schema = client.get_by_id(1)
        self.assertEqual(schema, None)
        latest = client.get_latest_schema(subject)
        self.assertEqual(latest, (None, None, None))

        # register
        schema_id = client.register(subject, parsed)
        latest = client.get_latest_schema(subject)
        version = client.get_version(subject, parsed)
        self.assertLatest(latest, schema_id, parsed, version)

        fetched = client.get_by_id(schema_id)
        self.assertEqual(fetched, parsed)
    def get_latest_schema(self, subject):
        """
        GET /subjects/(string: subject)/versions/(versionId: version)

        Return the latest 3-tuple of:
        (the schema id, the parsed avro schema, the schema version)
        for a particular subject.

        This call always contacts the registry.

        If the subject is not found, (None,None,None) is returned.
        @:param: subject: subject name
        @:returns: (schema_id, schema, version)
        """
        url = '/'.join([self.url, 'subjects', subject, 'versions', 'latest'])

        result, code = self._send_request(url)
        if code == 404:
            log.error("Schema not found:" + str(code))
            return (None, None, None)
        elif code == 422:
            log.error("Invalid version:" + str(code))
            return (None, None, None)
        elif not (code >= 200 and code <= 299):
            return (None, None, None)
        schema_id = result['id']
        version = result['version']
        if schema_id in self.id_to_schema:
            schema = self.id_to_schema[schema_id]
        else:
            try:
                schema = util.parse_schema_from_string(result['schema'])
            except:
                # bad schema - should not happen
                log.error("Received bad schema from registry.")
                raise ClientError("Received bad schema from registry.")

        self._cache_schema(schema, schema_id, subject, version)
        return (schema_id, schema, version)
Esempio n. 10
0
 def test_register(self):
     parsed = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
     client = self.client
     schema_id = client.register('test', parsed)
     self.assertTrue(schema_id > 0)
     self.assertEqual(len(client.id_to_schema), 1)
Esempio n. 11
0
 def test_schema_from_string(self):
     parsed = util.parse_schema_from_string(data_gen.BASIC_SCHEMA)
     self.assertTrue(isinstance(parsed, schema.Schema))