11import os
2+ import stat
23import unittest
3- from .helpers .ptrack_helpers import dir_files , ProbackupTest , ProbackupException
44import shutil
55
6+ from .helpers .ptrack_helpers import dir_files , ProbackupTest , ProbackupException
7+
68
79module_name = 'init'
810
11+ DIR_PERMISSION = 0o700
912
10- class InitTest ( ProbackupTest , unittest . TestCase ):
13+ CATALOG_DIRS = [ 'backups' , 'wal' ]
1114
15+ class InitTest (ProbackupTest , unittest .TestCase ):
1216 # @unittest.skip("skip")
1317 # @unittest.expectedFailure
1418 def test_success (self ):
@@ -19,8 +23,13 @@ def test_success(self):
1923 self .init_pb (backup_dir )
2024 self .assertEqual (
2125 dir_files (backup_dir ),
22- [ 'backups' , 'wal' ]
26+ CATALOG_DIRS
2327 )
28+
29+ for subdir in CATALOG_DIRS :
30+ dirname = os .path .join (backup_dir , subdir )
31+ self .assertEqual (DIR_PERMISSION , stat .S_IMODE (os .stat (dirname ).st_mode ))
32+
2433 self .add_instance (backup_dir , 'node' , node )
2534 self .assertIn (
2635 "INFO: Instance 'node' successfully deleted" ,
@@ -155,3 +164,78 @@ def test_add_instance_idempotence(self):
155164
156165 # Clean after yourself
157166 self .del_test_dir (module_name , fname )
167+
168+ def test_init_backup_catalog_no_access (self ):
169+ """ Test pg_probackup init -B backup_dir to a dir with no read access. """
170+ fname = self .id ().split ('.' )[3 ]
171+
172+ no_access_dir = os .path .join (self .tmp_path , module_name , fname ,
173+ 'noaccess' )
174+ backup_dir = os .path .join (no_access_dir , 'backup' )
175+ os .makedirs (no_access_dir )
176+ os .chmod (no_access_dir , stat .S_IREAD )
177+
178+ try :
179+ self .init_pb (backup_dir , cleanup = False )
180+ except ProbackupException as e :
181+ self .assertEqual (f'ERROR: cannot open backup catalog directory "{ backup_dir } ": Permission denied\n ' ,
182+ e .message )
183+ finally :
184+ self .del_test_dir (module_name , fname )
185+
186+ def test_init_backup_catalog_no_write (self ):
187+ """ Test pg_probackup init -B backup_dir to a dir with no write access. """
188+ fname = self .id ().split ('.' )[3 ]
189+
190+ no_access_dir = os .path .join (self .tmp_path , module_name , fname ,
191+ 'noaccess' )
192+ backup_dir = os .path .join (no_access_dir , 'backup' )
193+ os .makedirs (no_access_dir )
194+ os .chmod (no_access_dir , stat .S_IREAD | stat .S_IEXEC )
195+
196+ try :
197+ self .init_pb (backup_dir , cleanup = False )
198+ except ProbackupException as e :
199+ self .assertEqual (f'ERROR: Can not create backup catalog root directory: Cannot make dir "{ backup_dir } ": Permission denied\n ' ,
200+ e .message )
201+ finally :
202+ self .del_test_dir (module_name , fname )
203+
204+ def test_init_backup_catalog_no_create (self ):
205+ """ Test pg_probackup init -B backup_dir to a dir when backup dir exists but not writeable. """
206+ fname = self .id ().split ('.' )[3 ]
207+
208+ parent_dir = os .path .join (self .tmp_path , module_name , fname ,
209+ 'parent' )
210+ backup_dir = os .path .join (parent_dir , 'backup' )
211+ os .makedirs (backup_dir )
212+ os .chmod (backup_dir , stat .S_IREAD | stat .S_IEXEC )
213+
214+ try :
215+ self .init_pb (backup_dir , cleanup = False )
216+ except ProbackupException as e :
217+ backups_dir = os .path .join (backup_dir , 'backups' )
218+ self .assertEqual (f'ERROR: Can not create backup catalog data directory: Cannot make dir "{ backups_dir } ": Permission denied\n ' ,
219+ e .message )
220+ finally :
221+ self .del_test_dir (module_name , fname )
222+
223+ def test_init_backup_catalog_exists_not_empty (self ):
224+ """ Test pg_probackup init -B backup_dir which exists and not empty. """
225+ fname = self .id ().split ('.' )[3 ]
226+
227+ parent_dir = os .path .join (self .tmp_path , module_name , fname ,
228+ 'parent' )
229+ backup_dir = os .path .join (parent_dir , 'backup' )
230+ os .makedirs (backup_dir )
231+ with open (os .path .join (backup_dir , 'somefile.txt' ), 'w' ) as fout :
232+ fout .write ("42\n " )
233+
234+ try :
235+ self .init_pb (backup_dir , cleanup = False )
236+ self .fail ("This should have failed due to non empty catalog dir." )
237+ except ProbackupException as e :
238+ self .assertEqual ("ERROR: backup catalog already exist and it's not empty\n " ,
239+ e .message )
240+ finally :
241+ self .del_test_dir (module_name , fname )
0 commit comments