Skip to content

Commit 86f81d1

Browse files
sangramqltseaver
authored andcommitted
Avoid leaking instances from snippets. (#7800)
1 parent a114633 commit 86f81d1

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

bigtable/docs/snippets.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
import pytest
3434

3535
from test_utils.system import unique_resource_id
36+
from google.api_core.exceptions import NotFound
3637
from google.cloud._helpers import UTC
3738
from google.cloud.bigtable import Client
3839
from google.cloud.bigtable import enums
3940

4041

41-
INSTANCE_ID = "snippet-" + unique_resource_id("-")
42+
INSTANCE_ID = "snippet-tests" + unique_resource_id("-")
4243
CLUSTER_ID = "clus-1-" + unique_resource_id("-")
4344
LOCATION_ID = "us-central1-f"
4445
ALT_LOCATION_ID = "us-central1-a"
@@ -52,6 +53,7 @@
5253
.strftime("%Y-%m-%dt%H-%M-%S")
5354
)
5455
LABELS = {LABEL_KEY: str(LABEL_STAMP)}
56+
INSTANCES_TO_DELETE = []
5557

5658

5759
class Config(object):
@@ -79,10 +81,15 @@ def setup_module():
7981
operation = Config.INSTANCE.create(clusters=[cluster])
8082
# We want to make sure the operation completes.
8183
operation.result(timeout=100)
84+
INSTANCES_TO_DELETE.append(Config.INSTANCE)
8285

8386

8487
def teardown_module():
85-
Config.INSTANCE.delete()
88+
for instance in INSTANCES_TO_DELETE:
89+
try:
90+
instance.delete()
91+
except NotFound:
92+
pass
8693

8794

8895
def test_bigtable_create_instance():
@@ -107,9 +114,14 @@ def test_bigtable_create_instance():
107114
default_storage_type=storage_type,
108115
)
109116
operation = instance.create(clusters=[cluster])
117+
118+
# Make sure this instance gets deleted after the test case.
119+
INSTANCES_TO_DELETE.append(instance)
120+
110121
# We want to make sure the operation completes.
111122
operation.result(timeout=100)
112123
# [END bigtable_create_prod_instance]
124+
113125
assert instance.exists()
114126
instance.delete()
115127

@@ -281,6 +293,9 @@ def test_bigtable_update_instance():
281293
# [END bigtable_update_instance]
282294
assert instance.display_name == display_name
283295

296+
# Make sure this instance gets deleted after the test case.
297+
INSTANCES_TO_DELETE.append(instance)
298+
284299

285300
def test_bigtable_update_cluster():
286301
# [START bigtable_update_cluster]
@@ -367,6 +382,10 @@ def test_bigtable_delete_instance():
367382
default_storage_type=STORAGE_TYPE,
368383
)
369384
operation = instance.create(clusters=[cluster])
385+
386+
# Make sure this instance gets deleted after the test case.
387+
INSTANCES_TO_DELETE.append(instance)
388+
370389
# We want to make sure the operation completes.
371390
operation.result(timeout=100)
372391

0 commit comments

Comments
 (0)