|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# pylint: disable=superfluous-parens |
| 4 | + |
| 5 | +"""Run a suite of PgDBF test cases.""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +from glob import glob |
| 9 | +from hashlib import md5 |
| 10 | +from json import load |
| 11 | +from logging import basicConfig, getLogger, DEBUG, INFO |
| 12 | +from os import chdir, getcwd |
| 13 | +from subprocess import Popen, PIPE, STDOUT |
| 14 | + |
| 15 | +LOGGER = getLogger('') |
| 16 | + |
| 17 | +class TestError(ValueError): |
| 18 | + """A test failed""" |
| 19 | + |
| 20 | + |
| 21 | +def check_head(expected): |
| 22 | + """Check that the start of the file is as expected""" |
| 23 | + |
| 24 | + LOGGER.debug('opened a header check') |
| 25 | + body = '' |
| 26 | + length = len(expected) |
| 27 | + while True: |
| 28 | + data = yield() |
| 29 | + if not data: |
| 30 | + raise ValueError({'error': 'short read', 'expected': length, 'actual': len(body)}) |
| 31 | + body += data |
| 32 | + if len(body) >= length: |
| 33 | + actual = body[:length] |
| 34 | + if expected != actual: |
| 35 | + raise TestError('unequal head', expected, actual) |
| 36 | + LOGGER.info('passed the header check') |
| 37 | + break |
| 38 | + |
| 39 | + while True: |
| 40 | + data = yield() |
| 41 | + if data is None: |
| 42 | + LOGGER.debug('closed the header check') |
| 43 | + return |
| 44 | + |
| 45 | + |
| 46 | +def check_length(expected): |
| 47 | + """Check that the file has the expected length""" |
| 48 | + |
| 49 | + LOGGER.debug('opened a length check') |
| 50 | + actual = 0 |
| 51 | + while True: |
| 52 | + data = yield() |
| 53 | + if not data: |
| 54 | + if expected != actual: |
| 55 | + raise TestError('incorrect length', expected, actual) |
| 56 | + LOGGER.info('passed the length check') |
| 57 | + LOGGER.debug('closed the length check') |
| 58 | + return |
| 59 | + actual += len(data) |
| 60 | + |
| 61 | + |
| 62 | +def check_md5(expected): |
| 63 | + """Check that the file has the expected MD5 hash""" |
| 64 | + |
| 65 | + LOGGER.debug('opened an md5 check') |
| 66 | + hasher = md5() |
| 67 | + while True: |
| 68 | + data = yield() |
| 69 | + if data is None: |
| 70 | + actual = hasher.hexdigest() |
| 71 | + if expected != actual: |
| 72 | + raise TestError('bad md5 hash', actual, expected) |
| 73 | + LOGGER.info('passed the md5 check') |
| 74 | + LOGGER.debug('closed the md5 check') |
| 75 | + return |
| 76 | + hasher.update(data) |
| 77 | + |
| 78 | + |
| 79 | +def check_tail(expected): |
| 80 | + """Check that the end of the file is as expected""" |
| 81 | + |
| 82 | + LOGGER.debug('opened a tail check') |
| 83 | + actual = '' |
| 84 | + length = len(expected) |
| 85 | + while True: |
| 86 | + data = yield() |
| 87 | + if data is None: |
| 88 | + if expected != actual: |
| 89 | + raise TestError('incorrect tail', actual, expected) |
| 90 | + LOGGER.info('passed the tail check') |
| 91 | + LOGGER.debug('closed the tail check') |
| 92 | + return |
| 93 | + actual = (actual + data)[-length:] |
| 94 | + |
| 95 | + |
| 96 | +def run_test(pgdbf_path, config): |
| 97 | + """Run a test case with the given pgdbf executable""" |
| 98 | + |
| 99 | + tests = [] |
| 100 | + for key, value in config.items(): |
| 101 | + try: |
| 102 | + test_func = { |
| 103 | + 'head': check_head, |
| 104 | + 'length': check_length, |
| 105 | + 'md5': check_md5, |
| 106 | + 'tail': check_tail, |
| 107 | + }[key] |
| 108 | + except KeyError: |
| 109 | + pass |
| 110 | + else: |
| 111 | + test = test_func(value) |
| 112 | + next(test) |
| 113 | + tests.append(test) |
| 114 | + |
| 115 | + if not tests: |
| 116 | + raise ValueError('No tests are configured') |
| 117 | + |
| 118 | + args = config['cmd_args'] |
| 119 | + if not isinstance(args, list): |
| 120 | + args = [args] |
| 121 | + command = Popen([pgdbf_path] + args, stdout=PIPE, stderr=STDOUT) |
| 122 | + while True: |
| 123 | + chunk = command.stdout.read(128 * 1024) |
| 124 | + if not chunk: |
| 125 | + break |
| 126 | + for test in tests: |
| 127 | + test.send(chunk) |
| 128 | + |
| 129 | + for test in tests: |
| 130 | + try: |
| 131 | + test.send(None) |
| 132 | + except StopIteration: |
| 133 | + pass |
| 134 | + else: |
| 135 | + raise ValueError('test {} did not close cleanly'.format(test)) |
| 136 | + |
| 137 | + |
| 138 | +def handle_command_line(): |
| 139 | + """Evaluate the command line arguments and run tests""" |
| 140 | + |
| 141 | + parser = argparse.ArgumentParser(description=__doc__) |
| 142 | + parser.add_argument('--pgdbf', '-p', help='Path to the pgdbf executable') |
| 143 | + parser.add_argument('--verbose', '-v', action='count', help='Increase debugging verbosity') |
| 144 | + args = parser.parse_args() |
| 145 | + if args.verbose >= 2: |
| 146 | + basicConfig(level=DEBUG) |
| 147 | + elif args.verbose == 1: |
| 148 | + basicConfig(level=INFO) |
| 149 | + else: |
| 150 | + basicConfig() |
| 151 | + |
| 152 | + orig_dir = getcwd() |
| 153 | + for test_dir in ('cases', 'privatecases'): |
| 154 | + chdir(test_dir) |
| 155 | + for case in glob('*.json'): |
| 156 | + print('Running {}/{}'.format(test_dir, case)) |
| 157 | + with open(case) as infile: |
| 158 | + run_test(args.pgdbf or 'pgdbf', load(infile)) |
| 159 | + chdir(orig_dir) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + handle_command_line() |
0 commit comments