-
Notifications
You must be signed in to change notification settings - Fork 18
/
run-tests
executable file
·245 lines (223 loc) · 7.86 KB
/
run-tests
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
#! /usr/bin/env python3
#
# This file is part of the Green End SFTP Server.
# Copyright (C) 2007, 2011, 2016, 2018 Richard Kettlewell
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import os
import sys
import re
import string
from subprocess import Popen, PIPE, STDOUT
srcdir = os.path.abspath(os.getenv('srcdir'))
builddir = os.path.abspath('.')
client = os.path.abspath("sftpclient")
server = "gesftpserver"
def rmrf(path):
"""Remove path and everything below it
shutil.rmtree isn't suitable because it's not aggressive enough about
permissions."""
if os.path.lexists(path):
if (not os.path.islink(path)) and os.path.isdir(path):
os.chmod(path, 0o700)
for name in os.listdir(path):
rmrf(os.path.join(path, name))
os.rmdir(path)
else:
os.remove(path)
def fatal(msg):
"""Report an error and exist with nonzero status"""
sys.stderr.write("%s\n" % msg)
sys.exit(1)
if os.getuid() == 0:
fatal("These tests are not suitable for running as root.")
os.umask(0o22) # for consistent permissions
failed = 0
protocols = ['2', '3', '4', '5', '6', '7']
dir = 'tests'
debug = 'DEBUG' in os.environ
failfast = 'FAILFAST' in os.environ
threads = None # use default
reorder = True
args = sys.argv[1:]
while len(args) > 0 and args[0][0] == '-':
if args[0] == "--protocols":
protocols = args[1].split(',')
args = args[2:]
elif args[0] == "--server":
server = args[1]
args = args[2:]
elif args[0] == "--client":
client = args[1]
args = args[2:]
elif args[0] == "--directory":
dir = args[1]
args = args[2:]
elif args[0] == "--debug":
debug = True
args = args[1:]
elif args[0] == "--threads":
threads = int(args[1])
args = args[2:]
elif args[0] == "--no-reorder":
reorder = False
args = args[1:]
elif args[0] == "--fail-fast":
failfast = True
args = args[1:]
else:
fatal("unknown option '%s'" % args[0])
server = os.path.abspath(server)
client = os.path.abspath(client)
if len(args) > 0:
tests = args
else:
tests = os.listdir(os.path.join(srcdir, dir))
# Execute tests in a consistent order
tests.sort()
# Clean up
rmrf(os.path.join(builddir, ',testroot'))
# Colorize output (if possible)
def colored(text, color=None, on_color=None, attrs=None):
"""Uncolorized output"""
return text
if sys.stdout.isatty():
try:
# apt-get install python3-termcolor
import termcolor
colored = termcolor.colored
except:
pass
for test in tests:
for proto in protocols:
# Skip dotfiles, backup files, and tests that don't apply to the
# current protocol
if ('.' in test
or not proto in test
or '#' in test
or '~' in test):
continue
sys.stderr.write("Testing %s/%s protocol %s ... " % (dir, test, proto))
# Make a working directory for the test
root = os.path.join(builddir, ',testroot', '%s.%s' % (test, proto))
os.makedirs(root)
os.chdir(root)
# Make a config file if necessary
config = "/dev/null"
if (threads is not None) or not reorder:
config = os.path.join(builddir, ',testroot', 'gesftpserver.conf')
with open(config, "w") as f:
if threads is not None:
print(f"threads {threads}", file=f)
if reorder:
print("reorder true", file=f)
else:
print("reorder false", file=f)
# Run the client with the server as a child process
clientcmd = [client,
"--force-version", proto,
"-P", server,
"-b", os.path.join(srcdir, dir, test),
"--program-config", config,
'--echo',
'--fix-sigpipe', # stupid Python
'--no-stop-on-error']
if debug:
debug_path = "%s/%s-%s.debug" % (builddir, test, proto)
# Save server debug output
clientcmd += ["--program-debug-path", debug_path]
# Run the client
output = Popen(clientcmd,
stdout=PIPE,
stderr=STDOUT).communicate()[0].split(b'\n')
# Get the output, with newlines stripped
output = [str(s, "ASCII") for s in output]
if output[len(output)-1] == "":
output = output[:-1]
# Get the script, with newlines stripped
with open(os.path.join(srcdir, dir, '%s' % test), "r") as f:
script = [l[:-1] for l in f]
# Find the maximum line length in each
omax = max([len(l) for l in output])
smax = max([len(l) for l in script])
errors = 0
report = []
# Iterate over the lines, checking them and formatting the results
for lineno in range(0, max(len(output), len(script))):
if lineno >= len(output):
# Truncated output is bad
ok = False
sline = script[lineno]
oline = ""
elif lineno >= len(script):
# Excessive output is bad
ok = False
sline = ""
oline = output[lineno]
else:
sline = script[lineno]
oline = output[lineno]
if len(sline) > 0 and sline[0] == '#':
# Lines starting # are regexps matching expected output
pattern = sline[1:]
ok = re.match(pattern, oline)
else:
# Other lines are the commands, which we expect to be
# echoed back.
ok = (oline == sline)
if ok:
marker = " "
def f(t): return t
else:
# Mismatching lines are marked with a * and colored red (if
# possible)
marker = "*"
errors += 1
def f(t): return colored(t, "red")
report.append(f("%s %-*s %-*s\n" %
(marker, smax, sline, omax, oline)))
if errors > 0:
sys.stderr.write(colored("FAILED\n", "red"))
sys.stderr.write("Command:\n %s\n" % " ".join(clientcmd))
sys.stderr.write("Output discrepancies:\n")
for l in report:
sys.stderr.write(l)
if debug:
sys.stderr.write("Server debug:\n")
with open(debug_path, "r") as f:
for l in f.readlines():
sys.stderr.write(l)
failed += 1
if failfast:
break
else:
sys.stderr.write(colored("passed\n", "green"))
if failed > 0 and failfast:
break
if failed:
print(colored("%d tests failed" % failed, "red"))
sys.exit(1)
else:
# On success, delete the test root. It's preserved for inspection on
# failure.
os.chdir("/")
rmrf(os.path.join(builddir, ',testroot'))
print(colored("OK", "green"))
# Local Variables:
# mode:python
# indent-tabs-mode:nil
# py-indent-offset:4
# End: