Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1244cbd

Browse files
committed
basebackup and init from backup functions added
1 parent 7554721 commit 1244cbd

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

testgres/testgres.py

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def __init__(self, name, port):
5555
self.port = port
5656
self.base_dir = tempfile.mkdtemp()
5757
os.makedirs(self.logs_dir)
58-
os.makedirs(self.data_dir)
5958
self.working = False
6059
self.config = {}
6160
self.load_pg_config()
@@ -94,10 +93,11 @@ def get_bin_path(self, filename):
9493
else:
9594
return "%s/%s" % (self.config["BINDIR"], filename)
9695

97-
def init(self):
96+
def init(self, allows_streaming=False):
9897
""" Performs initdb """
9998

10099
# initialize cluster
100+
os.makedirs(self.data_dir)
101101
initdb = self.get_bin_path("initdb")
102102
with open(self.output_filename, "a") as file_out, \
103103
open(self.error_filename, "a") as file_err:
@@ -112,9 +112,41 @@ def init(self):
112112
# add parameters to config file
113113
config_name = "%s/postgresql.conf" % self.data_dir
114114
with open(config_name, "a") as conf:
115-
conf.write("fsync = off\n")
116-
conf.write("log_statement = all\n")
117-
conf.write("port = %s\n" % self.port)
115+
conf.write(
116+
"fsync = off\n"
117+
"log_statement = all\n"
118+
"port = %s\n" % self.port)
119+
120+
if allows_streaming:
121+
conf.write(
122+
"wal_level = replica\n"
123+
"max_wal_senders = 5\n"
124+
"wal_keep_segments = 20\n"
125+
"max_wal_size = 128MB\n"
126+
"shared_buffers = 1MB\n"
127+
"wal_log_hints = on\n"
128+
"hot_standby = on\n"
129+
"max_connections = 10\n")
130+
self.set_replication_conf()
131+
132+
def init_from_backup(self, root_node, backup_name):
133+
"""Initializes cluster from backup, made by another node"""
134+
135+
# Copy data from backup
136+
backup_path = "%s/%s" % (root_node.base_dir, backup_name)
137+
shutil.copytree(backup_path, self.data_dir)
138+
os.chmod(self.data_dir, 0o0700)
139+
140+
# Change port in config file
141+
self.append_conf(
142+
"postgresql.conf",
143+
"port = %s" % self.port
144+
)
145+
146+
def set_replication_conf(self):
147+
hba_conf = "%s/pg_hba.conf" % self.data_dir
148+
with open(hba_conf, "a") as conf:
149+
conf.write("local replication all trust\n")
118150

119151
def append_conf(self, filename, string):
120152
"""Appends line to a config file like "postgresql.conf"
@@ -133,7 +165,7 @@ def pg_ctl(self, command, params):
133165
functions"""
134166
pg_ctl = self.get_bin_path("pg_ctl")
135167

136-
arguments = ['pg_ctl']
168+
arguments = ["pg_ctl"]
137169
for key, value in params.iteritems():
138170
arguments.append(key)
139171
if value:
@@ -240,6 +272,24 @@ def execute(self, dbname, query):
240272

241273
return res
242274

275+
def backup(self, name):
276+
"""Performs pg_basebackup"""
277+
pg_basebackup = self.get_bin_path("pg_basebackup")
278+
backup_path = self.base_dir + "/" + name
279+
os.makedirs(backup_path)
280+
params = [pg_basebackup, "-D", backup_path, "-p %s" % self.port, "-x"]
281+
with open(self.output_filename, "a") as file_out, \
282+
open(self.error_filename, "a") as file_err:
283+
ret = subprocess.call(
284+
params,
285+
stdout=file_out,
286+
stderr=file_err
287+
)
288+
if ret:
289+
raise ClusterException("Base backup failed")
290+
291+
return backup_path
292+
243293

244294
def get_username():
245295
""" Returns current user name """

testgres/tests/test_simple.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def teardown(self):
77
# clean_all()
88
pass
99

10+
@unittest.skip("demo")
1011
def test_start_stop(self):
11-
assert(1)
1212
node = get_new_node('test')
1313
node.init()
1414
node.start()
@@ -17,5 +17,23 @@ def test_start_stop(self):
1717
self.assertEqual(res[0][0], 1)
1818
node.stop()
1919

20+
def test_backup(self):
21+
node = get_new_node('test')
22+
replica = get_new_node('repl')
23+
24+
node.init(allows_streaming=True)
25+
node.start()
26+
node.psql('postgres', 'create table abc(a int, b int)')
27+
node.psql('postgres', 'insert into abc values (1, 2)')
28+
node.backup('my_backup')
29+
30+
replica.init_from_backup(node, 'my_backup')
31+
replica.start()
32+
res = replica.execute('postgres', 'select * from abc')
33+
self.assertEqual(len(res), 1)
34+
self.assertEqual(res[0], (1, 2))
35+
36+
node.stop()
37+
2038
if __name__ == '__main__':
2139
unittest.main()

0 commit comments

Comments
 (0)