annotate src/capnproto-0.6.0/mega-test.py @ 169:223a55898ab9 tip default

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