From 93c09abc9754146a0094624173b01a7586ed3559 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 29 Jul 2021 18:51:55 -0400 Subject: [PATCH] tests: harden old instance cleanup against NotFound (#471) Toward #435, #436, #437. --- samples/samples/conftest.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/samples/samples/conftest.py b/samples/samples/conftest.py index 05c94f254f..9108a5892a 100644 --- a/samples/samples/conftest.py +++ b/samples/samples/conftest.py @@ -41,6 +41,17 @@ def spanner_client(): return client.Client() +def scrub_instance_ignore_not_found(to_scrub): + """Helper for func:`cleanup_old_instances`""" + try: + for backup_pb in to_scrub.list_backups(): + backup.Backup.from_pb(backup_pb, to_scrub).delete() + + to_scrub.delete() + except exceptions.NotFound: + pass + + @pytest.fixture(scope="session") def cleanup_old_instances(spanner_client): """Delete instances, created by samples, that are older than an hour.""" @@ -54,11 +65,7 @@ def cleanup_old_instances(spanner_client): create_time = int(inst.labels["created"]) if create_time <= cutoff: - - for backup_pb in inst.list_backups(): - backup.Backup.from_pb(backup_pb, inst).delete() - - inst.delete() + scrub_instance_ignore_not_found(inst) @pytest.fixture(scope="module") @@ -76,11 +83,7 @@ def instance_config(spanner_client): @pytest.fixture(scope="module") def sample_instance( - spanner_client, - cleanup_old_instances, - instance_id, - instance_config, - sample_name, + spanner_client, cleanup_old_instances, instance_id, instance_config, sample_name, ): sample_instance = spanner_client.instance( instance_id, @@ -88,7 +91,7 @@ def sample_instance( labels={ "cloud_spanner_samples": "true", "sample_name": sample_name, - "created": str(int(time.time())) + "created": str(int(time.time())), }, ) retry_429 = retry.RetryErrors(exceptions.ResourceExhausted, delay=15)