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

Commit 65cd359

Browse files
committed
moved exported metrics test to main system test class
1 parent 5ccb57c commit 65cd359

7 files changed

Lines changed: 294 additions & 205 deletions

File tree

google/cloud/bigtable/data/_sync_autogen/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ def __init__(
157157
credentials = google.auth.credentials.AnonymousCredentials()
158158
if project is None:
159159
project = _DEFAULT_BIGTABLE_EMULATOR_CLIENT
160-
self._gcp_metrics_exporter = BigtableMetricsExporter(
161-
project_id=project, credentials=credentials, client_options=client_options
162-
)
163160
self._metrics_interceptor = MetricInterceptorType()
164161
ClientWithProject.__init__(
165162
self,
@@ -183,6 +180,11 @@ def __init__(
183180
raise ValueError(
184181
f"The configured universe domain ({self.universe_domain}) does not match the universe domain found in the credentials ({self._credentials.universe_domain}). If you haven't configured the universe domain explicitly, `googleapis.com` is the default."
185182
)
183+
self._gcp_metrics_exporter = BigtableMetricsExporter(
184+
project_id=self.project,
185+
credentials=credentials,
186+
client_options=client_options,
187+
)
186188
self._is_closed = CrossSync._Sync_Impl.Event()
187189
self.transport = cast(TransportType, self._gapic_client.transport)
188190
self._active_instances: Set[_WarmedInstanceKey] = set()

tests/system/data/__init__.py

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@
1515
#
1616
import pytest
1717
import uuid
18+
import os
19+
import datetime
1820

1921
TEST_FAMILY = "test-family"
2022
TEST_FAMILY_2 = "test-family-2"
2123
TEST_AGGREGATE_FAMILY = "test-aggregate-family"
2224

2325

26+
# authorized view subset to allow all qualifiers
27+
ALLOW_ALL = ""
28+
ALL_QUALIFIERS = {"qualifier_prefixes": [ALLOW_ALL]}
29+
30+
2431
class SystemTestRunner:
2532
"""
2633
configures a system test class with configuration for clusters/tables/etc
2734
2835
used by standard system tests, and metrics tests
2936
"""
3037

31-
@pytest.fixture(scope="session")
32-
def init_table_id(self):
33-
"""
34-
The table_id to use when creating a new test table
35-
"""
36-
return f"test-table-{uuid.uuid4().hex}"
37-
3838
@pytest.fixture(scope="session")
3939
def cluster_config(self, project_id):
4040
"""
@@ -68,3 +68,132 @@ def column_family_config(self):
6868
value_type=types.Type(aggregate_type=int_aggregate_type)
6969
),
7070
}
71+
72+
@pytest.fixture(scope="session")
73+
def start_timestamp(self):
74+
"""
75+
A timestamp taken before any tests are run. Used to fetch back metrics relevant to the tests
76+
"""
77+
return datetime.datetime.now(datetime.timezone.utc)
78+
79+
def _generate_table_id(self):
80+
return f"test-table-{uuid.uuid4().hex}"
81+
82+
@pytest.fixture(scope="session")
83+
def table_id(
84+
self,
85+
admin_client,
86+
project_id,
87+
instance_id,
88+
column_family_config,
89+
init_table_id,
90+
column_split_config,
91+
start_timestamp,
92+
):
93+
"""
94+
Returns BIGTABLE_TEST_TABLE if set, otherwise creates a new temporary table for the test session
95+
96+
Args:
97+
- admin_client: Client for interacting with the Table Admin API. Supplied by the admin_client fixture.
98+
- project_id: The project ID of the GCP project to test against. Supplied by the project_id fixture.
99+
- instance_id: The ID of the Bigtable instance to test against. Supplied by the instance_id fixture.
100+
- init_column_families: A list of column families to initialize the table with, if pre-initialized table is not given with BIGTABLE_TEST_TABLE.
101+
Supplied by the init_column_families fixture.
102+
- init_table_id: The table ID to give to the test table, if pre-initialized table is not given with BIGTABLE_TEST_TABLE.
103+
Supplied by the init_table_id fixture.
104+
- column_split_config: A list of row keys to use as initial splits when creating the test table.
105+
- start_timestamp: accessed when building table to ensure timestamp data is loaded before tests are run
106+
"""
107+
from google.api_core import exceptions
108+
from google.api_core import retry
109+
110+
# use user-specified instance if available
111+
user_specified_table = os.getenv("BIGTABLE_TEST_TABLE")
112+
if user_specified_table:
113+
print("Using user-specified table: {}".format(user_specified_table))
114+
yield user_specified_table
115+
return
116+
117+
retry = retry.Retry(
118+
predicate=retry.if_exception_type(exceptions.FailedPrecondition)
119+
)
120+
try:
121+
parent_path = f"projects/{project_id}/instances/{instance_id}"
122+
print(f"Creating table: {parent_path}/tables/{init_table_id}")
123+
admin_client.table_admin_client.create_table(
124+
request={
125+
"parent": parent_path,
126+
"table_id": init_table_id,
127+
"table": {"column_families": column_family_config},
128+
"initial_splits": [{"key": key} for key in column_split_config],
129+
},
130+
retry=retry,
131+
)
132+
except exceptions.AlreadyExists:
133+
pass
134+
yield init_table_id
135+
print(f"Deleting table: {parent_path}/tables/{init_table_id}")
136+
try:
137+
admin_client.table_admin_client.delete_table(
138+
name=f"{parent_path}/tables/{init_table_id}"
139+
)
140+
except exceptions.NotFound:
141+
print(f"Table {init_table_id} not found, skipping deletion")
142+
143+
@pytest.fixture(scope="session")
144+
def authorized_view_id(
145+
self,
146+
admin_client,
147+
project_id,
148+
instance_id,
149+
table_id,
150+
):
151+
"""
152+
Creates and returns a new temporary authorized view for the test session
153+
154+
Args:
155+
- admin_client: Client for interacting with the Table Admin API. Supplied by the admin_client fixture.
156+
- project_id: The project ID of the GCP project to test against. Supplied by the project_id fixture.
157+
- instance_id: The ID of the Bigtable instance to test against. Supplied by the instance_id fixture.
158+
- table_id: The ID of the table to create the authorized view for. Supplied by the table_id fixture.
159+
"""
160+
from google.api_core import exceptions
161+
from google.api_core import retry
162+
163+
retry = retry.Retry(
164+
predicate=retry.if_exception_type(exceptions.FailedPrecondition)
165+
)
166+
new_view_id = uuid.uuid4().hex[:8]
167+
parent_path = f"projects/{project_id}/instances/{instance_id}/tables/{table_id}"
168+
new_path = f"{parent_path}/authorizedViews/{new_view_id}"
169+
try:
170+
print(f"Creating view: {new_path}")
171+
admin_client.table_admin_client.create_authorized_view(
172+
request={
173+
"parent": parent_path,
174+
"authorized_view_id": new_view_id,
175+
"authorized_view": {
176+
"subset_view": {
177+
"row_prefixes": [ALLOW_ALL],
178+
"family_subsets": {
179+
TEST_FAMILY: ALL_QUALIFIERS,
180+
TEST_FAMILY_2: ALL_QUALIFIERS,
181+
TEST_AGGREGATE_FAMILY: ALL_QUALIFIERS,
182+
},
183+
},
184+
},
185+
},
186+
retry=retry,
187+
)
188+
except exceptions.AlreadyExists:
189+
pass
190+
except exceptions.MethodNotImplemented:
191+
# will occur when run in emulator. Pass empty id
192+
new_view_id = None
193+
yield new_view_id
194+
if new_view_id:
195+
print(f"Deleting view: {new_path}")
196+
try:
197+
admin_client.table_admin_client.delete_authorized_view(name=new_path)
198+
except exceptions.NotFound:
199+
print(f"View {new_view_id} not found, skipping deletion")

tests/system/data/setup_fixtures.py

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
import uuid
2222
import datetime
2323

24-
from . import TEST_FAMILY, TEST_FAMILY_2, TEST_AGGREGATE_FAMILY
25-
26-
# authorized view subset to allow all qualifiers
27-
ALLOW_ALL = ""
28-
ALL_QUALIFIERS = {"qualifier_prefixes": [ALLOW_ALL]}
29-
3024

3125
@pytest.fixture(scope="session")
3226
def admin_client():
@@ -87,131 +81,6 @@ def column_split_config():
8781
"""
8882
return [(num * 1000).to_bytes(8, "big") for num in range(1, 10)]
8983

90-
@pytest.fixture(scope="session")
91-
def start_timestamp():
92-
"""
93-
A timestamp taken before any tests are run. Used to fetch back metrics relevant to the tests
94-
"""
95-
return datetime.datetime.now(datetime.timezone.utc)
96-
97-
@pytest.fixture(scope="session")
98-
def table_id(
99-
admin_client,
100-
project_id,
101-
instance_id,
102-
column_family_config,
103-
init_table_id,
104-
column_split_config,
105-
start_timestamp,
106-
):
107-
"""
108-
Returns BIGTABLE_TEST_TABLE if set, otherwise creates a new temporary table for the test session
109-
110-
Args:
111-
- admin_client: Client for interacting with the Table Admin API. Supplied by the admin_client fixture.
112-
- project_id: The project ID of the GCP project to test against. Supplied by the project_id fixture.
113-
- instance_id: The ID of the Bigtable instance to test against. Supplied by the instance_id fixture.
114-
- init_column_families: A list of column families to initialize the table with, if pre-initialized table is not given with BIGTABLE_TEST_TABLE.
115-
Supplied by the init_column_families fixture.
116-
- init_table_id: The table ID to give to the test table, if pre-initialized table is not given with BIGTABLE_TEST_TABLE.
117-
Supplied by the init_table_id fixture.
118-
- column_split_config: A list of row keys to use as initial splits when creating the test table.
119-
- start_timestamp: accessed when building table to ensure timestamp data is loaded before tests are run
120-
"""
121-
from google.api_core import exceptions
122-
from google.api_core import retry
123-
124-
# use user-specified instance if available
125-
user_specified_table = os.getenv("BIGTABLE_TEST_TABLE")
126-
if user_specified_table:
127-
print("Using user-specified table: {}".format(user_specified_table))
128-
yield user_specified_table
129-
return
130-
131-
retry = retry.Retry(
132-
predicate=retry.if_exception_type(exceptions.FailedPrecondition)
133-
)
134-
try:
135-
parent_path = f"projects/{project_id}/instances/{instance_id}"
136-
print(f"Creating table: {parent_path}/tables/{init_table_id}")
137-
admin_client.table_admin_client.create_table(
138-
request={
139-
"parent": parent_path,
140-
"table_id": init_table_id,
141-
"table": {"column_families": column_family_config},
142-
"initial_splits": [{"key": key} for key in column_split_config],
143-
},
144-
retry=retry,
145-
)
146-
except exceptions.AlreadyExists:
147-
pass
148-
yield init_table_id
149-
print(f"Deleting table: {parent_path}/tables/{init_table_id}")
150-
try:
151-
admin_client.table_admin_client.delete_table(
152-
name=f"{parent_path}/tables/{init_table_id}"
153-
)
154-
except exceptions.NotFound:
155-
print(f"Table {init_table_id} not found, skipping deletion")
156-
157-
158-
@pytest.fixture(scope="session")
159-
def authorized_view_id(
160-
admin_client,
161-
project_id,
162-
instance_id,
163-
table_id,
164-
):
165-
"""
166-
Creates and returns a new temporary authorized view for the test session
167-
168-
Args:
169-
- admin_client: Client for interacting with the Table Admin API. Supplied by the admin_client fixture.
170-
- project_id: The project ID of the GCP project to test against. Supplied by the project_id fixture.
171-
- instance_id: The ID of the Bigtable instance to test against. Supplied by the instance_id fixture.
172-
- table_id: The ID of the table to create the authorized view for. Supplied by the table_id fixture.
173-
"""
174-
from google.api_core import exceptions
175-
from google.api_core import retry
176-
177-
retry = retry.Retry(
178-
predicate=retry.if_exception_type(exceptions.FailedPrecondition)
179-
)
180-
new_view_id = uuid.uuid4().hex[:8]
181-
parent_path = f"projects/{project_id}/instances/{instance_id}/tables/{table_id}"
182-
new_path = f"{parent_path}/authorizedViews/{new_view_id}"
183-
try:
184-
print(f"Creating view: {new_path}")
185-
admin_client.table_admin_client.create_authorized_view(
186-
request={
187-
"parent": parent_path,
188-
"authorized_view_id": new_view_id,
189-
"authorized_view": {
190-
"subset_view": {
191-
"row_prefixes": [ALLOW_ALL],
192-
"family_subsets": {
193-
TEST_FAMILY: ALL_QUALIFIERS,
194-
TEST_FAMILY_2: ALL_QUALIFIERS,
195-
TEST_AGGREGATE_FAMILY: ALL_QUALIFIERS,
196-
},
197-
},
198-
},
199-
},
200-
retry=retry,
201-
)
202-
except exceptions.AlreadyExists:
203-
pass
204-
except exceptions.MethodNotImplemented:
205-
# will occur when run in emulator. Pass empty id
206-
new_view_id = None
207-
yield new_view_id
208-
if new_view_id:
209-
print(f"Deleting view: {new_path}")
210-
try:
211-
admin_client.table_admin_client.delete_authorized_view(name=new_path)
212-
except exceptions.NotFound:
213-
print(f"View {new_view_id} not found, skipping deletion")
214-
21584

21685
@pytest.fixture(scope="session")
21786
def project_id(client):

0 commit comments

Comments
 (0)