22
22
from urllib import request
23
23
import uuid
24
24
25
+ import backoff
25
26
import pytest
26
27
27
28
31
32
RENDERER_IMAGE_NAME = f"gcr.io/{ PROJECT } /renderer-{ SUFFIX } "
32
33
33
34
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
+
34
62
@pytest .fixture ()
35
63
def renderer_image ():
36
64
# Build container image for Cloud Run deployment
37
- subprocess . check_call (
65
+ run_shell_command (
38
66
[
39
67
"gcloud" ,
40
68
"builds" ,
@@ -50,7 +78,7 @@ def renderer_image():
50
78
yield RENDERER_IMAGE_NAME
51
79
52
80
# Delete container image
53
- subprocess . check_call (
81
+ run_shell_command (
54
82
[
55
83
"gcloud" ,
56
84
"container" ,
@@ -67,7 +95,7 @@ def renderer_image():
67
95
@pytest .fixture ()
68
96
def editor_image ():
69
97
# Build container image for Cloud Run deployment
70
- subprocess . check_call (
98
+ run_shell_command (
71
99
[
72
100
"gcloud" ,
73
101
"builds" ,
@@ -83,7 +111,7 @@ def editor_image():
83
111
yield EDITOR_IMAGE_NAME
84
112
85
113
# Delete container image
86
- subprocess . check_call (
114
+ run_shell_command (
87
115
[
88
116
"gcloud" ,
89
117
"container" ,
@@ -101,7 +129,7 @@ def editor_image():
101
129
def renderer_deployed_service (renderer_image ):
102
130
# Deploy image to Cloud Run
103
131
renderer_service_name = f"renderer-{ SUFFIX } "
104
- subprocess . check_call (
132
+ run_shell_command (
105
133
[
106
134
"gcloud" ,
107
135
"run" ,
@@ -119,7 +147,7 @@ def renderer_deployed_service(renderer_image):
119
147
120
148
yield renderer_service_name
121
149
122
- subprocess . check_call (
150
+ run_shell_command (
123
151
[
124
152
"gcloud" ,
125
153
"run" ,
@@ -140,7 +168,7 @@ def renderer_deployed_service(renderer_image):
140
168
def renderer_service_url_auth_token (renderer_deployed_service ):
141
169
# Get Cloud Run service URL and auth token
142
170
renderer_service_url = (
143
- subprocess . run (
171
+ run_shell_command (
144
172
[
145
173
"gcloud" ,
146
174
"run" ,
@@ -149,23 +177,19 @@ def renderer_service_url_auth_token(renderer_deployed_service):
149
177
renderer_deployed_service ,
150
178
"--platform=managed" ,
151
179
"--region=us-central1" ,
152
- "--format=value(status.url)" ,
180
+ "--format=\" value(status.url)\" " ,
153
181
"--project" ,
154
182
PROJECT ,
155
- ],
156
- stdout = subprocess .PIPE ,
157
- check = True ,
183
+ ]
158
184
)
159
- .stdout . strip ()
185
+ .strip ()
160
186
.decode ()
161
187
)
162
188
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" ]
167
191
)
168
- .stdout . strip ()
192
+ .strip ()
169
193
.decode ()
170
194
)
171
195
@@ -177,7 +201,7 @@ def editor_deployed_service(editor_image, renderer_service_url_auth_token):
177
201
# Deploy editor image with renderer URL environment var
178
202
editor_service_name = f"editor-{ SUFFIX } "
179
203
renderer_service_url , renderer_auth_token = renderer_service_url_auth_token
180
- subprocess . run (
204
+ run_shell_command (
181
205
[
182
206
"gcloud" ,
183
207
"run" ,
@@ -192,13 +216,12 @@ def editor_deployed_service(editor_image, renderer_service_url_auth_token):
192
216
"--set-env-vars" ,
193
217
f"EDITOR_UPSTREAM_RENDER_URL={ renderer_service_url } " ,
194
218
"--no-allow-unauthenticated" ,
195
- ],
196
- check = True ,
219
+ ]
197
220
)
198
221
199
222
yield editor_service_name
200
223
201
- subprocess . run (
224
+ run_shell_command (
202
225
[
203
226
"gcloud" ,
204
227
"run" ,
@@ -210,16 +233,15 @@ def editor_deployed_service(editor_image, renderer_service_url_auth_token):
210
233
"--quiet" ,
211
234
"--project" ,
212
235
PROJECT ,
213
- ],
214
- check = True ,
236
+ ]
215
237
)
216
238
217
239
218
240
@pytest .fixture
219
241
def editor_service_url_auth_token (editor_deployed_service ):
220
242
# Get Cloud Run service URL and auth token
221
243
editor_service_url = (
222
- subprocess . run (
244
+ run_shell_command (
223
245
[
224
246
"gcloud" ,
225
247
"run" ,
@@ -228,23 +250,19 @@ def editor_service_url_auth_token(editor_deployed_service):
228
250
editor_deployed_service ,
229
251
"--platform=managed" ,
230
252
"--region=us-central1" ,
231
- "--format=value(status.url)" ,
253
+ "--format=\" value(status.url)\" " ,
232
254
"--project" ,
233
255
PROJECT ,
234
- ],
235
- stdout = subprocess .PIPE ,
236
- check = True ,
256
+ ]
237
257
)
238
- .stdout . strip ()
258
+ .strip ()
239
259
.decode ()
240
260
)
241
261
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" ]
246
264
)
247
- .stdout . strip ()
265
+ .strip ()
248
266
.decode ()
249
267
)
250
268
0 commit comments