Debian Codegraph @

tests/integration/schema_registry/_async/test_api_client.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2020 Confluent Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 from uuid import uuid1
19
20 import pytest
21
22 from confluent_kafka.schema_registry import Schema
23 from confluent_kafka.schema_registry.error import SchemaRegistryError
24 from tests.integration.conftest import kafka_cluster_fixture
25
26
27 @pytest.fixture(scope="module")
28 def kafka_cluster_cp_7_0_1():
29 """
30 Returns a Trivup cluster with CP version 7.0.1.
31 SR version 7.0.1 is the last returning 500 instead of 422
32 for the invalid schema passed to test_api_get_register_schema_invalid
33 """
34 for fixture in kafka_cluster_fixture(
35 brokers_env="BROKERS_7_0_1",
36 sr_url_env="SR_URL_7_0_1",
37 trivup_cluster_conf={'cp_version': '7.0.1'}
38 ):
39 yield fixture
40
41
42 def _subject_name(prefix):
43 return prefix + "-" + str(uuid1())
44
45
46 async def test_api_register_schema(kafka_cluster, load_file):
47 """
48 Registers a schema, verifies the registration
49
50 Args:
51 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
52 load_file (callable(str)): Schema fixture constructor
53
54 """
55 sr = kafka_cluster.async_schema_registry()
56 avsc = 'basic_schema.avsc'
57 subject = _subject_name(avsc)
58 schema = Schema(load_file(avsc), schema_type='AVRO')
59
60 schema_id = await sr.register_schema(subject, schema)
61 registered_schema = await sr.lookup_schema(subject, schema)
62
63 assert registered_schema.schema_id == schema_id
64 assert registered_schema.subject == subject
65 assert schema.schema_str, registered_schema.schema.schema_str
66
67
68 async def test_api_register_normalized_schema(kafka_cluster, load_file):
69 """
70 Registers a schema, verifies the registration
71
72 Args:
73 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
74 load_file (callable(str)): Schema fixture constructor
75
76 """
77 sr = kafka_cluster.async_schema_registry()
78 avsc = 'basic_schema.avsc'
79 subject = _subject_name(avsc)
80 schema = Schema(load_file(avsc), schema_type='AVRO')
81
82 schema_id = await sr.register_schema(subject, schema, normalize_schemas=True)
83 registered_schema = await sr.lookup_schema(subject, schema, normalize_schemas=True)
84
85 assert registered_schema.schema_id == schema_id
86 assert registered_schema.subject == subject
87 assert schema.schema_str, registered_schema.schema.schema_str
88
89
90 async def test_api_register_schema_incompatible(kafka_cluster, load_file):
91 """
92 Attempts to register an incompatible Schema verifies the error.
93
94 Args:
95 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
96 load_file (callable(str)): Schema fixture constructor
97
98 """
99 sr = kafka_cluster.async_schema_registry()
100 schema1 = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
101 schema2 = Schema(load_file('adv_schema.avsc'), schema_type='AVRO')
102 subject = _subject_name('test_register_incompatible')
103
104 await sr.register_schema(subject, schema1)
105
106 with pytest.raises(SchemaRegistryError, match="Schema being registered is"
107 " incompatible with an"
108 " earlier schema") as e:
109 # The default Schema Registry compatibility type is BACKWARD.
110 # this allows 1) fields to be deleted, 2) optional fields to
111 # be added. schema2 adds non-optional fields to schema1, so
112 # registering schema2 after schema1 should fail.
113 await sr.register_schema(subject, schema2)
114 assert e.value.http_status_code == 409 # conflict
115 assert e.value.error_code == 409
116
117
118 async def test_api_register_schema_invalid(kafka_cluster, load_file):
119 """
120 Attempts to register an invalid schema, validates the error.
121
122 Args:
123 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
124 load_file (callable(str)): Schema fixture constructor
125
126 """
127 sr = kafka_cluster.async_schema_registry()
128 schema = Schema(load_file('invalid_schema.avsc'), schema_type='AVRO')
129 subject = _subject_name('test_invalid_schema')
130
131 with pytest.raises(SchemaRegistryError) as e:
132 await sr.register_schema(subject, schema)
133 assert e.value.http_status_code == 422
134 assert e.value.error_code == 42201
135
136
137 async def test_api_get_schema(kafka_cluster, load_file):
138 """
139 Registers a schema then retrieves it using the schema id returned by the
140 call to register the Schema.
141
142 Args:
143 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
144 load_file (callable(str)): Schema fixture constructor
145
146 """
147 sr = kafka_cluster.async_schema_registry()
148 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
149 subject = _subject_name('get_schema')
150
151 schema_id = await sr.register_schema(subject, schema)
152 registration = await sr.lookup_schema(subject, schema)
153
154 assert registration.schema_id == schema_id
155 assert registration.subject == subject
156 assert schema.schema_str, registration.schema.schema_str
157
158
159 async def test_api_get_schema_not_found(kafka_cluster, load_file):
160 """
161 Attempts to fetch an unknown schema by id, validates the error.
162
163 Args:
164 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
165 load_file (callable(str)): Schema fixture constructor
166
167 """
168 sr = kafka_cluster.async_schema_registry()
169
170 with pytest.raises(SchemaRegistryError, match="Schema .*not found.*") as e:
171 await sr.get_schema(999999)
172
173 assert e.value.http_status_code == 404
174 assert e.value.error_code == 40403
175
176
177 async def test_api_get_registration_subject_not_found(kafka_cluster, load_file):
178 """
179 Attempts to obtain information about a schema's subject registration for
180 an unknown subject.
181
182 Args:
183 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
184 load_file (callable(str)): Schema fixture constructor
185
186 """
187 sr = kafka_cluster.async_schema_registry()
188 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
189
190 subject = _subject_name("registration_subject_not_found")
191
192 with pytest.raises(SchemaRegistryError, match="Subject .*not found.*") as e:
193 await sr.lookup_schema(subject, schema)
194 assert e.value.http_status_code == 404
195 assert e.value.error_code == 40401
196
197
198 @pytest.mark.parametrize("kafka_cluster_name, http_status_code, error_code", [
199 ["kafka_cluster_cp_7_0_1", 500, 500],
200 ["kafka_cluster", 422, 42201],
201 ])
202 async def test_api_get_register_schema_invalid(
203 kafka_cluster_name,
204 http_status_code,
205 error_code,
206 load_file,
207 request):
208 """
209 Attempts to obtain registration information with an invalid schema
210 with different CP versions.
211
212 Args:
213 kafka_cluster_name (str): name of the Kafka Cluster fixture to use
214 http_status_code (int): HTTP status return code expected in this version
215 error_code (int): error code expected in this version
216 load_file (callable(str)): Schema fixture constructor
217 request (FixtureRequest): PyTest object giving access to the test context
218 """
219 kafka_cluster = request.getfixturevalue(kafka_cluster_name)
220 sr = kafka_cluster.async_schema_registry()
221 subject = _subject_name("registration_invalid_schema")
222 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
223
224 # register valid schema so we don't hit subject not found exception
225 await sr.register_schema(subject, schema)
226 schema2 = Schema(load_file('invalid_schema.avsc'), schema_type='AVRO')
227
228 with pytest.raises(SchemaRegistryError, match="Invalid schema") as e:
229 await sr.lookup_schema(subject, schema2)
230
231 assert e.value.http_status_code == http_status_code
232 assert e.value.error_code == error_code
233
234
235 async def test_api_get_subjects(kafka_cluster, load_file):
236 """
237 Populates KafkaClusterFixture SR instance with a fixed number of subjects
238 then verifies the response includes them all.
239
240 Args:
241 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
242 load_file (callable(str)): Schema fixture constructor
243
244 """
245 sr = kafka_cluster.async_schema_registry()
246
247 avscs = ['basic_schema.avsc', 'primitive_string.avsc',
248 'primitive_bool.avsc', 'primitive_float.avsc']
249
250 subjects = []
251 for avsc in avscs:
252 schema = Schema(load_file(avsc), schema_type='AVRO')
253 subject = _subject_name(avsc)
254 subjects.append(subject)
255
256 await sr.register_schema(subject, schema)
257
258 registered = await sr.get_subjects()
259
260 assert all([s in registered for s in subjects])
261
262
263 async def test_api_get_subject_versions(kafka_cluster, load_file):
264 """
265 Registers a Schema with a subject, lists the versions associated with that
266 subject and ensures the versions and their schemas match what was
267 registered.
268
269 Args:
270 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
271 load_file (callable(str)): Schema fixture constructor.
272
273 """
274 sr = kafka_cluster.async_schema_registry()
275
276 subject = _subject_name("list-version-test")
277 await sr.set_compatibility(level="NONE")
278
279 avscs = ['basic_schema.avsc', 'primitive_string.avsc',
280 'primitive_bool.avsc', 'primitive_float.avsc']
281
282 schemas = []
283 for avsc in avscs:
284 schema = Schema(load_file(avsc), schema_type='AVRO')
285 schemas.append(schema)
286 await sr.register_schema(subject, schema)
287
288 versions = await sr.get_versions(subject)
289 assert len(versions) == len(avscs)
290 for schema in schemas:
291 registered_schema = await sr.lookup_schema(subject, schema)
292 assert registered_schema.subject == subject
293 assert registered_schema.version in versions
294
295 # revert global compatibility level back to the default.
296 await sr.set_compatibility(level="BACKWARD")
297
298
299 async def test_api_delete_subject(kafka_cluster, load_file):
300 """
301 Registers a Schema under a specific subject then deletes it.
302
303 Args:
304 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
305 load_file (callable(str)): Schema fixture constructor
306
307 """
308 sr = kafka_cluster.async_schema_registry()
309
310 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
311 subject = _subject_name("test-delete")
312
313 await sr.register_schema(subject, schema)
314 assert subject in await sr.get_subjects()
315
316 await sr.delete_subject(subject)
317 assert subject not in await sr.get_subjects()
318
319
320 async def test_api_delete_subject_not_found(kafka_cluster):
321 sr = kafka_cluster.async_schema_registry()
322
323 subject = _subject_name("test-delete_invalid_subject")
324
325 with pytest.raises(SchemaRegistryError, match="Subject .*not found.*") as e:
326 await sr.delete_subject(subject)
327 assert e.value.http_status_code == 404
328 assert e.value.error_code == 40401
329
330
331 async def test_api_get_subject_version(kafka_cluster, load_file):
332 """
333 Registers a schema, fetches that schema by it's subject version id.
334
335 Args:
336 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
337 load_file (callable(str)): Schema fixture constructor
338
339 """
340 sr = kafka_cluster.async_schema_registry()
341
342 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
343 subject = _subject_name('test-get_subject')
344
345 await sr.register_schema(subject, schema)
346 registered_schema = await sr.lookup_schema(subject, schema)
347 registered_schema2 = await sr.get_version(subject, registered_schema.version)
348
349 assert registered_schema2.schema_id == registered_schema.schema_id
350 assert registered_schema2.schema.schema_str == registered_schema.schema.schema_str
351 assert registered_schema2.version == registered_schema.version
352
353
354 async def test_api_get_subject_version_no_version(kafka_cluster, load_file):
355 sr = kafka_cluster.async_schema_registry()
356
357 # ensures subject exists and has a single version
358 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
359 subject = _subject_name('test-get_subject')
360 await sr.register_schema(subject, schema)
361
362 with pytest.raises(SchemaRegistryError, match="Version .*not found") as e:
363 await sr.get_version(subject, version=3)
364 assert e.value.http_status_code == 404
365 assert e.value.error_code == 40402
366
367
368 async def test_api_get_subject_version_invalid(kafka_cluster, load_file):
369 sr = kafka_cluster.async_schema_registry()
370
371 # ensures subject exists and has a single version
372 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
373 subject = _subject_name('test-get_subject')
374 await sr.register_schema(subject, schema)
375
376 with pytest.raises(SchemaRegistryError,
377 match="The specified version .*is not"
378 " a valid version id.*") as e:
379 await sr.get_version(subject, version='a')
380 assert e.value.http_status_code == 422
381 assert e.value.error_code == 42202
382
383
384 async def test_api_post_subject_registration(kafka_cluster, load_file):
385 """
386 Registers a schema, fetches that schema by it's subject version id.
387
388 Args:
389 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
390 load_file (callable(str)): Schema fixture constructor
391
392 """
393 sr = kafka_cluster.async_schema_registry()
394
395 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
396 subject = _subject_name('test_registration')
397
398 schema_id = await sr.register_schema(subject, schema)
399 registered_schema = await sr.lookup_schema(subject, schema)
400
401 assert registered_schema.schema_id == schema_id
402 assert registered_schema.subject == subject
403
404
405 async def test_api_delete_subject_version(kafka_cluster, load_file):
406 """
407 Registers a Schema under a specific subject then deletes it.
408
409 Args:
410 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
411 load_file (callable(str)): Schema fixture constructor
412
413 """
414 sr = kafka_cluster.async_schema_registry()
415
416 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
417 subject = str(uuid1())
418
419 await sr.register_schema(subject, schema)
420 await sr.delete_version(subject, 1)
421
422 assert subject not in await sr.get_subjects()
423
424
425 async def test_api_subject_config_update(kafka_cluster, load_file):
426 """
427 Updates a subjects compatibility policy then ensures the same policy
428 is returned when queried.
429
430 Args:
431 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
432 load_file (callable(str)): Schema fixture constructor
433
434 """
435 sr = kafka_cluster.async_schema_registry()
436
437 schema = Schema(load_file('basic_schema.avsc'), schema_type='AVRO')
438 subject = str(uuid1())
439
440 await sr.register_schema(subject, schema)
441 await sr.set_compatibility(
442 subject_name=subject,
443 level="FULL_TRANSITIVE"
444 )
445
446 assert await sr.get_compatibility(subject_name=subject) == "FULL_TRANSITIVE"
447
448
449 async def test_api_config_invalid(kafka_cluster):
450 """
451 Sets an invalid compatibility level, validates the exception.
452
453 Args:
454 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
455 """
456 sr = kafka_cluster.async_schema_registry()
457
458 with pytest.raises(SchemaRegistryError, match="Invalid compatibility"
459 " level") as e:
460 await sr.set_compatibility(level="INVALID")
461 e.value.http_status_code = 422
462 e.value.error_code = 42203
463
464
465 async def test_api_config_update(kafka_cluster):
466 """
467 Updates the global compatibility policy then ensures the same policy
468 is returned when queried.
469
470 Args:
471 kafka_cluster (KafkaClusterFixture): Kafka Cluster fixture
472 """
473 sr = kafka_cluster.async_schema_registry()
474
475 for level in ["BACKWARD", "BACKWARD_TRANSITIVE", "FORWARD", "FORWARD_TRANSITIVE"]:
476 await sr.set_compatibility(level=level)
477 assert await sr.get_compatibility() == level
478
479 # revert global compatibility level back to the default.
480 await sr.set_compatibility(level="BACKWARD")
481
482
483 async def test_api_register_logical_schema(kafka_cluster, load_file):
484 sr = kafka_cluster.async_schema_registry()
485
486 schema = Schema(load_file('logical_date.avsc'), schema_type='AVRO')
487 subject = _subject_name('test_logical_registration')
488
489 schema_id = await sr.register_schema(subject, schema)
490 registered_schema = await sr.lookup_schema(subject, schema)
491
492 assert registered_schema.schema_id == schema_id
493 assert registered_schema.subject == subject