forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·126 lines (112 loc) · 4.03 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
#!/usr/bin/env python
import time
import random
import re
import subprocess
import signal
import sys
import os
def run(cmd, verbose=False, **kwargs):
if verbose:
print " [s] Running %r" % (cmd,)
p = subprocess.Popen(cmd.split(),
stdout=subprocess.PIPE,
**kwargs)
p.wait()
if verbose:
for line in p.stdout:
line = line.strip()
if line:
print ' [s] %s' % (line,)
print " [s] Done"
time.sleep(0.1)
return p.returncode
def spawn(cmd, verbose=False, **kwargs):
if verbose:
print " [r] Waiting for %r" % (cmd,)
p = subprocess.Popen(cmd.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs)
time.sleep(0.4)
return p
def wait(p, match, verbose=False):
os.kill(p.pid, signal.SIGINT)
p.wait()
r = False
for line in p.stdout:
line = line.strip()
if re.search(match, line):
r = True
if verbose:
print " [r] %s" % (line,)
if verbose:
print " [r] Done"
return r
def gen(prog, arg="", **kwargs):
Prog = ''.join([w.capitalize() for w in prog.split('_')])
ctx = {
'prog': prog,
'Prog': Prog,
'rubyver': os.environ.get('RUBYVER', '1.8'),
'arg': arg,
'java': kwargs.get('java', Prog),
'dotnet': kwargs.get('dotnet', Prog),
}
return [
('python', './venv/bin/python %(prog)s.py %(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),
('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%(rubyver)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'),
['erlang', 'php']),
skip(gen('receive_logs_direct', arg='%(arg)s'),
['erlang', 'php']),
'%(arg2)s'),
'tut5': (skip(gen('emit_log_topic', arg='%(arg)s.foo %(arg2)s'),
['erlang', 'php']),
skip(gen('receive_logs_topic', arg='%(arg)s.*'),
['erlang', 'php']),
'%(arg2)s'),
'tut6': (skip(gen('rpc_client', java='RPCClient', dotnet='RPCClient'),
['erlang', 'php']),
skip(gen('rpc_server', java='RPCServer', dotnet='RPCServer'),
['erlang', 'php']),
'fib[(]30[)]'),
}
verbose = len(sys.argv) > 1
errors = 0
for test in sorted(tests.keys()):
(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, verbose=verbose, cwd=rcwd)
e = run(scmd, verbose=verbose, cwd=scwd)
if wait(p, mask, verbose=verbose) and e == 0:
print " [+] %s %-30s ok" % (test, scwd+'/'+rcwd)
else:
print " [!] %s %-30s FAILED %r %r (error=%r)" % \
(test, scwd+'/'+rcwd, rcmd, scmd, e)
errors += 1
if errors:
print " [!] %s tests failed" % (errors,)
sys.exit(errors)