24
24
25
25
import pytest
26
26
27
+ # Unique suffix to create distinct service names
27
28
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_NETWORK_NAME = "memorystore-redis"
32
+ VPC_CONNECTOR_NAME = "test-connector"
33
+ MEMORYSTORE_REDIS_NAME = "static-test-instance"
33
34
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
- )
84
35
36
+ @pytest .fixture
37
+ def redis_host ():
85
38
# Get the Redis instance's IP
86
39
redis_host = subprocess .run (
87
40
[
88
41
"gcloud" ,
89
42
"redis" ,
90
43
"instances" ,
91
44
"describe" ,
92
- instance_name ,
45
+ MEMORYSTORE_REDIS_NAME ,
93
46
"--region=us-central1" ,
94
47
"--format=value(host)" ,
95
48
"--project" ,
96
- project ,
49
+ PROJECT ,
97
50
],
98
51
stdout = subprocess .PIPE ,
99
52
check = True
100
53
).stdout .strip ().decode ()
54
+ yield redis_host
55
+
56
+ # no deletion needs to happen, this is a "get" of a static instance
101
57
58
+
59
+ @pytest .fixture
60
+ def container_image ():
102
61
# Build container image for Cloud Run deployment
103
- image_name = f"gcr.io/{ project } /test-visit-count-{ suffix } "
62
+ image_name = f"gcr.io/{ PROJECT } /test-visit-count-{ SUFFIX } "
104
63
subprocess .run (
105
64
[
106
65
"cp" ,
@@ -116,46 +75,85 @@ def services():
116
75
"--tag" ,
117
76
image_name ,
118
77
"--project" ,
119
- project ,
78
+ PROJECT ,
120
79
], check = True
121
80
)
81
+ yield image_name
82
+
122
83
subprocess .run (["rm" , "Dockerfile" ], check = True )
123
84
85
+ # Delete container image
86
+ subprocess .run (
87
+ [
88
+ "gcloud" ,
89
+ "container" ,
90
+ "images" ,
91
+ "delete" ,
92
+ image_name ,
93
+ "--quiet" ,
94
+ "--project" ,
95
+ PROJECT ,
96
+ ], check = True
97
+ )
98
+
99
+
100
+ @pytest .fixture
101
+ def deployed_service (container_image , redis_host ):
124
102
# Deploy image to Cloud Run
125
- service_name = f"test-visit-count-{ suffix } "
103
+ service_name = f"test-visit-count-{ SUFFIX } "
126
104
subprocess .run (
127
105
[
128
106
"gcloud" ,
129
107
"run" ,
130
108
"deploy" ,
131
109
service_name ,
132
110
"--image" ,
133
- image_name ,
111
+ container_image ,
134
112
"--platform=managed" ,
135
113
"--no-allow-unauthenticated" ,
136
114
"--region=us-central1" ,
137
115
"--vpc-connector" ,
138
- connector_name ,
116
+ VPC_CONNECTOR_NAME ,
139
117
"--set-env-vars" ,
140
118
f"REDISHOST={ redis_host } ,REDISPORT=6379" ,
141
119
"--project" ,
142
- project ,
120
+ PROJECT ,
143
121
], check = True
144
122
)
123
+ yield service_name
145
124
125
+ # Delete Cloud Run service
126
+ subprocess .run (
127
+ [
128
+ "gcloud" ,
129
+ "run" ,
130
+ "services" ,
131
+ "delete" ,
132
+ service_name ,
133
+ "--platform=managed" ,
134
+ "--region=us-central1" ,
135
+ "--quiet" ,
136
+ "--project" ,
137
+ PROJECT ,
138
+ ], check = True
139
+ )
140
+
141
+
142
+ @pytest .fixture
143
+ def service_url_auth_token (deployed_service ):
146
144
# Get Cloud Run service URL and auth token
147
145
service_url = subprocess .run (
148
146
[
149
147
"gcloud" ,
150
148
"run" ,
151
149
"services" ,
152
150
"describe" ,
153
- service_name ,
151
+ deployed_service ,
154
152
"--platform=managed" ,
155
153
"--region=us-central1" ,
156
154
"--format=value(status.url)" ,
157
155
"--project" ,
158
- project ,
156
+ PROJECT ,
159
157
],
160
158
stdout = subprocess .PIPE ,
161
159
check = True
@@ -168,86 +166,11 @@ def services():
168
166
169
167
yield service_url , auth_token
170
168
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
- )
169
+ # no deletion needed
247
170
248
171
249
- def test_end_to_end (services ):
250
- service_url , auth_token = services
172
+ def test_end_to_end (service_url_auth_token ):
173
+ service_url , auth_token = service_url_auth_token
251
174
252
175
req = request .Request (
253
176
service_url ,
0 commit comments