Benny Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 1 | # $Id$ |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 2 | import sys |
| 3 | import imp |
| 4 | import re |
Benny Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 5 | import os |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 6 | import subprocess |
Benny Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 7 | import random |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 8 | import time |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 9 | import getopt |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 10 | |
| 11 | import inc_const as const |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 12 | import inc_cfg as inc |
| 13 | |
| 14 | # Vars |
| 15 | G_EXE = "" # pjsua executable path |
| 16 | G_INUNIX = False # flags that test is running in Unix |
| 17 | |
| 18 | |
| 19 | # Usage string |
| 20 | usage = \ |
| 21 | """ |
| 22 | run.py - Automated test driver |
| 23 | |
| 24 | Usage: |
| 25 | run.py [options] MODULE CONFIG |
| 26 | Options: |
| 27 | --exe, -e pjsua executable path |
| 28 | --null-audio, -n use null audio |
| 29 | Sample: |
| 30 | run.py -n mod_run.py scripts-run/100_simple.py |
| 31 | """ |
| 32 | |
| 33 | # Parse arguments |
| 34 | try: |
| 35 | opts, args = getopt.getopt(sys.argv[1:], "hne:", ["help", "null-audio", "exe="]) |
| 36 | except getopt.GetoptError, err: |
| 37 | print str(err) |
| 38 | print usage |
| 39 | sys.exit(2) |
| 40 | for 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 | |
| 52 | if len(args) != 2: |
| 53 | print "Invalid arguments" |
| 54 | print usage |
| 55 | sys.exit(2) |
| 56 | |
| 57 | # Set global ARGS to be used by modules |
| 58 | inc.ARGS = args |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 59 | |
Benny Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 60 | # Get the pjsua executable name |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 61 | if G_EXE == "": |
| 62 | if sys.platform.find("win32")!=-1: |
Benny Prijono | 945aeb2 | 2008-12-22 18:54:58 +0000 | [diff] [blame] | 63 | e = "../../pjsip-apps/bin/pjsua_vc6d.exe" |
Benny Prijono | ac1f484 | 2008-08-26 14:35:16 +0000 | [diff] [blame] | 64 | if os.access(e, os.F_OK): |
| 65 | st1 = os.stat(e) |
| 66 | else: |
| 67 | st1 = None |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 68 | if st1 != None: |
| 69 | G_EXE = e |
Benny Prijono | 945aeb2 | 2008-12-22 18:54:58 +0000 | [diff] [blame] | 70 | e = "../../pjsip-apps/bin/pjsua_vc6.exe" |
Benny Prijono | ac1f484 | 2008-08-26 14:35:16 +0000 | [diff] [blame] | 71 | 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 Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 76 | 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 Prijono | 43b6ece | 2008-12-29 14:52:29 +0000 | [diff] [blame] | 83 | f = open("../../build.mak", "r") |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 84 | while True: |
| 85 | line = f.readline() |
| 86 | if not line: |
| 87 | break |
| 88 | if line.find("TARGET_NAME")!=-1: |
| 89 | print line |
Benny Prijono | 945aeb2 | 2008-12-22 18:54:58 +0000 | [diff] [blame] | 90 | G_EXE="../../pjsip-apps/bin/pjsua-" + line.split(":= ")[1] |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 91 | 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 Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | G_EXE = G_EXE.rstrip("\n\r \t") |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 99 | |
| 100 | ################################### |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 101 | # Poor man's 'expect'-like class |
| 102 | class 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 Prijono | ddd02de | 2008-06-26 22:20:11 +0000 | [diff] [blame] | 111 | t0 = time.time() |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 112 | 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 Prijono | 1e65e9a | 2008-06-27 23:53:00 +0000 | [diff] [blame] | 117 | 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 Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 120 | self.trace("Popen " + fullcmd) |
Benny Prijono | 5242a42 | 2008-06-26 16:27:17 +0000 | [diff] [blame] | 121 | self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False) |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 122 | def send(self, cmd): |
| 123 | self.trace("send " + cmd) |
| 124 | self.proc.stdin.writelines(cmd + "\n") |
Benny Prijono | 5242a42 | 2008-06-26 16:27:17 +0000 | [diff] [blame] | 125 | self.proc.stdin.flush() |
| 126 | def expect(self, pattern, raise_on_error=True, title=""): |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 127 | 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 Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 133 | raise inc.TestError(self.name + ": Premature EOF") |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 134 | # 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 Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 140 | raise inc.TestError(self.name + ": " + line) |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 141 | 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 Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 149 | raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"") |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 150 | 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 Prijono | ddd02de | 2008-06-26 22:20:11 +0000 | [diff] [blame] | 158 | cmd = "echo 1" + str(random.randint(1000,9999)) |
| 159 | self.send(cmd) |
| 160 | self.expect(cmd) |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 161 | |
| 162 | def wait(self): |
| 163 | self.trace("wait") |
Benny Prijono | 8a0e97b | 2009-01-04 20:15:37 +0000 | [diff] [blame] | 164 | self.proc.communicate() |
Benny Prijono | d596267 | 2009-01-02 18:15:07 +0000 | [diff] [blame] | 165 | |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 166 | def trace(self, s): |
| 167 | if self.trace_enabled: |
Benny Prijono | ddd02de | 2008-06-26 22:20:11 +0000 | [diff] [blame] | 168 | now = time.time() |
| 169 | fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]" |
| 170 | print fmt % {'time':int(now - self.t0)} |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 171 | |
| 172 | ######################### |
| 173 | # Error handling |
Nanang Izzuddin | e6f85fb | 2008-06-20 17:43:55 +0000 | [diff] [blame] | 174 | def handle_error(errmsg, t, close_processes = True): |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 175 | print "====== Caught error: " + errmsg + " ======" |
Nanang Izzuddin | e6f85fb | 2008-06-20 17:43:55 +0000 | [diff] [blame] | 176 | if (close_processes): |
| 177 | time.sleep(1) |
| 178 | for p in t.process: |
| 179 | p.send("q") |
| 180 | p.send("q") |
Benny Prijono | d596267 | 2009-01-02 18:15:07 +0000 | [diff] [blame] | 181 | 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 Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 195 | print "Test completed with error: " + errmsg |
| 196 | sys.exit(1) |
| 197 | |
| 198 | |
| 199 | ######################### |
| 200 | # MAIN |
| 201 | |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 202 | # Import the test script |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 203 | script = imp.load_source("script", inc.ARGS[0]) |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 204 | |
Benny Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 205 | # Init random seed |
| 206 | random.seed() |
| 207 | |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 208 | # Validate |
| 209 | if script.test == None: |
| 210 | print "Error: no test defined" |
| 211 | sys.exit(1) |
| 212 | |
Nanang Izzuddin | 6ee166d | 2008-06-26 12:26:52 +0000 | [diff] [blame] | 213 | if script.test.skip: |
| 214 | print "Test " + script.test.title + " is skipped" |
| 215 | sys.exit(0) |
| 216 | |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 217 | if len(script.test.inst_params) == 0: |
| 218 | print "Error: test doesn't contain pjsua run descriptions" |
| 219 | sys.exit(1) |
| 220 | |
| 221 | # Instantiate pjsuas |
| 222 | print "====== Running " + script.test.title + " ======" |
Benny Prijono | f9bd1f2 | 2008-06-16 13:04:44 +0000 | [diff] [blame] | 223 | print "Using " + G_EXE + " as pjsua executable" |
| 224 | |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 225 | for 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 Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 241 | except inc.TestError, e: |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 242 | handle_error(e.desc, script.test) |
| 243 | |
| 244 | # Run the test function |
| 245 | if script.test.test_func != None: |
| 246 | try: |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 247 | script.test.test_func(script.test) |
| 248 | except inc.TestError, e: |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 249 | handle_error(e.desc, script.test) |
| 250 | |
| 251 | # Shutdown all instances |
| 252 | time.sleep(2) |
| 253 | for 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 Izzuddin | f810f95 | 2008-06-18 21:04:14 +0000 | [diff] [blame] | 265 | # Run the post test function |
| 266 | if script.test.post_func != None: |
| 267 | try: |
Nanang Izzuddin | a680bd6 | 2008-06-27 21:12:12 +0000 | [diff] [blame] | 268 | script.test.post_func(script.test) |
| 269 | except inc.TestError, e: |
Nanang Izzuddin | e6f85fb | 2008-06-20 17:43:55 +0000 | [diff] [blame] | 270 | handle_error(e.desc, script.test, False) |
Nanang Izzuddin | f810f95 | 2008-06-18 21:04:14 +0000 | [diff] [blame] | 271 | |
Benny Prijono | cc1ada5 | 2008-06-15 19:43:43 +0000 | [diff] [blame] | 272 | # Done |
| 273 | print "Test " + script.test.title + " completed successfully" |
| 274 | sys.exit(0) |
| 275 | |