2
2
# coding: utf-8
3
3
4
4
import os
5
+ import shutil
5
6
import subprocess
6
7
import tempfile
7
8
import testgres
@@ -60,7 +61,6 @@ def test_custom_init(self):
60
61
with get_new_node () as node :
61
62
# enable page checksums
62
63
node .init (initdb_params = ['-k' ]).start ()
63
- node .safe_psql ('select 1' )
64
64
65
65
with get_new_node () as node :
66
66
node .init (
@@ -79,27 +79,36 @@ def test_custom_init(self):
79
79
80
80
def test_double_init (self ):
81
81
with get_new_node ().init () as node :
82
-
83
82
# can't initialize node more than once
84
83
with self .assertRaises (InitNodeException ):
85
84
node .init ()
86
85
87
86
def test_init_after_cleanup (self ):
88
87
with get_new_node () as node :
89
- node .init ().start ()
90
- node .status ()
91
- node .safe_psql ('select 1' )
92
-
88
+ node .init ().start ().execute ('select 1' )
93
89
node .cleanup ()
90
+ node .init ().start ().execute ('select 1' )
94
91
95
- node .init ().start ()
96
- node .status ()
97
- node .safe_psql ('select 1' )
92
+ def test_node_exit (self ):
93
+ base_dir = None
98
94
99
- def test_double_start (self ):
100
- with get_new_node () as node :
101
- node .init ().start ()
95
+ with self .assertRaises (QueryException ):
96
+ with get_new_node ().init () as node :
97
+ base_dir = node .base_dir
98
+ node .safe_psql ('select 1' )
99
+
100
+ # we should save the DB for "debugging"
101
+ self .assertTrue (os .path .exists (base_dir ))
102
+ shutil .rmtree (base_dir , ignore_errors = True )
103
+
104
+ with get_new_node ().init () as node :
105
+ base_dir = node .base_dir
106
+
107
+ # should have been removed by default
108
+ self .assertFalse (os .path .exists (base_dir ))
102
109
110
+ def test_double_start (self ):
111
+ with get_new_node ().init ().start () as node :
103
112
# can't start node more than once
104
113
with self .assertRaises (StartNodeException ):
105
114
node .start ()
@@ -150,38 +159,33 @@ def test_pg_ctl(self):
150
159
self .assertTrue ('PID' in status )
151
160
152
161
def test_status (self ):
153
- # check NodeStatus cast to bool
154
162
self .assertTrue (NodeStatus .Running )
155
-
156
- # check NodeStatus cast to bool
157
163
self .assertFalse (NodeStatus .Stopped )
158
-
159
- # check NodeStatus cast to bool
160
164
self .assertFalse (NodeStatus .Uninitialized )
161
165
162
166
# check statuses after each operation
163
167
with get_new_node () as node :
164
- self .assertEqual (node .get_pid () , 0 )
168
+ self .assertEqual (node .pid , 0 )
165
169
self .assertEqual (node .status (), NodeStatus .Uninitialized )
166
170
167
171
node .init ()
168
172
169
- self .assertEqual (node .get_pid () , 0 )
173
+ self .assertEqual (node .pid , 0 )
170
174
self .assertEqual (node .status (), NodeStatus .Stopped )
171
175
172
176
node .start ()
173
177
174
- self .assertTrue (node .get_pid () > 0 )
178
+ self .assertNotEqual (node .pid , 0 )
175
179
self .assertEqual (node .status (), NodeStatus .Running )
176
180
177
181
node .stop ()
178
182
179
- self .assertEqual (node .get_pid () , 0 )
183
+ self .assertEqual (node .pid , 0 )
180
184
self .assertEqual (node .status (), NodeStatus .Stopped )
181
185
182
186
node .cleanup ()
183
187
184
- self .assertEqual (node .get_pid () , 0 )
188
+ self .assertEqual (node .pid , 0 )
185
189
self .assertEqual (node .status (), NodeStatus .Uninitialized )
186
190
187
191
def test_psql (self ):
@@ -283,8 +287,7 @@ def test_backup_simple(self):
283
287
master .psql ('create table test as select generate_series(1, 4) i' )
284
288
285
289
with master .backup (xlog_method = 'stream' ) as backup :
286
- with backup .spawn_primary () as slave :
287
- slave .start ()
290
+ with backup .spawn_primary ().start () as slave :
288
291
res = slave .execute ('select * from test order by i asc' )
289
292
self .assertListEqual (res , [(1 , ), (2 , ), (3 , ), (4 , )])
290
293
@@ -310,36 +313,12 @@ def test_backup_exhaust(self):
310
313
with node .backup (xlog_method = 'fetch' ) as backup :
311
314
312
315
# exhaust backup by creating new node
313
- with backup .spawn_primary () as node1 : # noqa
316
+ with backup .spawn_primary ():
314
317
pass
315
318
316
319
# now let's try to create one more node
317
320
with self .assertRaises (BackupException ):
318
- with backup .spawn_primary () as node2 : # noqa
319
- pass
320
-
321
- def test_backup_and_replication (self ):
322
- with get_new_node () as node :
323
- node .init (allow_streaming = True ).start ()
324
-
325
- node .psql ('create table abc(a int, b int)' )
326
- node .psql ('insert into abc values (1, 2)' )
327
-
328
- backup = node .backup ()
329
-
330
- with backup .spawn_replica ().start () as replica :
331
- res = replica .execute ('select * from abc' )
332
- self .assertListEqual (res , [(1 , 2 )])
333
-
334
- # Insert into master node
335
- node .psql ('insert into abc values (3, 4)' )
336
-
337
- # Wait until data syncronizes
338
- replica .catchup ()
339
-
340
- # Check that this record was exported to replica
341
- res = replica .execute ('select * from abc' )
342
- self .assertListEqual (res , [(1 , 2 ), (3 , 4 )])
321
+ backup .spawn_primary ()
343
322
344
323
def test_replicate (self ):
345
324
with get_new_node () as node :
@@ -574,8 +553,6 @@ def test_auto_name(self):
574
553
self .assertTrue (r .status ())
575
554
576
555
# check their names
577
- self .assertIsNotNone (m .name )
578
- self .assertIsNotNone (r .name )
579
556
self .assertNotEqual (m .name , r .name )
580
557
self .assertTrue ('testgres' in m .name )
581
558
self .assertTrue ('testgres' in r .name )
@@ -588,7 +565,9 @@ def test_file_tail(self):
588
565
s3 = "def\n "
589
566
590
567
with tempfile .NamedTemporaryFile (mode = 'r+' , delete = True ) as f :
591
- for i in range (1 , 5000 ):
568
+ sz = 0
569
+ while sz < 3 * 8192 :
570
+ sz += len (s1 )
592
571
f .write (s1 )
593
572
f .write (s2 )
594
573
f .write (s3 )
0 commit comments