From 26e104c34a4421bd3b7b637535d334ff87f945bb Mon Sep 17 00:00:00 2001 From: ohmayr Date: Sun, 29 Nov 2026 08:58:25 +0000 Subject: [PATCH 1/2] fix(bigtable): wire stale sweeper and safe teardown into v2_client tests Bigtable v2_client system tests never invoke clear_stale_instances(), data system tests pass only a single prefix instead of all default test prefixes, and v2_client session fixtures do not wrap creation waits in try/finally. --- .../tests/system/data/__init__.py | 2 +- .../tests/system/v2_client/conftest.py | 78 ++++++++++++++----- 2 files changed, 60 insertions(+), 20 deletions(-) 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..c0daa69abba0 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) + + @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") From 2afa3d75bcba2890e5ddb3675d7e928bbecee715 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Sun, 29 Nov 2026 09:26:26 +0000 Subject: [PATCH 2/2] fix(bigtable): pass older_than_days explicitly in v2_client cleanup --- .../google-cloud-bigtable/tests/system/v2_client/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c0daa69abba0..93c8e78c3841 100644 --- a/packages/google-cloud-bigtable/tests/system/v2_client/conftest.py +++ b/packages/google-cloud-bigtable/tests/system/v2_client/conftest.py @@ -84,7 +84,7 @@ def cleanup_old_instances(admin_client, in_emulator): return from tests.system.utils import clear_stale_instances - clear_stale_instances(admin_client.project) + clear_stale_instances(admin_client.project, older_than_days=1) @pytest.fixture(scope="session")