|
| 1 | +#!/usr/bin/env python |
| 2 | +import time |
| 3 | +import random |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import signal |
| 7 | +import sys |
| 8 | +import os |
| 9 | + |
| 10 | +multiplier = float(os.environ.get('SLOWNESS', 1)) |
| 11 | + |
| 12 | +def run(cmd, **kwargs): |
| 13 | + p = subprocess.Popen(cmd.split(), |
| 14 | + stdout=subprocess.PIPE, |
| 15 | + stderr=subprocess.PIPE, |
| 16 | + **kwargs) |
| 17 | + p.wait() |
| 18 | + out = p.stdout.read() |
| 19 | + err = p.stderr.read() |
| 20 | + |
| 21 | + time.sleep(0.2 * multiplier) |
| 22 | + return p.returncode, out + '\n' + err |
| 23 | + |
| 24 | +def spawn(cmd, **kwargs): |
| 25 | + p = subprocess.Popen(cmd.split(), |
| 26 | + stdout=subprocess.PIPE, |
| 27 | + stderr=subprocess.PIPE, |
| 28 | + **kwargs) |
| 29 | + time.sleep(0.5 * multiplier) |
| 30 | + return p |
| 31 | + |
| 32 | +def wait(p, match): |
| 33 | + os.kill(p.pid, signal.SIGINT) |
| 34 | + p.wait() |
| 35 | + out = p.stdout.read() |
| 36 | + err = p.stderr.read() |
| 37 | + return bool(re.search(match, out)), out + '\n' + err |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +def gen(prog, arg="", **kwargs): |
| 42 | + Prog = ''.join([w.capitalize() for w in prog.split('_')]) |
| 43 | + ctx = { |
| 44 | + 'prog': prog, |
| 45 | + 'Prog': Prog, |
| 46 | + 'arg': arg, |
| 47 | + 'java': kwargs.get('java', Prog), |
| 48 | + 'dotnet': kwargs.get('dotnet', Prog), |
| 49 | + 'ruby': kwargs.get('ruby', os.environ.get('RUBY', 'ruby1.9.1')), |
| 50 | + } |
| 51 | + return [ |
| 52 | + ('python', './venv/bin/python %(prog)s.py %(arg)s' % ctx), |
| 53 | + ('perl', 'perl %(prog)s.pl %(arg)s' % ctx), |
| 54 | + ('erlang', './%(prog)s.erl %(arg)s' % ctx), |
| 55 | + ('java', 'java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:' |
| 56 | + 'rabbitmq-client.jar %(java)s %(arg)s' % ctx), |
| 57 | + ('dotnet', 'env MONO_PATH=lib/bin mono %(dotnet)s.exe %(arg)s' % ctx), |
| 58 | + ('ruby', 'env RUBYOPT=-rubygems GEM_HOME=gems/gems RUBYLIB=gems/lib ' |
| 59 | + '%(ruby)s %(prog)s.rb %(arg)s' % ctx), |
| 60 | + ('ruby-amqp', 'env RUBYOPT=-rubygems GEM_HOME=gems/gems RUBYLIB=gems/lib ' |
| 61 | + '%(ruby)s %(prog)s.rb %(arg)s' % ctx) |
| 62 | + ] |
| 63 | + |
| 64 | +def skip(cwd_cmd, to_skip): |
| 65 | + return [(cwd,cmd) for cwd, cmd in cwd_cmd if cwd not in to_skip] |
| 66 | + |
| 67 | +tests = { |
| 68 | + 'tut1': (gen('send'), gen('receive', java='Recv'), 'Hello World!'), |
| 69 | + 'tut2': (gen('new_task', arg='%(arg)s'), gen('worker'), '%(arg)s'), |
| 70 | + 'tut3': (gen('emit_log', arg='%(arg)s'), gen('receive_logs'), '%(arg)s'), |
| 71 | + 'tut4': (skip(gen('emit_log_direct', arg='%(arg)s %(arg2)s'), |
| 72 | + ['php', 'ruby']), |
| 73 | + skip(gen('receive_logs_direct', arg='%(arg)s'), |
| 74 | + ['php', 'ruby']), |
| 75 | + '%(arg2)s'), |
| 76 | + 'tut5': (skip(gen('emit_log_topic', arg='%(arg)s.foo %(arg2)s'), |
| 77 | + ['php', 'ruby']), |
| 78 | + skip(gen('receive_logs_topic', arg='%(arg)s.*'), |
| 79 | + ['php', 'ruby']), |
| 80 | + '%(arg2)s'), |
| 81 | + 'tut6': (skip(gen('rpc_client', java='RPCClient', dotnet='RPCClient'), |
| 82 | + ['erlang', 'ruby']), |
| 83 | + skip(gen('rpc_server', java='RPCServer', dotnet='RPCServer'), |
| 84 | + ['erlang', 'ruby']), |
| 85 | + 'fib[(]30[)]'), |
| 86 | + } |
| 87 | + |
| 88 | +def tests_to_run(): |
| 89 | + if os.environ.get('TUTORIALS'): |
| 90 | + return sorted(str.split(os.environ.get('TUTORIALS'), ",")) |
| 91 | + else: |
| 92 | + return sorted(tests.keys()) |
| 93 | + |
| 94 | +errors = 0 |
| 95 | +ts = tests_to_run() |
| 96 | + |
| 97 | +print " [.] Running tests with SLOWNESS=%r" % (multiplier,) |
| 98 | +print " [.] Will test %s" % (ts) |
| 99 | +for test in ts: |
| 100 | + (send_progs, recv_progs, output_mask) = tests[test] |
| 101 | + for scwd, send_cmd in send_progs: |
| 102 | + for rcwd, recv_cmd in recv_progs: |
| 103 | + ctx = { |
| 104 | + 'arg': 'rand_%s' % (random.randint(1,100),), |
| 105 | + 'arg2': 'rand_%s' % (random.randint(1,100),), |
| 106 | + } |
| 107 | + rcmd = recv_cmd % ctx |
| 108 | + scmd = send_cmd % ctx |
| 109 | + mask = output_mask % ctx |
| 110 | + p = spawn(rcmd, cwd=rcwd) |
| 111 | + exit_code, sout = run(scmd, cwd=scwd) |
| 112 | + matched, rout = wait(p, mask) |
| 113 | + if matched and exit_code == 0: |
| 114 | + print " [+] %s %-30s ok" % (test, scwd+'/'+rcwd) |
| 115 | + else: |
| 116 | + print " [!] %s %-30s FAILED" % (test, scwd+'/'+rcwd) |
| 117 | + print " [!] %r exited with status %s, output:\n%s\n" % (scmd, exit_code, |
| 118 | + sout.strip()) |
| 119 | + print " [!] %r output:\n%s\n" % (rcmd, rout.strip()) |
| 120 | + errors += 1 |
| 121 | + |
| 122 | +if errors: |
| 123 | + print " [!] %s tests failed" % (errors,) |
| 124 | + |
| 125 | +sys.exit(errors) |
0 commit comments