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

Commit 5891cc7

Browse files
committed
addressed review feedback
1 parent ebe730a commit 5891cc7

3 files changed

Lines changed: 106 additions & 58 deletions

File tree

tests/system/admin_overlay/conftest.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from google.cloud.client import ClientWithProject
1+
import google.auth
22

3+
import os
34
import pytest
5+
import uuid
46

57

68
INSTANCE_PREFIX = "admin-overlay-instance"
@@ -20,7 +22,15 @@
2022

2123
@pytest.fixture(scope="session")
2224
def admin_overlay_project_id():
23-
# Instantiate a meaningless client to get project name
24-
# from environment variables.
25-
client = ClientWithProject()
26-
yield client.project
25+
_, default_project = google.auth.default()
26+
yield os.getenv("ADMIN_TEST_PROJECT") or default_project
27+
28+
29+
def generate_unique_suffix(name):
30+
"""
31+
Generates a unique suffix for the name.
32+
33+
Uses UUID4 because using time.time doesn't guarantee
34+
uniqueness when the time is frozen in containers.
35+
"""
36+
return f"{name}-{uuid.uuid4().hex[:7]}"

tests/system/admin_overlay/test_system_async.py

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
NUM_ROWS,
3232
INITIAL_CELL_VALUE,
3333
NEW_CELL_VALUE,
34+
generate_unique_suffix,
3435
)
3536

3637
from datetime import datetime, timedelta
3738

3839
import asyncio
3940
import pytest
40-
import time
4141

4242

4343
if CrossSync.is_async:
@@ -61,56 +61,59 @@ def event_loop():
6161

6262
@CrossSync.convert
6363
@CrossSync.pytest_fixture(scope="session")
64-
async def data_client():
65-
client = CrossSync.DataClient()
66-
yield client
67-
await client.close()
64+
async def data_client(admin_overlay_project_id):
65+
async with CrossSync.DataClient(project=admin_overlay_project_id) as client:
66+
yield client
6867

6968

7069
@CrossSync.convert(
7170
replace_symbols={"BigtableTableAdminAsyncClient": "BigtableTableAdminClient"}
7271
)
7372
@CrossSync.pytest_fixture(scope="session")
74-
async def table_admin_client():
75-
client = admin_v2.BigtableTableAdminAsyncClient()
76-
yield client
77-
await client.transport.close()
73+
async def table_admin_client(admin_overlay_project_id):
74+
async with admin_v2.BigtableTableAdminAsyncClient(
75+
client_options={
76+
"quota_project_id": admin_overlay_project_id,
77+
}
78+
) as client:
79+
yield client
7880

7981

8082
@CrossSync.convert(
8183
replace_symbols={"BigtableInstanceAdminAsyncClient": "BigtableInstanceAdminClient"}
8284
)
8385
@CrossSync.pytest_fixture(scope="session")
84-
async def instance_admin_client():
85-
client = admin_v2.BigtableInstanceAdminAsyncClient()
86-
yield client
87-
await client.transport.close()
86+
async def instance_admin_client(admin_overlay_project_id):
87+
async with admin_v2.BigtableInstanceAdminAsyncClient(
88+
client_options={
89+
"quota_project_id": admin_overlay_project_id,
90+
}
91+
) as client:
92+
yield client
8893

8994

9095
@CrossSync.convert
9196
@CrossSync.pytest_fixture(scope="session")
9297
async def instances_to_delete(instance_admin_client):
9398
instances = []
9499

95-
yield instances
96-
97-
for instance in instances:
98-
await instance_admin_client.delete_instance(name=instance.name)
100+
try:
101+
yield instances
102+
finally:
103+
for instance in instances:
104+
await instance_admin_client.delete_instance(name=instance.name)
99105

100106

101107
@CrossSync.convert
102108
@CrossSync.pytest_fixture(scope="session")
103109
async def backups_to_delete(table_admin_client):
104110
backups = []
105111

106-
yield backups
107-
108-
for backup in backups:
109-
await table_admin_client.delete_backup(name=backup.name)
110-
111-
112-
def ctime() -> int:
113-
return int(time.time())
112+
try:
113+
yield backups
114+
finally:
115+
for backup in backups:
116+
await table_admin_client.delete_backup(name=backup.name)
114117

