|
| 1 | +import uuid |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
3 | 5 | from kafka.admin import KafkaAdminClient, NewTopic, NewPartitions |
4 | 6 | from kafka.errors import KafkaTimeoutError, UnknownTopicOrPartitionError |
| 7 | +from kafka.protocol.admin import ( |
| 8 | + CreateTopicsRequest, |
| 9 | + CreateTopicsResponse, |
| 10 | + DeleteTopicsRequest, |
| 11 | + DeleteTopicsResponse, |
| 12 | +) |
| 13 | +from kafka.protocol.metadata import MetadataRequest, MetadataResponse |
5 | 14 |
|
6 | 15 |
|
7 | 16 | def test_new_partitions(): |
@@ -190,3 +199,112 @@ def fake_describe_topics(topics): |
190 | 199 | monkeypatch.setattr(admin, 'describe_topics', fake_describe_topics) |
191 | 200 | admin.wait_for_topics(['foo'], timeout_ms=5000) |
192 | 201 | assert state['calls'] == 2 |
| 202 | + |
| 203 | + |
| 204 | +# --------------------------------------------------------------------------- |
| 205 | +# KIP-516: topic UUIDs in admin requests/responses |
| 206 | +# --------------------------------------------------------------------------- |
| 207 | + |
| 208 | + |
| 209 | +class TestTopicIdAdmin: |
| 210 | + """KIP-516 topic ids in the admin surface: delete-by-id, describe-by-id, |
| 211 | + and CreateTopics surfacing topic_id from v7+ responses.""" |
| 212 | + |
| 213 | + def test_delete_topics_by_id_encodes_topic_id(self, broker, admin): |
| 214 | + """delete_topics already accepts uuid.UUID items; pin the wire shape.""" |
| 215 | + captured = {} |
| 216 | + |
| 217 | + def handler(api_key, api_version, correlation_id, request_bytes): |
| 218 | + decoded = DeleteTopicsRequest.decode( |
| 219 | + request_bytes, version=api_version, header=True) |
| 220 | + captured['request'] = decoded |
| 221 | + captured['version'] = api_version |
| 222 | + return DeleteTopicsResponse(throttle_time_ms=0, responses=[]) |
| 223 | + |
| 224 | + broker.respond_fn(DeleteTopicsRequest, handler) |
| 225 | + |
| 226 | + u = uuid.uuid4() |
| 227 | + admin.delete_topics([u]) |
| 228 | + |
| 229 | + req = captured['request'] |
| 230 | + # v6+ uses the structured DeleteTopicState carrying topic_id. |
| 231 | + assert captured['version'] >= 6 |
| 232 | + assert len(req.topics) == 1 |
| 233 | + assert req.topics[0].topic_id == u |
| 234 | + assert req.topics[0].name is None |
| 235 | + |
| 236 | + def test_describe_topics_by_id_sends_topic_id(self, broker, admin): |
| 237 | + captured = {} |
| 238 | + |
| 239 | + def handler(api_key, api_version, correlation_id, request_bytes): |
| 240 | + decoded = MetadataRequest.decode( |
| 241 | + request_bytes, version=api_version, header=True) |
| 242 | + captured['request'] = decoded |
| 243 | + captured['version'] = api_version |
| 244 | + return MetadataResponse(throttle_time_ms=0, brokers=[], cluster_id='c', |
| 245 | + controller_id=0, topics=[]) |
| 246 | + |
| 247 | + broker.respond_fn(MetadataRequest, handler) |
| 248 | + |
| 249 | + u = uuid.uuid4() |
| 250 | + admin.describe_topics([u]) |
| 251 | + |
| 252 | + req = captured['request'] |
| 253 | + # MetadataRequest must be v12+ for name=null + topic_id to work. |
| 254 | + assert captured['version'] >= 12 |
| 255 | + assert len(req.topics) == 1 |
| 256 | + assert req.topics[0].topic_id == u |
| 257 | + assert req.topics[0].name is None |
| 258 | + |
| 259 | + def test_describe_topics_mixed_name_and_id(self, broker, admin): |
| 260 | + captured = {} |
| 261 | + |
| 262 | + def handler(api_key, api_version, correlation_id, request_bytes): |
| 263 | + decoded = MetadataRequest.decode( |
| 264 | + request_bytes, version=api_version, header=True) |
| 265 | + captured['request'] = decoded |
| 266 | + return MetadataResponse(throttle_time_ms=0, brokers=[], cluster_id='c', |
| 267 | + controller_id=0, topics=[]) |
| 268 | + |
| 269 | + broker.respond_fn(MetadataRequest, handler) |
| 270 | + |
| 271 | + u = uuid.uuid4() |
| 272 | + admin.describe_topics(['by-name', u]) |
| 273 | + |
| 274 | + req = captured['request'] |
| 275 | + assert len(req.topics) == 2 |
| 276 | + assert req.topics[0].name == 'by-name' |
| 277 | + assert req.topics[0].topic_id is None |
| 278 | + assert req.topics[1].name is None |
| 279 | + assert req.topics[1].topic_id == u |
| 280 | + |
| 281 | + def test_create_topics_negotiates_v7_and_surfaces_topic_id(self, broker, admin): |
| 282 | + """With max_version=3 dropped, negotiation should reach v7+ on a |
| 283 | + modern MockBroker and the response's topic_id flows through to_dict().""" |
| 284 | + captured = {} |
| 285 | + u = uuid.uuid4() |
| 286 | + |
| 287 | + def handler(api_key, api_version, correlation_id, request_bytes): |
| 288 | + captured['version'] = api_version |
| 289 | + _Result = CreateTopicsResponse.CreatableTopicResult |
| 290 | + return CreateTopicsResponse( |
| 291 | + throttle_time_ms=0, |
| 292 | + topics=[_Result( |
| 293 | + name='foo', |
| 294 | + topic_id=u, |
| 295 | + error_code=0, |
| 296 | + error_message=None, |
| 297 | + num_partitions=1, |
| 298 | + replication_factor=1, |
| 299 | + configs=[], |
| 300 | + )], |
| 301 | + ) |
| 302 | + |
| 303 | + broker.respond_fn(CreateTopicsRequest, handler) |
| 304 | + |
| 305 | + result = admin.create_topics({'foo': {'num_partitions': 1, 'replication_factor': 1}}) |
| 306 | + |
| 307 | + # Default MockBroker is (4, 2); negotiation should land on v7+. |
| 308 | + assert captured['version'] >= 7 |
| 309 | + # to_dict() stringifies the UUID; compare via uuid.UUID round-trip. |
| 310 | + assert uuid.UUID(result['topics'][0]['topic_id']) == u |
0 commit comments