-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs0330_shell_1_test
More file actions
368 lines (326 loc) · 11.3 KB
/
Copy pathcs0330_shell_1_test
File metadata and controls
368 lines (326 loc) · 11.3 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
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
#! /usr/bin/python2.7
from subprocess import PIPE
from subprocess import Popen
from glob import glob
from getopt import gnu_getopt
from getopt import GetoptError
import os
import fcntl
import select
import time
import signal
import shutil
import sys
import tempfile
from os import chdir
execsBin = "/course/cs0330/bin/33sh"
pt_harness = "cs0330_pt_harness"
progFile = "noprompt"
testInput = "input"
testOutput = "output"
testError = "error"
testPoints = "points"
testSetup = "setup"
rubric = "rubric.txt"
# cleans out a directory
def cleanDir(path):
for d in glob(path + "/*"):
if os.path.isdir(d):
cleanDir(d)
os.rmdir(d)
else:
os.remove(d)
# run bash setup script for a test in directory
def setupDir(path, tpath):
saveCWD = os.getcwd()
chdir(path)
#f = open(playground + "/" + testOutput, "w+")
try:
shutil.copyfile(tpath + "/" + testOutput, playground + "/" + testOutput)
except IOError as e:
print("Error copying expected output file:")
print(e)
sys.exit(1)
pid = os.fork()
if pid == 0:
os.execv(tpath + "/" + testSetup, [tpath + "/" + testSetup, playground + "/"])
sys.exit(1)
else:
os.waitpid(pid, 0)
chdir(saveCWD)
# path should be the full or relative path to this test case
def runTest(tpath, testname, spath, report_file):
tpath = tpath + testname
retOut = ""
retErr = ""
saveCWD = os.getcwd()
chdir(playground)
cleanDir(playground)
setupDir(playground, tpath)
# start shell
prog = ""
try:
if (pseudoterm):
prog = Popen([execsBin + "/" + pt_harness, spath],
stdin=PIPE, stdout=PIPE, stderr=PIPE)
else:
prog = Popen([spath], stdin=PIPE, stdout=PIPE, stderr=PIPE)
except OSError as e:
print("Error executing shell.")
print(e)
return {testOutput:retOut, testError:retErr}
try:
with open(tpath + "/" + testInput, "r") as tfile: # open infile
prev_line = None
for line in tfile: # run tests
try:
prog.stdin.write(line)
# switching the orders of the next three lines
# so that exit test works.
time.sleep(.2)
if line.strip() == "exit" and prog.poll() == 0:
break
#time.sleep(.2)
except IOError as e:
if prog.returncode == None:
print"\nThe shell program exited abnormally."
print "The offending command was: %s" % prev_line
print("Error communicating with shell: " + str(e))
return {testOutput:retOut, testError:retErr}
prev_line = line
except IOError as e:
print("Error opening test input from file " + tpath + "/input.")
print(e)
return {testOutput:retOut, testError:retErr}
ret = ["", "", ""]
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(6)
try:
ret = prog.communicate()
signal.alarm(0)
except Alarm:
if (report_file):
print("\tTimeout. Test " + tpath + " failed.")
report_file.write("\tTimeout. Test " + tpath + " failed.\n")
else:
print("\t\tTimeout. Test " + tpath + " failed.")
return {testOutput:"Timeout error", testError:"Timeout error"}
retOut, retErr = (ret[0].replace("\r", ""), ret[1].replace("\r", ""))
retOut = retOut.strip()
retErr = retErr.strip()
# get output and error expected values
try:
with open(playground + "/" + testOutput, "r") as ofile:
goodOut = ofile.read().strip()
except IOError as e:
print("Error opening expected output file " + tpath + "/output.")
print(e)
return {testOutput:retOut, testError:retErr}
try:
with open(tpath + "/" + testError, "r") as efile:
goodErr = efile.read().strip()
except IOError as e:
print("Error opening expected error file " + tpath + "/error.")
print(e)
return {testOutput:retOut, testError:retErr}
# make sure they all match
out_failure = False
err_failure = False
msg = "\tTest " + testname + " failed: "
if retOut != goodOut:
out_failure = True
msg += "stdout mismatch"
if retErr != goodErr:
err_failure = True
if out_failure:
msg += ", "
msg += "stderr mismatch"
if not out_failure and not err_failure:
return {}
else:
if display_errors or verbose:
print(msg)
to_print = ""
if out_failure:
to_print += "Test: " + testname + "\n"
to_print += "----------------------\n"
to_print += "---Expected stdout:---\n"
to_print += goodOut + "\n"
to_print += "----------------------\n\n"
to_print += "---Received stdout:---\n"
to_print += retOut + "\n"
to_print += "----------------------\n"
if err_failure:
if not out_failure:
to_print += "Test: " + testname + "\n"
to_print += "----------------------\n"
to_print += "---Expected stderr:---\n"
to_print += goodErr + "\n"
to_print += "----------------------\n\n"
to_print += "---Received stderr:---\n"
to_print += retErr + "\n"
to_print += "----------------------\n"
if out_failure or err_failure:
to_print += "\n\n"
if report:
report_file.write(to_print)
else:
print(to_print)
return {testOutput:retOut, testError:retErr}
def testStudent(spath, report_path):
os.chdir(playground)
results = {}
try:
report_file = ""
if (report):
report_file = open(report_path, "w")
tlist = os.listdir(testSuite + "/")
tlist.sort()
for test in tlist:
if verbose:
print("\tRunning test " + test)
results[test] = runTest(testSuite + "/", test, spath, report_file)
scores = {} # dictionary of scores
for test in tlist:
with open(testSuite + "/" + test + "/" + testPoints, "r") as f:
try:
scores[test] = int(f.read());
except IOError:
if verbose:
print("Test value undefined.")
scores[test] = 0
totScore = 0
totPossible = 0
if (report_file):
report_file.write("Report:\n")
else:
print("Report:")
sorted_results = results.items()
# sorted_results.sort(key=lambda x:x[0])
sorted_results.sort(key=lambda x: int(x[0].split('_')[0]))
for test, val in sorted_results:
totPossible += scores[test]
if not val:
if (report_file):
report_file.write(test + ": Passed (" + str(scores[test]) + "/" + str(scores[test]) + ")\n")
else:
print "\t" + test + ": Passed (" + str(scores[test]) + "/" + str(scores[test]) + ")"
totScore += scores[test]
else:
if (report_file):
report_file.write(test + ": Failed (0/" + str(scores[test]) + ")\n")
else:
print "\t" + test + ": Failed (0/" + str(scores[test]) + ")"
if (report_file):
report_file.write("--------------------\n")
report_file.write("Total: "+str(totScore)+"/"+str(totPossible) + "\n")
else:
print("--------------------")
print("Total: "+str(totScore)+"/"+str(totPossible))
# TODO: remove this line if not using rubric from Fall 2019
# print("Functionality Score [for TAs]: "+str(((float(totScore) / totPossible) * 60) // 1))
print "\n"
if (report):
report_file.close()
return totScore
except IOError as e:
print(e)
print("I/O error. Tests failed.")
return 0
except OSError as e:
print(e)
print("Error executing shell. Tests failed.")
return 0
def usage():
print "Usage:", os.path.basename(__file__), "-s <sh> {-t <test>, -u <suite>} [-r] [-e] [-v] [-h] [-p]"
def help():
print "Shell Autotester"
print "----------------"
print "\t[-t, --test] <dir>: use test located in directory <dir>"
print "\t[-u, --suite] <dir>: run all tests located in directory <dir>"
print "\t[-s, --shell] <sh>: test the indicated sh executable <sh>"
print "\t\tTo pass any tests, <sh> must print only program output."
print "\t[-r, --report] <file>: generate report in the file <file>"
print "\t[-v, --verbose]: print each test to stdout as it is run."
print "\t[-e, --errors]: display failing tests as they are run."
print "\t[-p, --pseudoterm]: run tests in the pseudoterminal harness"
print "\t[-h, --help]: display this message and exit."
#########################
# Starting Main Body... #
#########################
test = ""
testSuite = ""
playground = ""
executable = ""
display_errors = False
verbose = False
pseudoterm = False
report = ""
try:
opts, args = gnu_getopt(sys.argv[1:], "t:u:s:r:pevh",
["test=", "suite=", "shell=", "report=", "verbose", "errors", "pseudoterm", "help"])
except GetoptError as err:
print str(err)
usage()
sys.exit(2)
for opt, val in opts:
if opt in ("-t", "--test"):
test = val
elif opt in ("-u", "--suite"):
testSuite = val
elif opt in ("-s", "--shell"):
executable = val
elif opt in ("-r", "--report"):
report = val
elif opt in ("-e", "--errors"):
display_errors = True
elif opt in ("-v", "--verbose"):
verbose = True
elif opt in ("-p", "--pseudoterm"):
pseudoterm = True
elif opt in ("-h", "--help"):
help()
sys.exit(0)
else:
print "Impossible option has been assigned. Exiting."
sys.exit(1)
# Make sure all required options have been supplied
if not test and not testSuite:
print("Either test or test suite (-t or -u) required but not found.")
usage()
sys.exit(1)
elif test and testSuite:
print("Conflicting -t and -u options provided.")
usage()
sys.exit(1)
if not playground:
playground = tempfile.mkdtemp()
if not executable:
print("Required --shell option unspecified.")
usage()
sys.exit(1)
# make sure all paths are absolute, for safety's sake.
# we'll be changing our cwd frequently, and don't want
# to lose track of the tests or handin.
assert(executable);
executable = os.path.abspath(executable)
if report:
report = os.path.abspath(report)
if (test):
test = os.path.abspath(test)
result = runTest(os.path.dirname(test) + "/", os.path.basename(test),
executable, report)
if result == {}:
print("Test passed.")
else:
print("Test failed.")
else:
testSuite = os.path.abspath(testSuite)
playground = os.path.abspath(playground)
print("Testing " + executable)
testStudent(executable, report)
cleanDir(playground)