forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
378 lines (330 loc) · 11.9 KB
/
conftest.py
File metadata and controls
378 lines (330 loc) · 11.9 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Copyright 2021 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
#
# 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.
from dataclasses import dataclass
import itertools
import json
import multiprocessing as mp
import os
import subprocess
import sys
import time
from typing import Any, Callable, Dict, Iterable, Optional
import uuid
import pytest
# Default options.
UUID = uuid.uuid4().hex[0:6]
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
REGION = "us-west1"
ZONE = "us-west1-b"
RETRY_MAX_TIME = 5 * 60 # 5 minutes in seconds
@dataclass
class Utils:
uuid: str = UUID
project: str = PROJECT
region: str = REGION
zone: str = ZONE
@staticmethod
def storage_bucket(bucket_name: str) -> str:
from google.cloud import storage
storage_client = storage.Client()
bucket_unique_name = f"{bucket_name}-{UUID}"
bucket = storage_client.create_bucket(bucket_unique_name)
print(f"storage_bucket: {bucket_unique_name}")
yield bucket_unique_name
bucket.delete(force=True)
@staticmethod
def bigquery_dataset(dataset_name: str, project: str = PROJECT) -> str:
from google.cloud import bigquery
bigquery_client = bigquery.Client()
dataset = bigquery_client.create_dataset(
bigquery.Dataset(f"{project}.{dataset_name.replace('-', '_')}_{UUID}")
)
print(f"bigquery_dataset: {dataset.full_dataset_id}")
yield dataset.full_dataset_id
bigquery_client.delete_dataset(
dataset.full_dataset_id.replace(":", "."), delete_contents=True
)
@staticmethod
def bigquery_query(query: str) -> Iterable[Dict[str, Any]]:
from google.cloud import bigquery
bigquery_client = bigquery.Client()
for row in bigquery_client.query(query):
yield dict(row)
@staticmethod
def pubsub_topic(topic_name: str, project: str = PROJECT) -> str:
from google.cloud import pubsub
publisher_client = pubsub.PublisherClient()
topic_path = publisher_client.topic_path(project, f"{topic_name}-{UUID}")
topic = publisher_client.create_topic(topic_path)
print(f"pubsub_topic: {topic.name}")
yield topic.name
# Due to the pinned library dependencies in apache-beam, client
# library throws an error upon deletion.
# We use gcloud for a workaround. See also:
# https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4492
cmd = ["gcloud", "pubsub", "--project", project, "topics", "delete", topic.name]
print(cmd)
subprocess.run(cmd, check=True)
@staticmethod
def pubsub_subscription(
topic_path: str,
subscription_name: str,
project: str = PROJECT,
) -> str:
from google.cloud import pubsub
subscriber = pubsub.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, f"{subscription_name}-{UUID}"
)
subscription = subscriber.create_subscription(subscription_path, topic_path)
print(f"pubsub_subscription: {subscription.name}")
yield subscription.name
# Due to the pinned library dependencies in apache-beam, client
# library throws an error upon deletion.
# We use gcloud for a workaround. See also:
# https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4492
cmd = [
"gcloud",
"pubsub",
"--project",
project,
"subscriptions",
"delete",
subscription.name,
]
print(cmd)
subprocess.run(cmd, check=True)
@staticmethod
def pubsub_publisher(
topic_path: str,
new_msg: Callable[[int], str] = lambda i: json.dumps(
{"id": i, "content": f"message {i}"}
),
sleep_sec: int = 1,
) -> bool:
from google.cloud import pubsub
def _infinite_publish_job() -> None:
publisher_client = pubsub.PublisherClient()
for i in itertools.count():
msg = new_msg(i)
publisher_client.publish(topic_path, msg.encode("utf-8")).result()
time.sleep(sleep_sec)
# Start a subprocess in the background to do the publishing.
print(f"Starting publisher on {topic_path}")
p = mp.Process(target=_infinite_publish_job)
p.start()
yield p.is_alive()
# For cleanup, terminate the background process.
print("Stopping publisher")
p.join(timeout=0)
p.terminate()
@staticmethod
def container_image(
image_path: str,
project: str = PROJECT,
tag: str = "latest",
) -> str:
image_name = f"gcr.io/{project}/{image_path}-{UUID}:{tag}"
cmd = ["gcloud", "auth", "configure-docker"]
print(cmd)
subprocess.run(cmd, check=True)
cmd = [
"gcloud",
"builds",
"submit",
f"--project={project}",
f"--tag={image_name}",
".",
]
print(cmd)
subprocess.run(cmd, check=True)
print(f"container_image: {image_name}")
yield image_name
cmd = [
"gcloud",
"container",
"images",
"delete",
image_name,
f"--project={project}",
"--quiet",
]
print(cmd)
subprocess.run(cmd, check=True)
@staticmethod
def dataflow_job_id_from_job_name(
job_name: str,
project: str = PROJECT,
) -> Optional[str]:
from googleapiclient.discovery import build
dataflow = build("dataflow", "v1b3")
# Only return the 50 most recent results - our job is likely to be in here.
# If the job is not found, first try increasing this number.[]''job_id
# For more info see:
# https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/list
jobs_request = (
dataflow.projects()
.jobs()
.list(
projectId=project,
filter="ACTIVE",
pageSize=50,
)
)
response = jobs_request.execute()
# Search for the job in the list that has our name (names are unique)
for job in response["jobs"]:
if job["name"] == job_name:
return job["id"]
return None
@staticmethod
def dataflow_jobs_wait(
job_id: str,
project: str = PROJECT,
status: str = "JOB_STATE_RUNNING",
) -> bool:
from googleapiclient.discovery import build
dataflow = build("dataflow", "v1b3")
sleep_time_seconds = 30
max_sleep_time = 10 * 60
print(f"Waiting for Dataflow job ID: {job_id} (until status {status})")
for _ in range(0, max_sleep_time, sleep_time_seconds):
try:
# For more info see:
# https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/get
jobs_request = (
dataflow.projects()
.jobs()
.get(
projectId=project,
jobId=job_id,
view="JOB_VIEW_SUMMARY",
)
)
response = jobs_request.execute()
print(response)
if response["currentState"] == status:
return True
except:
pass
time.sleep(sleep_time_seconds)
return False
@staticmethod
def dataflow_jobs_cancel_by_job_id(
job_id: str, project: str = PROJECT, region: str = REGION
) -> None:
print(f"Canceling Dataflow job ID: {job_id}")
# We get an error using the googleapiclient.discovery APIs, probably
# due to incompatible dependencies with apache-beam.
# We use gcloud instead to cancel the job.
# https://cloud.google.com/sdk/gcloud/reference/dataflow/jobs/cancel
cmd = [
"gcloud",
f"--project={project}",
"dataflow",
"jobs",
"cancel",
job_id,
f"--region={region}",
]
subprocess.run(cmd, check=True)
@staticmethod
def dataflow_jobs_cancel_by_job_name(
job_name: str, project: str = PROJECT, region: str = REGION
) -> None:
# To cancel a dataflow job, we need its ID, not its name.
# If it doesn't, job_id will be equal to None.
job_id = Utils.dataflow_job_id_from_job_name(project, job_name)
if job_id is not None:
Utils.dataflow_jobs_cancel_by_job_id(job_id, project, region)
@staticmethod
def dataflow_flex_template_build(
bucket_name: str,
template_image: str,
metadata_file: str,
project: str = PROJECT,
template_file: str = "template.json",
) -> str:
# https://cloud.google.com/sdk/gcloud/reference/dataflow/flex-template/build
template_gcs_path = f"gs://{bucket_name}/{template_file}"
cmd = [
"gcloud",
"dataflow",
"flex-template",
"build",
template_gcs_path,
f"--project={project}",
f"--image={template_image}",
"--sdk-language=PYTHON",
f"--metadata-file={metadata_file}",
]
print(cmd)
subprocess.run(cmd, check=True)
print(f"dataflow_flex_template_build: {template_gcs_path}")
yield template_gcs_path
# The template file gets deleted when we delete the bucket.
@staticmethod
def dataflow_flex_template_run(
job_name: str,
template_path: str,
bucket_name: str,
parameters: Dict[str, str] = {},
project: str = PROJECT,
region: str = REGION,
) -> str:
import yaml
# https://cloud.google.com/sdk/gcloud/reference/dataflow/flex-template/run
unique_job_name = f"{job_name}-{UUID}"
print(f"dataflow_job_name: {unique_job_name}")
cmd = [
"gcloud",
"dataflow",
"flex-template",
"run",
unique_job_name,
f"--template-file-gcs-location={template_path}",
f"--project={project}",
f"--region={region}",
] + [
f"--parameters={name}={value}"
for name, value in {
**parameters,
"temp_location": f"gs://{bucket_name}/temp",
}.items()
]
print(cmd)
try:
# The `capture_output` option was added in Python 3.7, so we must
# pass the `stdout` and `stderr` options explicitly to support 3.6.
# https://docs.python.org/3/library/subprocess.html#subprocess.run
p = subprocess.run(
cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout = p.stdout.decode("utf-8")
stderr = p.stderr.decode("utf-8")
print(f"Launched Dataflow Flex Template job: {unique_job_name}")
except subprocess.CalledProcessError as e:
print(e, file=sys.stderr)
stdout = stdout.decode("utf-8")
stderr = stderr.decode("utf-8")
finally:
print("--- stderr ---")
print(stderr)
print("--- stdout ---")
print(stdout)
print("--- end ---")
return yaml.safe_load(stdout)["job"]["id"]
@pytest.fixture(scope="session")
def utils() -> Utils:
print(f"Test unique identifier: {UUID}")
subprocess.run(["gcloud", "version"])
return Utils()