/
conftest.py
314 lines (233 loc) · 8.81 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Copyright 2020 Google LLC
#
# 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
#
# https://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 os
from uuid import uuid4
from google.api_core import exceptions
from google.cloud import aiplatform, aiplatform_v1beta1
from google.cloud import bigquery
from google.cloud import storage
import pytest
import helpers
@pytest.fixture()
def shared_state():
state = {}
yield state
@pytest.fixture
def storage_client():
storage_client = storage.Client()
return storage_client
@pytest.fixture()
def job_client():
job_client = aiplatform.gapic.JobServiceClient(
client_options={"api_endpoint": "us-central1-aiplatform.googleapis.com"}
)
return job_client
@pytest.fixture()
def data_labeling_job_client():
data_labeling_api_endpoint = os.getenv("DATA_LABELING_API_ENDPOINT")
data_labeling_job_client = aiplatform.gapic.JobServiceClient(
client_options={"api_endpoint": data_labeling_api_endpoint}
)
return data_labeling_job_client
@pytest.fixture
def pipeline_client():
pipeline_client = aiplatform.gapic.PipelineServiceClient(
client_options={"api_endpoint": "us-central1-aiplatform.googleapis.com"}
)
return pipeline_client
@pytest.fixture
def model_client():
model_client = aiplatform.gapic.ModelServiceClient(
client_options={"api_endpoint": "us-central1-aiplatform.googleapis.com"}
)
yield model_client
@pytest.fixture
def endpoint_client():
endpoint_client = aiplatform.gapic.EndpointServiceClient(
client_options={"api_endpoint": "us-central1-aiplatform.googleapis.com"}
)
yield endpoint_client
@pytest.fixture
def dataset_client():
dataset_client = aiplatform.gapic.DatasetServiceClient(
client_options={"api_endpoint": "us-central1-aiplatform.googleapis.com"}
)
yield dataset_client
@pytest.fixture
def featurestore_client():
featurestore_client = aiplatform_v1beta1.FeaturestoreServiceClient(
client_options={"api_endpoint": "us-central1-aiplatform.googleapis.com"}
)
yield featurestore_client
@pytest.fixture
def bigquery_client():
bigquery_client = bigquery.Client(
project=os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT")
)
yield bigquery_client
# Shared setup/teardown.
@pytest.fixture()
def teardown_batch_prediction_job(shared_state, job_client):
yield
job_client.cancel_batch_prediction_job(
name=shared_state["batch_prediction_job_name"]
)
# Waiting until the job is in CANCELLED state.
helpers.wait_for_job_state(
get_job_method=job_client.get_batch_prediction_job,
name=shared_state["batch_prediction_job_name"],
)
job_client.delete_batch_prediction_job(
name=shared_state["batch_prediction_job_name"]
)
@pytest.fixture()
def teardown_data_labeling_job(capsys, shared_state, data_labeling_job_client):
yield
assert "/" in shared_state["data_labeling_job_name"]
data_labeling_job_client.cancel_data_labeling_job(
name=shared_state["data_labeling_job_name"]
)
# Verify Data Labelling Job is cancelled, or timeout after 400 seconds
helpers.wait_for_job_state(
get_job_method=data_labeling_job_client.get_data_labeling_job,
name=shared_state["data_labeling_job_name"],
timeout=400,
freq=10,
)
# Delete the data labeling job
response = data_labeling_job_client.delete_data_labeling_job(
name=shared_state["data_labeling_job_name"]
)
print("Delete LRO:", response.operation.name)
delete_data_labeling_job_response = response.result(timeout=300)
print("delete_data_labeling_job_response", delete_data_labeling_job_response)
out, _ = capsys.readouterr()
assert "delete_data_labeling_job_response" in out
@pytest.fixture()
def teardown_hyperparameter_tuning_job(shared_state, job_client):
yield
# Cancel the created hyperparameter tuning job
job_client.cancel_hyperparameter_tuning_job(
name=shared_state["hyperparameter_tuning_job_name"]
)
# Waiting for hyperparameter tuning job to be in CANCELLED state
helpers.wait_for_job_state(
get_job_method=job_client.get_hyperparameter_tuning_job,
name=shared_state["hyperparameter_tuning_job_name"],
)
# Delete the created hyperparameter tuning job
job_client.delete_hyperparameter_tuning_job(
name=shared_state["hyperparameter_tuning_job_name"]
)
@pytest.fixture()
def teardown_training_pipeline(shared_state, pipeline_client):
yield
try:
pipeline_client.cancel_training_pipeline(
name=shared_state["training_pipeline_name"]
)
# Waiting for training pipeline to be in CANCELLED state
timeout = shared_state["cancel_batch_prediction_job_timeout"]
helpers.wait_for_job_state(
get_job_method=pipeline_client.get_training_pipeline,
name=shared_state["training_pipeline_name"],
timeout=timeout,
)
except exceptions.FailedPrecondition:
pass # If pipeline failed, ignore and skip directly to deletion
finally:
# Delete the training pipeline
pipeline_client.delete_training_pipeline(
name=shared_state["training_pipeline_name"]
)
@pytest.fixture()
def create_dataset(shared_state, dataset_client):
def create(
project, location, metadata_schema_uri, test_name="temp_import_dataset_test"
):
parent = f"projects/{project}/locations/{location}"
dataset = aiplatform.gapic.Dataset(
display_name=f"{test_name}_{uuid4()}",
metadata_schema_uri=metadata_schema_uri,
)
operation = dataset_client.create_dataset(parent=parent, dataset=dataset)
dataset = operation.result(timeout=300)
shared_state["dataset_name"] = dataset.name
yield create
@pytest.fixture()
def teardown_dataset(shared_state, dataset_client):
yield
# Delete the created dataset
dataset_client.delete_dataset(name=shared_state["dataset_name"])
@pytest.fixture()
def teardown_featurestore(shared_state, featurestore_client):
yield
# Force delete the created featurestore
force_delete_featurestore_request = {
"name": shared_state["featurestore_name"],
"force": True,
}
featurestore_client.delete_featurestore(request=force_delete_featurestore_request)
@pytest.fixture()
def teardown_entity_type(shared_state, featurestore_client):
yield
# Force delete the created entity type
force_delete_entity_type_request = {
"name": shared_state["entity_type_name"],
"force": True,
}
featurestore_client.delete_entity_type(request=force_delete_entity_type_request)
@pytest.fixture()
def teardown_feature(shared_state, featurestore_client):
yield
# Delete the created feature
featurestore_client.delete_feature(name=shared_state["feature_name"])
@pytest.fixture()
def teardown_features(shared_state, featurestore_client):
yield
# Delete the created features
for feature_name in shared_state["feature_names"]:
featurestore_client.delete_feature(name=feature_name)
@pytest.fixture()
def teardown_batch_read_feature_values(shared_state, bigquery_client):
yield
# Delete the created dataset
bigquery_client.delete_dataset(
shared_state["destination_data_set"], delete_contents=True, not_found_ok=True
)
@pytest.fixture()
def create_endpoint(shared_state, endpoint_client):
def create(project, location, test_name="temp_deploy_model_test"):
parent = f"projects/{project}/locations/{location}"
endpoint = aiplatform.gapic.Endpoint(display_name=f"{test_name}_{uuid4()}",)
create_endpoint_response = endpoint_client.create_endpoint(
parent=parent, endpoint=endpoint
)
endpoint = create_endpoint_response.result()
shared_state["endpoint_name"] = endpoint.name
yield create
@pytest.fixture()
def teardown_endpoint(shared_state, endpoint_client):
yield
undeploy_model_operation = endpoint_client.undeploy_model(
deployed_model_id=shared_state["deployed_model_id"],
endpoint=shared_state["endpoint_name"],
)
undeploy_model_operation.result()
# Delete the endpoint
endpoint_client.delete_endpoint(name=shared_state["endpoint_name"])
@pytest.fixture()
def teardown_model(shared_state, model_client):
yield
model_client.delete_model(name=shared_state["model_name"])