1- import logging
1+ from __future__import annotations
2+
23import os
34import shutil
4- import unittest
5+ import pytest
6+
57from ......import testgres
68from ...pg_probackup2 .app import ProbackupApp
79from ...pg_probackup2 .init_helpers import Init ,init_params
810from ..storage .fs_backup import FSTestBackupDir
911
1012
11- class TestUtils :
12- @staticmethod
13- def get_module_and_function_name (test_id ):
14- try :
15- module_name = test_id .split ('.' )[- 2 ]
16- fname = test_id .split ('.' )[- 1 ]
17- except IndexError :
18- logging .warning (f"Couldn't get module name and function name from test_id: `{ test_id } `" )
19- module_name ,fname = test_id .split ('(' )[1 ].split ('.' )[1 ],test_id .split ('(' )[0 ]
20- return module_name ,fname
13+ class ProbackupTest :
14+ pg_node :testgres .PostgresNode
15+
16+ @pytest .fixture (autouse = True ,scope = "function" )
17+ def implicit_fixture (self ,request :pytest .FixtureRequest ):
18+ assert isinstance (request ,pytest .FixtureRequest )
19+ self .helper__setUp (request )
20+ yield
21+ self .helper__tearDown ()
22+
23+ def helper__setUp (self ,request :pytest .FixtureRequest ):
24+ assert isinstance (request ,pytest .FixtureRequest )
2125
26+ self .helper__setup_test_environment (request )
27+ self .helper__setup_test_paths ()
28+ self .helper__setup_backup_dir ()
29+ self .helper__setup_probackup ()
2230
23- class ProbackupTest (unittest .TestCase ):
24- def setUp (self ):
25- self .setup_test_environment ()
26- self .setup_test_paths ()
27- self .setup_backup_dir ()
28- self .setup_probackup ()
31+ def helper__setup_test_environment (self ,request :pytest .FixtureRequest ):
32+ assert isinstance (request ,pytest .FixtureRequest )
2933
30- def setup_test_environment (self ):
3134self .output = None
3235self .cmd = None
3336self .nodes_to_cleanup = []
34- self .module_name ,self .fname = TestUtils . get_module_and_function_name ( self . id ())
37+ self .module_name ,self .fname = request . node . cls . __name__ , request . node . name
3538self .test_env = Init ().test_env ()
3639
37- def setup_test_paths (self ):
40+ def helper__setup_test_paths (self ):
3841self .rel_path = os .path .join (self .module_name ,self .fname )
3942self .test_path = os .path .join (init_params .tmp_path ,self .rel_path )
4043os .makedirs (self .test_path ,exist_ok = True )
4144self .pb_log_path = os .path .join (self .test_path ,"pb_log" )
4245
43- def setup_backup_dir (self ):
44- self .backup_dir = self .build_backup_dir ('backup' )
46+ def helper__setup_backup_dir (self ):
47+ self .backup_dir = self .helper__build_backup_dir ('backup' )
4548self .backup_dir .cleanup ()
4649
47- def setup_probackup (self ):
50+ def helper__setup_probackup (self ):
4851self .pg_node = testgres .NodeApp (self .test_path ,self .nodes_to_cleanup )
4952self .pb = ProbackupApp (self ,self .pg_node ,self .pb_log_path ,self .test_env ,
5053auto_compress_alg = 'zlib' ,backup_dir = self .backup_dir )
5154
52- def tearDown (self ):
55+ def helper__tearDown (self ):
5356if os .path .exists (self .test_path ):
5457shutil .rmtree (self .test_path )
5558
56- def build_backup_dir (self ,backup = 'backup' ):
59+ def helper__build_backup_dir (self ,backup = 'backup' ):
5760return FSTestBackupDir (rel_path = self .rel_path ,backup = backup )
5861
59- class BasicTest (ProbackupTest ):
62+
63+ class TestBasic (ProbackupTest ):
6064def test_full_backup (self ):
6165# Setting up a simple test node
6266node = self .pg_node .make_simple ('node' ,pg_options = {"fsync" :"off" ,"synchronous_commit" :"off" })
@@ -75,8 +79,4 @@ def test_full_backup(self):
7579out = self .pb .validate ('node' ,backup_id )
7680
7781# Check if the backup is valid
78- self .assertIn (f"INFO: Backup{ backup_id } is valid" ,out )
79-
80-
81- if __name__ == "__main__" :
82- unittest .main ()
82+ assert f"INFO: Backup{ backup_id } is valid" in out