2222from urllib import request
2323import uuid
2424
25+ import backoff
2526import pytest
2627
2728
3132RENDERER_IMAGE_NAME = f"gcr.io/{ PROJECT } /renderer-{ SUFFIX } "
3233
3334
35+ @backoff .on_exception (backoff .expo , subprocess .CalledProcessError , max_tries = 10 )
36+ def run_shell_command (args ):
37+ """
38+ Runs a command with given args.
39+ Usage: gcloud_cli(options)
40+ options: command line options
41+ Example:
42+ result = gcloud_cli("app deploy --no-promote")
43+ print(f"Deployed version {result['versions'][0]['id']}")
44+ Raises Exception with the stderr output of the last attempt on failure.
45+ """
46+ full_command = " " .join (args )
47+ print ("Running command:" , full_command )
48+
49+ try :
50+ output = subprocess .run (
51+ full_command ,
52+ capture_output = True ,
53+ shell = True ,
54+ check = True ,
55+ )
56+ return output .stdout
57+ except subprocess .CalledProcessError as e :
58+ print (f"Command failed: { e .stderr } " )
59+ raise e
60+
61+
3462@pytest .fixture ()
3563def renderer_image ():
3664 # Build container image for Cloud Run deployment
37- subprocess . check_call (
65+ run_shell_command (
3866 [
3967 "gcloud" ,
4068 "builds" ,
@@ -50,7 +78,7 @@ def renderer_image():
5078 yield RENDERER_IMAGE_NAME
5179
5280 # Delete container image
53- subprocess . check_call (
81+ run_shell_command (
5482 [
5583 "gcloud" ,
5684 "container" ,
@@ -67,7 +95,7 @@ def renderer_image():
6795@pytest .fixture ()
6896def editor_image ():
6997 # Build container image for Cloud Run deployment
70- subprocess . check_call (
98+ run_shell_command (
7199 [
72100 "gcloud" ,
73101 "builds" ,
@@ -83,7 +111,7 @@ def editor_image():
83111 yield EDITOR_IMAGE_NAME
84112
85113 # Delete container image
86- subprocess . check_call (
114+ run_shell_command (
87115 [
88116 "gcloud" ,
89117 "container" ,
@@ -101,7 +129,7 @@ def editor_image():
101129def renderer_deployed_service (renderer_image ):
102130 # Deploy image to Cloud Run
103131 renderer_service_name = f"renderer-{ SUFFIX } "
104- subprocess . check_call (
132+ run_shell_command (
105133 [
106134 "gcloud" ,
107135 "run" ,
@@ -119,7 +147,7 @@ def renderer_deployed_service(renderer_image):
119147
120148 yield renderer_service_name
121149
122- subprocess . check_call (
150+ run_shell_command (
123151 [
124152 "gcloud" ,
125153 "run" ,
@@ -140,7 +168,7 @@ def renderer_deployed_service(renderer_image):
140168def renderer_service_url_auth_token (renderer_deployed_service ):
141169 # Get Cloud Run service URL and auth token
142170 renderer_service_url = (
143- subprocess . run (
171+ run_shell_command (
144172 [
145173 "gcloud" ,
146174 "run" ,
@@ -149,23 +177,19 @@ def renderer_service_url_auth_token(renderer_deployed_service):
149177 renderer_deployed_service ,
150178 "--platform=managed" ,
151179 "--region=us-central1" ,
152- "--format=value(status.url)" ,
180+ "--format=\" value(status.url)\" " ,
153181 "--project" ,
154182 PROJECT ,
155- ],
156- stdout = subprocess .PIPE ,
157- check = True ,
183+ ]
158184 )
159- .stdout . strip ()
185+ .strip ()
160186 .decode ()
161187 )
162188 renderer_auth_token = (
163- subprocess .run (
164- ["gcloud" , "auth" , "print-identity-token" ],
165- stdout = subprocess .PIPE ,
166- check = True ,
189+ run_shell_command (
190+ ["gcloud" , "auth" , "print-identity-token" ]
167191 )
168- .stdout . strip ()
192+ .strip ()
169193 .decode ()
170194 )
171195
@@ -177,7 +201,7 @@ def editor_deployed_service(editor_image, renderer_service_url_auth_token):
177201 # Deploy editor image with renderer URL environment var
178202 editor_service_name = f"editor-{ SUFFIX } "
179203 renderer_service_url , renderer_auth_token = renderer_service_url_auth_token
180- subprocess . run (
204+ run_shell_command (
181205 [
182206 "gcloud" ,
183207 "run" ,
@@ -192,13 +216,12 @@ def editor_deployed_service(editor_image, renderer_service_url_auth_token):
192216 "--set-env-vars" ,
193217 f"EDITOR_UPSTREAM_RENDER_URL={ renderer_service_url } " ,
194218 "--no-allow-unauthenticated" ,
195- ],
196- check = True ,
219+ ]
197220 )
198221
199222 yield editor_service_name
200223
201- subprocess . run (
224+ run_shell_command (
202225 [
203226 "gcloud" ,
204227 "run" ,
@@ -210,16 +233,15 @@ def editor_deployed_service(editor_image, renderer_service_url_auth_token):
210233 "--quiet" ,
211234 "--project" ,
212235 PROJECT ,
213- ],
214- check = True ,
236+ ]
215237 )
216238
217239
218240@pytest .fixture
219241def editor_service_url_auth_token (editor_deployed_service ):
220242 # Get Cloud Run service URL and auth token
221243 editor_service_url = (
222- subprocess . run (
244+ run_shell_command (
223245 [
224246 "gcloud" ,
225247 "run" ,
@@ -228,23 +250,19 @@ def editor_service_url_auth_token(editor_deployed_service):
228250 editor_deployed_service ,
229251 "--platform=managed" ,
230252 "--region=us-central1" ,
231- "--format=value(status.url)" ,
253+ "--format=\" value(status.url)\" " ,
232254 "--project" ,
233255 PROJECT ,
234- ],
235- stdout = subprocess .PIPE ,
236- check = True ,
256+ ]
237257 )
238- .stdout . strip ()
258+ .strip ()
239259 .decode ()
240260 )
241261 editor_auth_token = (
242- subprocess .run (
243- ["gcloud" , "auth" , "print-identity-token" ],
244- stdout = subprocess .PIPE ,
245- check = True ,
262+ run_shell_command (
263+ ["gcloud" , "auth" , "print-identity-token" ]
246264 )
247- .stdout . strip ()
265+ .strip ()
248266 .decode ()
249267 )
250268
0 commit comments