diff --git a/packages/google-cloud-spanner/tests/system/_helpers.py b/packages/google-cloud-spanner/tests/system/_helpers.py index a4695a17c3e3..f1d0de42f70d 100644 --- a/packages/google-cloud-spanner/tests/system/_helpers.py +++ b/packages/google-cloud-spanner/tests/system/_helpers.py @@ -16,6 +16,7 @@ import os import time import uuid +import warnings from google.api_core import datetime_helpers, exceptions from test_utils import retry @@ -111,7 +112,7 @@ def scrub_referencing_databases(to_scrub, db_list): for db_name in db_list: db = to_scrub.database(db_name.split("/")[-1]) try: - retry_429_503(db.delete)() + retry_429_503(db.drop)() except exceptions.NotFound: # lost the race pass @@ -127,25 +128,34 @@ def scrub_instance_backups(to_scrub): retry_429_503(bkp.delete)() except exceptions.NotFound: # lost the race pass - except exceptions.MethodNotImplemented: - # The CI emulator raises 501: local versions seem fine. + except (exceptions.MethodNotImplemented, exceptions.NotFound): + # The CI emulator raises 501, and NotFound means the instance is gone. pass def scrub_instance_ignore_not_found(to_scrub): """Helper for func:`cleanup_old_instances`""" - scrub_instance_backups(to_scrub) + try: + for database_pb in to_scrub.list_databases(): + db = to_scrub.database(database_pb.name.split("/")[-1]) + try: + db.reload() + if db.enable_drop_protection: + db.enable_drop_protection = False + operation = db.update(["enable_drop_protection"]) + operation.result(DATABASE_OPERATION_TIMEOUT_IN_SECONDS) + except exceptions.NotFound: + pass + except Exception as exc: + warnings.warn( + f"Failed to disable drop protection for database {db.name}: {exc}" + ) + except exceptions.NotFound: + return + except Exception as exc: + warnings.warn(f"Failed to list databases for instance {to_scrub.name}: {exc}") - for database_pb in to_scrub.list_databases(): - db = to_scrub.database(database_pb.name.split("/")[-1]) - db.reload() - try: - if db.enable_drop_protection: - db.enable_drop_protection = False - operation = db.update(["enable_drop_protection"]) - operation.result(DATABASE_OPERATION_TIMEOUT_IN_SECONDS) - except exceptions.NotFound: - pass + scrub_instance_backups(to_scrub) try: retry_429_503(to_scrub.delete)() @@ -161,7 +171,10 @@ def cleanup_old_instances(spanner_client): instance = instance_mod.Instance.from_pb(instance_pb, spanner_client) if "created" in instance.labels: - create_time = int(instance.labels["created"]) + try: + create_time = int(instance.labels["created"]) + except ValueError: + continue if create_time <= cutoff: scrub_instance_ignore_not_found(instance) diff --git a/packages/google-cloud-spanner/tests/system/conftest.py b/packages/google-cloud-spanner/tests/system/conftest.py index 757068bebaa5..30c406307dba 100644 --- a/packages/google-cloud-spanner/tests/system/conftest.py +++ b/packages/google-cloud-spanner/tests/system/conftest.py @@ -223,25 +223,23 @@ def shared_instance( edition=spanner_instance_admin.Instance.Edition.ENTERPRISE, ), ) - created_op = _helpers.retry_429_503( - spanner_client.instance_admin_api.create_instance - )(request=request) - created_op.result(instance_operation_timeout) # block until completion - instance = spanner_client.instance( shared_instance_id, instance_config.name, labels=labels ) - + created_op = _helpers.retry_429_503( + spanner_client.instance_admin_api.create_instance + )(request=request) + try: + created_op.result(instance_operation_timeout) # block until completion + _helpers.cleanup_stale_databases(instance) + yield instance + finally: + _helpers.scrub_instance_ignore_not_found(instance) else: # reuse existing instance instance = spanner_client.instance(shared_instance_id) instance.reload() - - _helpers.cleanup_stale_databases(instance) - - yield instance - - if _helpers.CREATE_INSTANCE: - _helpers.retry_429_503(instance.delete)() + _helpers.cleanup_stale_databases(instance) + yield instance @pytest.fixture(scope="session")