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