Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed7b187

Browse files
Probackup plugin is updated
Probackup plugin tests - They are skipped if PGPROBACKUPBIN is not defined Global variable init_params is None when PGPROBACKUPBIN is not defined or version is not processed
1 parent f7f163d commit ed7b187

File tree

2 files changed

+91
-68
lines changed

2 files changed

+91
-68
lines changed
Lines changed: 78 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import logging
24
from functools import reduce
35
import getpass
@@ -37,21 +39,27 @@
3739

3840
class Init(object):
3941
def __init__(self):
42+
pass
43+
44+
@staticmethod
45+
def create() -> Init:
46+
SELF = __class__()
47+
4048
if '-v' in sys.argv or '--verbose' in sys.argv:
41-
self.verbose = True
49+
SELF.verbose = True
4250
else:
43-
self.verbose = False
44-
45-
self._pg_config = testgres.get_pg_config()
46-
self.is_enterprise = self._pg_config.get('PGPRO_EDITION', None) == 'enterprise'
47-
self.is_shardman = self._pg_config.get('PGPRO_EDITION', None) == 'shardman'
48-
self.is_pgpro = 'PGPRO_EDITION' in self._pg_config
49-
self.is_nls_enabled = 'enable-nls' in self._pg_config['CONFIGURE']
50-
self.is_lz4_enabled = '-llz4' in self._pg_config['LIBS']
51-
version = self._pg_config['VERSION'].rstrip('develalphabetapre')
51+
SELF.verbose = False
52+
53+
SELF._pg_config = testgres.get_pg_config()
54+
SELF.is_enterprise = SELF._pg_config.get('PGPRO_EDITION', None) == 'enterprise'
55+
SELF.is_shardman = SELF._pg_config.get('PGPRO_EDITION', None) == 'shardman'
56+
SELF.is_pgpro = 'PGPRO_EDITION' in SELF._pg_config
57+
SELF.is_nls_enabled = 'enable-nls' in SELF._pg_config['CONFIGURE']
58+
SELF.is_lz4_enabled = '-llz4' in SELF._pg_config['LIBS']
59+
version = SELF._pg_config['VERSION'].rstrip('develalphabetapre')
5260
parts = [*version.split(' ')[1].split('.'), '0', '0'][:3]
5361
parts[0] = re.match(r'\d+', parts[0]).group()
54-
self.pg_config_version = reduce(lambda v, x: v * 100 + int(x), parts, 0)
62+
SELF.pg_config_version = reduce(lambda v, x: v * 100 + int(x), parts, 0)
5563

5664
os.environ['LANGUAGE'] = 'en' # set default locale language to en. All messages will use this locale
5765
test_env = os.environ.copy()
@@ -75,31 +83,31 @@ def __init__(self):
7583

7684
test_env['LC_MESSAGES'] = 'C'
7785
test_env['LC_TIME'] = 'C'
78-
self._test_env = test_env
86+
SELF._test_env = test_env
7987

8088
# Get the directory from which the script was executed
81-
self.source_path = os.getcwd()
89+
SELF.source_path = os.getcwd()
8290
tmp_path = test_env.get('PGPROBACKUP_TMP_DIR')
8391
if tmp_path and os.path.isabs(tmp_path):
84-
self.tmp_path = tmp_path
92+
SELF.tmp_path = tmp_path
8593
else:
86-
self.tmp_path = os.path.abspath(
87-
os.path.join(self.source_path, tmp_path or os.path.join('tests', 'tmp_dirs'))
94+
SELF.tmp_path = os.path.abspath(
95+
os.path.join(SELF.source_path, tmp_path or os.path.join('tests', 'tmp_dirs'))
8896
)
8997

90-
os.makedirs(self.tmp_path, exist_ok=True)
98+
os.makedirs(SELF.tmp_path, exist_ok=True)
9199

92-
self.username = getpass.getuser()
100+
SELF.username = getpass.getuser()
93101

94-
self.probackup_path = None
102+
SELF.probackup_path = None
95103
if 'PGPROBACKUPBIN' in test_env:
96104
if shutil.which(test_env["PGPROBACKUPBIN"]):
97-
self.probackup_path = test_env["PGPROBACKUPBIN"]
105+
SELF.probackup_path = test_env["PGPROBACKUPBIN"]
98106
else:
99-
if self.verbose:
107+
if SELF.verbose:
100108
print('PGPROBACKUPBIN is not an executable file')
101109

102-
if not self.probackup_path:
110+
if not SELF.probackup_path:
103111
probackup_path_tmp = os.path.join(
104112
testgres.get_pg_config()['BINDIR'], 'pg_probackup')
105113

@@ -108,116 +116,118 @@ def __init__(self):
108116
logging.warning('{0} is not an executable file'.format(
109117
probackup_path_tmp))
110118
else:
111-
self.probackup_path = probackup_path_tmp
119+
SELF.probackup_path = probackup_path_tmp
112120

113-
if not self.probackup_path:
114-
probackup_path_tmp = self.source_path
121+
if not SELF.probackup_path:
122+
probackup_path_tmp = SELF.source_path
115123

116124
if os.path.isfile(probackup_path_tmp):
117125
if not os.access(probackup_path_tmp, os.X_OK):
118126
logging.warning('{0} is not an executable file'.format(
119127
probackup_path_tmp))
120128
else:
121-
self.probackup_path = probackup_path_tmp
129+
SELF.probackup_path = probackup_path_tmp
122130

123-
if not self.probackup_path:
131+
if not SELF.probackup_path:
124132
logging.error('pg_probackup binary is not found')
125-
exit(1)
133+
return None
126134

127135
if os.name == 'posix':
128-
self.EXTERNAL_DIRECTORY_DELIMITER = ':'
136+
SELF.EXTERNAL_DIRECTORY_DELIMITER = ':'
129137
os.environ['PATH'] = os.path.dirname(
130-
self.probackup_path) + ':' + os.environ['PATH']
138+
SELF.probackup_path) + ':' + os.environ['PATH']
131139

