Skip to content

Commit 2da870e

Browse files
sangramqltseaver
authored andcommitted
Avoid creating table in 'list_tables' snippet; harden 'delete_instance' snippet. (#8879)
Closes #8479.
1 parent 23225bc commit 2da870e

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

bigtable/docs/snippets.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from test_utils.retry import RetryErrors
3737
from google.api_core.exceptions import NotFound
3838
from google.api_core.exceptions import TooManyRequests
39+
from google.api_core.exceptions import DeadlineExceeded
3940
from google.cloud._helpers import UTC
4041
from google.cloud.bigtable import Client
4142
from google.cloud.bigtable import enums
@@ -45,6 +46,7 @@
4546
INSTANCE_ID = "snippet-tests" + UNIQUE_SUFFIX
4647
CLUSTER_ID = "clus-1-" + UNIQUE_SUFFIX
4748
APP_PROFILE_ID = "app-prof" + UNIQUE_SUFFIX
49+
TABLE_ID = "tabl-1" + UNIQUE_SUFFIX
4850
ROUTING_POLICY_TYPE = enums.RoutingPolicyType.ANY
4951
LOCATION_ID = "us-central1-f"
5052
ALT_LOCATION_ID = "us-central1-a"
@@ -61,6 +63,7 @@
6163
INSTANCES_TO_DELETE = []
6264

6365
retry_429 = RetryErrors(TooManyRequests, max_tries=9)
66+
retry_504 = RetryErrors(DeadlineExceeded, max_tries=4)
6467

6568

6669
class Config(object):
@@ -72,6 +75,7 @@ class Config(object):
7275

7376
CLIENT = None
7477
INSTANCE = None
78+
TABLE = None
7579

7680

7781
def setup_module():
@@ -88,6 +92,8 @@ def setup_module():
8892
operation = Config.INSTANCE.create(clusters=[cluster])
8993
# We want to make sure the operation completes.
9094
operation.result(timeout=100)
95+
Config.TABLE = Config.INSTANCE.table(TABLE_ID)
96+
retry_504(Config.TABLE.create)()
9197

9298

9399
def teardown_module():
@@ -421,14 +427,6 @@ def test_bigtable_create_table():
421427

422428

423429
def test_bigtable_list_tables():
424-
from google.cloud.bigtable import Client
425-
from google.cloud.bigtable import column_family
426-
427-
client = Client(admin=True)
428-
instance = client.instance(INSTANCE_ID)
429-
table = instance.table("to_list")
430-
max_versions_rule = column_family.MaxVersionsGCRule(2)
431-
table.create(column_families={"cf1": max_versions_rule})
432430

433431
# [START bigtable_list_tables]
434432
from google.cloud.bigtable import Client
@@ -438,11 +436,9 @@ def test_bigtable_list_tables():
438436
tables_list = instance.list_tables()
439437
# [END bigtable_list_tables]
440438

439+
# Check if returned list has expected table
441440
table_names = [table.name for table in tables_list]
442-
try:
443-
assert table.name in table_names
444-
finally:
445-
retry_429(table.delete)()
441+
assert Config.TABLE.name in table_names
446442

447443

448444
def test_bigtable_delete_cluster():
@@ -479,9 +475,10 @@ def test_bigtable_delete_instance():
479475

480476
client = Client(admin=True)
481477

482-
instance = client.instance("inst-my-123", instance_type=PRODUCTION, labels=LABELS)
478+
instance_id = "snipt-inst-del" + UNIQUE_SUFFIX
479+
instance = client.instance(instance_id, instance_type=PRODUCTION, labels=LABELS)
483480
cluster = instance.cluster(
484-
"clus-my-123",
481+
"clus-to-delete" + UNIQUE_SUFFIX,
485482
location_id=ALT_LOCATION_ID,
486483
serve_nodes=1,
487484
default_storage_type=STORAGE_TYPE,
@@ -499,7 +496,6 @@ def test_bigtable_delete_instance():
499496

500497
client = Client(admin=True)
501498

502-
instance_id = "inst-my-123"
503499
instance_to_delete = client.instance(instance_id)
504500
instance_to_delete.delete()
505501
# [END bigtable_delete_instance]

0 commit comments

Comments
 (0)