This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathconftest.py
More file actions
289 lines (220 loc) · 8.65 KB
/
conftest.py
File metadata and controls
289 lines (220 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright 2021 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import time
import pytest
from google.cloud import spanner_v1
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
from . import _helpers
from google.cloud.spanner_admin_database_v1.types.backup import (
CreateBackupEncryptionConfig,
)
@pytest.fixture(scope="function")
def if_create_instance():
if not _helpers.CREATE_INSTANCE:
pytest.skip(f"{_helpers.CREATE_INSTANCE_ENVVAR} not set in environment.")
@pytest.fixture(scope="function")
def no_create_instance():
if _helpers.CREATE_INSTANCE:
pytest.skip(f"{_helpers.CREATE_INSTANCE_ENVVAR} set in environment.")
@pytest.fixture(scope="function")
def if_backup_tests():
if _helpers.SKIP_BACKUP_TESTS:
pytest.skip(f"{_helpers.SKIP_BACKUP_TESTS_ENVVAR} set in environment.")
@pytest.fixture(scope="function")
def not_emulator():
if _helpers.USE_EMULATOR:
pytest.skip(f"{_helpers.USE_EMULATOR_ENVVAR} set in environment.")
@pytest.fixture(scope="module")
def not_experimental_host():
if _helpers.USE_EXPERIMENTAL_HOST:
pytest.skip(f"{_helpers.USE_EXPERIMENTAL_HOST_ENVVAR} set in environment.")
@pytest.fixture(scope="session")
def not_postgres(database_dialect):
if database_dialect == DatabaseDialect.POSTGRESQL:
pytest.skip(
f"{_helpers.DATABASE_DIALECT_ENVVAR} set to POSTGRES in environment."
)
@pytest.fixture(scope="session")
def not_google_standard_sql(database_dialect):
if database_dialect == DatabaseDialect.GOOGLE_STANDARD_SQL:
pytest.skip(
f"{_helpers.DATABASE_DIALECT_ENVVAR} set to GOOGLE_STANDARD_SQL in environment."
)
@pytest.fixture(scope="session")
def not_postgres_emulator(database_dialect):
if database_dialect == DatabaseDialect.POSTGRESQL and _helpers.USE_EMULATOR:
pytest.skip(
f"{_helpers.DATABASE_DIALECT_ENVVAR} set to POSTGRESQL and {_helpers.USE_EMULATOR_ENVVAR} set in "
"environment."
)
@pytest.fixture(scope="session")
def database_dialect():
return (
DatabaseDialect[_helpers.DATABASE_DIALECT]
if _helpers.DATABASE_DIALECT
else DatabaseDialect.GOOGLE_STANDARD_SQL
)
@pytest.fixture(scope="session")
def proto_descriptor_file():
import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, "testdata/descriptors.pb")
file = open(filename, "rb")
yield file.read()
file.close()
@pytest.fixture(scope="session")
def spanner_client():
if _helpers.USE_EMULATOR:
from google.auth.credentials import AnonymousCredentials
credentials = AnonymousCredentials()
return spanner_v1.Client(
project=_helpers.EMULATOR_PROJECT,
credentials=credentials,
)
elif _helpers.USE_EXPERIMENTAL_HOST:
from google.auth.credentials import AnonymousCredentials
credentials = AnonymousCredentials()
return spanner_v1.Client(
use_plain_text=_helpers.USE_PLAIN_TEXT,
ca_certificate=_helpers.CA_CERTIFICATE,
client_certificate=_helpers.CLIENT_CERTIFICATE,
client_key=_helpers.CLIENT_KEY,
credentials=credentials,
experimental_host=_helpers.EXPERIMENTAL_HOST,
)
else:
client_options = {"api_endpoint": _helpers.API_ENDPOINT}
return spanner_v1.Client(
client_options=client_options
) # use google.auth.default credentials
@pytest.fixture(scope="session")
def instance_operation_timeout():
return _helpers.INSTANCE_OPERATION_TIMEOUT_IN_SECONDS
@pytest.fixture(scope="session")
def database_operation_timeout():
return _helpers.DATABASE_OPERATION_TIMEOUT_IN_SECONDS
@pytest.fixture(scope="session")
def backup_operation_timeout():
return _helpers.BACKUP_OPERATION_TIMEOUT_IN_SECONDS
@pytest.fixture(scope="session")
def shared_instance_id():
if _helpers.CREATE_INSTANCE:
return f"{_helpers.unique_id('google-cloud')}"
if _helpers.USE_EXPERIMENTAL_HOST:
return _helpers.EXPERIMENTAL_HOST_INSTANCE
return _helpers.INSTANCE_ID
@pytest.fixture(scope="session")
def instance_configs(spanner_client):
configs = list(_helpers.retry_503(spanner_client.list_instance_configs)())
if not _helpers.USE_EMULATOR and not _helpers.USE_EXPERIMENTAL_HOST:
# Defend against back-end returning configs for regions we aren't
# actually allowed to use.
configs = [config for config in configs if "-us-" in config.name]
yield configs
@pytest.fixture(scope="session")
def instance_config(instance_configs):
if not instance_configs:
raise ValueError("No instance configs found.")
import random
us_configs = [
config
for config in instance_configs
if config.display_name in ["us-south1", "us-east4"]
]
config = (
random.choice(us_configs) if us_configs else random.choice(instance_configs)
)
yield config
@pytest.fixture(scope="session")
def existing_instances(spanner_client):
instances = list(_helpers.retry_503(spanner_client.list_instances)())
yield instances
@pytest.fixture(scope="session")
def shared_instance(
spanner_client,
instance_operation_timeout,
shared_instance_id,
instance_config,
existing_instances, # evalutate before creating one
):
_helpers.cleanup_old_instances(spanner_client)
if _helpers.CREATE_INSTANCE:
create_time = str(int(time.time()))
labels = {"python-spanner-systests": "true", "created": create_time}
instance = spanner_client.instance(
shared_instance_id, instance_config.name, labels=labels
)
created_op = _helpers.retry_429_503(instance.create)()
created_op.result(instance_operation_timeout) # block until completion
else: # reuse existing instance
instance = spanner_client.instance(shared_instance_id)
instance.reload()
yield instance
if _helpers.CREATE_INSTANCE:
_helpers.retry_429_503(instance.delete)()
@pytest.fixture(scope="session")
def shared_database(
shared_instance, database_operation_timeout, database_dialect, proto_descriptor_file
):
database_name = _helpers.unique_id("test_database")
pool = spanner_v1.BurstyPool(labels={"testcase": "database_api"})
if database_dialect == DatabaseDialect.POSTGRESQL:
database = shared_instance.database(
database_name,
pool=pool,
database_dialect=database_dialect,
)
operation = database.create()
operation.result(database_operation_timeout) # raises on failure / timeout.
operation = database.update_ddl(ddl_statements=_helpers.DDL_STATEMENTS)
operation.result(database_operation_timeout) # raises on failure / timeout.
else:
database = shared_instance.database(
database_name,
ddl_statements=_helpers.DDL_STATEMENTS,
pool=pool,
database_dialect=database_dialect,
proto_descriptors=proto_descriptor_file,
)
operation = database.create()
operation.result(database_operation_timeout) # raises on failure / timeout.
yield database
database.drop()
@pytest.fixture(scope="session")
def shared_backup(shared_instance, shared_database, backup_operation_timeout):
backup_name = _helpers.unique_id("test_backup")
expire_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
days=3
)
source_encryption_enum = CreateBackupEncryptionConfig.EncryptionType
source_encryption_config = CreateBackupEncryptionConfig(
encryption_type=source_encryption_enum.GOOGLE_DEFAULT_ENCRYPTION,
)
backup = shared_instance.backup(
backup_name,
database=shared_database,
expire_time=expire_time,
encryption_config=source_encryption_config,
)
operation = backup.create()
operation.result(backup_operation_timeout) # raises on failure / timeout.
yield backup
backup.delete()
@pytest.fixture(scope="function")
def databases_to_delete():
to_delete = []
yield to_delete
for database in to_delete:
database.drop()