Skip to content

Commit

Permalink
ci: delete all azure_ai_search indexes (#1247)
Browse files Browse the repository at this point in the history
* Add a new fixture to clean up undeleted indexes
  • Loading branch information
Amnah199 authored Dec 14, 2024
1 parent 0e20791 commit 3a3419a
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions integrations/azure_ai_search/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")

0 comments on commit 3a3419a

Please sign in to comment.