From 2e4572b89724aa10707472aa314d002b97daef55 Mon Sep 17 00:00:00 2001 From: Emmanuel Evbuomwan Date: Thu, 25 Jul 2024 12:21:57 +0200 Subject: [PATCH] fix: `Version` int log formatting in `schema_reader.py` --- karapace/schema_reader.py | 2 +- tests/unit/test_schema_reader.py | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/karapace/schema_reader.py b/karapace/schema_reader.py index 493019233..f36b85f67 100644 --- a/karapace/schema_reader.py +++ b/karapace/schema_reader.py @@ -488,7 +488,7 @@ def _handle_msg_schema_hard_delete(self, key: dict) -> None: if self.database.find_subject(subject=subject) is None: LOG.warning("Hard delete: Subject %s did not exist, should have", subject) elif version not in self.database.find_subject_schemas(subject=subject, include_deleted=True): - LOG.warning("Hard delete: Version %d for subject %s did not exist, should have", version, subject) + LOG.warning("Hard delete: version: %r for subject: %r did not exist, should have", version, subject) else: LOG.info("Hard delete: subject: %r version: %r", subject, version) self.database.delete_subject_schema(subject=subject, version=version) diff --git a/tests/unit/test_schema_reader.py b/tests/unit/test_schema_reader.py index ebbdf3418..afbcbb976 100644 --- a/tests/unit/test_schema_reader.py +++ b/tests/unit/test_schema_reader.py @@ -5,6 +5,7 @@ See LICENSE for details """ +from _pytest.logging import LogCaptureFixture from concurrent.futures import ThreadPoolExecutor from confluent_kafka import Message from dataclasses import dataclass @@ -20,12 +21,13 @@ OFFSET_EMPTY, OFFSET_UNINITIALIZED, ) -from karapace.typing import SchemaId +from karapace.typing import SchemaId, Version from tests.base_testcase import BaseTestCase from unittest.mock import Mock import confluent_kafka import json +import logging import pytest import random import time @@ -292,3 +294,27 @@ def test_soft_deleted_schema_storing() -> None: soft_deleted_stored_schema = schema_reader.database.find_schema(schema_id=SchemaId(1)) assert soft_deleted_stored_schema is not None + + +def test_handle_msg_delete_subject_logs(caplog: LogCaptureFixture) -> None: + database_mock = Mock(spec=InMemoryDatabase) + database_mock.find_subject.return_value = True + database_mock.find_subject_schemas.return_value = { + Version(1): "SchemaVersion" + } # `SchemaVersion` is an actual object, simplified for test + schema_reader = KafkaSchemaReader( + config=DEFAULTS, + offset_watcher=OffsetWatcher(), + key_formatter=KeyFormatter(), + master_coordinator=None, + database=database_mock, + ) + + with caplog.at_level(logging.WARNING, logger="karapace.schema_reader"): + schema_reader._handle_msg_schema_hard_delete( # pylint: disable=protected-access + key={"subject": "test-subject", "version": 2} + ) + for log in caplog.records: + assert log.name == "karapace.schema_reader" + assert log.levelname == "WARNING" + assert log.message == "Hard delete: version: Version(2) for subject: 'test-subject' did not exist, should have"