2424
2525import pytest
2626
27+ # Unique suffix to create distinct service names
2728
28- @pytest .fixture ()
29- def services ():
30- # Unique suffix to create distinct service names
31- suffix = uuid .uuid4 ().hex [:10 ]
32- project = os .environ ["GOOGLE_CLOUD_PROJECT" ]
29+ SUFFIX = uuid .uuid4 ().hex [:10 ]
30+ PROJECT = os .environ ["GOOGLE_CLOUD_PROJECT" ]
31+ VPC_CONNECTOR_NAME = "test-connector"
32+ MEMORYSTORE_REDIS_NAME = "static-test-instance"
3333
34- # Create a VPC network
35- network_name = f"test-network-{ suffix } "
36- subprocess .run (
37- [
38- "gcloud" ,
39- "compute" ,
40- "networks" ,
41- "create" ,
42- network_name ,
43- "--project" ,
44- project ,
45- ], check = True
46- )
47-
48- # Create a Serverless VPC Access connector
49- connector_name = f"test-connector-{ suffix } "
50- subprocess .run (
51- [
52- "gcloud" ,
53- "compute" ,
54- "networks" ,
55- "vpc-access" ,
56- "connectors" ,
57- "create" ,
58- connector_name ,
59- "--network" ,
60- network_name ,
61- "--region=us-central1" ,
62- "--range=192.168.16.0/28" ,
63- "--project" ,
64- project ,
65- ], check = True
66- )
67-
68- # Create a Memorystore Redis instance
69- instance_name = f"test-instance-{ suffix } "
70- subprocess .run (
71- [
72- "gcloud" ,
73- "redis" ,
74- "instances" ,
75- "create" ,
76- instance_name ,
77- "--region=us-central1" ,
78- "--network" ,
79- network_name ,
80- "--project" ,
81- project ,
82- ], check = True
83- )
8434
35+ @pytest .fixture
36+ def redis_host ():
8537 # Get the Redis instance's IP
8638 redis_host = subprocess .run (
8739 [
8840 "gcloud" ,
8941 "redis" ,
9042 "instances" ,
9143 "describe" ,
92- instance_name ,
44+ MEMORYSTORE_REDIS_NAME ,
9345 "--region=us-central1" ,
9446 "--format=value(host)" ,
9547 "--project" ,
96- project ,
48+ PROJECT ,
9749 ],
9850 stdout = subprocess .PIPE ,
9951 check = True
10052 ).stdout .strip ().decode ()
53+ yield redis_host
54+
55+ # no deletion needs to happen, this is a "get" of a static instance
10156
57+
58+ @pytest .fixture
59+ def container_image ():
10260 # Build container image for Cloud Run deployment
103- image_name = f"gcr.io/{ project } /test-visit-count-{ suffix } "
61+ image_name = f"gcr.io/{ PROJECT } /test-visit-count-{ SUFFIX } "
10462 subprocess .run (
10563 [
10664 "cp" ,
@@ -116,46 +74,85 @@ def services():
11674 "--tag" ,
11775 image_name ,
11876 "--project" ,
119- project ,
77+ PROJECT ,
12078 ], check = True
12179 )
80+ yield image_name
81+
12282 subprocess .run (["rm" , "Dockerfile" ], check = True )
12383
84+ # Delete container image
85+ subprocess .run (
86+ [
87+ "gcloud" ,
88+ "container" ,
89+ "images" ,
90+ "delete" ,
91+ image_name ,
92+ "--quiet" ,
93+ "--project" ,
94+ PROJECT ,
95+ ], check = True
96+ )
97+
98+
99+ @pytest .fixture
100+ def deployed_service (container_image , redis_host ):
124101 # Deploy image to Cloud Run
125- service_name = f"test-visit-count-{ suffix } "
102+ service_name = f"test-visit-count-{ SUFFIX } "
126103 subprocess .run (
127104 [
128105 "gcloud" ,
129106 "run" ,
130107 "deploy" ,
131108 service_name ,
132109 "--image" ,
133- image_name ,
110+ container_image ,
134111 "--platform=managed" ,
135112 "--no-allow-unauthenticated" ,
136113 "--region=us-central1" ,
137114 "--vpc-connector" ,
138- connector_name ,
115+ VPC_CONNECTOR_NAME ,
139116 "--set-env-vars" ,
140117 f"REDISHOST={ redis_host } ,REDISPORT=6379" ,
141118 "--project" ,
142- project ,
119+ PROJECT ,
143120 ], check = True
144121 )
122+ yield service_name
145123
124+ # Delete Cloud Run service
125+ subprocess .run (
126+ [
127+ "gcloud" ,
128+ "run" ,
129+ "services" ,
130+ "delete" ,
131+ service_name ,
132+ "--platform=managed" ,
133+ "--region=us-central1" ,
134+ "--quiet" ,
135+ "--project" ,
136+ PROJECT ,
137+ ], check = True
138+ )
139+
140+
141+ @pytest .fixture
142+ def service_url_auth_token (deployed_service ):
146143 # Get Cloud Run service URL and auth token
147144 service_url = subprocess .run (
148145 [
149146 "gcloud" ,
150147 "run" ,
151148 "services" ,
152149 "describe" ,
153- service_name ,
150+ deployed_service ,
154151 "--platform=managed" ,
155152 "--region=us-central1" ,
156153 "--format=value(status.url)" ,
157154 "--project" ,
158- project ,
155+ PROJECT ,
159156 ],
160157 stdout = subprocess .PIPE ,
161158 check = True
@@ -168,86 +165,11 @@ def services():
168165
169166 yield service_url , auth_token
170167
171- # Delete Cloud Run service
172- subprocess .run (
173- [
174- "gcloud" ,
175- "run" ,
176- "services" ,
177- "delete" ,
178- service_name ,
179- "--platform=managed" ,
180- "--region=us-central1" ,
181- "--quiet" ,
182- "--project" ,
183- project ,
184- ], check = True
185- )
186-
187- # Delete container image
188- subprocess .run (
189- [
190- "gcloud" ,
191- "container" ,
192- "images" ,
193- "delete" ,
194- image_name ,
195- "--quiet" ,
196- "--project" ,
197- project ,
198- ], check = True
199- )
200-
201- # Delete Redis instance
202- subprocess .run (
203- [
204- "gcloud" ,
205- "redis" ,
206- "instances" ,
207- "delete" ,
208- instance_name ,
209- "--region=us-central1" ,
210- "--quiet" ,
211- "--async" ,
212- "--project" ,
213- project ,
214- ], check = True
215- )
216-
217- # Delete Serverless VPC Access connector
218- subprocess .run (
219- [
220- "gcloud" ,
221- "compute" ,
222- "networks" ,
223- "vpc-access" ,
224- "connectors" ,
225- "delete" ,
226- connector_name ,
227- "--region=us-central1" ,
228- "--quiet" ,
229- "--project" ,
230- project ,
231- ], check = True
232- )
233-
234- # Delete VPC network
235- subprocess .run (
236- [
237- "gcloud" ,
238- "compute" ,
239- "networks" ,
240- "delete" ,
241- network_name ,
242- "--quiet" ,
243- "--project" ,
244- project ,
245- ], check = True
246- )
168+ # no deletion needed
247169
248170
249- def test_end_to_end (services ):
250- service_url , auth_token = services
171+ def test_end_to_end (service_url_auth_token ):
172+ service_url , auth_token = service_url_auth_token
251173
252174 req = request .Request (
253175 service_url ,
0 commit comments