2008-06-15 19:43:43 +00:00
|
|
|
import sys
|
|
|
|
import imp
|
|
|
|
import re
|
2008-06-16 13:04:44 +00:00
|
|
|
import os
|
2008-06-15 19:43:43 +00:00
|
|
|
import subprocess
|
2008-06-16 13:04:44 +00:00
|
|
|
import random
|
2020-03-02 02:48:54 +00:00
|
|
|
import telnetlib
|
2008-06-15 19:43:43 +00:00
|
|
|
import time
|
2015-04-13 12:28:02 +00:00
|
|
|
import threading
|
|
|
|
import traceback
|
2008-06-27 21:12:12 +00:00
|
|
|
import getopt
|
2008-06-15 19:43:43 +00:00
|
|
|
|
2008-06-27 21:12:12 +00:00
|
|
|
import inc_cfg as inc
|
2020-03-02 02:48:54 +00:00
|
|
|
import inc_const as const
|
2008-06-27 21:12:12 +00:00
|
|
|
|
|
|
|
# Vars
|
2020-03-02 02:48:54 +00:00
|
|
|
G_EXE = "" # pjsua executable path
|
|
|
|
G_INUNIX = False # flags that test is running in Unix
|
2008-06-27 21:12:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Usage string
|
|
|
|
usage = \
|
|
|
|
"""
|
|
|
|
run.py - Automated test driver
|
|
|
|
|
|
|
|
Usage:
|
2020-03-02 02:48:54 +00:00
|
|
|
run.py [options] MODULE CONFIG
|
2008-06-27 21:12:12 +00:00
|
|
|
Options:
|
2020-03-02 02:48:54 +00:00
|
|
|
--exe, -e pjsua executable path
|
|
|
|
--null-audio, -n use null audio
|
2008-06-27 21:12:12 +00:00
|
|
|
Sample:
|
2020-03-02 02:48:54 +00:00
|
|
|
run.py -n mod_run.py scripts-run/100_simple.py
|
2008-06-27 21:12:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "hne:", ["help", "null-audio", "exe="])
|
|
|
|
except getopt.GetoptError, err:
|
|
|
|
print str(err)
|
|
|
|
print usage
|
|
|
|
sys.exit(2)
|
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-h", "--help"):
|
2020-03-02 02:48:54 +00:00
|
|
|
print usage
|
|
|
|
sys.exit()
|
2008-06-27 21:12:12 +00:00
|
|
|
elif o in ("-n", "--null-audio"):
|
|
|
|
inc.HAS_SND_DEV = 0
|
|
|
|
elif o in ("-e", "--exe"):
|
|
|
|
G_EXE = a
|
|
|
|
else:
|
|
|
|
print "Unknown options"
|
2020-03-02 02:48:54 +00:00
|
|
|
sys.exit(2)
|
2008-06-27 21:12:12 +00:00
|
|
|
|
|
|
|
if len(args) != 2:
|
2020-03-02 02:48:54 +00:00
|
|
|
print "Invalid arguments"
|
|
|
|
print usage
|
|
|
|
sys.exit(2)
|
2008-06-27 21:12:12 +00:00
|
|
|
|
|
|
|
# Set global ARGS to be used by modules
|
|
|
|
inc.ARGS = args
|
2008-06-15 19:43:43 +00:00
|
|
|
|
2008-06-16 13:04:44 +00:00
|
|
|
# Get the pjsua executable name
|
2008-06-27 21:12:12 +00:00
|
|
|
if G_EXE == "":
|
2020-03-02 02:48:54 +00:00
|
|
|
if sys.platform.find("win32")!=-1:
|
|
|
|
EXE_DIR = "../../pjsip-apps/bin/"
|
|
|
|
EXECUTABLES = [ "pjsua_vc6d.exe",
|
|
|
|
"pjsua_vc6.exe",
|
|
|
|
"pjsua-i386-Win32-vc8-Debug.exe",
|
|
|
|
"pjsua-i386-Win32-vc8-Debug-Dynamic.exe",
|
|
|
|
"pjsua-i386-Win32-vc8-Debug-Static.exe",
|
|
|
|
"pjsua-i386-Win32-vc8-Release.exe",
|
|
|
|
"pjsua-i386-Win32-vc8-Release-Dynamic.exe",
|
2022-02-24 07:46:01 +00:00
|
|
|
"pjsua-i386-Win32-vc8-Release-Static.exe",
|
|
|
|
"pjsua-i386-Win32-vc14-Debug.exe",
|
|
|
|
"pjsua-i386-Win32-vc14-Debug-Dynamic.exe",
|
|
|
|
"pjsua-i386-Win32-vc14-Debug-Static.exe",
|
|
|
|
"pjsua-i386-Win32-vc14-Release.exe",
|
|
|
|
"pjsua-i386-Win32-vc14-Release-Dynamic.exe",
|
|
|
|
"pjsua-i386-Win32-vc14-Release-Static.exe"
|
2020-03-02 02:48:54 +00:00
|
|
|
]
|
|
|
|
e_ts = 0
|
|
|
|
for e in EXECUTABLES:
|
|
|
|
e = EXE_DIR + e
|
|
|
|
if os.access(e, os.F_OK):
|
|
|
|
st = os.stat(e)
|
|
|
|
if e_ts==0 or e_ts<st.st_mtime:
|
|
|
|
G_EXE = e
|
|
|
|
e_ts = st.st_mtime
|
|
|
|
|
|
|
|
if G_EXE=="":
|
|
|
|
print "Unable to find valid pjsua. Please build pjsip first"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
G_INUNIX = False
|
|
|
|
else:
|
|
|
|
f = open("../../build.mak", "r")
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
if line.find("TARGET_NAME")!=-1:
|
|
|
|
print line
|
|
|
|
G_EXE="../../pjsip-apps/bin/pjsua-" + line.split(":= ")[1]
|
|
|
|
break
|
|
|
|
if G_EXE=="":
|
|
|
|
print "Unable to find ../../../build.mak. Please build pjsip first"
|
|
|
|
sys.exit(1)
|
|
|
|
G_INUNIX = True
|
2010-04-20 09:52:54 +00:00
|
|
|
else:
|
2020-03-02 02:48:54 +00:00
|
|
|
if sys.platform.lower().find("win32")!=-1 or sys.platform.lower().find("microsoft")!=-1:
|
|
|
|
G_INUNIX = False
|
|
|
|
else:
|
|
|
|
G_INUNIX = True
|
2008-06-16 13:04:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
G_EXE = G_EXE.rstrip("\n\r \t")
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
###################################
|
|
|
|
# Poor man's 'expect'-like class
|
2015-04-13 12:28:02 +00:00
|
|
|
class Expect(threading.Thread):
|
2020-03-02 02:48:54 +00:00
|
|
|
proc = None
|
|
|
|
telnet = None
|
|
|
|
use_telnet = False
|
|
|
|
echo = False
|
|
|
|
trace_enabled = False
|
|
|
|
inst_param = None
|
|
|
|
rh = re.compile(const.DESTROYED)
|
|
|
|
ra = re.compile(const.ASSERT, re.I)
|
|
|
|
rr = re.compile(const.STDOUT_REFRESH)
|
|
|
|
t0 = time.time()
|
|
|
|
output = ""
|
|
|
|
lock = threading.Lock()
|
|
|
|
running = False
|
|
|
|
def __init__(self, inst_param):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.inst_param = inst_param
|
|
|
|
self.name = inst_param.name
|
|
|
|
self.echo = inst_param.echo_enabled
|
|
|
|
self.trace_enabled = inst_param.trace_enabled
|
|
|
|
self.use_telnet = inst_param.telnet_enabled
|
|
|
|
self.telnet = None
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.use_telnet:
|
2021-07-08 10:14:23 +00:00
|
|
|
fullcmd = G_EXE + " " + self.inst_param.arg + " --use-cli --no-cli-console --cli-telnet-port=%d" % (self.inst_param.telnet_port)
|
2020-03-02 02:48:54 +00:00
|
|
|
self.trace("Popen " + fullcmd)
|
|
|
|
self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX)
|
2020-03-03 03:17:27 +00:00
|
|
|
|
|
|
|
# start telnet-ing to pjsua, raise exception if telnet fails after 5s
|
|
|
|
t0 = time.time()
|
2020-03-04 02:11:37 +00:00
|
|
|
while self.proc.poll() is None and self.telnet is None:
|
2020-03-03 03:17:27 +00:00
|
|
|
try:
|
|
|
|
time.sleep(0.01)
|
|
|
|
self.telnet = telnetlib.Telnet('127.0.0.1', port=self.inst_param.telnet_port, timeout=60)
|
|
|
|
except Exception as e:
|
|
|
|
t1 = time.time()
|
|
|
|
dur = int(t1 - t0)
|
|
|
|
if dur > 5:
|
|
|
|
raise inc.TestError(self.name + ": Timeout connecting to pjsua: " + repr(e))
|
|
|
|
|
2020-03-02 02:48:54 +00:00
|
|
|
self.running = True
|
2020-03-04 02:11:37 +00:00
|
|
|
while self.proc.poll() is None:
|
2020-03-02 02:48:54 +00:00
|
|
|
line = self.telnet.read_until('\n', 60)
|
|
|
|
if line == "" or const.DESTROYED in line:
|
|
|
|
break;
|
|
|
|
|
|
|
|
#Print the line if echo is ON
|
|
|
|
if self.echo:
|
|
|
|
print self.name + ": " + line.rstrip()
|
|
|
|
|
|
|
|
self.lock.acquire()
|
|
|
|
self.output += line
|
|
|
|
self.lock.release()
|
|
|
|
self.running = False
|
|
|
|
else:
|
2021-07-08 10:14:23 +00:00
|
|
|
fullcmd = G_EXE + " " + self.inst_param.arg + " --stdout-refresh=5 --stdout-refresh-text=" + const.STDOUT_REFRESH
|
|
|
|
if not self.inst_param.enable_buffer:
|
2020-03-02 02:48:54 +00:00
|
|
|
fullcmd = fullcmd + " --stdout-no-buf"
|
|
|
|
self.trace("Popen " + fullcmd)
|
|
|
|
self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False)
|
|
|
|
self.running = True
|
2020-03-04 02:11:37 +00:00
|
|
|
while self.proc.poll() is None:
|
2020-03-02 02:48:54 +00:00
|
|
|
line = self.proc.stdout.readline()
|
|
|
|
if line == "":
|
|
|
|
break;
|
|
|
|
|
|
|
|
#Print the line if echo is ON
|
|
|
|
if self.echo:
|
|
|
|
print self.name + ": " + line.rstrip()
|
|
|
|
|
|
|
|
self.lock.acquire()
|
|
|
|
self.output += line
|
|
|
|
self.lock.release()
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def send(self, cmd):
|
|
|
|
self.trace("send " + cmd)
|
|
|
|
if self.use_telnet:
|
|
|
|
self.telnet.write(cmd + '\r\n')
|
|
|
|
else:
|
|
|
|
self.proc.stdin.writelines(cmd + "\n")
|
|
|
|
self.proc.stdin.flush()
|
|
|
|
|
2021-02-15 03:18:02 +00:00
|
|
|
def expect(self, pattern, raise_on_error=True, title="", timeout=15):
|
2020-03-02 02:48:54 +00:00
|
|
|
# no prompt for telnet
|
|
|
|
if self.use_telnet and pattern==const.PROMPT:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.trace("expect " + pattern)
|
|
|
|
r = re.compile(pattern, re.I)
|
|
|
|
found_at = -1
|
|
|
|
t0 = time.time()
|
|
|
|
while found_at < 0:
|
|
|
|
self.lock.acquire()
|
|
|
|
lines = self.output.splitlines()
|
|
|
|
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
# Search for expected text
|
|
|
|
if r.search(line) != None:
|
|
|
|
found_at = i
|
|
|
|
break
|
|
|
|
|
|
|
|
# Trap assertion error
|
|
|
|
if raise_on_error:
|
|
|
|
if self.ra.search(line) != None:
|
|
|
|
self.lock.release()
|
|
|
|
raise inc.TestError(self.name + ": " + line)
|
|
|
|
|
2020-12-11 02:40:57 +00:00
|
|
|
self.output = '\n'.join(lines[found_at+1:])+"\n" if found_at >= 0 else ""
|
2020-03-02 02:48:54 +00:00
|
|
|
self.lock.release()
|
|
|
|
|
|
|
|
if found_at >= 0:
|
|
|
|
return line
|
|
|
|
|
|
|
|
if not self.running:
|
|
|
|
if raise_on_error:
|
|
|
|
raise inc.TestError(self.name + ": Premature EOF")
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
t1 = time.time()
|
|
|
|
dur = int(t1 - t0)
|
2021-02-15 03:18:02 +00:00
|
|
|
if dur > timeout:
|
2020-03-02 02:48:54 +00:00
|
|
|
self.trace("Timed-out!")
|
|
|
|
if raise_on_error:
|
|
|
|
raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"")
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
time.sleep(0.01)
|
|
|
|
return None
|
|
|
|
|
2022-03-24 05:53:03 +00:00
|
|
|
def get_config(self, key_config):
|
|
|
|
self.send("dd")
|
|
|
|
line = self.expect(key_config);
|
|
|
|
return line
|
2020-03-02 02:48:54 +00:00
|
|
|
|
|
|
|
def sync_stdout(self):
|
|
|
|
if not self.use_telnet:
|
|
|
|
self.trace("sync_stdout")
|
|
|
|
cmd = "echo 1" + str(random.randint(1000,9999))
|
|
|
|
self.send(cmd)
|
|
|
|
self.expect(cmd)
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
self.trace("wait")
|
|
|
|
self.join()
|
|
|
|
self.proc.communicate()
|
|
|
|
if self.telnet:
|
|
|
|
self.telnet.close()
|
|
|
|
|
|
|
|
def trace(self, s):
|
|
|
|
if self.trace_enabled:
|
|
|
|
now = time.time()
|
|
|
|
fmt = self.name + ": " + "================== " + s + " ==================" + " [at t=%(time)03d]"
|
|
|
|
print fmt % {'time':int(now - self.t0)}
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
#########################
|
|
|
|
# Error handling
|
2008-06-20 17:43:55 +00:00
|
|
|
def handle_error(errmsg, t, close_processes = True):
|
2020-03-02 02:48:54 +00:00
|
|
|
print "====== Caught error: " + errmsg + " ======"
|
|
|
|
if (close_processes):
|
|
|
|
time.sleep(1)
|
|
|
|
for p in t.process:
|
|
|
|
# Protect against 'Broken pipe' exception
|
|
|
|
try:
|
|
|
|
if not p.use_telnet:
|
|
|
|
p.send("q")
|
|
|
|
p.send("q")
|
|
|
|
else:
|
|
|
|
p.send("shutdown")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
is_err = False
|
|
|
|
try:
|
|
|
|
ret = p.expect(const.DESTROYED, False)
|
2020-03-04 02:11:37 +00:00
|
|
|
if ret is None:
|
2020-03-02 02:48:54 +00:00
|
|
|
is_err = True
|
|
|
|
except:
|
|
|
|
is_err = True
|
2020-03-04 02:11:37 +00:00
|
|
|
if is_err and p.proc.poll() is None:
|
2020-03-02 02:48:54 +00:00
|
|
|
if sys.hexversion >= 0x02060000:
|
|
|
|
p.proc.terminate()
|
|
|
|
else:
|
|
|
|
p.wait()
|
|
|
|
else:
|
|
|
|
p.wait()
|
2020-03-04 02:11:37 +00:00
|
|
|
|
2020-03-02 02:48:54 +00:00
|
|
|
print "Test completed with error: " + errmsg
|
|
|
|
sys.exit(1)
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
#########################
|
2020-03-02 02:48:54 +00:00
|
|
|
# MAIN
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
# Import the test script
|
2008-06-27 21:12:12 +00:00
|
|
|
script = imp.load_source("script", inc.ARGS[0])
|
2008-06-15 19:43:43 +00:00
|
|
|
|
2008-06-16 13:04:44 +00:00
|
|
|
# Init random seed
|
|
|
|
random.seed()
|
|
|
|
|
2008-06-15 19:43:43 +00:00
|
|
|
# Validate
|
|
|
|
if script.test == None:
|
2020-03-02 02:48:54 +00:00
|
|
|
print "Error: no test defined"
|
|
|
|
sys.exit(1)
|
2008-06-15 19:43:43 +00:00
|
|
|
|
2008-06-26 12:26:52 +00:00
|
|
|
if script.test.skip:
|
2020-03-02 02:48:54 +00:00
|
|
|
print "Test " + script.test.title + " is skipped"
|
|
|
|
sys.exit(0)
|
2008-06-26 12:26:52 +00:00
|
|
|
|
2008-06-15 19:43:43 +00:00
|
|
|
if len(script.test.inst_params) == 0:
|
2020-03-02 02:48:54 +00:00
|
|
|
print "Error: test doesn't contain pjsua run descriptions"
|
|
|
|
sys.exit(1)
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
# Instantiate pjsuas
|
|
|
|
print "====== Running " + script.test.title + " ======"
|
2008-06-16 13:04:44 +00:00
|
|
|
print "Using " + G_EXE + " as pjsua executable"
|
|
|
|
|
2008-06-15 19:43:43 +00:00
|
|
|
for inst_param in script.test.inst_params:
|
2021-07-08 10:14:23 +00:00
|
|
|
retry = 0
|
|
|
|
process_running = False
|
|
|
|
while (not process_running) and retry < 3:
|
|
|
|
p = None
|
|
|
|
retry += 1
|
|
|
|
try:
|
|
|
|
# Create pjsua's Expect instance from the param
|
|
|
|
p = Expect(inst_param)
|
|
|
|
p.start()
|
|
|
|
except inc.TestError, e:
|
|
|
|
handle_error(e.desc, script.test)
|
|
|
|
|
|
|
|
# wait process ready
|
|
|
|
if not p.use_telnet:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
p.send("echo 1")
|
|
|
|
except:
|
|
|
|
time.sleep(0.1)
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
process_running = True
|
|
|
|
else:
|
|
|
|
t0 = time.time()
|
|
|
|
while p.telnet is None:
|
2020-03-02 02:48:54 +00:00
|
|
|
time.sleep(0.1)
|
2021-07-08 10:14:23 +00:00
|
|
|
dur = int(time.time() - t0)
|
|
|
|
if dur > 5:
|
|
|
|
break
|
|
|
|
process_running = p.telnet is not None
|
|
|
|
|
|
|
|
# wait before retrying
|
|
|
|
if not process_running and retry < 2:
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
if not process_running:
|
|
|
|
handle_error("Failed running pjsua", script.test)
|
2020-03-02 02:48:54 +00:00
|
|
|
|
|
|
|
# add running instance
|
|
|
|
script.test.process.append(p)
|
2015-04-13 12:28:02 +00:00
|
|
|
|
|
|
|
for p in script.test.process:
|
2020-03-02 02:48:54 +00:00
|
|
|
try:
|
|
|
|
# Wait until registration completes
|
|
|
|
if p.inst_param.have_reg:
|
2020-10-24 10:24:50 +00:00
|
|
|
p.send("rr")
|
2020-03-02 02:48:54 +00:00
|
|
|
p.expect(p.inst_param.uri+".*registration success")
|
|
|
|
# Synchronize stdout
|
|
|
|
if not p.use_telnet:
|
|
|
|
p.send("")
|
|
|
|
p.expect(const.PROMPT)
|
|
|
|
p.send("echo 1")
|
|
|
|
p.expect("echo 1")
|
|
|
|
|
|
|
|
except inc.TestError, e:
|
|
|
|
handle_error(e.desc, script.test)
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
# Run the test function
|
|
|
|
if script.test.test_func != None:
|
2020-03-02 02:48:54 +00:00
|
|
|
try:
|
|
|
|
script.test.test_func(script.test)
|
|
|
|
except inc.TestError, e:
|
|
|
|
handle_error(e.desc, script.test)
|
|
|
|
except:
|
|
|
|
handle_error("Unknown error: " + str(traceback.format_exc()), script.test)
|
2008-06-15 19:43:43 +00:00
|
|
|
|
|
|
|
# Shutdown all instances
|
|
|
|
for p in script.test.process:
|
2020-03-02 02:48:54 +00:00
|
|
|
# Unregister if we have_reg to make sure that next tests
|
2020-10-24 10:24:50 +00:00
|
|
|
# won't fail
|
2020-03-02 02:48:54 +00:00
|
|
|
if p.inst_param.have_reg:
|
|
|
|
p.send("ru")
|
|
|
|
p.expect(p.inst_param.uri+".*unregistration success")
|
|
|
|
|
|
|
|
if p.use_telnet:
|
|
|
|
p.send("shutdown")
|
|
|
|
else:
|
|
|
|
p.send("q")
|
2015-04-13 12:28:02 +00:00
|
|
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
for p in script.test.process:
|
2020-03-02 02:48:54 +00:00
|
|
|
if p.running:
|
|
|
|
p.expect(const.DESTROYED, False)
|
|
|
|
p.wait()
|
2008-06-15 19:43:43 +00:00
|
|
|
|
2008-06-18 21:04:14 +00:00
|
|
|
# Run the post test function
|
|
|
|
if script.test.post_func != None:
|
2020-03-02 02:48:54 +00:00
|
|
|
try:
|
|
|
|
script.test.post_func(script.test)
|
|
|
|
except inc.TestError, e:
|
|
|
|
handle_error(e.desc, script.test, False)
|
2008-06-18 21:04:14 +00:00
|
|
|
|
2008-06-15 19:43:43 +00:00
|
|
|
# Done
|
|
|
|
print "Test " + script.test.title + " completed successfully"
|
|
|
|
sys.exit(0)
|
|
|
|
|