|
4 | 4 | Copyright (c) 2023 Aiven Ltd
|
5 | 5 | See LICENSE for details
|
6 | 6 | """
|
| 7 | +from attr import dataclass |
7 | 8 | from http import HTTPStatus
|
8 | 9 | from karapace.client import Client
|
9 | 10 | from karapace.kafka.producer import KafkaProducer
|
10 | 11 | from karapace.rapu import is_success
|
11 | 12 | from karapace.schema_registry_apis import SchemaErrorMessages
|
| 13 | +from karapace.schema_type import SchemaType |
12 | 14 | from karapace.utils import json_encode
|
| 15 | +from tests.base_testcase import BaseTestCase |
13 | 16 | from tests.integration.utils.cluster import RegistryDescription
|
14 | 17 | from tests.integration.utils.kafka_server import KafkaServers
|
15 | 18 | from tests.utils import (
|
@@ -1079,85 +1082,159 @@ async def assert_schema_versions_failed(client: Client, trail: str, schema_id: i
|
1079 | 1082 | assert res.status_code == response_code
|
1080 | 1083 |
|
1081 | 1084 |
|
1082 |
| -async def register_schema(registry_async_client: Client, trail, subject: str, schema_str: str) -> Tuple[int, int]: |
| 1085 | +async def register_schema( |
| 1086 | + registry_async_client: Client, trail: str, subject: str, schema_str: str, schema_type: SchemaType = SchemaType.AVRO |
| 1087 | +) -> Tuple[int, int]: |
1083 | 1088 | # Register to get the id
|
| 1089 | + payload = {"schema": schema_str} |
| 1090 | + if schema_type == SchemaType.JSONSCHEMA: |
| 1091 | + payload["schemaType"] = "JSON" |
| 1092 | + elif schema_type == SchemaType.PROTOBUF: |
| 1093 | + payload["schemaType"] = "PROTO" |
| 1094 | + else: |
| 1095 | + pass |
1084 | 1096 | res = await registry_async_client.post(
|
1085 | 1097 | f"subjects/{subject}/versions{trail}",
|
1086 |
| - json={"schema": schema_str}, |
| 1098 | + json=payload, |
1087 | 1099 | )
|
1088 | 1100 | assert res.status_code == 200
|
1089 | 1101 | schema_id = res.json()["id"]
|
1090 | 1102 |
|
1091 | 1103 | # Get version
|
1092 |
| - res = await registry_async_client.post( |
1093 |
| - f"subjects/{subject}{trail}", |
1094 |
| - json={"schema": schema_str}, |
1095 |
| - ) |
| 1104 | + res = await registry_async_client.post(f"subjects/{subject}{trail}", json=payload) |
1096 | 1105 | assert res.status_code == 200
|
1097 | 1106 | assert res.json()["id"] == schema_id
|
1098 | 1107 | return schema_id, res.json()["version"]
|
1099 | 1108 |
|
1100 | 1109 |
|
1101 |
| -@pytest.mark.parametrize("trail", ["", "/"]) |
1102 |
| -async def test_schema_versions_multiple_subjects_same_schema(registry_async_client: Client, trail: str) -> None: |
| 1110 | +@dataclass |
| 1111 | +class MultipleSubjectsSameSchemaTestCase(BaseTestCase): |
| 1112 | + test_name: str |
| 1113 | + schema: str |
| 1114 | + other_schema: str |
| 1115 | + schema_type: SchemaType |
| 1116 | + |
| 1117 | + |
| 1118 | +@pytest.mark.parametrize( |
| 1119 | + "testcase", |
| 1120 | + [ |
| 1121 | + MultipleSubjectsSameSchemaTestCase( |
| 1122 | + test_name="Test same AVRO schema on multiple subjects", |
| 1123 | + schema=json.dumps( |
| 1124 | + { |
| 1125 | + "type": "record", |
| 1126 | + "name": "SimpleTestSchema", |
| 1127 | + "fields": [ |
| 1128 | + { |
| 1129 | + "name": "f1", |
| 1130 | + "type": "string", |
| 1131 | + }, |
| 1132 | + { |
| 1133 | + "name": "f2", |
| 1134 | + "type": "string", |
| 1135 | + }, |
| 1136 | + ], |
| 1137 | + }, |
| 1138 | + ), |
| 1139 | + other_schema=json.dumps( |
| 1140 | + { |
| 1141 | + "type": "record", |
| 1142 | + "name": "SimpleOtherTestSchema", |
| 1143 | + "fields": [ |
| 1144 | + { |
| 1145 | + "name": "f1", |
| 1146 | + "type": "string", |
| 1147 | + }, |
| 1148 | + ], |
| 1149 | + }, |
| 1150 | + ), |
| 1151 | + schema_type=SchemaType.AVRO, |
| 1152 | + ), |
| 1153 | + MultipleSubjectsSameSchemaTestCase( |
| 1154 | + test_name="Test same JSON schema on multiple subjects", |
| 1155 | + schema=json.dumps( |
| 1156 | + { |
| 1157 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 1158 | + "$id": "https://example.com/product.schema.json", |
| 1159 | + "title": "SimpleTest", |
| 1160 | + "description": "Test JSON schema", |
| 1161 | + "type": "object", |
| 1162 | + "properties": { |
| 1163 | + "f1": { |
| 1164 | + "type": "string", |
| 1165 | + }, |
| 1166 | + "f2": { |
| 1167 | + "type": "string", |
| 1168 | + }, |
| 1169 | + }, |
| 1170 | + }, |
| 1171 | + ), |
| 1172 | + other_schema=json.dumps( |
| 1173 | + { |
| 1174 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 1175 | + "$id": "https://example.com/product.schema.json", |
| 1176 | + "title": "SimpleTestOtherSchema", |
| 1177 | + "description": "Test JSON schema", |
| 1178 | + "type": "object", |
| 1179 | + "properties": { |
| 1180 | + "other_schema_field": { |
| 1181 | + "type": "integer", |
| 1182 | + }, |
| 1183 | + }, |
| 1184 | + } |
| 1185 | + ), |
| 1186 | + schema_type=SchemaType.JSONSCHEMA, |
| 1187 | + ), |
| 1188 | + ], |
| 1189 | +) |
| 1190 | +async def test_schema_versions_multiple_subjects_same_schema( |
| 1191 | + registry_async_client: Client, |
| 1192 | + testcase: MultipleSubjectsSameSchemaTestCase, |
| 1193 | +) -> None: |
1103 | 1194 | """
|
1104 | 1195 | Tests case where there are multiple subjects with the same schema.
|
1105 | 1196 | The schema/versions endpoint returns all these subjects.
|
1106 | 1197 | """
|
1107 |
| - subject_name_factory = create_subject_name_factory(f"test_schema_versions_multiple_subjects_same_schema-{trail}") |
1108 |
| - schema_name_factory = create_schema_name_factory(f"test_schema_versions_multiple_subjects_same_schema_{trail}") |
1109 |
| - |
1110 |
| - schema_1 = { |
1111 |
| - "type": "record", |
1112 |
| - "name": schema_name_factory(), |
1113 |
| - "fields": [ |
1114 |
| - { |
1115 |
| - "name": "f1", |
1116 |
| - "type": "string", |
1117 |
| - }, |
1118 |
| - { |
1119 |
| - "name": "f2", |
1120 |
| - "type": "string", |
1121 |
| - }, |
1122 |
| - ], |
1123 |
| - } |
1124 |
| - schema_str_1 = json.dumps(schema_1) |
1125 |
| - schema_2 = { |
1126 |
| - "type": "record", |
1127 |
| - "name": schema_name_factory(), |
1128 |
| - "fields": [ |
1129 |
| - { |
1130 |
| - "name": "f1", |
1131 |
| - "type": "string", |
1132 |
| - } |
1133 |
| - ], |
1134 |
| - } |
1135 |
| - schema_str_2 = json.dumps(schema_2) |
| 1198 | + subject_name_factory = create_subject_name_factory( |
| 1199 | + f"test_schema_versions_multiple_subjects_same_schema-{testcase.schema_type}" |
| 1200 | + ) |
1136 | 1201 |
|
1137 | 1202 | subject_1 = subject_name_factory()
|
1138 |
| - schema_id_1, version_1 = await register_schema(registry_async_client, trail, subject_1, schema_str_1) |
| 1203 | + schema_id_1, version_1 = await register_schema( |
| 1204 | + registry_async_client, "", subject_1, testcase.schema, schema_type=testcase.schema_type |
| 1205 | + ) |
1139 | 1206 | schema_1_versions = [(subject_1, version_1)]
|
1140 |
| - await assert_schema_versions(registry_async_client, trail, schema_id_1, schema_1_versions) |
| 1207 | + await assert_schema_versions(registry_async_client, "", schema_id_1, schema_1_versions) |
1141 | 1208 |
|
1142 | 1209 | subject_2 = subject_name_factory()
|
1143 |
| - schema_id_2, version_2 = await register_schema(registry_async_client, trail, subject_2, schema_str_1) |
| 1210 | + schema_id_2, version_2 = await register_schema( |
| 1211 | + registry_async_client, "", subject_2, testcase.schema, schema_type=testcase.schema_type |
| 1212 | + ) |
1144 | 1213 | schema_1_versions = [(subject_1, version_1), (subject_2, version_2)]
|
1145 | 1214 | assert schema_id_1 == schema_id_2
|
1146 |
| - await assert_schema_versions(registry_async_client, trail, schema_id_1, schema_1_versions) |
| 1215 | + await assert_schema_versions(registry_async_client, "", schema_id_1, schema_1_versions) |
1147 | 1216 |
|
1148 | 1217 | subject_3 = subject_name_factory()
|
1149 |
| - schema_id_3, version_3 = await register_schema(registry_async_client, trail, subject_3, schema_str_1) |
| 1218 | + schema_id_3, version_3 = await register_schema( |
| 1219 | + registry_async_client, "", subject_3, testcase.schema, schema_type=testcase.schema_type |
| 1220 | + ) |
1150 | 1221 | schema_1_versions = [(subject_1, version_1), (subject_2, version_2), (subject_3, version_3)]
|
1151 | 1222 | assert schema_id_1 == schema_id_3
|
1152 |
| - await assert_schema_versions(registry_async_client, trail, schema_id_1, schema_1_versions) |
| 1223 | + await assert_schema_versions(registry_async_client, "", schema_id_1, schema_1_versions) |
1153 | 1224 |
|
1154 | 1225 | # subject_4 with different schema to check there are no side effects
|
1155 | 1226 | subject_4 = subject_name_factory()
|
1156 |
| - schema_id_4, version_4 = await register_schema(registry_async_client, trail, subject_4, schema_str_2) |
| 1227 | + schema_id_4, version_4 = await register_schema( |
| 1228 | + registry_async_client, "", subject_4, testcase.other_schema, schema_type=testcase.schema_type |
| 1229 | + ) |
1157 | 1230 | schema_2_versions = [(subject_4, version_4)]
|
1158 | 1231 | assert schema_id_1 != schema_id_4
|
1159 |
| - await assert_schema_versions(registry_async_client, trail, schema_id_1, schema_1_versions) |
1160 |
| - await assert_schema_versions(registry_async_client, trail, schema_id_4, schema_2_versions) |
| 1232 | + await assert_schema_versions(registry_async_client, "", schema_id_1, schema_1_versions) |
| 1233 | + await assert_schema_versions(registry_async_client, "", schema_id_4, schema_2_versions) |
| 1234 | + |
| 1235 | + res = await registry_async_client.get("subjects") |
| 1236 | + assert res.status_code == 200 |
| 1237 | + assert res.json() == [subject_1, subject_2, subject_3, subject_4] |
1161 | 1238 |
|
1162 | 1239 |
|
1163 | 1240 | @pytest.mark.parametrize("trail", ["", "/"])
|
|
0 commit comments