Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 2b74f9c

Browse files
authored
test: improve cleanup to prevent RESOURCE_EXHAUSTED error (#195)
* test: add labels to test instance for cleanup * test: add labels to dbapi test instance for cleanup * style: fix lint errors Co-authored-by: larkee <larkee@users.noreply.github.com>
1 parent 1501022 commit 2b74f9c

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

tests/system/test_system.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ def setUpModule():
113113
instances = retry(_list_instances)()
114114
EXISTING_INSTANCES[:] = instances
115115

116+
# Delete test instances that are older than an hour.
117+
cutoff = int(time.time()) - 1 * 60 * 60
118+
for instance in Config.CLIENT.list_instances("labels.python-spanner-systests:true"):
119+
if "created" not in instance.labels:
120+
continue
121+
create_time = int(instance.labels["created"])
122+
if create_time > cutoff:
123+
continue
124+
# Instance cannot be deleted while backups exist.
125+
for backup in instance.list_backups():
126+
backup.delete()
127+
instance.delete()
128+
116129
if CREATE_INSTANCE:
117130
if not USE_EMULATOR:
118131
# Defend against back-end returning configs for regions we aren't
@@ -124,8 +137,12 @@ def setUpModule():
124137

125138
Config.INSTANCE_CONFIG = configs[0]
126139
config_name = configs[0].name
140+
create_time = str(int(time.time()))
141+
labels = {"python-spanner-systests": "true", "created": create_time}
127142

128-
Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID, config_name)
143+
Config.INSTANCE = Config.CLIENT.instance(
144+
INSTANCE_ID, config_name, labels=labels
145+
)
129146
created_op = Config.INSTANCE.create()
130147
created_op.result(30) # block until completion
131148

@@ -466,8 +483,10 @@ def setUpClass(cls):
466483

467484
current_config = Config.INSTANCE.configuration_name
468485
same_config_instance_id = "same-config" + unique_resource_id("-")
486+
create_time = str(int(time.time()))
487+
labels = {"python-spanner-systests": "true", "created": create_time}
469488
cls._same_config_instance = Config.CLIENT.instance(
470-
same_config_instance_id, current_config
489+
same_config_instance_id, current_config, labels=labels
471490
)
472491
op = cls._same_config_instance.create()
473492
op.result(30)
@@ -483,8 +502,10 @@ def setUpClass(cls):
483502
cls._diff_config_instance = None
484503
if len(diff_configs) > 0:
485504
diff_config_instance_id = "diff-config" + unique_resource_id("-")
505+
create_time = str(int(time.time()))
506+
labels = {"python-spanner-systests": "true", "created": create_time}
486507
cls._diff_config_instance = Config.CLIENT.instance(
487-
diff_config_instance_id, diff_configs[0]
508+
diff_config_instance_id, diff_configs[0], labels=labels
488509
)
489510
op = cls._diff_config_instance.create()
490511
op.result(30)

tests/system/test_system_dbapi.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import hashlib
1616
import os
1717
import pickle
18+
import time
1819
import unittest
1920

2021
from google.api_core import exceptions
@@ -53,6 +54,21 @@ def setUpModule():
5354
instances = retry(_list_instances)()
5455
EXISTING_INSTANCES[:] = instances
5556

57+
# Delete test instances that are older than an hour.
58+
cutoff = int(time.time()) - 1 * 60 * 60
59+
for instance in Config.CLIENT.list_instances(
60+
"labels.python-spanner-dbapi-systests:true"
61+
):
62+
if "created" not in instance.labels:
63+
continue
64+
create_time = int(instance.labels["created"])
65+
if create_time > cutoff:
66+
continue
67+
# Instance cannot be deleted while backups exist.
68+
for backup in instance.list_backups():
69+
backup.delete()
70+
instance.delete()
71+
5672
if CREATE_INSTANCE:
5773
if not USE_EMULATOR:
5874
# Defend against back-end returning configs for regions we aren't
@@ -64,8 +80,12 @@ def setUpModule():
6480

6581
Config.INSTANCE_CONFIG = configs[0]
6682
config_name = configs[0].name
83+
create_time = str(int(time.time()))
84+
labels = {"python-spanner-dbapi-systests": "true", "created": create_time}
6785

68-
Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID, config_name)
86+
Config.INSTANCE = Config.CLIENT.instance(
87+
INSTANCE_ID, config_name, labels=labels
88+
)
6989
created_op = Config.INSTANCE.create()
7090
created_op.result(30) # block until completion
7191

0 commit comments

Comments
 (0)