132140
elif os.name == 'nt':
133-
self.EXTERNAL_DIRECTORY_DELIMITER = ';'
141+
SELF.EXTERNAL_DIRECTORY_DELIMITER = ';'
134142
os.environ['PATH'] = os.path.dirname(
135-
self.probackup_path) + ';' + os.environ['PATH']
143+
SELF.probackup_path) + ';' + os.environ['PATH']
136144

137-
self.probackup_old_path = None
145+
SELF.probackup_old_path = None
138146
if 'PGPROBACKUPBIN_OLD' in test_env:
139147
if (os.path.isfile(test_env['PGPROBACKUPBIN_OLD']) and os.access(test_env['PGPROBACKUPBIN_OLD'], os.X_OK)):
140-
self.probackup_old_path = test_env['PGPROBACKUPBIN_OLD']
148+
SELF.probackup_old_path = test_env['PGPROBACKUPBIN_OLD']
141149
else:
142-
if self.verbose:
150+
if SELF.verbose:
143151
print('PGPROBACKUPBIN_OLD is not an executable file')
144152

145-
self.probackup_version = None
146-
self.old_probackup_version = None
153+
SELF.probackup_version = None
154+
SELF.old_probackup_version = None
147155

148156
probackup_version_output = subprocess.check_output(
149-
[self.probackup_path, "--version"],
157+
[SELF.probackup_path, "--version"],
150158
stderr=subprocess.STDOUT,
151159
).decode('utf-8')
152160
match = re.search(r"\d+\.\d+\.\d+",
153161
probackup_version_output)
154-
self.probackup_version = match.group(0) if match else None
162+
SELF.probackup_version = match.group(0) if match else None
155163
match = re.search(r"\(compressions: ([^)]*)\)", probackup_version_output)
156164
compressions = match.group(1) if match else None
157165
if compressions:
158-
self.probackup_compressions = {s.strip() for s in compressions.split(',')}
166+
SELF.probackup_compressions = {s.strip() for s in compressions.split(',')}
159167
else:
160-
self.probackup_compressions = []
168+
SELF.probackup_compressions = []
161169

162-
if self.probackup_old_path:
170+
if SELF.probackup_old_path:
163171
old_probackup_version_output = subprocess.check_output(
164-
[self.probackup_old_path, "--version"],
172+
[SELF.probackup_old_path, "--version"],
165173
stderr=subprocess.STDOUT,
166174
).decode('utf-8')
167175
match = re.search(r"\d+\.\d+\.\d+",
168176
old_probackup_version_output)
169-
self.old_probackup_version = match.group(0) if match else None
177+
SELF.old_probackup_version = match.group(0) if match else None
170178

171-
self.remote = test_env.get('PGPROBACKUP_SSH_REMOTE', None) == 'ON'
172-
self.ptrack = test_env.get('PG_PROBACKUP_PTRACK', None) == 'ON' and self.pg_config_version >= 110000
173-
self.wal_tree_enabled = test_env.get('PG_PROBACKUP_WAL_TREE_ENABLED', None) == 'ON'
179+
SELF.remote = test_env.get('PGPROBACKUP_SSH_REMOTE', None) == 'ON'
180+
SELF.ptrack = test_env.get('PG_PROBACKUP_PTRACK', None) == 'ON' and SELF.pg_config_version >= 110000
181+
SELF.wal_tree_enabled = test_env.get('PG_PROBACKUP_WAL_TREE_ENABLED', None) == 'ON'
174182

