Skip to content

Commit 54208af

Browse files
authored
refactor cloud run tests for container reg cleanup (GoogleCloudPlatform#5053)
* refactor hello-broken for cleanup * refactor helloworld test for better cleanup * remove debug suffix * fix logging test, remove nonsense gcloud commands * delete unused build file * remove debug statements * refactor markdown preview tests * remove debug comments * refactor system package tests * shorten suffixes * refactor image processing test * add project flags * refactor pubsub test * add missing check=true * run blacken, fix lint
1 parent 99f20c8 commit 54208af

32 files changed

+981
-767
lines changed

run/hello-broken/e2e_test.py

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,86 +23,139 @@
2323

2424
import pytest
2525

26+
# Unique suffix to create distinct service names
27+
SUFFIX = uuid.uuid4().hex[:10]
28+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
29+
IMAGE_NAME = f"gcr.io/{PROJECT}/hello-{SUFFIX}"
2630

27-
@pytest.fixture()
28-
def services():
29-
# Unique suffix to create distinct service names
30-
suffix = uuid.uuid4().hex
31-
project = os.environ["GOOGLE_CLOUD_PROJECT"]
3231

33-
# Build and Deploy Cloud Run Services
32+
@pytest.fixture
33+
def container_image():
34+
# Build container image for Cloud Run deployment
3435
subprocess.run(
3536
[
3637
"gcloud",
3738
"builds",
3839
"submit",
40+
"--tag",
41+
IMAGE_NAME,
3942
"--project",
40-
project,
41-
"--substitutions",
42-
f"_SUFFIX={suffix}",
43-
"--config",
44-
"e2e_test_setup.yaml",
43+
PROJECT,
4544
"--quiet",
4645
],
4746
check=True,
4847
)
4948

50-
# Get the URL for the service and the token
51-
service = subprocess.run(
49+
yield IMAGE_NAME
50+
51+
# Delete container image
52+
subprocess.run(
53+
[
54+
"gcloud",
55+
"container",
56+
"images",
57+
"delete",
58+
IMAGE_NAME,
59+
"--quiet",
60+
"--project",
61+
PROJECT,
62+
],
63+
check=True,
64+
)
65+
66+
67+
@pytest.fixture
68+
def deployed_service(container_image):
69+
# Deploy image to Cloud Run
70+
service_name = f"hello-{SUFFIX}"
71+
subprocess.run(
5272
[
5373
"gcloud",
5474
"run",
75+
"deploy",
76+
service_name,
77+
"--image",
78+
container_image,
5579
"--project",
56-
project,
57-
"--platform=managed",
80+
PROJECT,
5881
"--region=us-central1",
59-
"services",
60-
"describe",
61-
f"hello-{suffix}",
62-
"--format=value(status.url)",
82+
"--platform=managed",
83+
"--no-allow-unauthenticated",
6384
],
64-
stdout=subprocess.PIPE,
6585
check=True,
66-
).stdout.strip()
67-
68-
token = subprocess.run(
69-
["gcloud", "auth", "print-identity-token"], stdout=subprocess.PIPE, check=True
70-
).stdout.strip()
86+
)
7187

72-
yield service, token
88+
yield service_name
7389

7490
subprocess.run(
7591
[
7692
"gcloud",
7793
"run",
7894
"services",
7995
"delete",
80-
f"hello-{suffix}",
81-
"--project",
82-
project,
83-
"--platform",
84-
"managed",
85-
"--region",
86-
"us-central1",
96+
service_name,
97+
"--platform=managed",
98+
"--region=us-central1",
8799
"--quiet",
100+
"--project",
101+
PROJECT,
88102
],
89103
check=True,
90104
)
91105

92106

93-
def test_end_to_end(services):
94-
service = services[0].decode()
95-
token = services[1].decode()
107+
@pytest.fixture
108+
def service_url_auth_token(deployed_service):
109+
# Get Cloud Run service URL and auth token
110+
service_url = (
111+
subprocess.run(
112+
[
113+
"gcloud",
114+
"run",
115+
"services",
116+
"describe",
117+
deployed_service,
118+
"--platform=managed",
119+
"--region=us-central1",
120+
"--format=value(status.url)",
121+
"--project",
122+
PROJECT,
123+
],
124+
stdout=subprocess.PIPE,
125+
check=True,
126+
)
127+
.stdout.strip()
128+
.decode()
129+
)
130+
auth_token = (
131+
subprocess.run(
132+
["gcloud", "auth", "print-identity-token"],
133+
stdout=subprocess.PIPE,
134+
check=True,
135+
)
136+
.stdout.strip()
137+
.decode()
138+
)
139+
140+
yield service_url, auth_token
141+
142+
# no deletion needed
143+
144+
145+
def test_end_to_end(service_url_auth_token):
146+
service_url, auth_token = service_url_auth_token
96147

97148
# Broken
98149
with pytest.raises(Exception) as e:
99-
req = request.Request(service, headers={"Authorization": f"Bearer {token}"})
150+
req = request.Request(
151+
service_url, headers={"Authorization": f"Bearer {auth_token}"}
152+
)
100153
request.urlopen(req)
101154
assert "HTTP Error 500: Internal Server Error" in str(e.value)
102155

103156
# Improved
104157
req = request.Request(
105-
f"{service}/improved", headers={"Authorization": f"Bearer {token}"}
158+
f"{service_url}/improved", headers={"Authorization": f"Bearer {auth_token}"}
106159
)
107160
response = request.urlopen(req)
108161
assert response.status == 200

run/hello-broken/e2e_test_setup.yaml

Lines changed: 0 additions & 39 deletions
This file was deleted.

run/hello-broken/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def index():
3838
# [END cloudrun_broken_service_problem]
3939

4040
return f"Hello {NAME}"
41+
42+
4143
# [END run_broken_service]
4244
# [END cloudrun_broken_service]
4345

run/hello-broken/noxfile_config.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
2625
# We only run the cloud run tests in py38 session.
27-
'ignored_versions': ["2.7", "3.6", "3.7"],
28-
26+
"ignored_versions": ["2.7", "3.6", "3.7"],
2927
# An envvar key for determining the project id to use. Change it
3028
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
3129
# build specific Cloud project. You can also use your own string
3230
# to use your own Cloud project.
33-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
31+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
3432
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35-
3633
# A dictionary you want to inject into your test. Don't put any
3734
# secrets here. These values will override predefined values.
38-
'envs': {},
35+
"envs": {},
3936
}

0 commit comments

Comments
 (0)