-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_ftest.py
executable file
·391 lines (338 loc) · 11.3 KB
/
json_ftest.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/python3
from subprocess import Popen, PIPE
from enum import Enum
from sys import argv
from junit_xml import TestSuite, TestCase
from os import path, remove, getcwd
from json import load
import time
class State(Enum):
"""
Enum to represent the state of a test
"""
OK = 0
KO = 1
CRASH = 2
class Error(Enum):
"""
Enum to represent the state of the program
"""
HELP = 0
VERBOSE = 1
DELETE = 2
NONE = 3
ERROR = 84
class Test:
"""
Class to represent a test
"""
def __init__(self, testName, binaryPath, arguments, commandLineInputs, expectedReturnCode, expectedStdoutOutput, expectedStderrOutput):
self.testName = testName
self.binaryPath = binaryPath
self.arguments = arguments
self.commandLineInputs = commandLineInputs
self.expectedReturnCode = expectedReturnCode
self.expectedStdoutOutput = expectedStdoutOutput
self.expectedStderrOutput = expectedStderrOutput
self.stdout = ""
self.stderr = ""
self.result = ""
self.returnCode = 0
self.state = State.OK
class Arguments:
"""
Class to represent the arguments of the program
"""
def __init__(self):
self.help = False
self.verbose = False
self.delete = False
self.error = False
self.exitCode = 0
self.tests = {}
self.errorString = ""
def setHelp(self):
self.help = True
self.exitCode = Error.HELP
def setVerbose(self):
self.verbose = True
def setDelete(self):
self.delete = True
def setError(self, errorString):
self.error = True
self.errorString = errorString
self.exitCode = Error.ERROR
def addTest(self, test):
self.tests.append(test)
# Array that define every Arguments class function to call for earch options
options = [
("--help", Arguments.setHelp),
("-h", Arguments.setHelp),
("--verbose", Arguments.setVerbose),
("-v", Arguments.setVerbose),
("--delete", Arguments.setDelete),
("-d", Arguments.setDelete),
]
# Array that define what a test should contain
testKeys = [
"testName",
"binaryPath",
"arguments",
"commandLineInputs",
"expectedReturnCode",
"expectedStdoutOutput",
"expectedStderrOutput"
]
def setResult(test):
"""
Sets the result of a test
The result contains the state of the test and the result of the test
"""
try:
with open(test.expectedStdoutOutput, "r") as file:
test.expectedStdoutOutput = file.read()
with open(test.expectedStderrOutput, "r") as file:
test.expectedStderrOutput = file.read()
except:
pass
if test.returnCode == 139:
test.state = State.CRASH
test.result = f"Test \"{test.testName}\" failed, the program crashed with signal {test.returnCode}"
elif test.stdout != test.expectedStdoutOutput:
test.state = State.KO
test.result = f"Test \"{test.testName}\" failed, expected output is:\n{test.expectedStdoutOutput}Actual output is:\n{test.stdout}"
elif test.stderr != test.expectedStderrOutput:
test.state = State.KO
test.result = f"Test \"{test.testName}\" failed, expected error output is:\
\n{test.expectedStderrOutput}Actual error output is:\n{test.stderr}"
else:
if test.returnCode != test.expectedReturnCode:
test.state = State.KO
test.result = f"Test \"{test.testName}\" failed, expected return code is: {test.expectedReturnCode}, Actual return code is: {test.returnCode}"
else:
test.state = State.OK
test.result = f"Test \"{test.testName}\" passed"
def openCommand(commandLine):
"""
Opens a command line input
"""
try:
return Popen(commandLine,
cwd=getcwd(),
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
universal_newlines=True)
except FileNotFoundError:
return None
def handleProcess(test, process, start):
"""
Handles the process of the test
"""
while process.poll() is None:
for clInput in test.commandLineInputs:
if clInput[len(clInput) - 1] != '\n':
clInput += '\n'
process.stdin.write(clInput)
newTime = time.time()
if newTime - start > 1 * 60:
process.kill()
test.state = State.CRASH
test.result = f"Test \"{test.testName}\" failed: The program took more than 5 minutes to execute"
return
process.stdin.flush()
while (child_output := process.stdout.readline()) != "":
newTime = time.time()
if newTime - start > 1 * 60:
process.kill()
test.state = State.CRASH
test.result = f"Test \"{test.testName}\" failed: The program took more than 5 minutes to execute"
return
test.stdout += child_output
while (child_error := process.stderr.readline()) != "":
newTime = time.time()
if newTime - start > 1 * 60:
process.kill()
test.state = State.CRASH
test.result = f"Test \"{test.testName}\" failed: The program took more than 5 minutes to execute"
return
test.stderr += child_error
def closeFileDescriptors(process):
"""
Closes the file descriptors of the process
"""
process.stdin.close()
process.stdout.close()
process.stderr.close()
def runTest(test):
"""
Runs a given command with given arguments and returns the result
The function will also print the result if --verbose or -v is on
! WARNING !
! You must implement a command that stops the command line input
! if you don't do this, the process will be stuck in the while loop
! since you can't CTRL+D or CTRL+C the process.
! WARNING !
"""
commandLine = [test.binaryPath]
if test.arguments:
commandLine.extend(test.arguments)
start = time.time()
process = openCommand(commandLine)
if process is None:
test.state = State.CRASH
test.result = f"Test \"{test.testName}\" failed: The binary path \"{test.binaryPath}\" is invalid"
return
handleProcess(test, process, start)
closeFileDescriptors(process)
test.returnCode = process.returncode
setResult(test)
def runTests(arguments):
"""
Runs every test in every test files
"""
for testArray in arguments.tests.items():
for test in testArray[1]:
runTest(test)
def generateFile(arguments):
"""
Generates a .xml file with the results of the tests.
The file will be named f_test.xml
The file will be generated in the same directory as the script
The file will be generated in JUnit format, so it can be read by Jenkins
The script do not handle the elapsed time of the tests
"""
if arguments.delete:
return
testSuite = []
for testArray in arguments.tests.items():
testCases = []
for test in testArray[1]:
testCases.append(TestCase(test.testName, classname="",
stdout=test.stdout, stderr=test.stderr,
elapsed_sec=0.0, status=test.state))
testSuite.append(TestSuite(testArray[0], testCases))
with open("json_ftest.xml", "w") as file:
TestSuite.to_file(file, testSuite, prettyprint=True)
def deleteFile(arguments):
"""
Deletes the .xml file if the --delete/-d option is on
"""
if arguments.delete:
if path.exists("json_ftest.xml"):
remove("json_ftest.xml")
def createTest(test):
"""
Creates a Test object from a dictionary
"""
return Test(
test["testName"],
test["binaryPath"],
test["arguments"],
test["commandLineInputs"],
test["expectedReturnCode"],
test["expectedStdoutOutput"],
test["expectedStderrOutput"]
)
def parseTest(arguments, filePath):
"""
Parses the test file
"""
try:
if (filePath[0] == '-'):
raise ValueError
with open(filePath, "r") as file:
arguments.tests[path.basename(filePath)] = []
data = load(file)
for test in data:
arguments.tests[path.basename(filePath)].append(createTest(test))
except FileNotFoundError:
arguments.setError(f"File {filePath} not found")
except ValueError:
arguments.setError(f"Invalid option {filePath}")
def parseArgs(arguments, argv):
"""
Parses the arguments of the program
"""
for i in range(1, len(argv)):
if arguments.error:
return
found = False
for option in options:
if argv[i] == option[0]:
option[1](arguments)
found = True
break
if not found:
parseTest(arguments, argv[i])
def printResults(arguments):
"""
Prints the results of the tests
"""
koTests, crashTests, okTests, testNb = 0, 0, 0, 0
for testArray in arguments.tests.values():
for test in testArray:
testNb += 1
if test.state == State.KO:
koTests += 1
if test.state == State.CRASH:
crashTests += 1
if test.state == State.OK:
okTests += 1
if test.state == State.OK and not arguments.verbose:
continue
print(test.result)
if koTests > 0 or crashTests > 0:
arguments.exitCode = Error.ERROR
print(f"[====] Synthesis: Tested: {testNb}"
+ f" | Passing: {okTests} | Failing: {koTests} | Crashing: {crashTests}")
return (koTests, crashTests)
def printUsage(exitCode):
"""
Prints the usage of the script
"""
print("Usage:\n\
./json_ftest.py [options] [test_files.json] ...\n\
\n\tOptions:\n\
\t--help, -h: Display this help\n\
\t--verbose, -v: Display the result of passed tests\n\
\t--delete, -d: Do not generate the .xml file and delete it if it exists\n\
\n\tTest files:\n\
\tTest files must be a .json files\n\
\tTest files must contain an array of tests\n\
\tEach test must contain the following keys:\n\
\t\ttestName: The name of the test\n\
\t\tbinaryPath: The path to the binary to test\n\
\t\targuments: An array of arguments to pass to the binary\n\
\t\tcommandLineInputs: An array of command line inputs\n\
\t\texpectedReturnCode: The expected return code of the program\n\
\t\texpectedStdoutOutput: The expected stdout output of the program\n\
\t\texpectedStderrOutput: The expected stderr output of the program")
exit(exitCode.value)
def printError(errorString, exitCode):
"""
Prints an error message
"""
print(f"Error: {errorString}")
exit(exitCode.value)
def main():
"""
Main function of the script
"""
arguments = Arguments()
parseArgs(arguments, argv)
if len(argv) == 1:
return
if arguments.help:
printUsage(arguments.exitCode)
if arguments.error:
printError(arguments.errorString, arguments.exitCode)
runTests(arguments)
(koTests, crashedTests) = printResults(arguments)
generateFile(arguments)
deleteFile(arguments)
if koTests > 0 or crashedTests > 0:
exit(Error.ERROR.value)
exit(0)
if __name__ == "__main__":
main()