|
| 1 | +# Copyright (c) 2024 Microsoft Corporation. |
| 2 | +# Licensed under the MIT License |
| 3 | + |
| 4 | +"""Unit tests for the BlobWorkflowLogger.""" |
| 5 | + |
| 6 | +import json |
| 7 | +from unittest.mock import MagicMock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | +from graphrag.logger.blob_workflow_logger import BlobWorkflowLogger |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_blob_service(): |
| 15 | + """Create a mock BlobServiceClient and related objects.""" |
| 16 | + with patch( |
| 17 | + "graphrag.logger.blob_workflow_logger.BlobServiceClient" |
| 18 | + ) as mock_bsc_cls: |
| 19 | + mock_blob_client = MagicMock() |
| 20 | + mock_blob_client.exists.return_value = False |
| 21 | + |
| 22 | + mock_bsc = MagicMock() |
| 23 | + mock_bsc.get_blob_client.return_value = mock_blob_client |
| 24 | + |
| 25 | + mock_bsc_cls.return_value = mock_bsc |
| 26 | + |
| 27 | + yield mock_bsc, mock_blob_client |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def mock_credential(): |
| 32 | + """Mock DefaultAzureCredential.""" |
| 33 | + with patch( |
| 34 | + "graphrag.logger.blob_workflow_logger.DefaultAzureCredential" |
| 35 | + ) as mock_cred: |
| 36 | + yield mock_cred |
| 37 | + |
| 38 | + |
| 39 | +def _make_logger() -> BlobWorkflowLogger: |
| 40 | + return BlobWorkflowLogger( |
| 41 | + connection_string=None, |
| 42 | + container_name="test-container", |
| 43 | + blob_name="test.logs.json", |
| 44 | + base_dir="logs", |
| 45 | + account_url="https://test.blob.core.windows.net", |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_init_requires_container_name(mock_credential): |
| 50 | + """Test that a missing container name raises a ValueError.""" |
| 51 | + with pytest.raises(ValueError, match="No container name provided"): |
| 52 | + BlobWorkflowLogger( |
| 53 | + connection_string=None, |
| 54 | + container_name=None, |
| 55 | + account_url="https://test.blob.core.windows.net", |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def test_init_requires_connection_or_account_url(): |
| 60 | + """Test that missing both connection string and account url raises.""" |
| 61 | + with pytest.raises(ValueError, match="No storage account blob url provided"): |
| 62 | + BlobWorkflowLogger( |
| 63 | + connection_string=None, |
| 64 | + container_name="test-container", |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def test_init_creates_append_blob(mock_blob_service, mock_credential): |
| 69 | + """Test that a new append blob is created on initialization.""" |
| 70 | + _mock_bsc, mock_blob_client = mock_blob_service |
| 71 | + |
| 72 | + logger = _make_logger() |
| 73 | + |
| 74 | + mock_blob_client.create_append_blob.assert_called_once() |
| 75 | + assert logger._num_blocks == 0 # noqa: SLF001 |
| 76 | + assert logger._base_dir == "logs" # noqa: SLF001 |
| 77 | + |
| 78 | + |
| 79 | +def test_init_uses_provided_blob_name(mock_blob_service, mock_credential): |
| 80 | + """Test that the provided blob name is honored and nested under base_dir.""" |
| 81 | + logger = _make_logger() |
| 82 | + |
| 83 | + assert logger._blob_name == "logs/test.logs.json" # noqa: SLF001 |
| 84 | + |
| 85 | + |
| 86 | +def test_rotate_blob_does_not_reinitialize_handler(mock_blob_service, mock_credential): |
| 87 | + """Test that blob rotation does not call __init__ on the handler. |
| 88 | +
|
| 89 | + This verifies the fix for issue #2170 where calling self.__init__() |
| 90 | + during rotation caused 'cannot release un-acquired lock' errors. |
| 91 | + """ |
| 92 | + mock_bsc, _mock_blob_client = mock_blob_service |
| 93 | + |
| 94 | + logger = _make_logger() |
| 95 | + |
| 96 | + # Simulate reaching max block count |
| 97 | + logger._num_blocks = logger._max_block_count # noqa: SLF001 |
| 98 | + |
| 99 | + # Store reference to the original lock to verify it's not replaced |
| 100 | + original_lock = logger.lock |
| 101 | + |
| 102 | + # Write a log entry, which should trigger rotation |
| 103 | + logger._write_log({"type": "log", "data": "test message"}) # noqa: SLF001 |
| 104 | + |
| 105 | + # Verify the lock was NOT replaced (i.e., __init__ was not called) |
| 106 | + assert logger.lock is original_lock |
| 107 | + |
| 108 | + # Verify block counter was reset (rotation happened) |
| 109 | + # After rotation (reset to 0) + 1 write = 1 |
| 110 | + assert logger._num_blocks == 1 # noqa: SLF001 |
| 111 | + |
| 112 | + # Verify a new blob client was created during rotation |
| 113 | + assert mock_bsc.get_blob_client.call_count > 1 |
| 114 | + |
| 115 | + |
| 116 | +def test_rotate_blob_creates_new_blob_name(mock_blob_service, mock_credential): |
| 117 | + """Test that rotation generates a new blob name.""" |
| 118 | + _mock_bsc, _mock_blob_client = mock_blob_service |
| 119 | + |
| 120 | + logger = _make_logger() |
| 121 | + |
| 122 | + original_blob_name = logger._blob_name # noqa: SLF001 |
| 123 | + |
| 124 | + # Trigger rotation |
| 125 | + logger._rotate_blob() # noqa: SLF001 |
| 126 | + |
| 127 | + # New blob name should be different (auto-generated with timestamp) |
| 128 | + assert logger._blob_name != original_blob_name # noqa: SLF001 |
| 129 | + assert logger._num_blocks == 0 # noqa: SLF001 |
| 130 | + |
| 131 | + |
| 132 | +def test_write_log_appends_block(mock_blob_service, mock_credential): |
| 133 | + """Test that writing a log appends an encoded block and increments count.""" |
| 134 | + _mock_bsc, mock_blob_client = mock_blob_service |
| 135 | + |
| 136 | + logger = _make_logger() |
| 137 | + |
| 138 | + logger._write_log({"type": "log", "data": "hello"}) # noqa: SLF001 |
| 139 | + |
| 140 | + mock_blob_client.append_block.assert_called_once() |
| 141 | + (payload,), _kwargs = mock_blob_client.append_block.call_args |
| 142 | + decoded = json.loads(payload.decode("utf-8")) |
| 143 | + assert decoded == {"type": "log", "data": "hello"} |
| 144 | + assert logger._num_blocks == 1 # noqa: SLF001 |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.parametrize( |
| 148 | + ("level", "expected"), |
| 149 | + [ |
| 150 | + (logging_level, expected) |
| 151 | + for logging_level, expected in [ |
| 152 | + (40, "error"), # logging.ERROR |
| 153 | + (30, "warning"), # logging.WARNING |
| 154 | + (20, "log"), # logging.INFO |
| 155 | + (10, "log"), # logging.DEBUG |
| 156 | + ] |
| 157 | + ], |
| 158 | +) |
| 159 | +def test_get_log_type(mock_blob_service, mock_credential, level, expected): |
| 160 | + """Test that log levels map to the correct log type string.""" |
| 161 | + logger = _make_logger() |
| 162 | + |
| 163 | + assert logger._get_log_type(level) == expected # noqa: SLF001 |
0 commit comments