diff --git a/packages/google-cloud-bigtable/tests/system/data/__init__.py b/packages/google-cloud-bigtable/tests/system/data/__init__.py index b28ad6823b29..089d8c11e8ea 100644 --- a/packages/google-cloud-bigtable/tests/system/data/__init__.py +++ b/packages/google-cloud-bigtable/tests/system/data/__init__.py @@ -47,7 +47,7 @@ def cleanup_old_instances(self, project_id): if os.getenv(BIGTABLE_EMULATOR): return - clear_stale_instances(project_id, "python-bigtable-tests", older_than_days=1) + clear_stale_instances(project_id, older_than_days=1) @pytest.fixture(scope="session") def init_table_id(self, start_timestamp): diff --git a/packages/google-cloud-bigtable/tests/system/v2_client/conftest.py b/packages/google-cloud-bigtable/tests/system/v2_client/conftest.py index 59e016f52af8..93c8e78c3841 100644 --- a/packages/google-cloud-bigtable/tests/system/v2_client/conftest.py +++ b/packages/google-cloud-bigtable/tests/system/v2_client/conftest.py @@ -15,6 +15,7 @@ import os import pytest +from google.api_core import exceptions from google.cloud.environment_vars import BIGTABLE_EMULATOR from test_utils.system import unique_resource_id @@ -76,6 +77,16 @@ def admin_client(): return Client(admin=True) +@pytest.fixture(scope="session", autouse=True) +def cleanup_old_instances(admin_client, in_emulator): + """Automatically deletes stale test instances older than 1 day.""" + if in_emulator: + return + from tests.system.utils import clear_stale_instances + + clear_stale_instances(admin_client.project, older_than_days=1) + + @pytest.fixture(scope="session") def service_account(admin_client): from google.oauth2.service_account import Credentials @@ -131,14 +142,29 @@ def admin_cluster_with_autoscaling( def admin_instance_populated(admin_instance, admin_cluster, in_emulator): # Emulator does not support instance admin operations (create / delete). # See: https://cloud.google.com/bigtable/docs/emulator - if not in_emulator: - operation = admin_instance.create(clusters=[admin_cluster]) - operation.result(timeout=240) - - yield admin_instance + if in_emulator: + yield admin_instance + return - if not in_emulator: - _helpers.retry_429(admin_instance.delete)() + operation = admin_instance.create(clusters=[admin_cluster]) + created = False + try: + operation.result(timeout=240) + created = True + yield admin_instance + finally: + try: + _helpers.retry_429(admin_instance.delete)() + except exceptions.NotFound: + pass + except Exception as exc: + if not created: + print( + f"Failed to delete timed-out instance " + f"{admin_instance.instance_id}: {exc}" + ) + else: + raise @pytest.fixture(scope="session") @@ -169,19 +195,33 @@ def data_instance_populated( instance = admin_client.instance(data_instance_id, labels=instance_labels) # Emulator does not support instance admin operations (create / delete). # See: https://cloud.google.com/bigtable/docs/emulator - if not in_emulator: - cluster = instance.cluster( - data_cluster_id, - location_id=location_id, - serve_nodes=serve_nodes, - ) - operation = instance.create(clusters=[cluster]) - operation.result(timeout=240) - - yield instance + if in_emulator: + yield instance + return - if not in_emulator: - _helpers.retry_429(instance.delete)() + cluster = instance.cluster( + data_cluster_id, + location_id=location_id, + serve_nodes=serve_nodes, + ) + operation = instance.create(clusters=[cluster]) + created = False + try: + operation.result(timeout=240) + created = True + yield instance + finally: + try: + _helpers.retry_429(instance.delete)() + except exceptions.NotFound: + pass + except Exception as exc: + if not created: + print( + f"Failed to delete timed-out instance {instance.instance_id}: {exc}" + ) + else: + raise @pytest.fixture(scope="function")