1
1
package deploy_test
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"os"
6
7
"os/exec"
@@ -204,7 +205,18 @@ var _ = Describe("Deploy single instance", func() {
204
205
pre_stop_value := fmt .Sprintf (psql_command , pre_stop_role_name )
205
206
post_stop_value := fmt .Sprintf ("echo %s" , post_stop_uuid )
206
207
207
- err = createOrUpdateDeployment (version , manifestPath , envName , variables , helpers .DefineHooks ("0" , pre_start_value , post_start_value , pre_stop_value , post_stop_value ))
208
+ jan := helpers.Janitor {
209
+ Script : `${PACKAGE_DIR}/bin/psql -U vcap -p ${PORT} -d postgres << EOF
210
+ CREATE TABLE IF NOT EXISTS test_hook(name VARCHAR(10) NOT NULL UNIQUE,total INTEGER NOT NULL);
211
+ INSERT INTO test_hook (name, total) VALUES ('test', 1) ON CONFLICT (name) DO NOTHING;
212
+ UPDATE test_hook SET total = total + 1 WHERE name = 'test';
213
+ EOF
214
+ ` ,
215
+ Timeout : 60 ,
216
+ Interval : 1 ,
217
+ }
218
+
219
+ err = createOrUpdateDeployment (version , manifestPath , envName , variables , append (jan .GetOpDefinitions (), helpers .DefineHooks ("0" , pre_start_value , post_start_value , pre_stop_value , post_stop_value )... ))
208
220
Expect (err ).NotTo (HaveOccurred ())
209
221
210
222
sshKeyFile , err := writeSSHKey (envName )
@@ -235,6 +247,52 @@ var _ = Describe("Deploy single instance", func() {
235
247
cmd = exec .Command ("ssh" , "-i" , sshKeyFile , "-o" , "UserKnownHostsFile=/dev/null" , "-o" , "StrictHostKeyChecking=no" , fmt .Sprintf ("%s@%s" , variables ["testuser_name" ], pgHost ), fmt .Sprintf (bosh_ssh_command , post_stop_uuid ))
236
248
err = cmd .Run ()
237
249
Expect (err ).NotTo (HaveOccurred ())
250
+ err = os .Remove (sshKeyFile )
251
+
252
+ By ("Testing the frequency-based hook" )
253
+ conn , err := DB .GetSuperUserConnection ()
254
+ Eventually (func () int {
255
+ rows , err := conn .Run ("select total from test_hook where name = 'test'" )
256
+ var counter struct {
257
+ Total int `json:"total"`
258
+ }
259
+ Expect (err ).NotTo (HaveOccurred ())
260
+ err = json .Unmarshal ([]byte (rows [0 ]), & counter )
261
+ Expect (err ).NotTo (HaveOccurred ())
262
+ return counter .Total
263
+ }, "15s" , "2s" ).Should (BeNumerically (">" , 10 ))
264
+
265
+ By ("Verifying that janitor script failure causes monit to restart janitor" )
266
+ jan = helpers.Janitor {
267
+ Script : `#!/bin/bash
268
+ STATEFILE=/tmp/statefile
269
+ if [ -f $STATEFILE ]; then
270
+ echo second start >> $STATEFILE
271
+ else
272
+ touch $STATEFILE
273
+ chmod 777 $STATEFILE
274
+ exit 1
275
+ fi` ,
276
+ Timeout : 60 ,
277
+ Interval : 86400 ,
278
+ }
279
+
280
+ err = createOrUpdateDeployment (version , manifestPath , envName , variables , jan .GetOpDefinitions ())
281
+ Expect (err ).NotTo (HaveOccurred ())
282
+
283
+ sshKeyFile , err = writeSSHKey (envName )
284
+ Expect (err ).NotTo (HaveOccurred ())
285
+
286
+ Eventually (func () string {
287
+ bosh_ssh_command = "grep second /tmp/statefile"
288
+ cmd = exec .Command ("ssh" , "-i" , sshKeyFile , "-o" , "UserKnownHostsFile=/dev/null" , "-o" , "StrictHostKeyChecking=no" , fmt .Sprintf ("%s@%s" , variables ["testuser_name" ], pgHost ), bosh_ssh_command )
289
+ err = cmd .Run ()
290
+ if err != nil {
291
+ return err .Error ()
292
+ }
293
+ return ""
294
+ }, "10s" , "2s" ).Should (BeEmpty ())
295
+ err = os .Remove (sshKeyFile )
238
296
239
297
By ("Verifying that hooks failure does not prevent postgres to start" )
240
298
pre_start_uuid = helpers .GetUUID ()
@@ -248,6 +306,7 @@ var _ = Describe("Deploy single instance", func() {
248
306
Expect (err ).NotTo (HaveOccurred ())
249
307
_ , err = DB .GetPostgreSQLVersion ()
250
308
Expect (err ).NotTo (HaveOccurred ())
309
+ err = os .Remove (sshKeyFile )
251
310
})
252
311
253
312
It ("Successfully deploys a fresh env" , func () {
@@ -457,6 +516,7 @@ var _ = Describe("Deploy single instance", func() {
457
516
variables = make (map [string ]interface {})
458
517
variables ["defuser_name" ] = "pgadmin"
459
518
variables ["defuser_password" ] = "admin"
519
+ opDefs = nil
460
520
})
461
521
It ("Successfully upgrades from older" , AssertUpgradeSuccessful ())
462
522
})
@@ -467,6 +527,7 @@ var _ = Describe("Deploy single instance", func() {
467
527
variables = make (map [string ]interface {})
468
528
variables ["defuser_name" ] = "pgadmin"
469
529
variables ["defuser_password" ] = "admin"
530
+ opDefs = nil
470
531
})
471
532
It ("Successfully upgrades from old" , AssertUpgradeSuccessful ())
472
533
})
@@ -488,6 +549,7 @@ var _ = Describe("Deploy single instance", func() {
488
549
variables = make (map [string ]interface {})
489
550
variables ["defuser_name" ] = "pgadmin"
490
551
variables ["defuser_password" ] = "admin"
552
+ opDefs = nil
491
553
})
492
554
It ("Successfully upgrades from master" , AssertUpgradeSuccessful ())
493
555
})
0 commit comments