forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_test.py
More file actions
161 lines (128 loc) · 4.65 KB
/
Copy pathe2e_test.py
File metadata and controls
161 lines (128 loc) · 4.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
# 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
#
# 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.
# This tests the Pub/Sub image processing sample
import os
import subprocess
import time
import uuid
from google.cloud import storage
from google.cloud.storage import Blob, notification
import pytest
SUFFIX = uuid.uuid4().hex[0:6]
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
CLOUD_RUN_SERVICE = f"image-proc-{SUFFIX}"
INPUT_BUCKET = f"image-proc-input-{SUFFIX}"
OUTPUT_BUCKET = f"image-proc-output-{SUFFIX}"
@pytest.fixture()
def cloud_run_service():
# Build and Deploy Cloud Run Services
subprocess.run(
["gcloud", "builds", "submit", "--project", PROJECT,
f"--substitutions=_SERVICE={CLOUD_RUN_SERVICE},_OUTPUT_BUCKET={OUTPUT_BUCKET}",
"--config=e2e_test_setup.yaml", "--quiet"],
check=True
)
yield
# Delete Cloud Run service and image container
subprocess.run(
["gcloud", "run", "services", "delete", CLOUD_RUN_SERVICE,
"--project", PROJECT, "--platform", "managed", "--region",
"us-central1", "--quiet"],
check=True
)
subprocess.run(
["gcloud", "container", "images", "delete",
f"gcr.io/{PROJECT}/{CLOUD_RUN_SERVICE}", "--quiet"],
check=True
)
@pytest.fixture
def service_url(cloud_run_service):
# Get the URL for the cloud run service
service_url = subprocess.run(
[
"gcloud",
"run",
"--project",
PROJECT,
"--platform=managed",
"--region=us-central1",
"services",
"describe",
CLOUD_RUN_SERVICE,
"--format=value(status.url)",
],
stdout=subprocess.PIPE,
check=True
).stdout.strip()
yield service_url.decode()
@pytest.fixture()
def pubsub_topic(service_url):
# Create pub/sub topic
topic = f"image_proc_{SUFFIX}"
subprocess.run(
["gcloud", "pubsub", "topics", "create", topic,
"--project", PROJECT, "--quiet"], check=True
)
# Create pubsub push subscription to Cloud Run Service
# Attach service account with Cloud Run Invoker role
# See tutorial for details on setting up service-account:
# https://cloud.google.com/run/docs/tutorials/pubsub
subprocess.run(
["gcloud", "pubsub", "subscriptions", "create", f"{topic}_sub",
"--topic", topic, "--push-endpoint", service_url, "--project",
PROJECT, "--push-auth-service-account",
f"cloud-run-invoker@{PROJECT}.iam.gserviceaccount.com", "--quiet"], check=True
)
yield topic
# Delete topic
subprocess.run(
["gcloud", "pubsub", "topics", "delete", topic,
"--project", PROJECT, "--quiet"], check=True
)
# Delete subscription
subprocess.run(
["gcloud", "pubsub", "subscriptions", "delete", f"{topic}_sub",
"--project", PROJECT, "--quiet"], check=True
)
@pytest.fixture()
def storage_buckets(pubsub_topic):
# Create GCS Buckets
storage_client = storage.Client()
storage_client.create_bucket(INPUT_BUCKET)
storage_client.create_bucket(OUTPUT_BUCKET)
# Get input and output buckets
input_bucket = storage_client.get_bucket(INPUT_BUCKET)
output_bucket = storage_client.get_bucket(OUTPUT_BUCKET)
# Create pub/sub notification on input_bucket
notification.BucketNotification(input_bucket, topic_name=pubsub_topic,
topic_project=PROJECT, payload_format="JSON_API_V1").create()
yield input_bucket, output_bucket
# Delete GCS buckets
input_bucket.delete(force=True)
output_bucket.delete(force=True)
def test_end_to_end(storage_buckets):
# Upload image to the input bucket
input_bucket = storage_buckets[0]
output_bucket = storage_buckets[1]
blob = Blob("zombie.jpg", input_bucket)
blob.upload_from_filename("test-images/zombie.jpg", content_type="image/jpeg")
# Wait for image processing to complete
time.sleep(30)
for x in range(10):
# Check for blurred image in output bucket
output_blobs = list(output_bucket.list_blobs())
if len(output_blobs) > 0:
break
time.sleep(5)
assert len(output_blobs) > 0