| 1 |
import sys |
| 2 |
import re |
| 3 |
import os |
| 4 |
import subprocess |
| 5 |
import random |
| 6 |
import telnetlib |
| 7 |
import time |
| 8 |
import threading |
| 9 |
import traceback |
| 10 |
import getopt |
| 11 |
|
| 12 |
import inc_cfg as inc |
| 13 |
import inc_const as const |
| 14 |
import inc_util as util |
| 15 |
|
| 16 |
|
| 17 |
G_EXE = "" |
| 18 |
G_INUNIX = False |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
usage = \ |
| 23 |
""" |
| 24 |
run.py - Automated test driver |
| 25 |
|
| 26 |
Usage: |
| 27 |
run.py [options] MODULE CONFIG |
| 28 |
Options: |
| 29 |
--exe, -e pjsua executable path |
| 30 |
--null-audio, -n use null audio |
| 31 |
Sample: |
| 32 |
run.py -n mod_run.py scripts-run/100_simple.py |
| 33 |
""" |
| 34 |
|
| 35 |
|
| 36 |
try: |
| 37 |
opts, args = getopt.getopt(sys.argv[1:], "hne:", ["help", "null-audio", "exe="]) |
| 38 |
except getopt.GetoptError as err: |
| 39 |
print(str(err)) |
| 40 |
print(usage) |
| 41 |
sys.exit(2) |
| 42 |
for o, a in opts: |
| 43 |
if o in ("-h", "--help"): |
| 44 |
print(usage) |
| 45 |
sys.exit() |
| 46 |
elif o in ("-n", "--null-audio"): |
| 47 |
inc.HAS_SND_DEV = 0 |
| 48 |
elif o in ("-e", "--exe"): |
| 49 |
G_EXE = a |
| 50 |
else: |
| 51 |
print("Unknown options") |
| 52 |
sys.exit(2) |
| 53 |
|
| 54 |
if len(args) != 2: |
| 55 |
print("Invalid arguments") |
| 56 |
print(usage) |
| 57 |
sys.exit(2) |
| 58 |
|
| 59 |
|
| 60 |
inc.ARGS = args |
| 61 |
|
| 62 |
|
| 63 |
if G_EXE == "": |
| 64 |
if sys.platform.find("win32")!=-1: |
| 65 |
EXE_DIR = "../../pjsip-apps/bin/" |
| 66 |
EXECUTABLES = [ "pjsua_vc6d.exe", |
| 67 |
"pjsua_vc6.exe", |
| 68 |
"pjsua-i386-Win32-vc8-Debug.exe", |
| 69 |
"pjsua-i386-Win32-vc8-Debug-Dynamic.exe", |
| 70 |
"pjsua-i386-Win32-vc8-Debug-Static.exe", |
| 71 |
"pjsua-i386-Win32-vc8-Release.exe", |
| 72 |
"pjsua-i386-Win32-vc8-Release-Dynamic.exe", |
| 73 |
"pjsua-i386-Win32-vc8-Release-Static.exe", |
| 74 |
"pjsua-i386-Win32-vc14-Debug.exe", |
| 75 |
"pjsua-i386-Win32-vc14-Debug-Dynamic.exe", |
| 76 |
"pjsua-i386-Win32-vc14-Debug-Static.exe", |
| 77 |
"pjsua-i386-Win32-vc14-Release.exe", |
| 78 |
"pjsua-i386-Win32-vc14-Release-Dynamic.exe", |
| 79 |
"pjsua-i386-Win32-vc14-Release-Static.exe" |
| 80 |
] |
| 81 |
e_ts = 0 |
| 82 |
for e in EXECUTABLES: |
| 83 |
e = EXE_DIR + e |
| 84 |
if os.access(e, os.F_OK): |
| 85 |
st = os.stat(e) |
| 86 |
if e_ts==0 or e_ts<st.st_mtime: |
| 87 |
G_EXE = e |
| 88 |
e_ts = st.st_mtime |
| 89 |
|
| 90 |
if G_EXE=="": |
| 91 |
print("Unable to find valid pjsua. Please build pjsip first") |
| 92 |
sys.exit(1) |
| 93 |
|
| 94 |
G_INUNIX = False |
| 95 |
else: |
| 96 |
f = open("../../build.mak", "r") |
| 97 |
while True: |
| 98 |
line = f.readline() |
| 99 |
if not line: |
| 100 |
break |
| 101 |
if line.find("TARGET_NAME")!=-1: |
| 102 |
print(line) |
| 103 |
G_EXE="../../pjsip-apps/bin/pjsua-" + line.split(":= ")[1] |
| 104 |
break |
| 105 |
if G_EXE=="": |
| 106 |
print("Unable to find ../../../build.mak. Please build pjsip first") |
| 107 |
sys.exit(1) |
| 108 |
G_INUNIX = True |
| 109 |
else: |
| 110 |
if sys.platform.lower().find("win32")!=-1 or sys.platform.lower().find("microsoft")!=-1: |
| 111 |
G_INUNIX = False |
| 112 |
else: |
| 113 |
G_INUNIX = True |
| 114 |
|
| 115 |
|
| 116 |
G_EXE = G_EXE.rstrip("\n\r \t") |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
class Expect(threading.Thread): |
| 121 |
proc = None |
| 122 |
telnet = None |
| 123 |
use_telnet = False |
| 124 |
echo = False |
| 125 |
trace_enabled = False |
| 126 |
inst_param = None |
| 127 |
rh = re.compile(const.DESTROYED) |
| 128 |
ra = re.compile(const.ASSERT, re.I) |
| 129 |
rr = re.compile(const.STDOUT_REFRESH) |
| 130 |
t0 = time.time() |
| 131 |
output = "" |
| 132 |
lock = threading.Lock() |
| 133 |
running = False |
| 134 |
def __init__(self, inst_param): |
| 135 |
threading.Thread.__init__(self) |
| 136 |
self.inst_param = inst_param |
| 137 |
self.name = inst_param.name |
| 138 |
self.echo = inst_param.echo_enabled |
| 139 |
self.trace_enabled = inst_param.trace_enabled |
| 140 |
self.use_telnet = inst_param.telnet_enabled |
| 141 |
self.telnet = None |
| 142 |
|
| 143 |
def run(self): |
| 144 |
if self.use_telnet: |
| 145 |
fullcmd = G_EXE + " " + self.inst_param.arg + " --use-cli --no-cli-console --cli-telnet-port=%d" % (self.inst_param.telnet_port) |
| 146 |
self.trace("Popen " + fullcmd) |
| 147 |
self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX) |
| 148 |
|
| 149 |
|
| 150 |
t0 = time.time() |
| 151 |
while self.proc.poll() is None and self.telnet is None: |
| 152 |
try: |
| 153 |
time.sleep(0.01) |
| 154 |
self.telnet = telnetlib.Telnet('127.0.0.1', port=self.inst_param.telnet_port, timeout=60) |
| 155 |
except Exception as e: |
| 156 |
t1 = time.time() |
| 157 |
dur = int(t1 - t0) |
| 158 |
if dur > 5: |
| 159 |
raise inc.TestError(self.name + ": Timeout connecting to pjsua: " + repr(e)) |
| 160 |
|
| 161 |
self.running = True |
| 162 |
while self.proc.poll() is None: |
| 163 |
line = self.telnet.read_until(b'\n', 60) |
| 164 |
linestr = line.decode('utf-8') |
| 165 |
if linestr == "" or const.DESTROYED in linestr: |
| 166 |
break; |
| 167 |
|
| 168 |
|
| 169 |
if self.echo: |
| 170 |
try: |
| 171 |
print(self.name + ": " + linestr.rstrip()) |
| 172 |
except UnicodeEncodeError: |
| 173 |
print((self.name + ": " + linestr.rstrip()).encode('utf-8')) |
| 174 |
|
| 175 |
self.lock.acquire() |
| 176 |
self.output += linestr |
| 177 |
self.lock.release() |
| 178 |
self.running = False |
| 179 |
else: |
| 180 |
fullcmd = G_EXE + " " + self.inst_param.arg + " --stdout-refresh=5 --stdout-refresh-text=" + const.STDOUT_REFRESH |
| 181 |
if not self.inst_param.enable_buffer: |
| 182 |
fullcmd = fullcmd + " --stdout-no-buf" |
| 183 |
self.trace("Popen " + fullcmd) |
| 184 |
self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False) |
| 185 |
self.running = True |
| 186 |
while self.proc.poll() is None: |
| 187 |
line = self.proc.stdout.readline() |
| 188 |
if line == "": |
| 189 |
break; |
| 190 |
|
| 191 |
|
| 192 |
if self.echo: |
| 193 |
print(self.name + ": " + line.rstrip()) |
| 194 |
|
| 195 |
self.lock.acquire() |
| 196 |
self.output += line |
| 197 |
self.lock.release() |
| 198 |
self.running = False |
| 199 |
|
| 200 |
def send(self, cmd): |
| 201 |
self.trace("send " + cmd) |
| 202 |
if self.use_telnet: |
| 203 |
self.telnet.write((cmd + '\r\n').encode('utf-8')) |
| 204 |
else: |
| 205 |
self.proc.stdin.writelines(cmd + "\n") |
| 206 |
self.proc.stdin.flush() |
| 207 |
|
| 208 |
def expect(self, pattern, raise_on_error=True, title="", timeout=15): |
| 209 |
|
| 210 |
if self.use_telnet and pattern==const.PROMPT: |
| 211 |
return |
| 212 |
|
| 213 |
self.trace("expect " + pattern) |
| 214 |
r = re.compile(pattern, re.I) |
| 215 |
found_at = -1 |
| 216 |
t0 = time.time() |
| 217 |
while found_at < 0: |
| 218 |
self.lock.acquire() |
| 219 |
lines = self.output.splitlines() |
| 220 |
|
| 221 |
for i, line in enumerate(lines): |
| 222 |
|
| 223 |
if r.search(line) != None: |
| 224 |
found_at = i |
| 225 |
break |
| 226 |
|
| 227 |
|
| 228 |
if raise_on_error: |
| 229 |
if self.ra.search(line) != None: |
| 230 |
self.lock.release() |
| 231 |
raise inc.TestError(self.name + ": " + line) |
| 232 |
|
| 233 |
self.output = '\n'.join(lines[found_at+1:])+"\n" if found_at >= 0 else "" |
| 234 |
self.lock.release() |
| 235 |
|
| 236 |
if found_at >= 0: |
| 237 |
return line |
| 238 |
|
| 239 |
if not self.running: |
| 240 |
if raise_on_error: |
| 241 |
raise inc.TestError(self.name + ": Premature EOF") |
| 242 |
break |
| 243 |
else: |
| 244 |
t1 = time.time() |
| 245 |
dur = int(t1 - t0) |
| 246 |
if dur > timeout: |
| 247 |
self.trace("Timed-out!") |
| 248 |
if raise_on_error: |
| 249 |
raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"") |
| 250 |
break |
| 251 |
else: |
| 252 |
time.sleep(0.01) |
| 253 |
return None |
| 254 |
|
| 255 |
def get_config(self, key_config): |
| 256 |
self.send("dd") |
| 257 |
line = self.expect(key_config); |
| 258 |
return line |
| 259 |
|
| 260 |
def sync_stdout(self): |
| 261 |
if not self.use_telnet: |
| 262 |
self.trace("sync_stdout") |
| 263 |
cmd = "echo 1" + str(random.randint(1000,9999)) |
| 264 |
self.send(cmd) |
| 265 |
self.expect(cmd) |
| 266 |
|
| 267 |
def wait(self): |
| 268 |
self.trace("wait") |
| 269 |
self.join() |
| 270 |
self.proc.communicate() |
| 271 |
if self.telnet: |
| 272 |
self.telnet.close() |
| 273 |
|
| 274 |
def trace(self, s): |
| 275 |
if self.trace_enabled: |
| 276 |
now = time.time() |
| 277 |
fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]" |
| 278 |
try: |
| 279 |
print(fmt % {'time':int(now - self.t0)}) |
| 280 |
except UnicodeEncodeError: |
| 281 |
print((fmt % {'time':int(now - self.t0)}).encode('utf-8')) |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
def handle_error(errmsg, t, close_processes = True): |
| 286 |
print("====== Caught error: " + errmsg + " ======") |
| 287 |
if (close_processes): |
| 288 |
time.sleep(1) |
| 289 |
for p in t.process: |
| 290 |
|
| 291 |
try: |
| 292 |
if not p.use_telnet: |
| 293 |
p.send("q") |
| 294 |
p.send("q") |
| 295 |
else: |
| 296 |
p.send("shutdown") |
| 297 |
except: |
| 298 |
pass |
| 299 |
is_err = False |
| 300 |
try: |
| 301 |
ret = p.expect(const.DESTROYED, False) |
| 302 |
if ret is None: |
| 303 |
is_err = True |
| 304 |
except: |
| 305 |
is_err = True |
| 306 |
if is_err and p.proc.poll() is None: |
| 307 |
if sys.hexversion >= 0x02060000: |
| 308 |
p.proc.terminate() |
| 309 |
else: |
| 310 |
p.wait() |
| 311 |
else: |
| 312 |
p.wait() |
| 313 |
|
| 314 |
print("Test completed with error: " + errmsg) |
| 315 |
sys.exit(1) |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
script = util.load_module_from_file("script", inc.ARGS[0]) |
| 323 |
|
| 324 |
|
| 325 |
random.seed() |
| 326 |
|
| 327 |
|
| 328 |
if script.test == None: |
| 329 |
print("Error: no test defined") |
| 330 |
sys.exit(1) |
| 331 |
|
| 332 |
if script.test.skip: |
| 333 |
print("Test " + script.test.title + " is skipped") |
| 334 |
sys.exit(0) |
| 335 |
|
| 336 |
if len(script.test.inst_params) == 0: |
| 337 |
print("Error: test doesn't contain pjsua run descriptions") |
| 338 |
sys.exit(1) |
| 339 |
|
| 340 |
|
| 341 |
print("====== Running " + script.test.title + " ======") |
| 342 |
print("Using " + G_EXE + " as pjsua executable") |
| 343 |
|
| 344 |
for inst_param in script.test.inst_params: |
| 345 |
retry = 0 |
| 346 |
process_running = False |
| 347 |
while (not process_running) and retry < 3: |
| 348 |
p = None |
| 349 |
retry += 1 |
| 350 |
try: |
| 351 |
|
| 352 |
p = Expect(inst_param) |
| 353 |
p.start() |
| 354 |
except inc.TestError as e: |
| 355 |
handle_error(e.desc, script.test) |
| 356 |
|
| 357 |
|
| 358 |
if not p.use_telnet: |
| 359 |
while True: |
| 360 |
try: |
| 361 |
p.send("echo 1") |
| 362 |
except: |
| 363 |
time.sleep(0.1) |
| 364 |
continue |
| 365 |
break |
| 366 |
process_running = True |
| 367 |
else: |
| 368 |
t0 = time.time() |
| 369 |
while p.telnet is None: |
| 370 |
time.sleep(0.1) |
| 371 |
dur = int(time.time() - t0) |
| 372 |
if dur > 5: |
| 373 |
break |
| 374 |
process_running = p.telnet is not None |
| 375 |
|
| 376 |
|
| 377 |
if not process_running and retry < 2: |
| 378 |
time.sleep(2) |
| 379 |
|
| 380 |
if not process_running: |
| 381 |
handle_error("Failed running pjsua", script.test) |
| 382 |
|
| 383 |
|
| 384 |
script.test.process.append(p) |
| 385 |
|
| 386 |
for p in script.test.process: |
| 387 |
try: |
| 388 |
|
| 389 |
if p.inst_param.have_reg: |
| 390 |
p.send("rr") |
| 391 |
p.expect(p.inst_param.uri+".*registration success") |
| 392 |
|
| 393 |
if not p.use_telnet: |
| 394 |
p.send("") |
| 395 |
p.expect(const.PROMPT) |
| 396 |
p.send("echo 1") |
| 397 |
p.expect("echo 1") |
| 398 |
|
| 399 |
except inc.TestError as e: |
| 400 |
handle_error(e.desc, script.test) |
| 401 |
|
| 402 |
|
| 403 |
if script.test.test_func != None: |
| 404 |
try: |
| 405 |
script.test.test_func(script.test) |
| 406 |
except inc.TestError as e: |
| 407 |
handle_error(e.desc, script.test) |
| 408 |
except: |
| 409 |
handle_error("Unknown error: " + str(traceback.format_exc()), script.test) |
| 410 |
|
| 411 |
|
| 412 |
for p in script.test.process: |
| 413 |
|
| 414 |
|
| 415 |
if p.inst_param.have_reg: |
| 416 |
p.send("ru") |
| 417 |
p.expect(p.inst_param.uri+".*unregistration success") |
| 418 |
|
| 419 |
if p.use_telnet: |
| 420 |
p.send("shutdown") |
| 421 |
else: |
| 422 |
p.send("q") |
| 423 |
|
| 424 |
time.sleep(0.5) |
| 425 |
for p in script.test.process: |
| 426 |
if p.running: |
| 427 |
p.expect(const.DESTROYED, False) |
| 428 |
p.wait() |
| 429 |
|
| 430 |
|
| 431 |
if script.test.post_func != None: |
| 432 |
try: |
| 433 |
script.test.post_func(script.test) |
| 434 |
except inc.TestError as e: |
| 435 |
handle_error(e.desc, script.test, False) |
| 436 |
|
| 437 |
|
| 438 |
print("Test " + script.test.title + " completed successfully") |
| 439 |
sys.exit(0) |
| 440 |
|