115118

116119
@CrossSync.convert
@@ -123,10 +126,16 @@ async def create_instance(
123126
storage_type=admin_v2.StorageType.HDD,
124127
cluster_locations=DEFAULT_CLUSTER_LOCATIONS,
125128
) -> Tuple[admin_v2.Instance, admin_v2.Table]:
129+
"""
130+
Creates a new Bigtable instance with the specified project_id, storage type, and cluster locations.
131+
132+
After creating the Bigtable instance, it will create a test table and populate it with dummy data.
133+
This is not defined as a fixture because the different system tests need different kinds of instances.
134+
"""
126135
# Create the instance
127136
clusters = {}
128137

129-
instance_id = f"{INSTANCE_PREFIX}-{ctime()}"
138+
instance_id = generate_unique_suffix(INSTANCE_PREFIX)
130139

131140
for idx, location in enumerate(cluster_locations):
132141
clusters[location] = admin_v2.Cluster(
@@ -175,6 +184,12 @@ async def create_instance(
175184

176185
@CrossSync.convert
177186
async def populate_table(table_admin_client, data_client, instance, table, cell_value):
187+
"""
188+
Populates all the test cells in the given table with the given cell value.
189+
190+
This is used to populate test data when creating an instance, and for testing the
191+
wait_for_consistency call.
192+
"""
178193
data_client_table = data_client.get_table(
179194
table_admin_client.parse_instance_path(instance.name)["instance"],
180195
table_admin_client.parse_table_path(table.name)["table"],
@@ -202,13 +217,19 @@ async def populate_table(table_admin_client, data_client, instance, table, cell_
202217
async def create_backup(
203218
instance_admin_client, table_admin_client, instance, table, backups_to_delete
204219
) -> admin_v2.Backup:
220+
"""
221+
Creates a backup of the given table under the given instance.
222+
223+
This will be restored to a different instance later on, to test
224+
optimize_restored_table.
225+
"""
205226
# Get a cluster in the instance for the backup
206227
list_clusters_response = await instance_admin_client.list_clusters(
207228
parent=instance.name
208229
)
209230
cluster_name = list_clusters_response.clusters[0].name
210231

211-
backup_id = f"{BACKUP_PREFIX}-{ctime()}"
232+
backup_id = generate_unique_suffix(BACKUP_PREFIX)
212233

213234
# Create the backup
214235
operation = await table_admin_client.create_backup(
@@ -232,6 +253,9 @@ async def create_backup(
232253
async def assert_table_cell_value_equal_to(
233254
table_admin_client, data_client, instance, table, value
234255
):
256+
"""
257+
Asserts that all cells in the given table have the given value.
258+
"""
235259
data_client_table = data_client.get_table(
236260
table_admin_client.parse_instance_path(instance.name)["instance"],
237261
table_admin_client.parse_table_path(table.name)["table"],
@@ -330,7 +354,6 @@ async def test_optimize_restored_table(
330354
)
331355

332356

333-
@CrossSync.convert
334357
@CrossSync.pytest
335358
async def test_wait_for_consistency(
336359
instance_admin_client,

tests/system/admin_overlay/test_system_autogen.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,53 @@
3232
NUM_ROWS,
3333
INITIAL_CELL_VALUE,
3434
NEW_CELL_VALUE,
35+
generate_unique_suffix,
3536
)
3637
from datetime import datetime, timedelta
3738
import pytest
38-
import time
3939
from google.api_core import operation as api_core_operation
4040

4141

4242
@pytest.fixture(scope="session")
43-
def data_client():
44-
client = CrossSync._Sync_Impl.DataClient()
45-
yield client
46-
client.close()
43+
def data_client(admin_overlay_project_id):
44+
with CrossSync._Sync_Impl.DataClient(project=admin_overlay_project_id) as client:
45+
yield client
4746

4847

4948
@pytest.fixture(scope="session")
50-
def table_admin_client():
51-
client = admin_v2.BigtableTableAdminClient()
52-
yield client
53-
client.transport.close()
49+
def table_admin_client(admin_overlay_project_id):
50+
with admin_v2.BigtableTableAdminClient(
51+
client_options={"quota_project_id": admin_overlay_project_id}
52+
) as client:
53+
yield client
5454

5555

5656
@pytest.fixture(scope="session")
57-
def instance_admin_client():
58-
client = admin_v2.BigtableInstanceAdminClient()
59-
yield client
60-
client.transport.close()
57+
def instance_admin_client(admin_overlay_project_id):
58+
with admin_v2.BigtableInstanceAdminClient(
59+
client_options={"quota_project_id": admin_overlay_project_id}
60+
) as client:
61+
yield client
6162

6263

6364
@pytest.fixture(scope="session")
6465
def instances_to_delete(instance_admin_client):
6566
instances = []
66-
yield instances
67-
for instance in instances:
68-
instance_admin_client.delete_instance(name=instance.name)
67+
try:
68+
yield instances
69+
finally:
70+
for instance in instances:
71+
instance_admin_client.delete_instance(name=instance.name)
6972

7073

7174
@pytest.fixture(scope="session")
7275
def backups_to_delete(table_admin_client):
7376
backups = []
74-
yield backups
75-
for backup in backups:
76-
table_admin_client.delete_backup(name=backup.name)
77-
78-
79-
def ctime() -> int:
80-
return int(time.time())
77+
try:
78+
yield backups
79+
finally:
80+
for backup in backups:
81+
table_admin_client.delete_backup(name=backup.name)
8182

8283

8384
def create_instance(
@@ -89,8 +90,13 @@ def create_instance(
8990
storage_type=admin_v2.StorageType.HDD,
9091
cluster_locations=DEFAULT_CLUSTER_LOCATIONS,
9192
) -> Tuple[admin_v2.Instance, admin_v2.Table]:
93+
"""Creates a new Bigtable instance with the specified project_id, storage type, and cluster locations.
94+
95+
After creating the Bigtable instance, it will create a test table and populate it with dummy data.
96+
This is not defined as a fixture because the different system tests need different kinds of instances.
97+
"""
9298
clusters = {}
93-
instance_id = f"{INSTANCE_PREFIX}-{ctime()}"
99+
instance_id = generate_unique_suffix(INSTANCE_PREFIX)
94100
for idx, location in enumerate(cluster_locations):
95101
clusters[location] = admin_v2.Cluster(
96102
name=instance_admin_client.cluster_path(
@@ -121,6 +127,10 @@ def create_instance(
121127

122128

123129
def populate_table(table_admin_client, data_client, instance, table, cell_value):
130+
"""Populates all the test cells in the given table with the given cell value.
131+
132+
This is used to populate test data when creating an instance, and for testing the
133+
wait_for_consistency call."""
124134
data_client_table = data_client.get_table(
125135
table_admin_client.parse_instance_path(instance.name)["instance"],
126136
table_admin_client.parse_table_path(table.name)["table"],
@@ -146,9 +156,13 @@ def populate_table(table_admin_client, data_client, instance, table, cell_value)
146156
def create_backup(
147157
instance_admin_client, table_admin_client, instance, table, backups_to_delete
148158
) -> admin_v2.Backup:
159+
"""Creates a backup of the given table under the given instance.
160+
161+
This will be restored to a different instance later on, to test
162+
optimize_restored_table."""
149163
list_clusters_response = instance_admin_client.list_clusters(parent=instance.name)
150164
cluster_name = list_clusters_response.clusters[0].name
151-
backup_id = f"{BACKUP_PREFIX}-{ctime()}"
165+
backup_id = generate_unique_suffix(BACKUP_PREFIX)
152166
operation = table_admin_client.create_backup(
153167
admin_v2.CreateBackupRequest(
154168
parent=cluster_name,
@@ -168,6 +182,7 @@ def create_backup(
168182
def assert_table_cell_value_equal_to(
169183
table_admin_client, data_client, instance, table, value
170184
):
185+
"""Asserts that all cells in the given table have the given value."""
171186
data_client_table = data_client.get_table(
172187
table_admin_client.parse_instance_path(instance.name)["instance"],
173188
table_admin_client.parse_table_path(table.name)["table"],

0 commit comments

Comments
 (0)