blob: 90ed9e7e784aef919c6a78044365e6ee203ec7fc [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:
Benny Prijono945aeb22008-12-22 18:54:58 +000063 e = "../../pjsip-apps/bin/pjsua_vc6d.exe"
Benny Prijonoac1f4842008-08-26 14:35:16 +000064 if os.access(e, os.F_OK):
65 st1 = os.stat(e)
66 else:
67 st1 = None
Nanang Izzuddina680bd62008-06-27 21:12:12 +000068 if st1 != None:
69 G_EXE = e
Benny Prijono945aeb22008-12-22 18:54:58 +000070 e = "../../pjsip-apps/bin/pjsua_vc6.exe"
Benny Prijonoac1f4842008-08-26 14:35:16 +000071 if os.access(e, os.F_OK):
72 st2 = os.stat(e)
73 else:
74 st2 = None
75 if st2 != None and (st1==None or st2.st_mtime > st1.st_mtime):
Nanang Izzuddina680bd62008-06-27 21:12:12 +000076 G_EXE = e
77 st1 = st2
78 if G_EXE=="":
79 print "Unable to find valid pjsua. Please build pjsip first"
80 sys.exit(1)
81 G_INUNIX = False
82 else:
Benny Prijono43b6ece2008-12-29 14:52:29 +000083 f = open("../../build.mak", "r")
Nanang Izzuddina680bd62008-06-27 21:12:12 +000084 while True:
85 line = f.readline()
86 if not line:
87 break
88 if line.find("TARGET_NAME")!=-1:
89 print line
Benny Prijono945aeb22008-12-22 18:54:58 +000090 G_EXE="../../pjsip-apps/bin/pjsua-" + line.split(":= ")[1]
Nanang Izzuddina680bd62008-06-27 21:12:12 +000091 break
92 if G_EXE=="":
93 print "Unable to find ../../../build.mak. Please build pjsip first"
94 sys.exit(1)
95 G_INUNIX = True
Benny Prijonof9bd1f22008-06-16 13:04:44 +000096
97
98G_EXE = G_EXE.rstrip("\n\r \t")
Benny Prijonocc1ada52008-06-15 19:43:43 +000099
100###################################
Benny Prijonocc1ada52008-06-15 19:43:43 +0000101# Poor man's 'expect'-like class
102class Expect:
103 proc = None
104 echo = False
105 trace_enabled = False
106 name = ""
107 inst_param = None
108 rh = re.compile(const.DESTROYED)
109 ra = re.compile(const.ASSERT, re.I)
110 rr = re.compile(const.STDOUT_REFRESH)
Benny Prijonoddd02de2008-06-26 22:20:11 +0000111 t0 = time.time()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000112 def __init__(self, inst_param):
113 self.inst_param = inst_param
114 self.name = inst_param.name
115 self.echo = inst_param.echo_enabled
116 self.trace_enabled = inst_param.trace_enabled
Benny Prijono1e65e9a2008-06-27 23:53:00 +0000117 fullcmd = G_EXE + " " + inst_param.arg + " --stdout-refresh=5 --stdout-refresh-text=" + const.STDOUT_REFRESH
118 if not inst_param.enable_buffer:
119 fullcmd = fullcmd + " --stdout-no-buf"
Benny Prijonocc1ada52008-06-15 19:43:43 +0000120 self.trace("Popen " + fullcmd)
Benny Prijono5242a422008-06-26 16:27:17 +0000121 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 +0000122 def send(self, cmd):
123 self.trace("send " + cmd)
124 self.proc.stdin.writelines(cmd + "\n")
Benny Prijono5242a422008-06-26 16:27:17 +0000125 self.proc.stdin.flush()
126 def expect(self, pattern, raise_on_error=True, title=""):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000127 self.trace("expect " + pattern)
128 r = re.compile(pattern, re.I)
129 refresh_cnt = 0
130 while True:
131 line = self.proc.stdout.readline()
132 if line == "":
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000133 raise inc.TestError(self.name + ": Premature EOF")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000134 # Print the line if echo is ON
135 if self.echo:
136 print self.name + ": " + line,
137 # Trap assertion error
138 if self.ra.search(line) != None:
139 if raise_on_error:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000140 raise inc.TestError(self.name + ": " + line)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000141 else:
142 return None
143 # Count stdout refresh text.
144 if self.rr.search(line) != None:
145 refresh_cnt = refresh_cnt+1
146 if refresh_cnt >= 6:
147 self.trace("Timed-out!")
148 if raise_on_error:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000149 raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000150 else:
151 return None # timeout
152 # Search for expected text
153 if r.search(line) != None:
154 return line
155
156 def sync_stdout(self):
157 self.trace("sync_stdout")
Benny Prijonoddd02de2008-06-26 22:20:11 +0000158 cmd = "echo 1" + str(random.randint(1000,9999))
159 self.send(cmd)
160 self.expect(cmd)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000161
162 def wait(self):
163 self.trace("wait")
Benny Prijono8a0e97b2009-01-04 20:15:37 +0000164 self.proc.communicate()
Benny Prijonod5962672009-01-02 18:15:07 +0000165
Benny Prijonocc1ada52008-06-15 19:43:43 +0000166 def trace(self, s):
167 if self.trace_enabled:
Benny Prijonoddd02de2008-06-26 22:20:11 +0000168 now = time.time()
169 fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]"
170 print fmt % {'time':int(now - self.t0)}
Benny Prijonocc1ada52008-06-15 19:43:43 +0000171
172#########################
173# Error handling
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000174def handle_error(errmsg, t, close_processes = True):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000175 print "====== Caught error: " + errmsg + " ======"
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000176 if (close_processes):
177 time.sleep(1)
178 for p in t.process:
179 p.send("q")
180 p.send("q")
Benny Prijonod5962672009-01-02 18:15:07 +0000181 is_err = False
182 try:
183 ret = p.expect(const.DESTROYED, False)
184 if not ret:
185 is_err = True
186 except:
187 is_err = True
188 if is_err:
189 if sys.hexversion >= 0x02060000:
190 p.proc.terminate()
191 else:
192 p.wait()
193 else:
194 p.wait()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000195 print "Test completed with error: " + errmsg
196 sys.exit(1)
197
198
199#########################
200# MAIN
201
Benny Prijonocc1ada52008-06-15 19:43:43 +0000202# Import the test script
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000203script = imp.load_source("script", inc.ARGS[0])
Benny Prijonocc1ada52008-06-15 19:43:43 +0000204
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000205# Init random seed
206random.seed()
207
Benny Prijonocc1ada52008-06-15 19:43:43 +0000208# Validate
209if script.test == None:
210 print "Error: no test defined"
211 sys.exit(1)
212
Nanang Izzuddin6ee166d2008-06-26 12:26:52 +0000213if script.test.skip:
214 print "Test " + script.test.title + " is skipped"
215 sys.exit(0)
216
Benny Prijonocc1ada52008-06-15 19:43:43 +0000217if len(script.test.inst_params) == 0:
218 print "Error: test doesn't contain pjsua run descriptions"
219 sys.exit(1)
220
221# Instantiate pjsuas
222print "====== Running " + script.test.title + " ======"
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000223print "Using " + G_EXE + " as pjsua executable"
224
Benny Prijonocc1ada52008-06-15 19:43:43 +0000225for inst_param in script.test.inst_params:
226 try:
227 # Create pjsua's Expect instance from the param
228 p = Expect(inst_param)
229 # Wait until registration completes
230 if inst_param.have_reg:
231 p.expect(inst_param.uri+".*registration success")
232 # Synchronize stdout
233 p.send("")
234 p.expect(const.PROMPT)
235 p.send("echo 1")
236 p.send("echo 1")
237 p.expect("echo 1")
238 # add running instance
239 script.test.process.append(p)
240
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000241 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000242 handle_error(e.desc, script.test)
243
244# Run the test function
245if script.test.test_func != None:
246 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000247 script.test.test_func(script.test)
248 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000249 handle_error(e.desc, script.test)
250
251# Shutdown all instances
252time.sleep(2)
253for p in script.test.process:
254 # Unregister if we have_reg to make sure that next tests
255 # won't wail
256 if p.inst_param.have_reg:
257 p.send("ru")
258 p.expect(p.inst_param.uri+".*unregistration success")
259 p.send("q")
260 p.send("q")
261 time.sleep(0.5)
262 p.expect(const.DESTROYED, False)
263 p.wait()
264
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000265# Run the post test function
266if script.test.post_func != None:
267 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000268 script.test.post_func(script.test)
269 except inc.TestError, e:
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000270 handle_error(e.desc, script.test, False)
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000271
Benny Prijonocc1ada52008-06-15 19:43:43 +0000272# Done
273print "Test " + script.test.title + " completed successfully"
274sys.exit(0)
275