31
31
class SimpleTest (unittest .TestCase ):
32
32
def test_double_init (self ):
33
33
with get_new_node ('test' ) as node :
34
- got_exception = False
35
-
36
- try :
34
+ # can't initialize node more than once
35
+ with self .assertRaises (InitNodeException ):
37
36
node .init ()
38
37
node .init ()
39
- except InitNodeException as e :
40
- got_exception = True
41
38
42
- self .assertTrue (got_exception )
39
+ def test_init_after_cleanup (self ):
40
+ with get_new_node ('test' ) as node :
41
+ node .init ().start ()
42
+ node .status ()
43
+ node .safe_psql ('postgres' , 'select 1' )
44
+
45
+ node .cleanup ()
46
+
47
+ node .init ().start ()
48
+ node .status ()
49
+ node .safe_psql ('postgres' , 'select 1' )
43
50
44
51
def test_uninitialized_start (self ):
45
52
with get_new_node ('test' ) as node :
46
- got_exception = False
47
-
48
- try :
53
+ # node is not initialized yet
54
+ with self .assertRaises (StartNodeException ):
49
55
node .start ()
50
- except StartNodeException as e :
51
- got_exception = True
52
-
53
- self .assertTrue (got_exception )
54
56
55
57
def test_restart (self ):
56
58
with get_new_node ('test' ) as node :
@@ -66,12 +68,8 @@ def test_psql(self):
66
68
node .init ().start ()
67
69
68
70
# check default params
69
- got_exception = False
70
- try :
71
+ with self .assertRaises (QueryException ):
71
72
node .psql ('postgres' )
72
- except QueryException as e :
73
- got_exception = True
74
- self .assertTrue (got_exception )
75
73
76
74
# check returned values
77
75
res = node .psql ('postgres' , 'select 1' )
@@ -86,12 +84,8 @@ def test_psql(self):
86
84
node .stop ()
87
85
88
86
# check psql on stopped node
89
- got_exception = False
90
- try :
87
+ with self .assertRaises (QueryException ):
91
88
node .safe_psql ('postgres' , 'select 1' )
92
- except QueryException as e :
93
- got_exception = True
94
- self .assertTrue (got_exception )
95
89
96
90
def test_status (self ):
97
91
# check NodeStatus cast to bool
@@ -181,28 +175,17 @@ def test_transactions(self):
181
175
182
176
def test_control_data (self ):
183
177
with get_new_node ('test' ) as node :
184
- got_exception = False
185
178
186
- try :
179
+ # node is not initialized yet
180
+ with self .assertRaises (ExecUtilException ):
187
181
node .get_control_data ()
188
- except ExecUtilException as e :
189
- got_exception = True
190
- self .assertTrue (got_exception )
191
182
192
- got_exception = False
193
-
194
- try :
195
- node .init ()
196
- data = node .get_control_data ()
197
-
198
- # check returned dict
199
- self .assertIsNotNone (data )
200
- self .assertTrue (any ('pg_control' in s for s in data .keys ()))
183
+ node .init ()
184
+ data = node .get_control_data ()
201
185
202
- except ExecUtilException as e :
203
- print (e .message )
204
- got_exception = True
205
- self .assertFalse (got_exception )
186
+ # check returned dict
187
+ self .assertIsNotNone (data )
188
+ self .assertTrue (any ('pg_control' in s for s in data .keys ()))
206
189
207
190
def test_backup_simple (self ):
208
191
with get_new_node ('master' ) as master :
@@ -238,19 +221,15 @@ def test_backup_exhaust(self):
238
221
node .init (allow_streaming = True ).start ()
239
222
240
223
with node .backup (xlog_method = 'fetch' ) as backup :
224
+
225
+ # exhaust backup by creating new node
241
226
with backup .spawn_primary ('node1' ) as node1 :
242
227
pass
243
228
244
- got_exception = False
245
- try :
229
+ # now let's try to create one more node
230
+ with self . assertRaises ( BackupException ) :
246
231
with backup .spawn_primary ('node2' ) as node2 :
247
232
pass
248
- except BackupException as e :
249
- got_exception = True
250
- except Exception as e :
251
- pass
252
-
253
- self .assertTrue (got_exception )
254
233
255
234
def test_backup_and_replication (self ):
256
235
with get_new_node ('node' ) as node , get_new_node ('repl' ) as replica :
@@ -296,12 +275,9 @@ def test_incorrect_catchup(self):
296
275
with get_new_node ('node' ) as node :
297
276
node .init (allow_streaming = True ).start ()
298
277
299
- got_exception = False
300
- try :
278
+ # node has no master, can't catch up
279
+ with self . assertRaises ( CatchUpException ) :
301
280
node .catchup ()
302
- except CatchUpException as e :
303
- got_exception = True
304
- self .assertTrue (got_exception )
305
281
306
282
def test_dump (self ):
307
283
with get_new_node ('node1' ) as node1 :
@@ -348,41 +324,24 @@ def test_poll_query_until(self):
348
324
self .assertTrue (end_time - start_time >= 5 )
349
325
350
326
# check 0 rows
351
- got_exception = False
352
- try :
327
+ with self .assertRaises (QueryException ):
353
328
node .poll_query_until (
354
329
'postgres' , 'select * from pg_class where true = false' )
355
- except QueryException as e :
356
- got_exception = True
357
- self .assertTrue (got_exception )
358
330
359
331
# check 0 columns
360
- got_exception = False
361
- try :
332
+ with self .assertRaises (QueryException ):
362
333
node .poll_query_until ('postgres' ,
363
334
'select from pg_class limit 1' )
364
- except QueryException as e :
365
- got_exception = True
366
- self .assertTrue (got_exception )
367
-
368
335
# check None
369
- got_exception = False
370
- try :
336
+ with self .assertRaises (QueryException ):
371
337
node .poll_query_until ('postgres' , 'create table abc (val int)' )
372
- except QueryException as e :
373
- got_exception = True
374
- self .assertTrue (got_exception )
375
338
376
339
# check timeout
377
- got_exception = False
378
- try :
340
+ with self .assertRaises (TimeoutException ):
379
341
node .poll_query_until (dbname = 'postgres' ,
380
342
query = 'select 1 > 2' ,
381
343
max_attempts = 5 ,
382
344
sleep_time = 0.2 )
383
- except TimeoutException as e :
384
- got_exception = True
385
- self .assertTrue (got_exception )
386
345
387
346
def test_logging (self ):
388
347
logfile = tempfile .NamedTemporaryFile ('w' , delete = True )
@@ -511,13 +470,8 @@ def test_isolation_levels(self):
511
470
con .begin (IsolationLevel .Serializable ).commit ()
512
471
513
472
# check wrong level
514
- got_exception = False
515
- try :
473
+ with self .assertRaises (QueryException ):
516
474
con .begin ('Garbage' ).commit ()
517
- except QueryException :
518
- got_exception = True
519
-
520
- self .assertTrue (got_exception )
521
475
522
476
523
477
if __name__ == '__main__' :
0 commit comments