forked from Jasonysli/tiflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-test.py
356 lines (305 loc) · 11.6 KB
/
run-test.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding:utf-8 -*-
# !/usr/bin/python2
import os
import sys
import time
import urllib2
CMD_PREFIX = '>> '
CMD_PREFIX_ALTER = '=> '
CMD_PREFIX_TIDB = 'mysql> '
CMD_PREFIX_FUNC = 'func> '
RETURN_PREFIX = '#RETURN'
SLEEP_PREFIX = 'SLEEP '
TODO_PREFIX = '#TODO'
COMMENT_PREFIX = '#'
UNFINISHED_1_PREFIX = '\t'
UNFINISHED_2_PREFIX = ' '
WORD_PH = '{#WORD}'
CURL_TIDB_STATUS_PREFIX = 'curl_tidb> '
verbose = False
class Executor:
def __init__(self, dbc):
self.dbc = dbc
def exe(self, cmd):
return os.popen((self.dbc + ' "' + cmd + '" 2>&1').strip()).readlines()
class ShellFuncExecutor:
def __init__(self, dbc):
self.dbc = dbc
def exe(self, cmd):
return os.popen((cmd + ' "' + self.dbc + '" 2>&1').strip()).readlines()
class CurlTiDBExecutor:
def __init__(self):
self.tidb_status_addr = '{}:{}'.format(
os.getenv('tidb_server', "127.0.0.1"),
os.getenv('tidb_status_port', 10080)
)
def exe(self, context):
context = [e for e in context.split(' ') if e]
# put uri data
# post uri data
# delete uri
# get uri
method = context[0].upper()
uri = "http://{}/{}".format(self.tidb_status_addr, context[1])
# print 'uri is {} {} {}'.format(method, uri, context[2:])
request = urllib2.Request(uri)
request.get_method = lambda: method
if request.get_method() == 'POST' or request.get_method() == 'PUT':
request.data = context[2]
response = urllib2.urlopen(request).read().strip()
return [response] if request.get_method() == 'GET' and response else None
def parse_line(line):
words = [w.strip() for w in line.split("│") if w.strip() != ""]
return "@".join(words)
def parse_table_parts(lines, fuzz):
parts = set()
if not fuzz:
curr = []
for line in lines:
if line.startswith('┌'):
if len(curr) != 0:
parts.add('\n'.join(curr))
curr = []
curr.append(parse_line(line))
if len(curr) != 0:
parts.add('\n'.join(curr))
else:
for line in lines:
if not line.startswith('┌') and not line.startswith('└'):
line = parse_line(line)
if line in parts:
line += '-extra'
parts.add(line)
return parts
def is_blank_char(c):
return c in [' ', '\n', '\t']
def is_brace_char(c):
return c in ['{', '[', '(', ')', ']', '}']
def is_break_char(c):
return (c in [',', ';']) or is_brace_char(c) or is_blank_char(c)
def match_ph_word(line):
i = 0
while is_blank_char(line[i]):
i += 1
found = False
while not is_break_char(line[i]):
i += 1
found = True
if not found:
return 0
return i
# TODO: Support more place holders, eg: {#NUMBER}
def compare_line(line, template):
while True:
i = template.find(WORD_PH)
if i < 0:
return line == template
else:
if line[:i] != template[:i]:
return False
j = match_ph_word(line[i:])
if j == 0:
return False
template = template[i + len(WORD_PH):]
line = line[i + j:]
class MySQLCompare:
@staticmethod
def parse_output_line(line):
words = [w.strip() for w in line.split("\t") if w.strip() != ""]
return "@".join(words)
@staticmethod
def parse_mysql_line(line):
words = [w.strip() for w in line.split("|") if w.strip() != ""]
return "@".join(words)
@staticmethod
def parse_mysql_outputs(outputs):
results = set()
for output_line in outputs:
parsed_line = MySQLCompare.parse_output_line(output_line)
while parsed_line in results:
parsed_line += '-extra'
results.add(parsed_line)
return results
@staticmethod
def parse_excepted_outputs(outputs):
results = set()
for output_line in outputs:
if not output_line.startswith('+'):
parsed_line = MySQLCompare.parse_mysql_line(output_line)
while parsed_line in results:
parsed_line += '-extra'
results.add(parsed_line)
return results
@staticmethod
def matched(outputs, matches):
if len(outputs) == 0 and len(matches) == 0:
return True
is_table_parts = len(matches) > 0 and matches[0].startswith('+')
if is_table_parts:
a = MySQLCompare.parse_mysql_outputs(outputs)
b = MySQLCompare.parse_excepted_outputs(matches)
return a == b
else:
if len(outputs) != len(matches):
return False
for i in range(0, len(outputs)):
if not compare_line(outputs[i], matches[i]):
return False
return True
def matched(outputs, matches, fuzz):
if len(outputs) == 0 and len(matches) == 0:
return True
is_table_parts = len(matches) > 0 and matches[0].startswith('┌')
if is_table_parts:
a = parse_table_parts(outputs, fuzz)
b = parse_table_parts(matches, fuzz)
return a == b
else:
if len(outputs) != len(matches):
return False
for i in range(0, len(outputs)):
if not compare_line(outputs[i], matches[i]):
return False
return True
class Matcher:
def __init__(self, executor, executor_tidb, executor_func, executor_curl_tidb, fuzz):
self.executor = executor
self.executor_tidb = executor_tidb
self.executor_func = executor_func
self.executor_curl_tidb = executor_curl_tidb
self.query_line_number = 0
self.fuzz = fuzz
self.query = None
self.outputs = None
self.matches = []
self.is_mysql = False
def on_line(self, line, line_number):
if line.startswith(SLEEP_PREFIX):
time.sleep(float(line[len(SLEEP_PREFIX):]))
elif line.startswith(CMD_PREFIX_TIDB):
if verbose: print 'running', line
if self.outputs != None and ((not self.is_mysql and not matched(self.outputs, self.matches, self.fuzz)) or (
self.is_mysql and not MySQLCompare.matched(self.outputs, self.matches))):
return False
self.query_line_number = line_number
self.is_mysql = True
self.query = line[len(CMD_PREFIX_TIDB):]
self.outputs = self.executor_tidb.exe(self.query)
self.outputs = map(lambda x: x.strip(), self.outputs)
self.outputs = filter(lambda x: len(x) != 0, self.outputs)
self.matches = []
elif line.startswith(CURL_TIDB_STATUS_PREFIX):
if verbose:
print 'running', line
if self.outputs != None and ((not self.is_mysql and not matched(self.outputs, self.matches, self.fuzz)) or (
self.is_mysql and not MySQLCompare.matched(self.outputs, self.matches))):
return False
self.query_line_number = line_number
self.is_mysql = True
self.query = line[len(CURL_TIDB_STATUS_PREFIX):]
self.outputs = self.executor_curl_tidb.exe(self.query)
self.matches = []
elif line.startswith(CMD_PREFIX) or line.startswith(CMD_PREFIX_ALTER):
if verbose: print 'running', line
if self.outputs != None and ((not self.is_mysql and not matched(self.outputs, self.matches, self.fuzz)) or (
self.is_mysql and not MySQLCompare.matched(self.outputs, self.matches))):
return False
self.query_line_number = line_number
self.is_mysql = False
self.query = line[len(CMD_PREFIX):]
self.outputs = self.executor.exe(self.query)
self.outputs = map(lambda x: x.strip(), self.outputs)
self.outputs = filter(lambda x: len(x) != 0, self.outputs)
self.matches = []
elif line.startswith(CMD_PREFIX_FUNC):
if verbose: print 'running', line
if self.outputs != None and ((not self.is_mysql and not matched(self.outputs, self.matches, self.fuzz)) or (
self.is_mysql and not MySQLCompare.matched(self.outputs, self.matches))):
return False
self.query_line_number = line_number
self.is_mysql = False
self.query = line[len(CMD_PREFIX_FUNC):]
self.executor_func.exe(self.query)
self.outputs = []
self.matches = []
else:
self.matches.append(line)
return True
def on_finish(self):
if self.outputs != None and ((not self.is_mysql and not matched(self.outputs, self.matches, self.fuzz)) or (
self.is_mysql and not MySQLCompare.matched(self.outputs, self.matches))):
return False
return True
def parse_exe_match(path, executor, executor_tidb, executor_func, executor_curl_tidb, fuzz):
todos = []
line_number = 0
line_number_cached = 0
with open(path) as file:
matcher = Matcher(executor, executor_tidb, executor_func, executor_curl_tidb, fuzz)
cached = None
for origin in file:
line_number += 1
line = origin.strip()
if line.startswith(RETURN_PREFIX):
break
if line.startswith(TODO_PREFIX):
todos.append(line[len(TODO_PREFIX):].strip())
continue
if line.startswith(COMMENT_PREFIX) or len(line) == 0:
continue
if origin.startswith(UNFINISHED_1_PREFIX) or origin.startswith(UNFINISHED_2_PREFIX):
if cached[-1] == ',':
cached += ' '
cached += line
continue
if cached != None and not matcher.on_line(cached, line_number_cached):
return False, matcher, todos
cached = line
line_number_cached = line_number
if (cached != None and not matcher.on_line(cached, line_number)) or not matcher.on_finish():
return False, matcher, todos
return True, matcher, todos
def run():
if len(sys.argv) not in (5, 6):
print 'usage: <bin> tiflash-client-cmd test-file-path fuzz-check tidb-client-cmd [verbose]'
sys.exit(1)
dbc = sys.argv[1]
path = sys.argv[2]
fuzz = (sys.argv[3] == 'true')
mysql_client = sys.argv[4]
global verbose
if len(sys.argv) == 6:
verbose = (sys.argv[5] == 'true')
if verbose: print 'parsing file: `{}`'.format(path)
matched, matcher, todos = parse_exe_match(path, Executor(dbc), Executor(mysql_client),
ShellFuncExecutor(mysql_client),
CurlTiDBExecutor(),
fuzz,
)
def display(lines):
if len(lines) == 0:
print ' ' * 4 + '<nothing>'
else:
for it in lines:
print ' ' * 4 + it
if not matched:
print ' File:', path
print ' Error line:', matcher.query_line_number
print ' Error:', matcher.query
print ' Result:'
display(matcher.outputs)
print ' Expected:'
display(matcher.matches)
sys.exit(1)
if len(todos) != 0:
print ' TODO:'
for it in todos:
print ' ' * 4 + it
def main():
try:
run()
except KeyboardInterrupt:
print 'KeyboardInterrupted'
sys.exit(1)
if __name__ == '__main__':
main()