annotate src/capnproto-0.6.0/mega-test.py @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 #! /usr/bin/env python
cannam@62 2
cannam@62 3 # MEGA TEST
cannam@62 4 #
cannam@62 5 # usage: mega-test.py <config>
cannam@62 6 #
cannam@62 7 # This runs several tests in parallel and shows progress bars for each, based on a config file.
cannam@62 8 #
cannam@62 9 # <config> is a file containing a list of commands to run along with the expected number of lines
cannam@62 10 # they will output (to stdout and stderr combined), which is how the progress bar is calculated.
cannam@62 11 # The format of the file is simply one test per line, with the line containing the test name,
cannam@62 12 # the number of output lines expected, and the test command. Example:
cannam@62 13 #
cannam@62 14 # mytest 1523 ./my-test --foo bar
cannam@62 15 # another 862 ./another-test --baz
cannam@62 16 #
cannam@62 17 # Each command is interpreted by `sh -euc`, therefore it is acceptable to use environment
cannam@62 18 # variables and other shell syntax.
cannam@62 19 #
cannam@62 20 # After all tests complete, the config file will be rewritten to update the line counts to the
cannam@62 21 # actual number of lines seen for all passing tests (failing tests are not updated).
cannam@62 22
cannam@62 23 import sys
cannam@62 24 import re
cannam@62 25 import os
cannam@62 26 from errno import EAGAIN
cannam@62 27 from fcntl import fcntl, F_GETFL, F_SETFL
cannam@62 28 from select import poll, POLLIN, POLLHUP
cannam@62 29 from subprocess import Popen, PIPE, STDOUT
cannam@62 30
cannam@62 31 CONFIG_LINE = re.compile("^([^ ]+) +([0-9]+) +(.*)$")
cannam@62 32
cannam@62 33 if len(sys.argv) != 2:
cannam@62 34 sys.stderr.write("Wrong number of arguments.\n");
cannam@62 35 sys.exit(1)
cannam@62 36
cannam@62 37 if not os.access("/tmp/test-output", os.F_OK):
cannam@62 38 os.mkdir("/tmp/test-output")
cannam@62 39
cannam@62 40 config = open(sys.argv[1], 'r')
cannam@62 41
cannam@62 42 tests = []
cannam@62 43
cannam@62 44 class Test:
cannam@62 45 def __init__(self, name, command, lines):
cannam@62 46 self.name = name
cannam@62 47 self.command = command
cannam@62 48 self.lines = lines
cannam@62 49 self.count = 0
cannam@62 50 self.done = False
cannam@62 51
cannam@62 52 def start(self, poller):
cannam@62 53 self.proc = Popen(["sh", "-euc", test.command], stdin=dev_null, stdout=PIPE, stderr=STDOUT)
cannam@62 54 fd = self.proc.stdout.fileno()
cannam@62 55 flags = fcntl(fd, F_GETFL)
cannam@62 56 fcntl(fd, F_SETFL, flags | os.O_NONBLOCK)
cannam@62 57 poller.register(self.proc.stdout, POLLIN)
cannam@62 58 self.log = open("/tmp/test-output/" + self.name + ".log", "w")
cannam@62 59
cannam@62 60 def update(self):
cannam@62 61 try:
cannam@62 62 while True:
cannam@62 63 text = self.proc.stdout.read()
cannam@62 64 if text == "":
cannam@62 65 self.proc.wait()
cannam@62 66 self.done = True
cannam@62 67 self.log.close()
cannam@62 68 return True
cannam@62 69 self.count += text.count("\n")
cannam@62 70 self.log.write(text)
cannam@62 71 except IOError as e:
cannam@62 72 if e.errno == EAGAIN:
cannam@62 73 return False
cannam@62 74 raise
cannam@62 75
cannam@62 76 def print_bar(self):
cannam@62 77 percent = self.count * 100 / self.lines
cannam@62 78 status = "(%3d%%)" % percent
cannam@62 79
cannam@62 80 color_on = ""
cannam@62 81 color_off = ""
cannam@62 82
cannam@62 83 if self.done:
cannam@62 84 if self.proc.returncode == 0:
cannam@62 85 color_on = "\033[0;32m"
cannam@62 86 status = "PASS"
cannam@62 87 else:
cannam@62 88 color_on = "\033[0;31m"
cannam@62 89 status = "FAIL: /tmp/test-output/%s.log" % self.name
cannam@62 90 color_off = "\033[0m"
cannam@62 91
cannam@62 92 print "%s%-16s |%-25s| %6d/%6d %s%s " % (
cannam@62 93 color_on, self.name, '=' * min(percent / 4, 25), self.count, self.lines, status, color_off)
cannam@62 94
cannam@62 95 def passed(self):
cannam@62 96 return self.proc.returncode == 0
cannam@62 97
cannam@62 98 for line in config:
cannam@62 99 if len(line) > 0 and not line.startswith("#"):
cannam@62 100 match = CONFIG_LINE.match(line)
cannam@62 101 if not match:
cannam@62 102 sys.stderr.write("Invalid config syntax: %s\n" % line);
cannam@62 103 sys.exit(1)
cannam@62 104 test = Test(match.group(1), match.group(3), int(match.group(2)))
cannam@62 105 tests.append(test)
cannam@62 106
cannam@62 107 config.close()
cannam@62 108
cannam@62 109 dev_null = open("/dev/null", "rw")
cannam@62 110 poller = poll()
cannam@62 111 fd_map = {}
cannam@62 112
cannam@62 113 for test in tests:
cannam@62 114 test.start(poller)
cannam@62 115 fd_map[test.proc.stdout.fileno()] = test
cannam@62 116
cannam@62 117 active_count = len(tests)
cannam@62 118
cannam@62 119 def print_bars():
cannam@62 120 for test in tests:
cannam@62 121 test.print_bar()
cannam@62 122
cannam@62 123 print_bars()
cannam@62 124
cannam@62 125 while active_count > 0:
cannam@62 126 for (fd, event) in poller.poll():
cannam@62 127 if fd_map[fd].update():
cannam@62 128 active_count -= 1
cannam@62 129 poller.unregister(fd)
cannam@62 130 sys.stdout.write("\033[%dA\r" % len(tests))
cannam@62 131 print_bars()
cannam@62 132
cannam@62 133 new_config = open(sys.argv[1], "w")
cannam@62 134 for test in tests:
cannam@62 135 if test.passed():
cannam@62 136 new_config.write("%-16s %6d %s\n" % (test.name, test.count, test.command))
cannam@62 137 else:
cannam@62 138 new_config.write("%-16s %6d %s\n" % (test.name, test.lines, test.command))
cannam@62 139
cannam@62 140 for test in tests:
cannam@62 141 if not test.passed():
cannam@62 142 sys.exit(1)
cannam@62 143
cannam@62 144 sys.exit(0)