Debian Codegraph @

tests/schema_registry/_sync/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 import pytest
19
20 from concurrent.futures import ThreadPoolExecutor, wait
21
22 from confluent_kafka.schema_registry.common.schema_registry_client import SchemaVersion
23 from confluent_kafka.schema_registry.error import SchemaRegistryError
24 from confluent_kafka.schema_registry.schema_registry_client import Schema, \
25 SchemaRegistryClient
26 from tests.schema_registry.conftest import USERINFO, \
27 SCHEMA_ID, SCHEMA, SUBJECTS, COUNTER, VERSION, VERSIONS
28
29 """
30 Basic SchemaRegistryClient API functionality tests.
31
32 These tests cover the following criteria using the mock SchemaRegistryClient:
33 - Proper request/response handling:
34 The right data sent to the right place in the right format
35 - Error handling: (SR error codes are converted to a
36 SchemaRegistryError correctly)
37 - Caching: Caching of schema_ids and schemas works as expected.
38
39 See ./conftest.py for details on mock SchemaRegistryClient usage.
40 """
41 TEST_URL = 'http://SchemaRegistry:65534'
42 TEST_USERNAME = 'sr_user'
43 TEST_USER_PASSWORD = 'sr_user_secret'
44
45
46 def cmp_schema(schema1: Schema, schema2: Schema) -> bool:
47 """
48 Compare to Schemas for equivalence
49
50 Args:
51 schema1 (Schema): Schema instance to compare
52 schema2 (Schema): Schema instance to compare against
53
54 Returns:
55 bool: True if the schema's match else False
56
57 """
58 return all([schema1.schema_str == schema2.schema_str,
59 schema1.schema_type == schema2.schema_type])
60
61
62 def test_basic_auth_unauthorized(mock_schema_registry, load_avsc):
63 conf = {'url': TEST_URL,
64 'basic.auth.user.info': "user:secret"}
65 sr = SchemaRegistryClient(conf)
66
67 with pytest.raises(SchemaRegistryError, match="401 Unauthorized"):
68 sr.get_subjects()
69
70
71 def test_basic_auth_authorized(mock_schema_registry, load_avsc):
72 conf = {'url': TEST_URL,
73 'basic.auth.user.info': USERINFO}
74 sr = SchemaRegistryClient(conf)
75
76 result = sr.get_subjects()
77
78 assert result == SUBJECTS
79
80
81 def test_register_schema(mock_schema_registry, load_avsc):
82 conf = {'url': TEST_URL}
83 sr = SchemaRegistryClient(conf)
84 schema = Schema(load_avsc('basic_schema.avsc'), schema_type='AVRO')
85
86 result = sr.register_schema('test-key', schema)
87 assert result == SCHEMA_ID
88
89
90 def test_register_schema_full_response_recall(mock_schema_registry, load_avsc):
91 conf = {'url': TEST_URL}
92 sr = SchemaRegistryClient(conf)
93 schema = Schema(load_avsc('basic_schema.avsc'), schema_type='AVRO')
94
95 sr.register_schema('test-key', schema)
96
97 result = sr.register_schema_full_response('test-key', schema)
98 assert result.schema_id == SCHEMA_ID
99
100
101 def test_register_schema_incompatible(mock_schema_registry, load_avsc):
102 conf = {'url': TEST_URL}
103 sr = SchemaRegistryClient(conf)
104 schema = Schema(load_avsc('basic_schema.avsc'), schema_type='AVRO')
105
106 with pytest.raises(SchemaRegistryError, match="Incompatible Schema") as e:
107 sr.register_schema('conflict', schema)
108
109 assert e.value.http_status_code == 409
110 assert e.value.error_code == -1
111
112
113 def test_register_schema_invalid(mock_schema_registry, load_avsc):
114 conf = {'url': TEST_URL}
115 sr = SchemaRegistryClient(conf)
116 schema = Schema(load_avsc('invalid_schema.avsc'), schema_type='AVRO')
117
118 with pytest.raises(SchemaRegistryError, match="Invalid Schema") as e:
119 sr.register_schema('invalid', schema)
120
121 assert e.value.http_status_code == 422
122 assert e.value.error_code == 42201
123
124
125 def test_register_schema_cache(mock_schema_registry, load_avsc):
126 conf = {'url': TEST_URL}
127 sr = SchemaRegistryClient(conf)
128 schema = load_avsc('basic_schema.avsc')
129
130 count_before = COUNTER['POST'].get(
131 '/subjects/test-cache/versions', 0)
132
133 # Caching only starts after the first response is handled.
134 # A possible improvement would be to add request caching to the http client
135 # to catch in-flight requests as well.
136 sr.register_schema('test-cache', Schema(schema, 'AVRO'))
137
138 def register():
139 return sr.register_schema('test-cache', schema)
140
141 fs = []
142 with ThreadPoolExecutor(max_workers=10) as executor:
143 for _ in range(0, 1000):
144 fs.append(executor.submit(register))
145 wait(fs)
146
147 count_after = COUNTER['POST'].get(
148 '/subjects/test-cache/versions')
149
150 assert count_after - count_before == 1
151
152
153 def test_get_schema(mock_schema_registry, load_avsc):
154 conf = {'url': TEST_URL}
155 sr = SchemaRegistryClient(conf)
156
157 expected = Schema(load_avsc(SCHEMA), schema_type='AVRO')
158 actual = sr.get_schema(47)
159
160 assert cmp_schema(expected, actual)
161
162
163 def test_get_schema_not_found(mock_schema_registry):
164 conf = {'url': TEST_URL}
165 sr = SchemaRegistryClient(conf)
166
167 with pytest.raises(SchemaRegistryError, match="Schema not found") as e:
168 sr.get_schema(404)
169 assert e.value.http_status_code == 404
170 assert e.value.error_code == 40403
171
172
173 def test_get_schema_cache(mock_schema_registry):
174 conf = {'url': TEST_URL}
175 sr = SchemaRegistryClient(conf)
176
177 count_before = COUNTER['GET'].get(
178 '/schemas/ids/47', 0)
179
180 # Caching only starts after the first response is handled.
181 # A possible improvement would be to add request caching to the http client
182 # to catch in-flight requests as well.
183 sr.get_schema(47)
184
185 def get():
186 return sr.get_schema(47)
187
188 fs = []
189 with ThreadPoolExecutor(max_workers=10) as executor:
190 for _ in range(0, 1000):
191 fs.append(executor.submit(get))
192 wait(fs)
193
194 count_after = COUNTER['GET'].get(
195 '/schemas/ids/47')
196
197 assert count_after - count_before == 1
198
199
200 def test_get_schema_types(mock_schema_registry):
201 conf = {'url': TEST_URL}
202 sr = SchemaRegistryClient(conf)
203
204 expected = ['AVRO', 'JSON', 'PROTOBUF']
205 actual = sr.get_schema_types()
206 assert expected == actual
207
208
209 def test_get_subjects_by_schema_id(mock_schema_registry):
210 conf = {'url': TEST_URL}
211 sr = SchemaRegistryClient(conf)
212
213 expected = SUBJECTS
214 actual = sr.get_subjects_by_schema_id(47)
215 assert expected == actual
216
217
218 def test_get_schema_versions(mock_schema_registry):
219 conf = {'url': TEST_URL}
220 sr = SchemaRegistryClient(conf)
221
222 expected = [SchemaVersion(subject='subject1', version=1), SchemaVersion(subject='subject2', version=2)]
223 actual = sr.get_schema_versions(47)
224 assert expected == actual
225
226
227 def test_get_registration(mock_schema_registry, load_avsc):
228 conf = {'url': TEST_URL}
229 sr = SchemaRegistryClient(conf)
230
231 subject = 'get_registration'
232 schema = Schema(load_avsc(SCHEMA), schema_type='AVRO')
233
234 response = sr.lookup_schema(subject, schema)
235
236 assert response.subject == subject
237 assert response.version == VERSION
238 assert response.schema_id == SCHEMA_ID
239 assert cmp_schema(response.schema, schema)
240
241
242 def test_get_registration_subject_not_found(mock_schema_registry, load_avsc):
243 conf = {'url': TEST_URL}
244 sr = SchemaRegistryClient(conf)
245
246 subject = 'notfound'
247 schema = Schema(load_avsc(SCHEMA), schema_type='AVRO')
248
249 with pytest.raises(SchemaRegistryError, match="Subject not found") as e:
250 sr.lookup_schema(subject, schema)
251 assert e.value.http_status_code == 404
252 assert e.value.error_code == 40401
253
254
255 def test_get_registration_schema_not_found(mock_schema_registry, load_avsc):
256 conf = {'url': TEST_URL}
257 sr = SchemaRegistryClient(conf)
258
259 subject = 'schemanotfound'
260 schema = Schema(load_avsc(SCHEMA), schema_type='AVRO')
261
262 with pytest.raises(SchemaRegistryError, match="Schema not found") as e:
263 sr.lookup_schema(subject, schema)
264 assert e.value.http_status_code == 404
265 assert e.value.error_code == 40403
266
267
268 def test_get_subjects(mock_schema_registry):
269 conf = {'url': TEST_URL}
270 sr = SchemaRegistryClient(conf)
271
272 result = sr.get_subjects()
273
274 assert result == SUBJECTS
275
276
277 def test_delete(mock_schema_registry):
278 conf = {'url': TEST_URL}
279 sr = SchemaRegistryClient(conf)
280
281 result = sr.delete_subject("delete_subject")
282 assert result == VERSIONS
283
284
285 def test_delete_subject_not_found(mock_schema_registry):
286 conf = {'url': TEST_URL}
287 sr = SchemaRegistryClient(conf)
288
289 with pytest.raises(SchemaRegistryError, match="Subject not found") as e:
290 sr.delete_subject("notfound")
291 assert e.value.http_status_code == 404
292 assert e.value.error_code == 40401
293
294
295 def test_get_version(mock_schema_registry, load_avsc):
296 conf = {'url': TEST_URL}
297 sr = SchemaRegistryClient(conf)
298
299 subject = "get_version"
300 version = 3
301 schema = Schema(load_avsc(SCHEMA), schema_type='AVRO')
302
303 result = sr.get_version(subject, version)
304 assert result.subject == subject
305 assert result.version == version
306 assert cmp_schema(result.schema, schema)
307 assert result.schema_id == SCHEMA_ID
308
309
310 def test_get_version_no_version(mock_schema_registry):
311 conf = {'url': TEST_URL}
312 sr = SchemaRegistryClient(conf)
313
314 subject = "get_version"
315 version = 404
316
317 with pytest.raises(SchemaRegistryError, match="Version not found") as e:
318 sr.get_version(subject, version)
319 assert e.value.http_status_code == 404
320 assert e.value.error_code == 40402
321
322
323 def test_get_version_invalid(mock_schema_registry):
324 conf = {'url': TEST_URL}
325 sr = SchemaRegistryClient(conf)
326
327 subject = "get_version"
328 version = 422
329
330 with pytest.raises(SchemaRegistryError, match="Invalid version") as e:
331 sr.get_version(subject, version)
332 assert e.value.http_status_code == 422
333 assert e.value.error_code == 42202
334
335
336 def test_get_version_subject_not_found(mock_schema_registry):
337 conf = {'url': TEST_URL}
338 sr = SchemaRegistryClient(conf)
339
340 subject = "notfound"
341 version = 3
342
343 with pytest.raises(SchemaRegistryError, match="Subject not found") as e:
344 sr.get_version(subject, version)
345 assert e.value.http_status_code == 404
346 assert e.value.error_code == 40401
347
348
349 def test_delete_version(mock_schema_registry):
350 conf = {'url': TEST_URL}
351 sr = SchemaRegistryClient(conf)
352
353 result = sr.delete_version("delete_version", 3)
354
355 assert result == 3
356
357
358 def test_delete_version_not_found(mock_schema_registry):
359 conf = {'url': TEST_URL}
360 sr = SchemaRegistryClient(conf)
361
362 with pytest.raises(SchemaRegistryError, match="Version not found") as e:
363 sr.delete_version("delete_version", 404)
364 assert e.value.http_status_code == 404
365 assert e.value.error_code == 40402
366
367
368 def test_delete_version_subject_not_found(mock_schema_registry):
369 conf = {'url': TEST_URL}
370 sr = SchemaRegistryClient(conf)
371
372 with pytest.raises(SchemaRegistryError, match="Subject not found") as e:
373 sr.delete_version("notfound", 3)
374 assert e.value.http_status_code == 404
375 assert e.value.error_code == 40401
376
377
378 def test_delete_version_invalid(mock_schema_registry):
379 conf = {'url': TEST_URL}
380 sr = SchemaRegistryClient(conf)
381
382 with pytest.raises(SchemaRegistryError, match="Invalid version") as e:
383 sr.delete_version("invalid_version", 422)
384 assert e.value.http_status_code == 422
385 assert e.value.error_code == 42202
386
387
388 def test_get_referenced_by(mock_schema_registry):
389 conf = {'url': TEST_URL}
390 sr = SchemaRegistryClient(conf)
391
392 assert sr.get_referenced_by("get_version", 3) == [1, 2]
393
394
395 def test_set_compatibility(mock_schema_registry):
396 conf = {'url': TEST_URL}
397 sr = SchemaRegistryClient(conf)
398
399 result = sr.set_compatibility(level="FULL")
400 assert result == {'compatibility': 'FULL'}
401
402
403 def test_set_compatibility_invalid(mock_schema_registry):
404 conf = {'url': TEST_URL}
405 sr = SchemaRegistryClient(conf)
406 with pytest.raises(SchemaRegistryError, match="Invalid compatibility level") as e:
407 sr.set_compatibility(level="INVALID")
408 e.value.http_status_code = 422
409 e.value.error_code = 42203
410
411
412 def test_delete_config(mock_schema_registry):
413 conf = {'url': TEST_URL}
414 sr = SchemaRegistryClient(conf)
415
416 result = sr.delete_config()
417 assert result.compatibility == 'FULL'
418
419
420 def test_delete_config_subject_not_found(mock_schema_registry):
421 conf = {'url': TEST_URL}
422 sr = SchemaRegistryClient(conf)
423
424 with pytest.raises(SchemaRegistryError, match="Subject not found") as e:
425 sr.delete_config("notfound")
426 assert e.value.http_status_code == 404
427 assert e.value.error_code == 40401
428
429
430 def test_get_compatibility_subject_not_found(mock_schema_registry):
431 conf = {'url': TEST_URL}
432 sr = SchemaRegistryClient(conf)
433
434 with pytest.raises(SchemaRegistryError, match="Subject not found") as e:
435 sr.get_compatibility("notfound")
436 assert e.value.http_status_code == 404
437 assert e.value.error_code == 40401
438
439
440 def test_get_contexts(mock_schema_registry):
441 conf = {'url': TEST_URL}
442 sr = SchemaRegistryClient(conf)
443
444 result = sr.get_contexts()
445 assert result == ['context1', 'context2']
446
447
448 def test_schema_equivilence(load_avsc):
449 schema_str1 = load_avsc('basic_schema.avsc')
450 schema_str2 = load_avsc('basic_schema.avsc')
451
452 schema = Schema(schema_str1, 'AVRO')
453 schema2 = Schema(schema_str2, 'AVRO')
454
455 assert schema.__eq__(schema2)
456 assert schema == schema2
457 assert schema_str1.__eq__(schema_str2)
458 assert schema_str1 == schema_str2
459
460
461 @pytest.mark.parametrize(
462 'subject_name,version,expected_compatibility',
463 [
464 ('conflict', 'latest', False),
465 ('conflict', 1, False),
466 ('test-key', 'latest', True),
467 ('test-key', 1, True),
468 ]
469 )
470 def test_test_compatibility_no_error(
471 mock_schema_registry, load_avsc, subject_name, version, expected_compatibility
472 ):
473 conf = {'url': TEST_URL}
474 sr = SchemaRegistryClient(conf)
475 schema = Schema(load_avsc('basic_schema.avsc'), schema_type='AVRO')
476
477 is_compatible = sr.test_compatibility(subject_name, schema)
478 assert is_compatible is expected_compatibility
479
480
481 @pytest.mark.parametrize(
482 'subject_name,version,match_str,status_code,error_code',
483 [
484 ('notfound', 'latest', 'Subject not found', 404, 40401),
485 ('invalid', 'latest', 'Invalid Schema', 422, 42201),
486 ('invalid', '422', 'Invalid version', 422, 42202),
487 ('notfound', 404, 'Version not found', 404, 40402),
488 ]
489 )
490 def test_test_compatibility_with_error(
491 mock_schema_registry, load_avsc, subject_name, version, match_str, status_code, error_code
492 ):
493 conf = {'url': TEST_URL}
494 sr = SchemaRegistryClient(conf)
495 schema = Schema(load_avsc('basic_schema.avsc'), schema_type='AVRO')
496
497 with pytest.raises(SchemaRegistryError, match=match_str) as e:
498 sr.test_compatibility(subject_name, schema, version)
499 assert e.value.http_status_code == status_code
500 assert e.value.error_code == error_code
501
502
503 def test_test_compatibility_all_versions_no_error(mock_schema_registry, load_avsc):
504 conf = {'url': TEST_URL}
505 sr = SchemaRegistryClient(conf)
506 schema = Schema(load_avsc('basic_schema.avsc'), schema_type='AVRO')
507
508 is_compatible = sr.test_compatibility_all_versions('subject-all-versions', schema)
509 assert is_compatible is True
510
511
512 def test_get_global_mode(mock_schema_registry):
513 conf = {'url': TEST_URL}
514 sr = SchemaRegistryClient(conf)
515
516 mode = sr.get_global_mode()
517 assert mode == 'READWRITE'
518
519
520 def test_set_global_mode(mock_schema_registry):
521 conf = {'url': TEST_URL}
522 sr = SchemaRegistryClient(conf)
523
524 result = sr.update_global_mode('READONLY')
525 assert result == 'READONLY'
526
527
528 def test_get_mode(mock_schema_registry):
529 conf = {'url': TEST_URL}
530 sr = SchemaRegistryClient(conf)
531
532 result = sr.get_mode('test-key')
533 assert result == 'READWRITE'
534
535
536 @pytest.mark.parametrize(
537 'subject_name,match_str,status_code,error_code',
538 [
539 ('invalid_mode', 'Invalid mode', 422, 42204),
540 ('operation_not_permitted', 'Operation not permitted', 422, 42205),
541 ]
542 )
543 def test_update_mode_with_error(mock_schema_registry, subject_name, match_str, status_code, error_code):
544 conf = {'url': TEST_URL}
545 sr = SchemaRegistryClient(conf)
546
547 with pytest.raises(SchemaRegistryError, match=match_str) as e:
548 sr.update_mode(subject_name, 'READONLY')
549 assert e.value.http_status_code == status_code
550 assert e.value.error_code == error_code
551
552
553 def test_update_mode(mock_schema_registry):
554 conf = {'url': TEST_URL}
555 sr = SchemaRegistryClient(conf)
556
557 result = sr.update_mode('test-key', 'READONLY')
558 assert result == 'READONLY'
559
560
561 def test_delete_mode(mock_schema_registry):
562 conf = {'url': TEST_URL}
563 sr = SchemaRegistryClient(conf)
564
565 result = sr.delete_mode('test-key')
566 assert result == 'READWRITE'