blob: df940ea7e71b62b4cb6298f98643f7103dbed422 [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:
83 f = open("../../../build.mak", "r")
84 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")
164 self.proc.wait()
165 def trace(self, s):
166 if self.trace_enabled:
Benny Prijonoddd02de2008-06-26 22:20:11 +0000167 now = time.time()
168 fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]"
169 print fmt % {'time':int(now - self.t0)}
Benny Prijonocc1ada52008-06-15 19:43:43 +0000170
171#########################
172# Error handling
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000173def handle_error(errmsg, t, close_processes = True):
Benny Prijonocc1ada52008-06-15 19:43:43 +0000174 print "====== Caught error: " + errmsg + " ======"
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000175 if (close_processes):
176 time.sleep(1)
177 for p in t.process:
178 p.send("q")
179 p.send("q")
180 p.expect(const.DESTROYED, False)
181 p.wait()
Benny Prijonocc1ada52008-06-15 19:43:43 +0000182 print "Test completed with error: " + errmsg
183 sys.exit(1)
184
185
186#########################
187# MAIN
188
Benny Prijonocc1ada52008-06-15 19:43:43 +0000189# Import the test script
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000190script = imp.load_source("script", inc.ARGS[0])
Benny Prijonocc1ada52008-06-15 19:43:43 +0000191
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000192# Init random seed
193random.seed()
194
Benny Prijonocc1ada52008-06-15 19:43:43 +0000195# Validate
196if script.test == None:
197 print "Error: no test defined"
198 sys.exit(1)
199
Nanang Izzuddin6ee166d2008-06-26 12:26:52 +0000200if script.test.skip:
201 print "Test " + script.test.title + " is skipped"
202 sys.exit(0)
203
Benny Prijonocc1ada52008-06-15 19:43:43 +0000204if len(script.test.inst_params) == 0:
205 print "Error: test doesn't contain pjsua run descriptions"
206 sys.exit(1)
207
208# Instantiate pjsuas
209print "====== Running " + script.test.title + " ======"
Benny Prijonof9bd1f22008-06-16 13:04:44 +0000210print "Using " + G_EXE + " as pjsua executable"
211
Benny Prijonocc1ada52008-06-15 19:43:43 +0000212for inst_param in script.test.inst_params:
213 try:
214 # Create pjsua's Expect instance from the param
215 p = Expect(inst_param)
216 # Wait until registration completes
217 if inst_param.have_reg:
218 p.expect(inst_param.uri+".*registration success")
219 # Synchronize stdout
220 p.send("")
221 p.expect(const.PROMPT)
222 p.send("echo 1")
223 p.send("echo 1")
224 p.expect("echo 1")
225 # add running instance
226 script.test.process.append(p)
227
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000228 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000229 handle_error(e.desc, script.test)
230
231# Run the test function
232if script.test.test_func != None:
233 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000234 script.test.test_func(script.test)
235 except inc.TestError, e:
Benny Prijonocc1ada52008-06-15 19:43:43 +0000236 handle_error(e.desc, script.test)
237
238# Shutdown all instances
239time.sleep(2)
240for p in script.test.process:
241 # Unregister if we have_reg to make sure that next tests
242 # won't wail
243 if p.inst_param.have_reg:
244 p.send("ru")
245 p.expect(p.inst_param.uri+".*unregistration success")
246 p.send("q")
247 p.send("q")
248 time.sleep(0.5)
249 p.expect(const.DESTROYED, False)
250 p.wait()
251
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000252# Run the post test function
253if script.test.post_func != None:
254 try:
Nanang Izzuddina680bd62008-06-27 21:12:12 +0000255 script.test.post_func(script.test)
256 except inc.TestError, e:
Nanang Izzuddine6f85fb2008-06-20 17:43:55 +0000257 handle_error(e.desc, script.test, False)
Nanang Izzuddinf810f952008-06-18 21:04:14 +0000258
Benny Prijonocc1ada52008-06-15 19:43:43 +0000259# Done
260print "Test " + script.test.title + " completed successfully"
261sys.exit(0)
262