175-
self.bckp_source = test_env.get('PG_PROBACKUP_SOURCE', 'pro').lower()
176-
if self.bckp_source not in ('base', 'direct', 'pro'):
183+
SELF.bckp_source = test_env.get('PG_PROBACKUP_SOURCE', 'pro').lower()
184+
if SELF.bckp_source not in ('base', 'direct', 'pro'):
177185
raise Exception("Wrong PG_PROBACKUP_SOURCE value. Available options: base|direct|pro")
178186

179-
self.paranoia = test_env.get('PG_PROBACKUP_PARANOIA', None) == 'ON'
187+
SELF.paranoia = test_env.get('PG_PROBACKUP_PARANOIA', None) == 'ON'
180188
env_compress = test_env.get('ARCHIVE_COMPRESSION', None)
181189
if env_compress:
182190
env_compress = env_compress.lower()
183191
if env_compress in ('on', 'zlib'):
184-
self.compress_suffix = '.gz'
185-
self.archive_compress = 'zlib'
192+
SELF.compress_suffix = '.gz'
193+
SELF.archive_compress = 'zlib'
186194
elif env_compress == 'lz4':
187195
if not HAVE_LZ4:
188196
raise LZ4_error
189-
if 'lz4' not in self.probackup_compressions:
197+
if 'lz4' not in SELF.probackup_compressions:
190198
raise Exception("pg_probackup is not compiled with lz4 support")
191-
self.compress_suffix = '.lz4'
192-
self.archive_compress = 'lz4'
199+
SELF.compress_suffix = '.lz4'
200+
SELF.archive_compress = 'lz4'
193201
elif env_compress == 'zstd':
194202
if not HAVE_ZSTD:
195203
raise ZSTD_error
196-
if 'zstd' not in self.probackup_compressions:
204+
if 'zstd' not in SELF.probackup_compressions:
197205
raise Exception("pg_probackup is not compiled with zstd support")
198-
self.compress_suffix = '.zst'
199-
self.archive_compress = 'zstd'
206+
SELF.compress_suffix = '.zst'
207+
SELF.archive_compress = 'zstd'
200208
else:
201-
self.compress_suffix = ''
202-
self.archive_compress = False
209+
SELF.compress_suffix = ''
210+
SELF.archive_compress = False
203211

204212
cfs_compress = test_env.get('PG_PROBACKUP_CFS_COMPRESS', None)
205213
if cfs_compress:
206-
self.cfs_compress = cfs_compress.lower()
214+
SELF.cfs_compress = cfs_compress.lower()
207215
else:
208-
self.cfs_compress = self.archive_compress
216+
SELF.cfs_compress = SELF.archive_compress
209217

210218
os.environ["PGAPPNAME"] = "pg_probackup"
211-
self.delete_logs = delete_logs
219+
SELF.delete_logs = delete_logs
212220

213-
if self.probackup_version.split('.')[0].isdigit():
214-
self.major_version = int(self.probackup_version.split('.')[0])
221+
if SELF.probackup_version.split('.')[0].isdigit():
222+
SELF.major_version = int(SELF.probackup_version.split('.')[0])
215223
else:
216-
logging.error('Can\'t process pg_probackup version \"{}\": the major version is expected to be a number'.format(self.probackup_version))
217-
sys.exit(1)
224+
logging.error('Can\'t process pg_probackup version \"{}\": the major version is expected to be a number'.format(SELF.probackup_version))
225+
return None
226+
227+
return SELF
218228

219229
def test_env(self):
220230
return self._test_env.copy()
221231

222232

223-
init_params = Init()
233+
init_params = Init.create()

testgres/plugins/pg_probackup2/pg_probackup2/tests/test_basic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
class ProbackupTest:
1414
pg_node: testgres.PostgresNode
1515

16+
@staticmethod
17+
def probackup_is_available() -> bool:
18+
p = os.environ.get("PGPROBACKUPBIN")
19+
20+
if p is None:
21+
return False
22+
23+
if not os.path.exists(p):
24+
return False
25+
26+
return True
27+
1628
@pytest.fixture(autouse=True, scope="function")
1729
def implicit_fixture(self, request: pytest.FixtureRequest):
1830
assert isinstance(request, pytest.FixtureRequest)
@@ -60,6 +72,7 @@ def helper__build_backup_dir(self, backup='backup'):
6072
return FSTestBackupDir(rel_path=self.rel_path, backup=backup)
6173

6274

75+
@pytest.mark.skipif(not ProbackupTest.probackup_is_available(), reason="Check that PGPROBACKUPBIN is defined and is valid.")
6376
class TestBasic(ProbackupTest):
6477
def test_full_backup(self):
6578
# Setting up a simple test node

0 commit comments

Comments
 (0)