@@ -93,3 +93,262 @@ spec:
93
93
io .Copy (& buf , r )
94
94
assert .Equal (t , expectedOutput , buf .String ())
95
95
}
96
+
97
+ func Test_runInjectorCorrectTimeout (t * testing.T ) {
98
+
99
+ expectedOutput := `apiVersion: extensions/v1beta1
100
+ kind: Deployment
101
+ metadata:
102
+ creationTimestamp: null
103
+ name: test
104
+ spec:
105
+ replicas: 1
106
+ strategy: {}
107
+ template:
108
+ metadata:
109
+ creationTimestamp: null
110
+ spec:
111
+ containers:
112
+ - image: some-image
113
+ name: name-test
114
+ resources: {}
115
+ - command:
116
+ - /cloud_sql_proxy
117
+ - -instances=project-test:region-test:instance-test
118
+ - -log_debug_stdout=true
119
+ - -verbose=
120
+ - -credential_file=/secrets/cloudsql/credentials.json
121
+ - -term_timeout=35s
122
+ image: gcr.io/cloudsql-docker/gce-proxy:1.11
123
+ name: cloudsql-proxy
124
+ resources:
125
+ limits:
126
+ cpu: 100m
127
+ memory: 128Mi
128
+ requests:
129
+ cpu: 5m
130
+ memory: 8Mi
131
+ securityContext:
132
+ allowPrivilegeEscalation: false
133
+ runAsUser: 2
134
+ volumeMounts:
135
+ - mountPath: /secrets/cloudsql
136
+ name: cloudsql-proxy-credentials
137
+ readOnly: true
138
+ volumes:
139
+ - name: test-volume
140
+ secret:
141
+ secretName: test-secret
142
+ - name: cloudsql-proxy-credentials
143
+ secret:
144
+ secretName: cloudsql-proxy-credentials
145
+ status: {}
146
+
147
+ ---
148
+
149
+ apiVersion: v1
150
+ kind: Service
151
+ metadata:
152
+ name: test-svc
153
+ spec:
154
+ ports:
155
+ - name: web
156
+ port: 8080`
157
+
158
+ // Just to trick to get control other stdout
159
+ // r and w are linked => whatever is written in w is readable in r
160
+ oldStdout := os .Stdout
161
+ r , w , err := os .Pipe ()
162
+ require .NoError (t , err )
163
+ os .Stdout = w
164
+
165
+ * path = "./test/test.yaml"
166
+ * instance = "instance-test"
167
+ * region = "region-test"
168
+ * project = "project-test"
169
+ * cpuRequest = "5m"
170
+ * memoryRequest = "8Mi"
171
+ * cpuLimit = "100m"
172
+ * memoryLimit = "128Mi"
173
+ * proxyVersion = "1.11"
174
+ * termTimeout = "35s"
175
+
176
+ runInjector ()
177
+ os .Stdout = oldStdout
178
+ w .Close ()
179
+ var buf bytes.Buffer
180
+ io .Copy (& buf , r )
181
+ assert .Equal (t , expectedOutput , buf .String ())
182
+ }
183
+
184
+ func Test_runInjectorTimeoutIncorrect (t * testing.T ) {
185
+
186
+ expectedOutput := `apiVersion: extensions/v1beta1
187
+ kind: Deployment
188
+ metadata:
189
+ creationTimestamp: null
190
+ name: test
191
+ spec:
192
+ replicas: 1
193
+ strategy: {}
194
+ template:
195
+ metadata:
196
+ creationTimestamp: null
197
+ spec:
198
+ containers:
199
+ - image: some-image
200
+ name: name-test
201
+ resources: {}
202
+ - command:
203
+ - /cloud_sql_proxy
204
+ - -instances=project-test:region-test:instance-test
205
+ - -log_debug_stdout=true
206
+ - -verbose=
207
+ - -credential_file=/secrets/cloudsql/credentials.json
208
+ image: gcr.io/cloudsql-docker/gce-proxy:1.11
209
+ name: cloudsql-proxy
210
+ resources:
211
+ limits:
212
+ cpu: 100m
213
+ memory: 128Mi
214
+ requests:
215
+ cpu: 5m
216
+ memory: 8Mi
217
+ securityContext:
218
+ allowPrivilegeEscalation: false
219
+ runAsUser: 2
220
+ volumeMounts:
221
+ - mountPath: /secrets/cloudsql
222
+ name: cloudsql-proxy-credentials
223
+ readOnly: true
224
+ volumes:
225
+ - name: test-volume
226
+ secret:
227
+ secretName: test-secret
228
+ - name: cloudsql-proxy-credentials
229
+ secret:
230
+ secretName: cloudsql-proxy-credentials
231
+ status: {}
232
+
233
+ ---
234
+
235
+ apiVersion: v1
236
+ kind: Service
237
+ metadata:
238
+ name: test-svc
239
+ spec:
240
+ ports:
241
+ - name: web
242
+ port: 8080`
243
+
244
+ // Just to trick to get control other stdout
245
+ // r and w are linked => whatever is written in w is readable in r
246
+ oldStdout := os .Stdout
247
+ r , w , err := os .Pipe ()
248
+ require .NoError (t , err )
249
+ os .Stdout = w
250
+
251
+ * path = "./test/test.yaml"
252
+ * instance = "instance-test"
253
+ * region = "region-test"
254
+ * project = "project-test"
255
+ * cpuRequest = "5m"
256
+ * memoryRequest = "8Mi"
257
+ * cpuLimit = "100m"
258
+ * memoryLimit = "128Mi"
259
+ * proxyVersion = "1.11"
260
+ * termTimeout = "35"
261
+
262
+ runInjector ()
263
+ os .Stdout = oldStdout
264
+ w .Close ()
265
+ var buf bytes.Buffer
266
+ io .Copy (& buf , r )
267
+ assert .Equal (t , expectedOutput , buf .String ())
268
+ }
269
+
270
+ func Test_runInjectorTimeoutEmpty (t * testing.T ) {
271
+
272
+ expectedOutput := `apiVersion: extensions/v1beta1
273
+ kind: Deployment
274
+ metadata:
275
+ creationTimestamp: null
276
+ name: test
277
+ spec:
278
+ replicas: 1
279
+ strategy: {}
280
+ template:
281
+ metadata:
282
+ creationTimestamp: null
283
+ spec:
284
+ containers:
285
+ - image: some-image
286
+ name: name-test
287
+ resources: {}
288
+ - command:
289
+ - /cloud_sql_proxy
290
+ - -instances=project-test:region-test:instance-test
291
+ - -log_debug_stdout=true
292
+ - -verbose=
293
+ - -credential_file=/secrets/cloudsql/credentials.json
294
+ image: gcr.io/cloudsql-docker/gce-proxy:1.11
295
+ name: cloudsql-proxy
296
+ resources:
297
+ limits:
298
+ cpu: 100m
299
+ memory: 128Mi
300
+ requests:
301
+ cpu: 5m
302
+ memory: 8Mi
303
+ securityContext:
304
+ allowPrivilegeEscalation: false
305
+ runAsUser: 2
306
+ volumeMounts:
307
+ - mountPath: /secrets/cloudsql
308
+ name: cloudsql-proxy-credentials
309
+ readOnly: true
310
+ volumes:
311
+ - name: test-volume
312
+ secret:
313
+ secretName: test-secret
314
+ - name: cloudsql-proxy-credentials
315
+ secret:
316
+ secretName: cloudsql-proxy-credentials
317
+ status: {}
318
+
319
+ ---
320
+
321
+ apiVersion: v1
322
+ kind: Service
323
+ metadata:
324
+ name: test-svc
325
+ spec:
326
+ ports:
327
+ - name: web
328
+ port: 8080`
329
+
330
+ // Just to trick to get control other stdout
331
+ // r and w are linked => whatever is written in w is readable in r
332
+ oldStdout := os .Stdout
333
+ r , w , err := os .Pipe ()
334
+ require .NoError (t , err )
335
+ os .Stdout = w
336
+
337
+ * path = "./test/test.yaml"
338
+ * instance = "instance-test"
339
+ * region = "region-test"
340
+ * project = "project-test"
341
+ * cpuRequest = "5m"
342
+ * memoryRequest = "8Mi"
343
+ * cpuLimit = "100m"
344
+ * memoryLimit = "128Mi"
345
+ * proxyVersion = "1.11"
346
+ * termTimeout = ""
347
+
348
+ runInjector ()
349
+ os .Stdout = oldStdout
350
+ w .Close ()
351
+ var buf bytes.Buffer
352
+ io .Copy (& buf , r )
353
+ assert .Equal (t , expectedOutput , buf .String ())
354
+ }
0 commit comments