|
0 |
From 65eece0995146a2de68c408332b6d4ef16aaced5 Mon Sep 17 00:00:00 2001
|
|
1 |
From: Sumit Bose <sbose@redhat.com>
|
|
2 |
Date: Wed, 4 Jul 2018 16:41:16 +0200
|
|
3 |
Subject: tests: run tests with python3
|
|
4 |
|
|
5 |
To allow the test to run with python3 build/tap-driver and
|
|
6 |
build/tap-gtester are updated to the latest version provided by the
|
|
7 |
cockpit project https://github.com/cockpit-project/cockpit.
|
|
8 |
|
|
9 |
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1595813
|
|
10 |
---
|
|
11 |
build/tap-driver | 104 +++++++++++++++++++++++++++++++++++++++++++-----------
|
|
12 |
build/tap-gtester | 59 ++++++++++++++++++++++---------
|
|
13 |
2 files changed, 125 insertions(+), 38 deletions(-)
|
|
14 |
|
|
15 |
diff --git a/build/tap-driver b/build/tap-driver
|
|
16 |
index 42f57c8..241fd50 100755
|
|
17 |
--- a/build/tap-driver
|
|
18 |
+++ b/build/tap-driver
|
|
19 |
@@ -1,4 +1,5 @@
|
|
20 |
-#!/usr/bin/python
|
|
21 |
+#!/usr/bin/python3
|
|
22 |
+# This can also be run with Python 2.
|
|
23 |
|
|
24 |
# Copyright (C) 2013 Red Hat, Inc.
|
|
25 |
#
|
|
26 |
@@ -29,20 +30,58 @@
|
|
27 |
#
|
|
28 |
|
|
29 |
import argparse
|
|
30 |
+import fcntl
|
|
31 |
import os
|
|
32 |
import select
|
|
33 |
+import struct
|
|
34 |
import subprocess
|
|
35 |
import sys
|
|
36 |
+import termios
|
|
37 |
+import errno
|
|
38 |
+
|
|
39 |
+_PY3 = sys.version[0] >= '3'
|
|
40 |
+_str = _PY3 and str or unicode
|
|
41 |
+
|
|
42 |
+def out(data, stream=None, flush=False):
|
|
43 |
+ if not isinstance(data, bytes):
|
|
44 |
+ data = data.encode("UTF-8")
|
|
45 |
+ if not stream:
|
|
46 |
+ stream = _PY3 and sys.stdout.buffer or sys.stdout
|
|
47 |
+ while True:
|
|
48 |
+ try:
|
|
49 |
+ if data:
|
|
50 |
+ stream.write(data)
|
|
51 |
+ data = None
|
|
52 |
+ if flush:
|
|
53 |
+ stream.flush()
|
|
54 |
+ flush = False
|
|
55 |
+ break
|
|
56 |
+ except IOError as e:
|
|
57 |
+ if e.errno == errno.EAGAIN:
|
|
58 |
+ continue
|
|
59 |
+ raise
|
|
60 |
+
|
|
61 |
+def terminal_width():
|
|
62 |
+ try:
|
|
63 |
+ h, w, hp, wp = struct.unpack('HHHH',
|
|
64 |
+ fcntl.ioctl(1, termios.TIOCGWINSZ,
|
|
65 |
+ struct.pack('HHHH', 0, 0, 0, 0)))
|
|
66 |
+ return w
|
|
67 |
+ except IOError as e:
|
|
68 |
+ if e.errno != errno.ENOTTY:
|
|
69 |
+ sys.stderr.write("%i %s %s\n" % (e.errno, e.strerror, sys.exc_info()))
|
|
70 |
+ return sys.maxsize
|
|
71 |
|
|
72 |
class Driver:
|
|
73 |
def __init__(self, args):
|
|
74 |
self.argv = args.command
|
|
75 |
self.test_name = args.test_name
|
|
76 |
- self.log = open(args.log_file, "w")
|
|
77 |
- self.log.write("# %s\n" % " ".join(sys.argv))
|
|
78 |
+ self.log = open(args.log_file, "wb")
|
|
79 |
+ self.log.write(("# %s\n" % " ".join(sys.argv)).encode("UTF-8"))
|
|
80 |
self.trs = open(args.trs_file, "w")
|
|
81 |
self.color_tests = args.color_tests
|
|
82 |
self.expect_failure = args.expect_failure
|
|
83 |
+ self.width = terminal_width() - 9
|
|
84 |
|
|
85 |
def report(self, code, *args):
|
|
86 |
CODES = {
|
|
87 |
@@ -57,17 +96,18 @@ class Driver:
|
|
88 |
# Print out to console
|
|
89 |
if self.color_tests:
|
|
90 |
if code in CODES:
|
|
91 |
- sys.stdout.write(CODES[code])
|
|
92 |
- sys.stdout.write(code)
|
|
93 |
+ out(CODES[code])
|
|
94 |
+ out(code)
|
|
95 |
if self.color_tests:
|
|
96 |
- sys.stdout.write('\x1b[m')
|
|
97 |
- sys.stdout.write(": ")
|
|
98 |
- sys.stdout.write(self.test_name)
|
|
99 |
- sys.stdout.write(" ")
|
|
100 |
- for arg in args:
|
|
101 |
- sys.stdout.write(str(arg))
|
|
102 |
- sys.stdout.write("\n")
|
|
103 |
- sys.stdout.flush()
|
|
104 |
+ out('\x1b[m')
|
|
105 |
+ out(": ")
|
|
106 |
+ msg = "".join([ self.test_name + " " ] + list(map(_str, args)))
|
|
107 |
+ if code == "PASS" and len(msg) > self.width:
|
|
108 |
+ out(msg[:self.width])
|
|
109 |
+ out("...")
|
|
110 |
+ else:
|
|
111 |
+ out(msg)
|
|
112 |
+ out("\n", flush=True)
|
|
113 |
|
|
114 |
# Book keeping
|
|
115 |
if code in CODES:
|
|
116 |
@@ -100,12 +140,14 @@ class Driver:
|
|
117 |
def execute(self):
|
|
118 |
try:
|
|
119 |
proc = subprocess.Popen(self.argv, close_fds=True,
|
|
120 |
+ stdin=subprocess.PIPE,
|
|
121 |
stdout=subprocess.PIPE,
|
|
122 |
stderr=subprocess.PIPE)
|
|
123 |
- except OSError, ex:
|
|
124 |
+ except OSError as ex:
|
|
125 |
self.report_error("Couldn't run %s: %s" % (self.argv[0], str(ex)))
|
|
126 |
return
|
|
127 |
|
|
128 |
+ proc.stdin.close()
|
|
129 |
outf = proc.stdout.fileno()
|
|
130 |
errf = proc.stderr.fileno()
|
|
131 |
rset = [outf, errf]
|
|
132 |
@@ -113,18 +155,25 @@ class Driver:
|
|
133 |
ret = select.select(rset, [], [], 10)
|
|
134 |
if outf in ret[0]:
|
|
135 |
data = os.read(outf, 1024)
|
|
136 |
- if data == "":
|
|
137 |
+ if data == b"":
|
|
138 |
rset.remove(outf)
|
|
139 |
self.log.write(data)
|
|
140 |
self.process(data)
|
|
141 |
if errf in ret[0]:
|
|
142 |
data = os.read(errf, 1024)
|
|
143 |
- if data == "":
|
|
144 |
+ if data == b"":
|
|
145 |
rset.remove(errf)
|
|
146 |
self.log.write(data)
|
|
147 |
- sys.stderr.write(data)
|
|
148 |
+ stream = _PY3 and sys.stderr.buffer or sys.stderr
|
|
149 |
+ out(data, stream=stream, flush=True)
|
|
150 |
|
|
151 |
proc.wait()
|
|
152 |
+
|
|
153 |
+ # Make sure the test didn't change blocking output
|
|
154 |
+ assert fcntl.fcntl(0, fcntl.F_GETFL) & os.O_NONBLOCK == 0
|
|
155 |
+ assert fcntl.fcntl(1, fcntl.F_GETFL) & os.O_NONBLOCK == 0
|
|
156 |
+ assert fcntl.fcntl(2, fcntl.F_GETFL) & os.O_NONBLOCK == 0
|
|
157 |
+
|
|
158 |
return proc.returncode
|
|
159 |
|
|
160 |
|
|
161 |
@@ -137,6 +186,7 @@ class TapDriver(Driver):
|
|
162 |
self.late_plan = False
|
|
163 |
self.errored = False
|
|
164 |
self.bail_out = False
|
|
165 |
+ self.skip_all_reason = None
|
|
166 |
|
|
167 |
def report(self, code, num, *args):
|
|
168 |
if num:
|
|
169 |
@@ -170,13 +220,19 @@ class TapDriver(Driver):
|
|
170 |
else:
|
|
171 |
self.result_fail(num, description)
|
|
172 |
|
|
173 |
- def consume_test_plan(self, first, last):
|
|
174 |
+ def consume_test_plan(self, line):
|
|
175 |
# Only one test plan is supported
|
|
176 |
if self.test_plan:
|
|
177 |
self.report_error("Get a second TAP test plan")
|
|
178 |
return
|
|
179 |
|
|
180 |
+ if line.lower().startswith('1..0 # skip'):
|
|
181 |
+ self.skip_all_reason = line[5:].strip()
|
|
182 |
+ self.bail_out = True
|
|
183 |
+ return
|
|
184 |
+
|
|
185 |
try:
|
|
186 |
+ (first, unused, last) = line.partition("..")
|
|
187 |
first = int(first)
|
|
188 |
last = int(last)
|
|
189 |
except ValueError:
|
|
190 |
@@ -192,7 +248,7 @@ class TapDriver(Driver):
|
|
191 |
|
|
192 |
def process(self, output):
|
|
193 |
if output:
|
|
194 |
- self.output += output
|
|
195 |
+ self.output += output.decode("UTF-8")
|
|
196 |
elif self.output:
|
|
197 |
self.output += "\n"
|
|
198 |
(ready, unused, self.output) = self.output.rpartition("\n")
|
|
199 |
@@ -202,8 +258,7 @@ class TapDriver(Driver):
|
|
200 |
elif line.startswith("not ok "):
|
|
201 |
self.consume_test_line(False, line[7:])
|
|
202 |
elif line and line[0].isdigit() and ".." in line:
|
|
203 |
- (first, unused, last) = line.partition("..")
|
|
204 |
- self.consume_test_plan(first, last)
|
|
205 |
+ self.consume_test_plan(line)
|
|
206 |
elif line.lower().startswith("bail out!"):
|
|
207 |
self.consume_bail_out(line)
|
|
208 |
|
|
209 |
@@ -213,6 +268,13 @@ class TapDriver(Driver):
|
|
210 |
failed = False
|
|
211 |
skipped = True
|
|
212 |
|
|
213 |
+ if self.skip_all_reason is not None:
|
|
214 |
+ self.result_skip("skipping:", self.skip_all_reason)
|
|
215 |
+ self.trs.write(":global-test-result: SKIP\n")
|
|
216 |
+ self.trs.write(":test-global-result: SKIP\n")
|
|
217 |
+ self.trs.write(":recheck: no\n")
|
|
218 |
+ return 0
|
|
219 |
+
|
|
220 |
# Basic collation of results
|
|
221 |
for (num, code) in self.reported.items():
|
|
222 |
if code == "ERROR":
|
|
223 |
diff --git a/build/tap-gtester b/build/tap-gtester
|
|
224 |
index 7e667d4..bbda266 100755
|
|
225 |
--- a/build/tap-gtester
|
|
226 |
+++ b/build/tap-gtester
|
|
227 |
@@ -1,4 +1,5 @@
|
|
228 |
-#!/usr/bin/python
|
|
229 |
+#!/usr/bin/python3
|
|
230 |
+# This can also be run with Python 2.
|
|
231 |
|
|
232 |
# Copyright (C) 2014 Red Hat, Inc.
|
|
233 |
#
|
|
234 |
@@ -30,9 +31,19 @@
|
|
235 |
import argparse
|
|
236 |
import os
|
|
237 |
import select
|
|
238 |
+import signal
|
|
239 |
import subprocess
|
|
240 |
import sys
|
|
241 |
|
|
242 |
+# Yes, it's dumb, but strsignal is not exposed in python
|
|
243 |
+# In addition signal numbers varify heavily from arch to arch
|
|
244 |
+def strsignal(sig):
|
|
245 |
+ for name in dir(signal):
|
|
246 |
+ if name.startswith("SIG") and sig == getattr(signal, name):
|
|
247 |
+ return name
|
|
248 |
+ return str(sig)
|
|
249 |
+
|
|
250 |
+
|
|
251 |
class NullCompiler:
|
|
252 |
def __init__(self, command):
|
|
253 |
self.command = command
|
|
254 |
@@ -76,22 +87,22 @@ class GTestCompiler(NullCompiler):
|
|
255 |
elif cmd == "result":
|
|
256 |
if self.test_name:
|
|
257 |
if data == "OK":
|
|
258 |
- print "ok %d %s" % (self.test_num, self.test_name)
|
|
259 |
+ print("ok %d %s" % (self.test_num, self.test_name))
|
|
260 |
if data == "FAIL":
|
|
261 |
- print "not ok %d %s", (self.test_num, self.test_name)
|
|
262 |
+ print("not ok %d %s" % (self.test_num, self.test_name))
|
|
263 |
self.test_name = None
|
|
264 |
elif cmd == "skipping":
|
|
265 |
if "/subprocess" not in data:
|
|
266 |
- print "ok %d # skip -- %s" % (self.test_num, data)
|
|
267 |
+ print("ok %d # skip -- %s" % (self.test_num, data))
|
|
268 |
self.test_name = None
|
|
269 |
elif data:
|
|
270 |
- print "# %s: %s" % (cmd, data)
|
|
271 |
+ print("# %s: %s" % (cmd, data))
|
|
272 |
else:
|
|
273 |
- print "# %s" % cmd
|
|
274 |
+ print("# %s" % cmd)
|
|
275 |
elif line.startswith("(MSG: "):
|
|
276 |
- print "# %s" % line[6:-1]
|
|
277 |
+ print("# %s" % line[6:-1])
|
|
278 |
elif line:
|
|
279 |
- print "# %s" % line
|
|
280 |
+ print("# %s" % line)
|
|
281 |
sys.stdout.flush()
|
|
282 |
|
|
283 |
def run(self, proc, output=""):
|
|
284 |
@@ -106,22 +117,26 @@ class GTestCompiler(NullCompiler):
|
|
285 |
if line.startswith("/"):
|
|
286 |
self.test_remaining.append(line.strip())
|
|
287 |
if not self.test_remaining:
|
|
288 |
- print "Bail out! No tests found in GTest: %s" % self.command[0]
|
|
289 |
+ print("Bail out! No tests found in GTest: %s" % self.command[0])
|
|
290 |
return 0
|
|
291 |
|
|
292 |
- print "1..%d" % len(self.test_remaining)
|
|
293 |
+ print("1..%d" % len(self.test_remaining))
|
|
294 |
|
|
295 |
# First try to run all the tests in a batch
|
|
296 |
- proc = subprocess.Popen(self.command + ["--verbose" ], close_fds=True, stdout=subprocess.PIPE)
|
|
297 |
+ proc = subprocess.Popen(self.command + ["--verbose" ], close_fds=True,
|
|
298 |
+ stdout=subprocess.PIPE, universal_newlines=True)
|
|
299 |
result = self.process(proc)
|
|
300 |
if result == 0:
|
|
301 |
return 0
|
|
302 |
|
|
303 |
+ if result < 0:
|
|
304 |
+ sys.stderr.write("%s terminated with %s\n" % (self.command[0], strsignal(-result)))
|
|
305 |
+
|
|
306 |
# Now pick up any stragglers due to failures
|
|
307 |
while True:
|
|
308 |
# Assume that the last test failed
|
|
309 |
if self.test_name:
|
|
310 |
- print "not ok %d %s" % (self.test_num, self.test_name)
|
|
311 |
+ print("not ok %d %s" % (self.test_num, self.test_name))
|
|
312 |
self.test_name = None
|
|
313 |
|
|
314 |
# Run any tests which didn't get run
|
|
315 |
@@ -129,7 +144,8 @@ class GTestCompiler(NullCompiler):
|
|
316 |
break
|
|
317 |
|
|
318 |
proc = subprocess.Popen(self.command + ["--verbose", "-p", self.test_remaining[0]],
|
|
319 |
- close_fds=True, stdout=subprocess.PIPE)
|
|
320 |
+ close_fds=True, stdout=subprocess.PIPE,
|
|
321 |
+ universal_newlines=True)
|
|
322 |
result = self.process(proc)
|
|
323 |
|
|
324 |
# The various exit codes and signals we continue for
|
|
325 |
@@ -139,24 +155,32 @@ class GTestCompiler(NullCompiler):
|
|
326 |
return result
|
|
327 |
|
|
328 |
def main(argv):
|
|
329 |
- parser = argparse.ArgumentParser(description='Automake TAP compiler')
|
|
330 |
+ parser = argparse.ArgumentParser(description='Automake TAP compiler',
|
|
331 |
+ usage="tap-gtester [--format FORMAT] command ...")
|
|
332 |
parser.add_argument('--format', metavar='FORMAT', choices=[ "auto", "gtest", "tap" ],
|
|
333 |
default="auto", help='The input format to compile')
|
|
334 |
parser.add_argument('--verbose', action='store_true',
|
|
335 |
default=True, help='Verbose mode (ignored)')
|
|
336 |
- parser.add_argument('command', nargs='+', help="A test command to run")
|
|
337 |
+ parser.add_argument('command', nargs=argparse.REMAINDER, help="A test command to run")
|
|
338 |
args = parser.parse_args(argv[1:])
|
|
339 |
|
|
340 |
output = None
|
|
341 |
format = args.format
|
|
342 |
cmd = args.command
|
|
343 |
+ if not cmd:
|
|
344 |
+ sys.stderr.write("tap-gtester: specify a command to run\n")
|
|
345 |
+ return 2
|
|
346 |
+ if cmd[0] == '--':
|
|
347 |
+ cmd.pop(0)
|
|
348 |
+
|
|
349 |
proc = None
|
|
350 |
|
|
351 |
os.environ['HARNESS_ACTIVE'] = '1'
|
|
352 |
|
|
353 |
if format in ["auto", "gtest"]:
|
|
354 |
list_cmd = cmd + ["-l", "--verbose"]
|
|
355 |
- proc = subprocess.Popen(list_cmd, close_fds=True, stdout=subprocess.PIPE)
|
|
356 |
+ proc = subprocess.Popen(list_cmd, close_fds=True, stdout=subprocess.PIPE,
|
|
357 |
+ universal_newlines=True)
|
|
358 |
output = proc.stdout.readline()
|
|
359 |
# Smell whether we're dealing with GTest list output from first line
|
|
360 |
if "random seed" in output or "GTest" in output or output.startswith("/"):
|
|
361 |
@@ -164,7 +188,8 @@ def main(argv):
|
|
362 |
else:
|
|
363 |
format = "tap"
|
|
364 |
else:
|
|
365 |
- proc = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE)
|
|
366 |
+ proc = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE,
|
|
367 |
+ universal_newlines=True)
|
|
368 |
|
|
369 |
if format == "gtest":
|
|
370 |
compiler = GTestCompiler(cmd)
|
|
371 |
--
|
|
372 |
cgit v1.1
|
|
373 |
|