Skip to content

Commit

Permalink
tests: harden old instance cleanup against NotFound (#471)
Browse files Browse the repository at this point in the history
Toward #435, #436, #437.
  • Loading branch information
tseaver committed Jul 29, 2021
1 parent 2487800 commit 93c09ab
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions samples/samples/conftest.py
Expand Up @@ -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."""
Expand All @@ -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")
Expand All @@ -76,19 +83,15 @@ 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,
instance_config,
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)
Expand Down

0 comments on commit 93c09ab

Please sign in to comment.