Skip to content
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

fix index cleanup #201

Open
wants to merge 6 commits into
base: mainline-poc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions tests/cloud_test_logic/cloud_instance_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ class GetIndexesIndexResponseObject(NamedTuple):
This class is used in the mock_instance_mappings decorator.

Example Usage:
This will create basic index object with name "test-index", status "READY", and endpoint "endpoint"
This will create basic index object with name "py_marqo_test_index", status "READY", and endpoint "endpoint"
>>> @mock_get_indexes_response([GetIndexesIndexResponseObject.get_default_index_object()])
Response object would look:
{"results": [{"index_name": "test-index", "index_status": "READY", "endpoint": "endpoint"}]}
{"results": [{"index_name": "py_marqo_test_index", "index_status": "READY", "endpoint": "endpoint"}]}

This will create index object with specified parameters
>>> @mock_get_indexes_response([GetIndexesIndexResponseObject("other-index", "CREATING", "endpoint2")])
Response object would look:
{"results": [{"index_name": "other-index", "index_status": "CREATING", "endpoint": "endpoint2"}]}

"""
index_name: str
index_status: str
endpoint: str
indexName: str
indexStatus: str
marqoEndpoint: str

@staticmethod
def get_default_index_object():
return GetIndexesIndexResponseObject(
index_name="test_index",
index_status="READY",
endpoint="endpoint"
indexName="py_marqo_test_index",
indexStatus="READY",
marqoEndpoint="endpoint"
)


Expand Down
12 changes: 6 additions & 6 deletions tests/cloud_test_logic/cloud_test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class CloudTestIndex(str, Enum):
-> 4) use structured_image_prepro
"""

unstructured_text = "unstr_txt"
unstructured_image = "unstr_img"
unstructured_text_custom_prepro = "unstr_txt_custom_prepro"
unstructured_text = "pymarqo_unstr_txt"
unstructured_image = "pymarqo_unstr_img"
unstructured_text_custom_prepro = "pymarqo_unstr_txt_custom_prepro"

structured_image_prepro = "str_img_prepro"
structured_image_custom = "str_img_custom"
structured_text = "str_txt"
structured_image_prepro = "pymarqo_str_img_prepro"
structured_image_custom = "pymarqo_str_img_custom"
structured_text = "pymarqo_str_txt"


index_name_to_settings_mappings = {
Expand Down
5 changes: 3 additions & 2 deletions tests/cloud_test_logic/delete_all_cloud_test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ def delete_all_test_indices(wait_for_readiness=False):
"url": os.environ.get("MARQO_URL", 'http://localhost:8882'),
}
suffix = os.environ.get("MQ_TEST_RUN_IDENTIFIER", None)
prefix = "pymarqo"
api_key = os.environ.get("MARQO_API_KEY", None)
if api_key:
local_marqo_settings["api_key"] = api_key
print(f"Deleting all test indices from Marqo Cloud Account that match the following criteria:")
print(f"- index name starts with 'test_index'")
print(f"- index name starts with '{prefix}'")
print(f"- index name contains the value of the environment variable MQ_TEST_RUN_IDENTIFIER: {suffix}\n")
client = marqo.Client(**local_marqo_settings)
indexes = client.get_indexes()
indices_to_delete = []
for index in indexes['results']:
if index["indexName"].startswith('test_index'):
if index["indexName"].startswith(prefix):
if suffix is not None and suffix in index["indexName"].split('_'):
indices_to_delete.append(index["indexName"])
elif suffix is None:
Expand Down
16 changes: 12 additions & 4 deletions tests/marqo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ def setUpClass(cls) -> None:
cls.authorized_url = cls.client_settings["url"]
cls.index_to_documents_cleanup_mapping = {}
cls.client=marqo.Client(**cls.client_settings)
cls.generic_test_index_name = "py_marqo_test_index"
cls.unstructured_index_name = "unstructured_index"
cls.structured_index_name = "structured_index"
cls.unstructured_image_index_name = "unstructured_image_index"
# TODO: include structured when boolean_field bug for structured is fixed
cls.test_cases = [
(CloudTestIndex.unstructured_image, cls.unstructured_index_name),
]

# class property to indicate if test is being run on multi
cls.IS_MULTI_INSTANCE = (True if os.environ.get("IS_MULTI_INSTANCE", False) in ["True", "TRUE", "true", True] else False)
Expand All @@ -211,8 +219,8 @@ def tearDownClass(cls) -> None:
"""
if cls.client.config.is_marqo_cloud:
for index in cls.client.get_indexes()['results']:
if index.index_name.endswith(cls.index_suffix):
cls.cleanup_documents_from_index(cls, index.index_name)
if index["indexName"].endswith(cls.index_suffix):
cls.cleanup_documents_from_index(cls, index["indexName"])
else:
if cls.open_source_indexes_list:
cls.delete_open_source_indexes(cls.open_source_indexes_list)
Expand Down Expand Up @@ -282,7 +290,7 @@ def get_test_index_name(
if client.config.is_marqo_cloud:
if cloud_test_index_to_use is None:
raise ValueError("cloud_test_index_to_use must be specified for cloud tests")
index_name_to_return = f"{cloud_test_index_to_use.value}-{self.index_suffix}"
index_name_to_return = f"{cloud_test_index_to_use.value}_{self.index_suffix}"
self.prepare_cloud_index_for_test(index_name_to_return, delete_index_documents_before_test)
else:
index_name_to_return = open_source_test_index_name
Expand Down Expand Up @@ -378,7 +386,6 @@ def cleanup_documents_from_index(self, index_to_cleanup: str):
idx = client.index(index_to_cleanup)
print(f"Deleting documents from index {idx.index_name}")
try:
idx.refresh()
# verifying that index is in the mapping
client.config.instance_mapping.get_index_base_url(idx.index_name)

Expand All @@ -394,6 +401,7 @@ def cleanup_documents_from_index(self, index_to_cleanup: str):
docs_to_delete = [i['_id'] for i in idx.search(q, limit=100)['hits']]
while idx.get_stats()["numberOfDocuments"] > 0 or docs_to_delete:
docs_to_delete = [i['_id'] for i in idx.search(q, limit=100)['hits']]
print(docs_to_delete)
if docs_to_delete:
idx.delete_documents(docs_to_delete)
q = ''.join(choice(ascii_letters) for _ in range(8))
Expand Down
Loading
Loading