blob: 58a45a851a0ce4f161947d9d8849bc8a30ed8436 [file] [log] [blame]
Benny Prijonof9bd1f22008-06-16 13:04:44 +00001# $Id$
Benny Prijonocc1ada52008-06-15 19:43:43 +00002import sys
3import imp
4import re
Benny Prijonof9bd1f22008-06-16 13:04:44 +00005import os
Benny Prijonocc1ada52008-06-15 19:43:43 +00006import subprocess
Benny Prijonof9bd1f22008-06-16 13:04:44 +00007import random
Benny Prijonocc1ada52008-06-15 19:43:43 +00008import time
Nanang Izzuddina680bd62008-06-27 21:12:12 +00009import getopt
Benny Prijonocc1ada52008-06-15 19:43:43 +000010
11import inc_const as const
Nanang Izzuddina680bd62008-06-27 21:12:12 +000012import inc_cfg as inc
13
14# Vars
15G_EXE = "" # pjsua executable path
16G_INUNIX = False # flags that test is running in Unix
17
18
19# Usage string
20usage = \
21"""
22run.py - Automated test driver
23
24Usage:
25 run.py [options] MODULE CONFIG
26Options:
27 --exe, -e pjsua executable path
28 --null-audio, -n use null audio
29Sample:
30 run.py -n mod_run.py scripts-run/100_simple.py
31"""
32
33# Parse arguments
34try:
35 opts, args = getopt.getopt(sys.argv[1:], "hne:", ["help", "null-audio", "exe="])
36except getopt.GetoptError, err:
37 print str(err)
38 print usage
39 sys.exit(2)
40for o, a in opts:
41 if o in ("-h", "--help"):
42 print usage
43 sys.exit()
44 elif o in ("-n", "--null-audio"):
45 inc.HAS_SND_DEV = 0
46 elif o in ("-e", "--exe"):
47 G_EXE = a
48 else:
49 print "Unknown options"
50 sys.exit(2)
51
52if len(args) != 2:
53 print "Invalid arguments"
54 print usage
55 sys.exit(2)
56
57# Set global ARGS to be used by modules
58inc.ARGS = args
Benny Prijonocc1ada52008-06-15 19:43:43 +000059
Benny Prijonof9bd1f22008-06-16 13:04:44 +000060# Get the pjsua executable name
Nanang Izzuddina680bd62008-06-27 21:12:12 +000061if G_EXE == "":
62 if sys.platform.find("win32")!=-1:
63 e = "../../bin/pjsua_vc6d.exe"
64 st1 = os.stat(e)
65 if st1 != None:
66 G_EXE = e
67 e = "../../bin/pjsua_vc6d.exe"
68 st2 = os.stat(e)
69 if st2 != None and st2.st_mtime > st1.st_mtime:
70 G_EXE = e
71 st1 = st2
72 if G_EXE=="":
73 print "Unable to find valid pjsua. Please build pjsip first"
74 sys.exit(1)
75 G_INUNIX = False
76 else:
77 f = open("../../../build.mak", "r")
78 while True:
79 line = f.readline()
80 if not line:
81 break
82 if line.find("TARGET_NAME")!=-1:
83 print line
84 G_EXE="../../bin/pjsua-" + line.split(":= ")[1]
85 break
86 if G_EXE=="":
87 print "Unable to find ../../../build.mak. Please build pjsip first"
88 sys.exit(1)
89 G_INUNIX = True
Benny Prijonof9bd1f22008-06-16 13:04:44 +000090
91
92G_EXE = G_EXE.rstrip("\n\r \t")
Benny Prijonocc1ada52008-06-15 19:43:43 +000093
94###################################
Benny Prijonocc1ada52008-06-15 19:43:43 +000095# Poor man's 'expect'-like class
96class Expect:
97 proc = None
98 echo = False
99 trace_enabled = False
100 name = ""
101 inst_param = None
102 rh = re.compile(const.DESTROYED)
103 ra = re.compile(const.ASSERT, re.I)
104 rr = re.compile(const.STDOUT_REFRESH)
Benny Prijonoddd02de2008-06-26 22:20:11 +0000105 t0 = time.time()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000106 def __init__(self, inst_param):
107 self.inst_param = inst_param
108 self.name = inst_param.name
109 self.echo = inst_param.echo_enabled
110 self.trace_enabled = inst_param.trace_enabled
Benny Prijono1e65e9a2008-06-27 23:53:00 +0000111 fullcmd = G_EXE + " " + inst_param.arg + " --stdout-refresh=5 --stdout-refresh-text=" + const.STDOUT_REFRESH
112 if not inst_param.enable_buffer:
113 fullcmd = fullcmd + " --stdout-no-buf"
Benny Prijonocc1ada52008-06-15 19:43:43 +0000114 self.trace("Popen " + fullcmd)
Benny Prijono5242a422008-06-26 16:27:17 +0000115 self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000116 def send(self, cmd):
117 self.trace("send " + cmd)
118 self.proc.stdin.writelines(cmd + "\n")
Benny Prijono5242a422008-06-26 16:27:17 +0000119 self.proc.stdin.flush()
120 def expect(self, pattern, raise_on_error=True, title=""):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000121 self.trace("expect " + pattern)
122 r = re.compile(pattern, re.I)
123 refresh_cnt = 0
124 while True:
125 line = self.proc.stdout.readline()
126 if line == "":
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000127 raise inc.TestError(self.name + ": Premature EOF")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000128 # Print the line if echo is ON
129 if self.echo:
130 print self.name + ": " + line,
131 # Trap assertion error
132 if self.ra.search(line) != None:
133 if raise_on_error:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000134 raise inc.TestError(self.name + ": " + line)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000135 else:
136 return None
137 # Count stdout refresh text.
138 if self.rr.search(line) != None:
139 refresh_cnt = refresh_cnt+1
140 if refresh_cnt >= 6:
141 self.trace("Timed-out!")
142 if raise_on_error:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000143 raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000144 else:
145 return None # timeout
146 # Search for expected text
147 if r.search(line) != None:
148 return line
149
150 def sync_stdout(self):
151 self.trace("sync_stdout")
Benny Prijonoddd02de2008-06-26 22:20:11 +0000152 cmd = "echo 1" + str(random.randint(1000,9999))
153 self.send(cmd)
154 self.expect(cmd)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000155
156 def wait(self):
157 self.trace("wait")
158 self.proc.wait()
159 def trace(self, s):
160 if self.trace_enabled:
Benny Prijonoddd02de2008-06-26 22:20:11 +0000161 now = time.time()
162 fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]"
163 print fmt % {'time':int(now - self.t0)}
Benny Prijonocc1ada52008-06-15 19:43:43 +0000164
165#########################
166# Error handling
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000167def handle_error(errmsg, t, close_processes = True):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000168 print "====== Caught error: " + errmsg + " ======"
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000169 if (close_processes):
170 time.sleep(1)
171 for p in t.process:
172 p.send("q")
173 p.send("q")
174 p.expect(const.DESTROYED, False)
175 p.wait()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000176 print "Test completed with error: " + errmsg
177 sys.exit(1)
178
179
180#########################
181# MAIN
182
Benny Prijonocc1ada52008-06-15 19:43:43 +0000183# Import the test script
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000184script = imp.load_source("script", inc.ARGS[0])
Benny Prijonocc1ada52008-06-15 19:43:43 +0000185
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000186# Init random seed
187random.seed()
188
Benny Prijonocc1ada52008-06-15 19:43:43 +0000189# Validate
190if script.test == None:
191 print "Error: no test defined"
192 sys.exit(1)
193
Nanang Izzuddin6ee166d2008-06-26 12:26:52 +0000194if script.test.skip:
195 print "Test " + script.test.title + " is skipped"
196 sys.exit(0)
197
Benny Prijonocc1ada52008-06-15 19:43:43 +0000198if len(script.test.inst_params) == 0:
199 print "Error: test doesn't contain pjsua run descriptions"
200 sys.exit(1)
201
202# Instantiate pjsuas
203print "====== Running " + script.test.title + " ======"
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000204print "Using " + G_EXE + " as pjsua executable"
205
Benny Prijonocc1ada52008-06-15 19:43:43 +0000206for inst_param in script.test.inst_params:
207 try:
208 # Create pjsua's Expect instance from the param
209 p = Expect(inst_param)
210 # Wait until registration completes
211 if inst_param.have_reg:
212 p.expect(inst_param.uri+".*registration success")
213 # Synchronize stdout
214 p.send("")
215 p.expect(const.PROMPT)
216 p.send("echo 1")
217 p.send("echo 1")
218 p.expect("echo 1")
219 # add running instance
220 script.test.process.append(p)
221
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000222 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000223 handle_error(e.desc, script.test)
224
225# Run the test function
226if script.test.test_func != None:
227 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000228 script.test.test_func(script.test)
229 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000230 handle_error(e.desc, script.test)
231
232# Shutdown all instances
233time.sleep(2)
234for p in script.test.process:
235 # Unregister if we have_reg to make sure that next tests
236 # won't wail
237 if p.inst_param.have_reg:
238 p.send("ru")
239 p.expect(p.inst_param.uri+".*unregistration success")
240 p.send("q")
241 p.send("q")
242 time.sleep(0.5)
243 p.expect(const.DESTROYED, False)
244 p.wait()
245
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000246# Run the post test function
247if script.test.post_func != None:
248 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000249 script.test.post_func(script.test)
250 except inc.TestError, e:
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000251 handle_error(e.desc, script.test, False)
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000252
Benny Prijonocc1ada52008-06-15 19:43:43 +0000253# Done
254print "Test " + script.test.title + " completed successfully"
255sys.exit(0)
256