blob: a73e9068e468687eb1a3600ee66d8da731d2c87c [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 Prijono4fa16022010-04-20 09:52:54 +000096else:
97 if sys.platform.lower().find("win32")!=-1 or sys.platform.lower().find("microsoft")!=-1:
98 G_INUNIX = False
99 else:
100 G_INUNIX = True
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000101
102
103G_EXE = G_EXE.rstrip("\n\r \t")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000104
105###################################
Benny Prijonocc1ada52008-06-15 19:43:43 +0000106# Poor man's 'expect'-like class
107class Expect:
108 proc = None
109 echo = False
110 trace_enabled = False
111 name = ""
112 inst_param = None
113 rh = re.compile(const.DESTROYED)
114 ra = re.compile(const.ASSERT, re.I)
115 rr = re.compile(const.STDOUT_REFRESH)
Benny Prijonoddd02de2008-06-26 22:20:11 +0000116 t0 = time.time()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000117 def __init__(self, inst_param):
118 self.inst_param = inst_param
119 self.name = inst_param.name
120 self.echo = inst_param.echo_enabled
121 self.trace_enabled = inst_param.trace_enabled
Benny Prijono1e65e9a2008-06-27 23:53:00 +0000122 fullcmd = G_EXE + " " + inst_param.arg + " --stdout-refresh=5 --stdout-refresh-text=" + const.STDOUT_REFRESH
123 if not inst_param.enable_buffer:
124 fullcmd = fullcmd + " --stdout-no-buf"
Benny Prijonocc1ada52008-06-15 19:43:43 +0000125 self.trace("Popen " + fullcmd)
Benny Prijono5242a422008-06-26 16:27:17 +0000126 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 +0000127 def send(self, cmd):
128 self.trace("send " + cmd)
129 self.proc.stdin.writelines(cmd + "\n")
Benny Prijono5242a422008-06-26 16:27:17 +0000130 self.proc.stdin.flush()
131 def expect(self, pattern, raise_on_error=True, title=""):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000132 self.trace("expect " + pattern)
133 r = re.compile(pattern, re.I)
134 refresh_cnt = 0
135 while True:
136 line = self.proc.stdout.readline()
137 if line == "":
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000138 raise inc.TestError(self.name + ": Premature EOF")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000139 # Print the line if echo is ON
140 if self.echo:
141 print self.name + ": " + line,
142 # Trap assertion error
143 if self.ra.search(line) != None:
144 if raise_on_error:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000145 raise inc.TestError(self.name + ": " + line)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000146 else:
147 return None
148 # Count stdout refresh text.
149 if self.rr.search(line) != None:
150 refresh_cnt = refresh_cnt+1
151 if refresh_cnt >= 6:
152 self.trace("Timed-out!")
153 if raise_on_error:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000154 raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"")
Benny Prijonocc1ada52008-06-15 19:43:43 +0000155 else:
156 return None # timeout
157 # Search for expected text
158 if r.search(line) != None:
159 return line
160
161 def sync_stdout(self):
162 self.trace("sync_stdout")
Benny Prijonoddd02de2008-06-26 22:20:11 +0000163 cmd = "echo 1" + str(random.randint(1000,9999))
164 self.send(cmd)
165 self.expect(cmd)
Benny Prijonocc1ada52008-06-15 19:43:43 +0000166
167 def wait(self):
168 self.trace("wait")
Benny Prijono8a0e97b2009-01-04 20:15:37 +0000169 self.proc.communicate()
Benny Prijonod5962672009-01-02 18:15:07 +0000170
Benny Prijonocc1ada52008-06-15 19:43:43 +0000171 def trace(self, s):
172 if self.trace_enabled:
Benny Prijonoddd02de2008-06-26 22:20:11 +0000173 now = time.time()
174 fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]"
175 print fmt % {'time':int(now - self.t0)}
Benny Prijonocc1ada52008-06-15 19:43:43 +0000176
177#########################
178# Error handling
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000179def handle_error(errmsg, t, close_processes = True):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000180 print "====== Caught error: " + errmsg + " ======"
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000181 if (close_processes):
182 time.sleep(1)
183 for p in t.process:
184 p.send("q")
185 p.send("q")
Benny Prijonod5962672009-01-02 18:15:07 +0000186 is_err = False
187 try:
188 ret = p.expect(const.DESTROYED, False)
189 if not ret:
190 is_err = True
191 except:
192 is_err = True
193 if is_err:
194 if sys.hexversion >= 0x02060000:
195 p.proc.terminate()
196 else:
197 p.wait()
198 else:
199 p.wait()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000200 print "Test completed with error: " + errmsg
201 sys.exit(1)
202
203
204#########################
205# MAIN
206
Benny Prijonocc1ada52008-06-15 19:43:43 +0000207# Import the test script
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000208script = imp.load_source("script", inc.ARGS[0])
Benny Prijonocc1ada52008-06-15 19:43:43 +0000209
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000210# Init random seed
211random.seed()
212
Benny Prijonocc1ada52008-06-15 19:43:43 +0000213# Validate
214if script.test == None:
215 print "Error: no test defined"
216 sys.exit(1)
217
Nanang Izzuddin6ee166d2008-06-26 12:26:52 +0000218if script.test.skip:
219 print "Test " + script.test.title + " is skipped"
220 sys.exit(0)
221
Benny Prijonocc1ada52008-06-15 19:43:43 +0000222if len(script.test.inst_params) == 0:
223 print "Error: test doesn't contain pjsua run descriptions"
224 sys.exit(1)
225
226# Instantiate pjsuas
227print "====== Running " + script.test.title + " ======"
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000228print "Using " + G_EXE + " as pjsua executable"
229
Benny Prijonocc1ada52008-06-15 19:43:43 +0000230for inst_param in script.test.inst_params:
231 try:
232 # Create pjsua's Expect instance from the param
233 p = Expect(inst_param)
234 # Wait until registration completes
235 if inst_param.have_reg:
236 p.expect(inst_param.uri+".*registration success")
237 # Synchronize stdout
238 p.send("")
239 p.expect(const.PROMPT)
240 p.send("echo 1")
241 p.send("echo 1")
242 p.expect("echo 1")
243 # add running instance
244 script.test.process.append(p)
245
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000246 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000247 handle_error(e.desc, script.test)
248
249# Run the test function
250if script.test.test_func != None:
251 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000252 script.test.test_func(script.test)
253 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000254 handle_error(e.desc, script.test)
255
256# Shutdown all instances
257time.sleep(2)
258for p in script.test.process:
259 # Unregister if we have_reg to make sure that next tests
260 # won't wail
261 if p.inst_param.have_reg:
262 p.send("ru")
263 p.expect(p.inst_param.uri+".*unregistration success")
264 p.send("q")
265 p.send("q")
266 time.sleep(0.5)
267 p.expect(const.DESTROYED, False)
268 p.wait()
269
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000270# Run the post test function
271if script.test.post_func != None:
272 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000273 script.test.post_func(script.test)
274 except inc.TestError, e:
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000275 handle_error(e.desc, script.test, False)
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000276
Benny Prijonocc1ada52008-06-15 19:43:43 +0000277# Done
278print "Test " + script.test.title + " completed successfully"
279sys.exit(0)
280