3333import pytest
3434
3535from test_utils .system import unique_resource_id
36+ from google .api_core .exceptions import NotFound
3637from google .cloud ._helpers import UTC
3738from google .cloud .bigtable import Client
3839from google .cloud .bigtable import enums
3940
4041
41- INSTANCE_ID = "snippet-" + unique_resource_id ("-" )
42+ INSTANCE_ID = "snippet-tests " + unique_resource_id ("-" )
4243CLUSTER_ID = "clus-1-" + unique_resource_id ("-" )
4344LOCATION_ID = "us-central1-f"
4445ALT_LOCATION_ID = "us-central1-a"
5253 .strftime ("%Y-%m-%dt%H-%M-%S" )
5354)
5455LABELS = {LABEL_KEY : str (LABEL_STAMP )}
56+ INSTANCES_TO_DELETE = []
5557
5658
5759class 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
8487def 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
8895def 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
285300def 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