Skip to content

Commit 03bce05

Browse files
authored
Adding e2e test for Cloud Run PubSub sample (GoogleCloudPlatform#4835)
1 parent 47600bb commit 03bce05

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

run/pubsub/e2e_test.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 to Cloud Run integration
16+
17+
import datetime
18+
import os
19+
import subprocess
20+
import time
21+
import uuid
22+
23+
from google.cloud import logging_v2
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"pubsub-test-{SUFFIX}"
31+
32+
33+
@pytest.fixture()
34+
def cloud_run_service():
35+
# Build and Deploy Cloud Run Services
36+
subprocess.run(
37+
["gcloud", "builds", "submit", "--project", PROJECT,
38+
f"--substitutions=_SERVICE={CLOUD_RUN_SERVICE}",
39+
"--config=e2e_test_setup.yaml", "--quiet"],
40+
check=True
41+
)
42+
43+
yield
44+
45+
# Delete Cloud Run service and image container
46+
subprocess.run(
47+
["gcloud", "run", "services", "delete", CLOUD_RUN_SERVICE,
48+
"--project", PROJECT, "--platform", "managed", "--region",
49+
"us-central1", "--quiet"],
50+
check=True
51+
)
52+
53+
subprocess.run(
54+
["gcloud", "container", "images", "delete",
55+
f"gcr.io/{PROJECT}/{CLOUD_RUN_SERVICE}", "--quiet"],
56+
check=True
57+
)
58+
59+
60+
@pytest.fixture
61+
def service_url(cloud_run_service):
62+
# Get the URL for the cloud run service
63+
service_url = subprocess.run(
64+
[
65+
"gcloud",
66+
"run",
67+
"--project",
68+
PROJECT,
69+
"--platform=managed",
70+
"--region=us-central1",
71+
"services",
72+
"describe",
73+
CLOUD_RUN_SERVICE,
74+
"--format=value(status.url)",
75+
],
76+
stdout=subprocess.PIPE,
77+
check=True
78+
).stdout.strip()
79+
80+
yield service_url.decode()
81+
82+
83+
@pytest.fixture()
84+
def pubsub_topic(service_url):
85+
# Create pub/sub topic
86+
topic = f"e2e_{SUFFIX}"
87+
subprocess.run(
88+
["gcloud", "pubsub", "topics", "create", topic,
89+
"--project", PROJECT, "--quiet"], check=True
90+
)
91+
92+
# Create pubsub push subscription to Cloud Run Service
93+
# Attach service account with Cloud Run Invoker role
94+
# See tutorial for details on setting up service-account:
95+
# https://cloud.google.com/run/docs/tutorials/pubsub
96+
subprocess.run(
97+
["gcloud", "pubsub", "subscriptions", "create", f"{topic}_sub",
98+
"--topic", topic, "--push-endpoint", service_url, "--project",
99+
PROJECT, "--push-auth-service-account",
100+
f"cloud-run-invoker@{PROJECT}.iam.gserviceaccount.com",
101+
"--quiet"], check=True
102+
)
103+
104+
yield topic
105+
106+
# Delete topic
107+
subprocess.run(
108+
["gcloud", "pubsub", "topics", "delete", topic,
109+
"--project", PROJECT, "--quiet"], check=True
110+
)
111+
112+
# Delete subscription
113+
subprocess.run(
114+
["gcloud", "pubsub", "subscriptions", "delete", f"{topic}_sub",
115+
"--project", PROJECT, "--quiet"], check=True
116+
)
117+
118+
119+
def test_end_to_end(pubsub_topic):
120+
# Post the message "Runner" to the topic
121+
subprocess.run(
122+
["gcloud", "pubsub", "topics", "publish", f"{pubsub_topic}",
123+
"--project", PROJECT, "--message", "Runner", "--quiet"], check=True
124+
)
125+
126+
# Check the logs for "Hello Runner"
127+
time.sleep(20) # Slight delay writing to stackdriver
128+
client = logging_v2.LoggingServiceV2Client()
129+
resource_names = [f"projects/{PROJECT}"]
130+
131+
# We add timestamp for making the query faster.
132+
now = datetime.datetime.now(datetime.timezone.utc)
133+
filter_date = now - datetime.timedelta(minutes=1)
134+
filters = (
135+
f"timestamp>=\"{filter_date.isoformat('T')}\" "
136+
"resource.type=cloud_run_revision "
137+
f"AND resource.labels.service_name={CLOUD_RUN_SERVICE} "
138+
)
139+
140+
# Retry a maximum number of 10 times to find results in stackdriver
141+
found = False
142+
for x in range(10):
143+
iterator = client.list_log_entries(resource_names, filter_=filters)
144+
for entry in iterator:
145+
if entry.text_payload == "Hello Runner!":
146+
found = True
147+
break
148+
# When message found, exit loop
149+
if found is True:
150+
break
151+
time.sleep(5) # Slight delay before retry
152+
153+
assert found

run/pubsub/e2e_test_setup.yaml

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+
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+
38+
images:
39+
- 'gcr.io/$PROJECT_ID/${_SERVICE}'

run/pubsub/noxfile_config.py

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+
}

run/pubsub/requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==6.0.1
2+
google-cloud-logging==1.15.1

0 commit comments

Comments
 (0)