diff --git a/samples/samples/conftest.py b/samples/samples/conftest.py index 4c6c51301e..4943c94ed4 100644 --- a/samples/samples/conftest.py +++ b/samples/samples/conftest.py @@ -15,6 +15,7 @@ import time import uuid +from random import randrange from google.api_core import exceptions @@ -26,8 +27,7 @@ from google.cloud.spanner_v1 import instance import pytest from test_utils import retry - -from tests.system import _helpers +from test_utils import system INSTANCE_CREATION_TIMEOUT = 560 # seconds @@ -82,17 +82,18 @@ def scrub_instance_ignore_not_found(to_scrub): @pytest.fixture(scope="session") def cleanup_old_instances(spanner_client): """Delete instances, created by samples, that are older than an hour.""" - cutoff = int(time.time()) - 1 * 60 * 60 - instance_filter = "labels.cloud_spanner_samples:true" + if CREATE_INSTANCE: + cutoff = int(time.time()) - 1 * 60 * 60 + instance_filter = "labels.cloud_spanner_samples:true" - for instance_pb in spanner_client.list_instances(filter_=instance_filter): - inst = instance.Instance.from_pb(instance_pb, spanner_client) + for instance_pb in spanner_client.list_instances(filter_=instance_filter): + inst = instance.Instance.from_pb(instance_pb, spanner_client) - if "created" in inst.labels: - create_time = int(inst.labels["created"]) + if "created" in inst.labels: + create_time = int(inst.labels["created"]) - if create_time <= cutoff: - scrub_instance_ignore_not_found(inst) + if create_time <= cutoff: + scrub_instance_ignore_not_found(inst) @pytest.fixture(scope="module") @@ -202,7 +203,7 @@ def database_id(): Sample testcase modules can override as needed. """ - return _helpers.unique_id("dbapi-txn") + return unique_id("dbapi-txn") @pytest.fixture(scope="module") @@ -304,3 +305,6 @@ def kms_key_name(spanner_client): "spanner-test-keyring", "spanner-test-cmek", ) + +def unique_id(prefix, separator="-"): + return f"{prefix}{system.unique_resource_id(separator)}{randrange(100)}" \ No newline at end of file diff --git a/tests/system/test_database_api.py b/tests/system/test_database_api.py index 7ff26ba70c..f7a233ff57 100644 --- a/tests/system/test_database_api.py +++ b/tests/system/test_database_api.py @@ -342,7 +342,7 @@ def test_update_ddl_w_pitr_invalid( databases_to_delete, ): pool = spanner_v1.BurstyPool(labels={"testcase": "update_database_ddl_pitr"}) - temp_db_id = _helpers.unique_id("pitr_upd_ddl_inv", separator="_") + temp_db_id = _helpers.unique_id("pitr_upd_ddl", separator="_") retention_period = "0d" temp_db = shared_instance.database(temp_db_id, pool=pool) @@ -367,7 +367,7 @@ def test_update_ddl_w_pitr_success( databases_to_delete, ): pool = spanner_v1.BurstyPool(labels={"testcase": "update_database_ddl_pitr"}) - temp_db_id = _helpers.unique_id("pitr_upd_ddl_inv", separator="_") + temp_db_id = _helpers.unique_id("pitr_upd_ddl", separator="_") retention_period = "7d" temp_db = shared_instance.database(temp_db_id, pool=pool) @@ -399,7 +399,7 @@ def test_update_ddl_w_default_leader_success( labels={"testcase": "update_database_ddl_default_leader"}, ) - temp_db_id = _helpers.unique_id("dfl_ldrr_upd_ddl", separator="_") + temp_db_id = _helpers.unique_id("dfl_upd_ddl", separator="_") default_leader = "us-east4" temp_db = multiregion_instance.database(temp_db_id, pool=pool)