Repository navigation
Expand file tree
/
Copy pathbase_test.py
More file actions
223 lines (194 loc) · 8.54 KB
/
Copy pathbase_test.py
File metadata and controls
223 lines (194 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import argparse
import atexit
import json
import os
import re
import sys
from config import WORKDIR, NODE, PT_BASEDIR, BASEDIR, SERVER, PXC_LOWER_BASE, PXC_UPPER_BASE, SKIP_TEST_EXIT_CODE
from util import pxc_startup, ps_startup
from util.utility import *
workdir = WORKDIR
pt_basedir = PT_BASEDIR
server = SERVER
comp_name = 'component_keyring_file'
# Read argument
parser = argparse.ArgumentParser(prog='PXC test', usage='%(prog)s [options]')
parser.add_argument('-e', '--encryption-run', action='store_true',
help='This option will enable encryption options')
parser.add_argument('-d', '--debug', action='store_true',
help='This option will enable debug logging')
args, extra_args = parser.parse_known_args()
worker_id = 0
for extra_arg in extra_args:
if re.fullmatch(r'-[1-9][0-9]*', extra_arg):
if worker_id != 0:
parser.error('Only one worker option can be specified')
worker_id = int(extra_arg[1:])
else:
parser.error('unrecognized arguments: ' + extra_arg)
if worker_id > 0:
workdir = workdir + "/w" + str(worker_id)
tests_log_dir = workdir + "/log" + "/tests_log"
os.makedirs(tests_log_dir, exist_ok=True)
test_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
test_log = open(tests_log_dir + "/" + test_name + ".log", "a", buffering=1)
sys.stdout = test_log
sys.stderr = test_log
def _restore_test_logging():
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
test_log.close()
atexit.register(_restore_test_logging)
encryption = args.encryption_run
debug = 'NO'
if args.debug is True:
debug = 'YES'
utility_cmd = Utility(debug)
utility_cmd.check_python_version()
version = utility_cmd.version_check(BASEDIR)
lower_version = get_mysql_version(PXC_LOWER_BASE)
upper_version = get_mysql_version(PXC_UPPER_BASE)
low_version_num = utility_cmd.version_check(PXC_LOWER_BASE)
high_version_num = utility_cmd.version_check(PXC_UPPER_BASE)
db = "test"
class BaseTest:
def __init__(self, number_of_nodes: int = int(NODE),
wsrep_provider_options=None,
my_extra=None,
extra_config_file=None,
init_extra=None,
vers: Version = None,
encrypt: bool = False,
ssl: bool = False):
self.__number_of_nodes = number_of_nodes
self.__my_extra = my_extra
self.__wsrep_provider_options = wsrep_provider_options
self.__extra_config_file = extra_config_file
self.__init_extra = init_extra
self.__version = vers
self.encrypt = encrypt
self.ssl = ssl
self.pxc_nodes: list[DbConnection] = None
self.node1: DbConnection = None
self.node2: DbConnection = None
self.node3: DbConnection = None
self.ps_nodes: list[DbConnection] = None
self._shutdown_registered = False
if vers == Version.LOWER:
server_basedir = PXC_LOWER_BASE
elif vers == Version.HIGHER:
server_basedir = PXC_UPPER_BASE
else:
server_basedir = BASEDIR
if vers in (Version.LOWER, Version.HIGHER):
self.ensure_upgrade_basedirs_configured()
else:
self._ensure_basedir_configured("basedir", BASEDIR)
self.is_wsrep_cluster = is_wsrep_cluster_build(server_basedir)
@staticmethod
def _ensure_basedir_configured(label: str, basedir: str):
if not os.path.isfile(os.path.join(basedir, 'bin', 'mysqld')):
print(label + " (" + basedir + ") does not point to a valid mysqld binary; "
"check the corresponding path in config.ini")
exit(1)
@classmethod
def ensure_upgrade_basedirs_configured(cls):
""" Upgrade tests need both pxc_lower_base and pxc_upper_base
(config.ini) to point to a valid mysqld binary, regardless of
which one this test instance starts from - one is the
upgrade source, the other the target.
"""
cls._ensure_basedir_configured("pxc_lower_base", PXC_LOWER_BASE)
cls._ensure_basedir_configured("pxc_upper_base", PXC_UPPER_BASE)
def _register_shutdown_on_exit(self):
if not self._shutdown_registered:
atexit.register(self._shutdown_on_exit)
self._shutdown_registered = True
def _shutdown_on_exit(self):
if self.pxc_nodes:
self.shutdown_nodes(self.pxc_nodes)
if self.ps_nodes:
self.shutdown_nodes(self.ps_nodes)
def start_pxc(self, my_extra: str = None, custom_conf_settings: dict = None,
terminate_on_startup_failure: bool = True):
if my_extra is not None:
my_extra_options = my_extra
else:
my_extra_options = self.__my_extra
# Start PXC cluster
server_startup = pxc_startup.StartCluster(self.__number_of_nodes, debug, self.__version, worker_id,
is_wsrep_cluster=self.is_wsrep_cluster)
server_startup.sanity_check()
if encryption or self.encrypt:
server_startup.create_config('encryption', self.__wsrep_provider_options,
custom_conf_settings=custom_conf_settings)
elif self.ssl:
server_startup.create_config('ssl', self.__wsrep_provider_options,
custom_conf_settings=custom_conf_settings)
else:
server_startup.create_config('none', self.__wsrep_provider_options,
custom_conf_settings=custom_conf_settings)
server_startup.initialize_cluster(encryption=encryption or self.encrypt)
if self.__extra_config_file is not None:
server_startup.add_myextra_configuration(self.__extra_config_file)
self.pxc_nodes = server_startup.start_cluster(my_extra_options, terminate_on_startup_failure)
self._register_shutdown_on_exit()
if len(self.pxc_nodes) == self.__number_of_nodes:
self.node1 = self.pxc_nodes[0]
self.node2 = self.pxc_nodes[1]
self.node3 = self.pxc_nodes[2]
self.node1.test_connection_check(retries=10, retry_wait=20)
else:
print("Some problem while setting up cluster nodes. Not all nodes seems in healthy state")
print("Number of nodes: " + str(len(self.pxc_nodes)))
if terminate_on_startup_failure:
exit(1)
if debug == 'YES':
for node in self.pxc_nodes:
print("node is " + node.get_socket())
def start_ps(self, my_extra=None):
""" Start Percona Server. This method will
perform sanity checks for PS startup
"""
# Start PXC cluster for replication test
if my_extra is not None:
my_extra_options = my_extra
else:
my_extra_options = self.__my_extra
server_startup = ps_startup.StartPerconaServer(self.__number_of_nodes, debug, self.__version, worker_id)
server_startup.test_sanity_check()
if encryption or self.encrypt:
server_startup.create_config('encryption')
else:
server_startup.create_config()
server_startup.initialize_server(encryption=encryption or self.encrypt)
if self.__extra_config_file is not None:
server_startup.add_myextra_configuration(self.__extra_config_file)
self.ps_nodes = server_startup.start_server(my_extra_options)
if self.ps_nodes:
self._register_shutdown_on_exit()
def shutdown_nodes(self, nodes=None):
if nodes is None:
nodes = self.pxc_nodes
for node in nodes:
node.shutdown()
def set_extra_conf_file(self, conf_file):
self.__extra_config_file = conf_file
def set_wsrep_provider_options(self, options):
self.__wsrep_provider_options = options
def set_number_of_nodes(self, number_of_nodes: int):
self.__number_of_nodes = number_of_nodes
def get_number_of_nodes(self):
return self.__number_of_nodes
def skip_if_wsrep_cluster(self, reason: str = None):
""" Skip when this test's source build is a plain MySQL
wsrep/Galera Cluster build rather than PXC. Exits with
SKIP_TEST_EXIT_CODE so the test runner reports [Skipped]
instead of [Pass]/[Fail].
"""
if self.is_wsrep_cluster:
message = "Skipping test: source build is a MySQL wsrep/Galera Cluster build, not PXC"
if reason:
message += " - " + reason
print(message)
exit(SKIP_TEST_EXIT_CODE)