forked from googleapis/python-spanner
/
conftest.py
162 lines (115 loc) · 4.61 KB
/
conftest.py
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
# 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 time
import pytest
from google.cloud import spanner_v1
from . import _helpers
@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="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,
)
else:
return spanner_v1.Client() # 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 shared_instance_id():
if _helpers.CREATE_INSTANCE:
return f"{_helpers.unique_id('google-cloud')}"
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:
# 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.")
us_west1_config = [
config for config in instance_configs if config.display_name == "us-west1"
]
config = us_west1_config[0] if len(us_west1_config) > 0 else instance_configs[0]
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_name = _helpers.unique_id("test_database")
pool = spanner_v1.BurstyPool(labels={"testcase": "database_api"})
database = shared_instance.database(
database_name, ddl_statements=_helpers.DDL_STATEMENTS, pool=pool
)
operation = database.create()
operation.result(database_operation_timeout) # raises on failure / timeout.
yield database
database.drop()
@pytest.fixture(scope="function")
def databases_to_delete():
to_delete = []
yield to_delete
for database in to_delete:
database.drop()