Skip to content

Commit 91266a7

Browse files
sdks/python: handle transient testcontainer startup/teardown errors
1 parent 5df9628 commit 91266a7

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment_test.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class TestContainerStartupError(Exception):
6868
"""Raised when any test container fails to start."""
6969
pass
7070

71+
class TestContainerTeardownError(Exception):
72+
"""Raised when any test container fails to teardown."""
73+
pass
7174

7275
def validate_enrichment_with_bigtable():
7376
expected = '''[START enrichment_with_bigtable]
@@ -186,7 +189,7 @@ def test_enrichment_with_external_pg(self, mock_stdout):
186189
output = mock_stdout.getvalue().splitlines()
187190
expected = validate_enrichment_with_external_pg()
188191
self.assertEqual(output, expected)
189-
except TestContainerStartupError as e:
192+
except (TestContainerStartupError, TestContainerTeardownError) as e:
190193
raise unittest.SkipTest(str(e))
191194
except Exception as e:
192195
self.fail(f"Test failed with unexpected error: {e}")
@@ -199,7 +202,7 @@ def test_enrichment_with_external_mysql(self, mock_stdout):
199202
output = mock_stdout.getvalue().splitlines()
200203
expected = validate_enrichment_with_external_mysql()
201204
self.assertEqual(output, expected)
202-
except TestContainerStartupError as e:
205+
except (TestContainerStartupError, TestContainerTeardownError) as e:
203206
raise unittest.SkipTest(str(e))
204207
except Exception as e:
205208
self.fail(f"Test failed with unexpected error: {e}")
@@ -212,7 +215,7 @@ def test_enrichment_with_external_sqlserver(self, mock_stdout):
212215
output = mock_stdout.getvalue().splitlines()
213216
expected = validate_enrichment_with_external_sqlserver()
214217
self.assertEqual(output, expected)
215-
except TestContainerStartupError as e:
218+
except (TestContainerStartupError, TestContainerTeardownError) as e:
216219
raise unittest.SkipTest(str(e))
217220
except Exception as e:
218221
self.fail(f"Test failed with unexpected error: {e}")
@@ -227,7 +230,7 @@ def test_enrichment_with_milvus(self, mock_stdout):
227230
output = parse_chunk_strings(output)
228231
expected = parse_chunk_strings(expected)
229232
assert_chunks_equivalent(output, expected)
230-
except TestContainerStartupError as e:
233+
except (TestContainerStartupError, TestContainerTeardownError) as e:
231234
raise unittest.SkipTest(str(e))
232235
except Exception as e:
233236
self.fail(f"Test failed with unexpected error: {e}")
@@ -373,19 +376,17 @@ def post_sql_enrichment_test(res: CloudSQLEnrichmentTestDataConstruct):
373376
def pre_milvus_enrichment() -> MilvusDBContainerInfo:
374377
try:
375378
db = MilvusEnrichmentTestHelper.start_db_container()
376-
except Exception as e:
377-
raise TestContainerStartupError(
378-
f"Milvus container failed to start: {str(e)}")
379-
380-
connection_params = MilvusConnectionParameters(
379+
connection_params = MilvusConnectionParameters(
381380
uri=db.uri,
382381
user=db.user,
383382
password=db.password,
384383
db_id=db.id,
385384
token=db.token)
386-
387-
collection_name = MilvusEnrichmentTestHelper.initialize_db_with_data(
385+
collection_name = MilvusEnrichmentTestHelper.initialize_db_with_data(
388386
connection_params)
387+
except Exception as e:
388+
raise TestContainerStartupError(
389+
f"Milvus container failed to start: {str(e)}")
389390

390391
# Setup environment variables for db and collection configuration. This will
391392
# be used downstream by the milvus enrichment handler.
@@ -400,7 +401,12 @@ def pre_milvus_enrichment() -> MilvusDBContainerInfo:
400401

401402
@staticmethod
402403
def post_milvus_enrichment(db: MilvusDBContainerInfo):
403-
MilvusEnrichmentTestHelper.stop_db_container(db)
404+
try:
405+
MilvusEnrichmentTestHelper.stop_db_container(db)
406+
except Exception:
407+
raise TestContainerTeardownError(
408+
f"Milvus container failed to tear down: {str(e)}")
409+
404410
os.environ.pop('MILVUS_VECTOR_DB_URI', None)
405411
os.environ.pop('MILVUS_VECTOR_DB_USER', None)
406412
os.environ.pop('MILVUS_VECTOR_DB_PASSWORD', None)

sdks/python/apache_beam/ml/rag/enrichment/milvus_search_it_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def start_db_container(
312312
with user_yaml_creator(service_container_port, max_vec_fields) as cfg:
313313
info = None
314314
original_tc_max_tries = testcontainers_config.max_tries
315-
if not testcontainers_config.max_tries:
315+
if testcontainers_config.max_tries is not None:
316316
testcontainers_config.max_tries = tc_max_retries
317317
for i in range(vector_client_max_retries):
318318
try:
@@ -326,7 +326,6 @@ def start_db_container(
326326
host = vector_db_container.get_container_host_ip()
327327
port = vector_db_container.get_exposed_port(service_container_port)
328328
info = MilvusDBContainerInfo(vector_db_container, host, port)
329-
testcontainers_config.max_tries = original_tc_max_tries
330329
_LOGGER.info(
331330
"milvus db container started successfully on %s.", info.uri)
332331
break
@@ -351,20 +350,18 @@ def start_db_container(
351350
stdout_logs,
352351
stderr_logs)
353352
raise e
353+
finally:
354+
testcontainers_config.max_tries = original_tc_max_tries
354355
return info
355356

356357
@staticmethod
357358
def stop_db_container(db_info: MilvusDBContainerInfo):
358359
if db_info is None:
359360
_LOGGER.warning("Milvus db info is None. Skipping stop operation.")
360361
return
361-
try:
362-
_LOGGER.debug("Stopping milvus db container.")
363-
db_info.container.stop()
364-
_LOGGER.info("milvus db container stopped successfully.")
365-
except Exception as e:
366-
_LOGGER.warning(
367-
"Error encountered while stopping milvus db container: %s", e)
362+
_LOGGER.debug("Stopping milvus db container.")
363+
db_info.container.stop()
364+
_LOGGER.info("milvus db container stopped successfully.")
368365

369366
@staticmethod
370367
def initialize_db_with_data(connc_params: MilvusConnectionParameters):

0 commit comments

Comments
 (0)