From 3a3419a21bcc08da7cdf52d033c89656112dad98 Mon Sep 17 00:00:00 2001 From: Amna Mubashar Date: Sat, 14 Dec 2024 23:30:53 +0100 Subject: [PATCH] ci: delete all azure_ai_search indexes (#1247) * Add a new fixture to clean up undeleted indexes --- .../azure_ai_search/tests/conftest.py | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/integrations/azure_ai_search/tests/conftest.py b/integrations/azure_ai_search/tests/conftest.py index e741e3066..02742031c 100644 --- a/integrations/azure_ai_search/tests/conftest.py +++ b/integrations/azure_ai_search/tests/conftest.py @@ -6,13 +6,10 @@ from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceNotFoundError from azure.search.documents.indexes import SearchIndexClient -from haystack import logging from haystack.document_stores.types import DuplicatePolicy from haystack_integrations.document_stores.azure_ai_search import AzureAISearchDocumentStore -logger = logging.getLogger(__name__) - # This is the approximate time in seconds it takes for the documents to be available in Azure Search index SLEEP_TIME_IN_SECONDS = 10 @@ -78,8 +75,32 @@ def wait_for_index_deletion(client, index_name): try: client.delete_index(index_name) if not wait_for_index_deletion(client, index_name): - logger.error(f"Index {index_name} was not properly deleted.") + print(f"Index {index_name} was not properly deleted.") except ResourceNotFoundError: - logger.error(f"Index {index_name} was already deleted or not found.") + print(f"Index {index_name} was already deleted or not found.") except Exception as e: - logger.error(f"Unexpected error when deleting index {index_name}: {e}") + print(f"Unexpected error when deleting index {index_name}: {e}") + raise + + +@pytest.fixture(scope="session", autouse=True) +def cleanup_indexes(): + """ + Fixture to clean up all remaining indexes at the end of the test session. + Automatically runs after all tests. + """ + azure_endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"] + api_key = os.environ["AZURE_SEARCH_API_KEY"] + + client = SearchIndexClient(azure_endpoint, AzureKeyCredential(api_key)) + + yield # Allow tests to run before performing cleanup + + # Cleanup: Delete all remaining indexes + print("Starting session-level cleanup of all Azure Search indexes.") + existing_indexes = client.list_index_names() + for index in existing_indexes: + try: + client.delete_index(index) + except Exception as e: + print(f"Failed to delete index during clean up {index}: {e}")