-
Notifications
You must be signed in to change notification settings - Fork 9.2k
fix(azure): prepend bucket prefix to all Azure Blob storage paths #14561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ricardo-M-L
wants to merge
3
commits into
infiniflow:main
Choose a base branch
from
Ricardo-M-L:fix/azure-blob-missing-bucket-prefix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
test/unit_test/rag/utils/test_azure_blob_bucket_prefix.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| # | ||
| # Copyright 2025 The InfiniFlow Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| """ | ||
| Unit tests for Azure Blob storage path construction (issue #14159). | ||
|
|
||
| Both AzureSpn and AzureSas implementations must prepend the bucket | ||
| parameter to file paths so that files with the same name from different | ||
| datasets do not overwrite each other in flat blob storage. | ||
| """ | ||
| import importlib | ||
| import sys | ||
| import types | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| def _install_stubs(): | ||
| """Replace heavyweight runtime modules so the connection modules can be | ||
| imported in isolation without the full ragflow runtime or the real | ||
| `azure` SDK being installed.""" | ||
|
|
||
| decorator_mod = types.ModuleType("common.decorator") | ||
| decorator_mod.singleton = lambda cls: cls | ||
|
|
||
| settings_mod = types.ModuleType("common.settings") | ||
| settings_mod.AZURE = { | ||
| "account_url": "https://example.dfs.core.windows.net", | ||
| "client_id": "x", | ||
| "secret": "x", | ||
| "tenant_id": "x", | ||
| "container_name": "c", | ||
| "cloud": "public", | ||
| "container_url": "https://example.blob.core.windows.net/c", | ||
| "sas_token": "sig=x", | ||
| } | ||
|
|
||
| common_pkg = types.ModuleType("common") | ||
| common_pkg.decorator = decorator_mod | ||
| common_pkg.settings = settings_mod | ||
|
|
||
| azure_pkg = types.ModuleType("azure") | ||
| azure_identity = types.ModuleType("azure.identity") | ||
| azure_identity.ClientSecretCredential = MagicMock() | ||
| azure_identity.AzureAuthorityHosts = types.SimpleNamespace( | ||
| AZURE_PUBLIC_CLOUD="public", | ||
| AZURE_CHINA="china", | ||
| AZURE_GOVERNMENT="gov", | ||
| AZURE_GERMANY="de", | ||
| ) | ||
| azure_storage = types.ModuleType("azure.storage") | ||
| azure_fdl = types.ModuleType("azure.storage.filedatalake") | ||
| azure_fdl.FileSystemClient = MagicMock() | ||
| azure_blob = types.ModuleType("azure.storage.blob") | ||
| azure_blob.ContainerClient = MagicMock() | ||
| azure_pkg.identity = azure_identity | ||
| azure_pkg.storage = azure_storage | ||
| azure_storage.filedatalake = azure_fdl | ||
| azure_storage.blob = azure_blob | ||
|
|
||
| sys.modules.update({ | ||
| "common": common_pkg, | ||
| "common.decorator": decorator_mod, | ||
| "common.settings": settings_mod, | ||
| "azure": azure_pkg, | ||
| "azure.identity": azure_identity, | ||
| "azure.storage": azure_storage, | ||
| "azure.storage.filedatalake": azure_fdl, | ||
| "azure.storage.blob": azure_blob, | ||
| }) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def spn_module(): | ||
| _install_stubs() | ||
| sys.modules.pop("rag.utils.azure_spn_conn", None) | ||
| return importlib.import_module("rag.utils.azure_spn_conn") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def sas_module(): | ||
| _install_stubs() | ||
| sys.modules.pop("rag.utils.azure_sas_conn", None) | ||
| return importlib.import_module("rag.utils.azure_sas_conn") | ||
|
|
||
|
|
||
| def _make_instance(module, cls_name): | ||
| """Build an instance with a mocked underlying connection, bypassing | ||
| __init__ so we don't need real Azure credentials or connectivity.""" | ||
| cls = getattr(module, cls_name) | ||
| inst = cls.__new__(cls) | ||
| inst.conn = MagicMock() | ||
| return inst | ||
|
|
||
|
|
||
| class TestAzureSpnBucketPrefix: | ||
| """RAGFlowAzureSpnBlob must include the bucket as a path prefix in all | ||
| operations so that identical filenames from different datasets are | ||
| isolated.""" | ||
|
|
||
| def test_put_uses_bucket_prefix(self, spn_module): | ||
| spn = _make_instance(spn_module, "RAGFlowAzureSpnBlob") | ||
| spn.put("kb_a", "doc.pdf", b"data") | ||
| spn.conn.create_file.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_get_uses_bucket_prefix(self, spn_module): | ||
| spn = _make_instance(spn_module, "RAGFlowAzureSpnBlob") | ||
| spn.get("kb_a", "doc.pdf") | ||
| spn.conn.get_file_client.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_rm_uses_bucket_prefix(self, spn_module): | ||
| spn = _make_instance(spn_module, "RAGFlowAzureSpnBlob") | ||
| spn.rm("kb_a", "doc.pdf") | ||
| spn.conn.delete_file.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_obj_exist_uses_bucket_prefix(self, spn_module): | ||
| spn = _make_instance(spn_module, "RAGFlowAzureSpnBlob") | ||
| spn.obj_exist("kb_a", "doc.pdf") | ||
| spn.conn.get_file_client.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_get_presigned_url_uses_bucket_prefix(self, spn_module): | ||
| spn = _make_instance(spn_module, "RAGFlowAzureSpnBlob") | ||
| spn.get_presigned_url("kb_a", "doc.pdf", 3600) | ||
| spn.conn.get_presigned_url.assert_called_once_with("GET", "kb_a/doc.pdf", 3600) | ||
|
|
||
| def test_same_filename_in_different_buckets_does_not_collide(self, spn_module): | ||
| """Regression test for issue #14159: two datasets uploading a file | ||
| with the same name must produce two distinct storage paths.""" | ||
| spn = _make_instance(spn_module, "RAGFlowAzureSpnBlob") | ||
| spn.put("kb_a", "report.pdf", b"data_a") | ||
| spn.put("kb_b", "report.pdf", b"data_b") | ||
| called_paths = [c.args[0] for c in spn.conn.create_file.call_args_list] | ||
| assert called_paths == ["kb_a/report.pdf", "kb_b/report.pdf"] | ||
| assert called_paths[0] != called_paths[1] | ||
|
|
||
|
|
||
| class TestAzureSasBucketPrefix: | ||
| """Same contract for RAGFlowAzureSasBlob.""" | ||
|
|
||
| def test_put_uses_bucket_prefix(self, sas_module): | ||
| sas = _make_instance(sas_module, "RAGFlowAzureSasBlob") | ||
| sas.put("kb_a", "doc.pdf", b"data") | ||
| kwargs = sas.conn.upload_blob.call_args.kwargs | ||
| assert kwargs["name"] == "kb_a/doc.pdf" | ||
|
|
||
| def test_get_uses_bucket_prefix(self, sas_module): | ||
| sas = _make_instance(sas_module, "RAGFlowAzureSasBlob") | ||
| sas.get("kb_a", "doc.pdf") | ||
| sas.conn.download_blob.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_rm_uses_bucket_prefix(self, sas_module): | ||
| sas = _make_instance(sas_module, "RAGFlowAzureSasBlob") | ||
| sas.rm("kb_a", "doc.pdf") | ||
| sas.conn.delete_blob.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_obj_exist_uses_bucket_prefix(self, sas_module): | ||
| sas = _make_instance(sas_module, "RAGFlowAzureSasBlob") | ||
| sas.obj_exist("kb_a", "doc.pdf") | ||
| sas.conn.get_blob_client.assert_called_once_with("kb_a/doc.pdf") | ||
|
|
||
| def test_get_presigned_url_uses_bucket_prefix(self, sas_module): | ||
| sas = _make_instance(sas_module, "RAGFlowAzureSasBlob") | ||
| sas.get_presigned_url("kb_a", "doc.pdf", 3600) | ||
| sas.conn.get_presigned_url.assert_called_once_with("GET", "kb_a/doc.pdf", 3600) | ||
|
|
||
| def test_same_filename_in_different_buckets_does_not_collide(self, sas_module): | ||
| sas = _make_instance(sas_module, "RAGFlowAzureSasBlob") | ||
| sas.put("kb_a", "report.pdf", b"data_a") | ||
| sas.put("kb_b", "report.pdf", b"data_b") | ||
| names = [c.kwargs["name"] for c in sas.conn.upload_blob.call_args_list] | ||
| assert names == ["kb_a/report.pdf", "kb_b/report.pdf"] | ||
| assert names[0] != names[1] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isolate
sys.modulesstubbing to avoid cross-test pollution.These fixtures replace global module entries but never restore them. That can make unrelated tests fail depending on run order. Please switch to
monkeypatch.setitem(or restore snapshot on teardown) so patches are automatically reverted.Also applies to: 74-83, 86-97
🤖 Prompt for AI Agents