[#2521] Update python sample program with tests

This commit is contained in:
Alexandre Savard
2010-02-11 15:48:23 -05:00
parent e8c9c69906
commit 8767040663
4 changed files with 139 additions and 42 deletions

View File

@ -162,6 +162,8 @@
<method name="addAccount">
<!--* Add a new account to the SFLphone-daemon list. If no
details are specified, default parameters are used.
A REGISTER is automatically sent and configuration is
saved if account created successfully.
@param[in] input details
@param[out] output accountID

View File

@ -61,7 +61,8 @@ def printHelp():
--gaia Get all IAX accounts. \n\
--gcc Get current callid. \n\
--gacl Get active codec list. \n\
\n\
--sac Set accout for next call \n\
\n\
--gad <account> Get account details . \n\
--enable <account> Enable the account. \n\
--disable <account> Disable the account. \n\
@ -83,9 +84,12 @@ def printHelp():
# Option definition
try:
opts, args = getopt.getopt(sys.argv[1:],"", [ "help", "gaa", "gal", "gara", "gaea", "gasa", "gaia", "gacl", "gac", "gcc",
"hangup=", "refuse=", "hold", "unhold=", "transfer=","dtmf=", "accept=",
"gcd=", "gad=", "register=", "unregister=", "enable=", "disable=", "call=" ])
opts, args = getopt.getopt(sys.argv[1:],"",
[ "help", "gaa", "gal", "gara", "gaea", "gasa", "gaia",
"gacl", "gac", "gcc", "hangup=", "refuse=", "hold",
"unhold=", "transfer=","dtmf=", "accept=", "gcd=",
"gad=", "register=", "unregister=", "enable=", "disable=",
"call=", "sac=" ])
except getopt.GetoptError,err:
print str(err)
sys.exit(2)
@ -166,6 +170,12 @@ else:
print "Account: " + details['ACCOUNTID']
print "Peer: " + details['PEER_NAME'] + "<" + details['PEER_NUMBER'] + ">"
elif opt == "--sac":
if arg is "":
print "Must specifies the accout to be set"
else:
sflphone.setAccount(arg)
#
# call options

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python
import time
from sflphonectrlsimple import SflPhoneCtrlSimple
@ -7,40 +8,76 @@ class SflPhoneTests(SflPhoneCtrlSimple):
def test_get_allaccounts_methods(self):
print "--- getAllAccounts() ---"
for account in self.getAllAccounts():
print " " + account
print "\n"
print "--- getAllRegisteredAccounts() ---"
for account in self.getAllRegisteredAccounts():
print " " + account
print "\n"
print "--- getAllSipAccounts() ---"
for account in self.getAllSipAccounts():
print " " + account
print "\n"
print "--- getAllIaxAccounts() ---"
for account in self.getAllIaxAccounts():
print " " + account
print "\n"
# def test_codecs_methods(self):
def test_make_iptoip_call(self):
"""Make a call to a server (sipp) on port 5062"""
i = 0
while(i < 50):
# print "--- getCodecList() ---"
# for codec int self.getCodecList():
# print " " + codec
# print "\n"
callid = self.Call("sip:test@127.0.0.1:5062")
time.sleep(0.4)
self.HangUp(callid)
time.sleep(0.4)
i = i+1
def test_make_account_call(self):
"""Register an account on a remote server and make several calls"""
self.setAccount("Account:1258495784");
time.sleep(3)
i = 0
while(i < 50):
callid = self.Call("5000")
time.sleep(0.4)
self.HangUp(callid)
time.sleep(0.4)
i = i+1
sfl = SflPhoneTests()
def test_create_account(self):
"""Create a new sip fake account and remove it"""
sfl.test_get_allaccounts_methods()
CONFIG_ACCOUNT_TYPE = "Account.type"
CONFIG_ACCOUNT_ALIAS = "Account.alias"
HOSTNAME = "hostname"
USERNAME = "username"
PASSWORD = "password"
accDetails = {CONFIG_ACCOUNT_TYPE:"SIP", CONFIG_ACCOUNT_ALIAS:"myNewAccount",
HOSTNAME:"192.168.50.3", USERNAME:"431",
PASSWORD:"alexandre"}
accountID = self.addAccount(accDetails)
print "New Account ID " + accountID
time.sleep(3)
self.removeAccount(accountID)
print "Account with ID " + accountID + " removed"
sflphone = SflPhoneTests()
sflphone.test_get_allaccounts_methods()
sflphone.test_make_iptoip_call()
sflphone.test_make_account_call()
sflphone.test_create_account()

View File

@ -170,13 +170,40 @@ class SflPhoneCtrlSimple(object):
#
# Account management
#
def addAccount(self, details=None):
"""Add a new account account
Add a new account to the SFLphone-daemon. Default parameters are \
used for missing account configuration field.
Required parameters are type, alias, hostname, username and password
input details
"""
if details is None:
raise SPaccountError("Must specifies type, alias, hostname, \
username and password in \
order to create a new account")
return self.configurationmanager.addAccount(details)
def removeAccount(self, accountID=None):
"""Remove an account from internal list"""
if accountID is None:
raise SPaccountError("Account ID must be specified")
self.configurationmanager.removeAccount(accountID)
def getAllAccounts(self):
""" Return a list with all accounts"""
"""Return a list with all accounts"""
return self.configurationmanager.getAccountList()
def getAllEnabledAccounts(self):
""" Return a list with all enabled accounts"""
"""Return a list with all enabled accounts"""
accounts = self.getAllAccounts()
activeaccounts = []
for testedaccount in accounts:
@ -222,11 +249,15 @@ class SflPhoneCtrlSimple(object):
raise SPaccountError("No account matched with alias")
def setAccount(self, account):
"""Define the active account"""
"""Define the active account
The active account will be used when sending a new call
"""
if account in self.getAllAccounts():
self.account = account
else:
print account
raise SflPhoneError("Not a valid account")
def setFirstRegisteredAccount(self):
@ -315,7 +346,6 @@ class SflPhoneCtrlSimple(object):
def getAllSipAccounts(self):
"""Return a list of SIP accounts"""
sipAccountsList = []
for accountName in self.getAllAccounts():
if self.getAccountDetails(accountName)['Account.type'] == "SIP":
@ -395,25 +425,36 @@ class SflPhoneCtrlSimple(object):
# Action
#
def Call(self, dest):
"""Start a call and return a CallID"""
if not self.account:
"""Start a call and return a CallID
Use the current account previously set using setAccount().
If no account specified, first registered one in account list is used.
For phone number prefixed using SIP scheme (i.e. sip: or sips:),
IP2IP profile is automatically selected and set as the default account
return callID Newly generated callidentifier for this call
"""
if dest is None or dest == "":
raise SflPhoneError("Invalid call destination")
# Set the account to be used for this call
if dest.find('sip:') is 0 or dest.find('sips:') is 0:
print "Ip 2 IP call"
self.setAccount("IP2IP")
elif not self.account:
self.setFirstRegisteredAccount()
if not self.isAccountRegistered():
if self.account is "IP2IP" and self.isAccountRegistered():
raise SflPhoneError("Can't place a call without a registered account")
if dest is None or dest == "":
raise SflPhoneError("Invalid call destination")
# Generate a call ID for this
m = hashlib.md5()
t = long( time.time() * 1000 )
r = long( random.random()*100000000000000000L )
m.update(str(t) + str(r))
callid = m.hexdigest()
# Generate a call ID for this call
callid = self.GenerateCallID()
# Add the call to the list of active calls and set status to SENT
self.activeCalls[callid] = {'Account': self.account, 'To': dest, 'State': 'SENT' }
# Send the request to the CallManager
self.callmanager.placeCall(self.account, callid, dest)
@ -425,14 +466,14 @@ class SflPhoneCtrlSimple(object):
if not self.account:
self.setFirstRegisteredAccount()
if not self.isAccountRegistered():
raise SflPhoneError("Can't hangup a call without a registered account")
# if not self.isAccountRegistered() and self.accout is not "IP2IP":
# raise SflPhoneError("Can't hangup a call without a registered account")
if callid is None or callid == "":
pass # just to see
#raise SflPhoneError("Invalid callID")
self.callmanager.hangUp(callid)
self.callmanager.hangUp(callid)
def Transfert(self, callid, to):
@ -510,4 +551,11 @@ class SflPhoneCtrlSimple(object):
self.callmanager.playDTMF(key)
def GenerateCallID(self):
"""Generate Call ID"""
m = hashlib.md5()
t = long( time.time() * 1000 )
r = long( random.random()*100000000000000000L )
m.update(str(t) + str(r))
callid = m.hexdigest()
return callid