Skip to content

Commit c4132c0

Browse files
authored
Adding e2e_test for Cloud Run image processing sample (GoogleCloudPlatform#4852)
1 parent 997e881 commit c4132c0

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed

run/image-processing/e2e_test.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Copyright 2020 Google, LLC.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This tests the Pub/Sub image processing sample
16+
17+
import os
18+
import subprocess
19+
import time
20+
import uuid
21+
22+
from google.cloud import storage
23+
from google.cloud.storage import Blob, notification
24+
25+
import pytest
26+
27+
28+
SUFFIX = uuid.uuid4().hex[0:6]
29+
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
30+
CLOUD_RUN_SERVICE = f"image-proc-{SUFFIX}"
31+
INPUT_BUCKET = f"image-proc-input-{SUFFIX}"
32+
OUTPUT_BUCKET = f"image-proc-output-{SUFFIX}"
33+
34+
35+
@pytest.fixture()
36+
def cloud_run_service():
37+
# Build and Deploy Cloud Run Services
38+
subprocess.run(
39+
["gcloud", "builds", "submit", "--project", PROJECT,
40+
f"--substitutions=_SERVICE={CLOUD_RUN_SERVICE},_OUTPUT_BUCKET={OUTPUT_BUCKET}",
41+
"--config=e2e_test_setup.yaml", "--quiet"],
42+
check=True
43+
)
44+
45+
yield
46+
47+
# Delete Cloud Run service and image container
48+
subprocess.run(
49+
["gcloud", "run", "services", "delete", CLOUD_RUN_SERVICE,
50+
"--project", PROJECT, "--platform", "managed", "--region",
51+
"us-central1", "--quiet"],
52+
check=True
53+
)
54+
55+
subprocess.run(
56+
["gcloud", "container", "images", "delete",
57+
f"gcr.io/{PROJECT}/{CLOUD_RUN_SERVICE}", "--quiet"],
58+
check=True
59+
)
60+
61+
62+
@pytest.fixture
63+
def service_url(cloud_run_service):
64+
# Get the URL for the cloud run service
65+
service_url = subprocess.run(
66+
[
67+
"gcloud",
68+
"run",
69+
"--project",
70+
PROJECT,
71+
"--platform=managed",
72+
"--region=us-central1",
73+
"services",
74+
"describe",
75+
CLOUD_RUN_SERVICE,
76+
"--format=value(status.url)",
77+
],
78+
stdout=subprocess.PIPE,
79+
check=True
80+
).stdout.strip()
81+
82+
yield service_url.decode()
83+
84+
85+
@pytest.fixture()
86+
def pubsub_topic(service_url):
87+
# Create pub/sub topic
88+
topic = f"image_proc_{SUFFIX}"
89+
subprocess.run(
90+
["gcloud", "pubsub", "topics", "create", topic,
91+
"--project", PROJECT, "--quiet"], check=True
92+
)
93+
94+
# Create pubsub push subscription to Cloud Run Service
95+
# Attach service account with Cloud Run Invoker role
96+
# See tutorial for details on setting up service-account:
97+
# https://cloud.google.com/run/docs/tutorials/pubsub
98+
subprocess.run(
99+
["gcloud", "pubsub", "subscriptions", "create", f"{topic}_sub",
100+
"--topic", topic, "--push-endpoint", service_url, "--project",
101+
PROJECT, "--push-auth-service-account",
102+
f"cloud-run-invoker@{PROJECT}.iam.gserviceaccount.com", "--quiet"], check=True
103+
)
104+
105+
yield topic
106+
107+
# Delete topic
108+
subprocess.run(
109+
["gcloud", "pubsub", "topics", "delete", topic,
110+
"--project", PROJECT, "--quiet"], check=True
111+
)
112+
113+
# Delete subscription
114+
subprocess.run(
115+
["gcloud", "pubsub", "subscriptions", "delete", f"{topic}_sub",
116+
"--project", PROJECT, "--quiet"], check=True
117+
)
118+
119+
120+
@pytest.fixture()
121+
def storage_buckets(pubsub_topic):
122+
# Create GCS Buckets
123+
storage_client = storage.Client()
124+
storage_client.create_bucket(INPUT_BUCKET)
125+
storage_client.create_bucket(OUTPUT_BUCKET)
126+
127+
# Get input and output buckets
128+
input_bucket = storage_client.get_bucket(INPUT_BUCKET)
129+
output_bucket = storage_client.get_bucket(OUTPUT_BUCKET)
130+
131+
# Create pub/sub notification on input_bucket
132+
notification.BucketNotification(input_bucket, topic_name=pubsub_topic,
133+
topic_project=PROJECT, payload_format="JSON_API_V1").create()
134+
135+
yield input_bucket, output_bucket
136+
137+
# Delete GCS buckets
138+
input_bucket.delete(force=True)
139+
output_bucket.delete(force=True)
140+
141+
142+
def test_end_to_end(storage_buckets):
143+
# Upload image to the input bucket
144+
input_bucket = storage_buckets[0]
145+
output_bucket = storage_buckets[1]
146+
147+
blob = Blob("zombie.jpg", input_bucket)
148+
blob.upload_from_filename("test-images/zombie.jpg", content_type="image/jpeg")
149+
150+
# Wait for image processing to complete
151+
time.sleep(30)
152+
153+
for x in range(10):
154+
# Check for blurred image in output bucket
155+
output_blobs = list(output_bucket.list_blobs())
156+
if len(output_blobs) > 0:
157+
break
158+
159+
time.sleep(5)
160+
161+
assert len(output_blobs) > 0
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2020 Google, LLC.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
steps:
16+
- # Build the renderer image
17+
name: gcr.io/cloud-builders/docker:latest
18+
args: ['build', '--tag=gcr.io/$PROJECT_ID/${_SERVICE}', '.']
19+
20+
- # Push the container image to Container Registry
21+
name: gcr.io/cloud-builders/docker
22+
args: ['push', 'gcr.io/$PROJECT_ID/${_SERVICE}']
23+
24+
- # Deploy to Cloud Run
25+
name: gcr.io/cloud-builders/gcloud
26+
args:
27+
- run
28+
- deploy
29+
- ${_SERVICE}
30+
- --image
31+
- gcr.io/$PROJECT_ID/${_SERVICE}
32+
- --region
33+
- us-central1
34+
- --platform
35+
- managed
36+
- --no-allow-unauthenticated
37+
- --set-env-vars=BLURRED_BUCKET_NAME=${_OUTPUT_BUCKET}
38+
39+
images:
40+
- 'gcr.io/$PROJECT_ID/${_SERVICE}'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
26+
# We only run the cloud run tests in py38 session.
27+
'ignored_versions': ["2.7", "3.6", "3.7"],
28+
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
36+
# A dictionary you want to inject into your test. Don't put any
37+
# secrets here. These values will override predefined values.
38+
'envs': {},
39+
}
146 KB
Loading

0 commit comments

Comments
 (0)