- Added kam_utils test

- Updated kam_utils
- Added more tests to ngcp
squeeze-backports
Victor Seva 12 years ago
parent eda053bf69
commit 9b07ff8e33

@ -1,5 +1,6 @@
#!/usr/bin/env lua5.1
-- Kamailio Lua utils
require 'utils'
-- kamailio log for a table
function table.log(t, msg, level)
@ -13,24 +14,15 @@ function table.log(t, msg, level)
-- empty table
return
end
for i,v in pairs(t) do
if type(i) == "number" then
iformat = "%d"
elseif type(i) == "string" then
iformat = "%s"
end
if type(v) == "string" then
sr.log(level, string.format("i:" .. iformat .. " v: %s", i, v))
elseif type(v) == "number" then
sr.log(level, string.format("i:" .. iformat .. " v: %d", i, v))
elseif type(v) == "table" then
table.log(v,string.format("i:" .. iformat .. " v:", i),level)
end
end
sr.log(level, table.tostring(t))
end
-- cleans and sets string values from the table list
function sets_avps(list)
if not list then
error("list is empty")
end
local i, v
for i,v in pairs(list) do
@ -42,6 +34,9 @@ end
-- cleans and sets int values from the table list
function seti_avps(list)
if not list then
error("list is empty")
end
local i, v
for i,v in pairs(list) do
@ -51,14 +46,19 @@ function seti_avps(list)
end
end
function clean_avps(list)
if not list then
error("list is empty")
function clean_avp(obj)
if not obj then
error("obj is empty")
end
local i,v
for i,v in pairs(list) do
sr.pv.unset('$avp(' .. i .. ')[*]')
if type(obj) == "string" then
sr.pv.unset('$avp(' .. obj .. ')[*]')
elseif type(obj) == "table" then
local i,_
for i,_ in pairs(obj) do
sr.pv.unset('$avp(' .. i .. ')[*]')
end
end
end

@ -5,7 +5,8 @@ mc = lemock.controller()
srMock = {
__class__ = 'srMock',
pv = mc:mock()
pv = mc:mock(),
log = mc:mock()
}
srMock_MT = { __index = srMock, __newindex = mc:mock() }
function srMock:new()

@ -24,17 +24,25 @@ NGCPPrefs_MT = { __index = NGCPPrefs }
return t
end
function NGCPPrefs._cleanvars(t)
for k,_ in pairs(t) do
t[k] = ""
end
end
function NGCPPrefs:clean(group)
--print("NGCPPrefs:clean")
--print(table.tostring(getmetatable(self)))
--print(table.tostring(self))
if group then
if self[group] then
clean_avps(self[group])
clean_avp(self[group])
NGCPPrefs._cleanvars(self[group])
end
else
for k,v in ipairs(self.groups) do
clean_avps(self[v])
for k,v in pairs(self.groups) do
clean_avp(self[v])
NGCPPrefs._cleanvars(self[v])
end
end
end

@ -0,0 +1,48 @@
#!/usr/bin/env lua5.1
require('luaunit')
require 'mocks.sr'
require 'kam_utils'
sr = srMock:new()
TestKamUtils = {} --class
function TestKamUtils:setUp()
self.list_ids = {
id1 = 1,
id2 = 2,
id3 = 3
}
end
function TestKamUtils:test_table_log()
table.log(self.list_ids, "list ids")
table.log(self.list_ids, "list ids", "info")
table.log(nil, "list ids")
table.log(self.list_ids, "list ids", nil)
table.log(self.list_ids, nil)
end
function TestKamUtils:test_sets_avps()
assertError(sets_avps, nil)
sets_avps(self.list_ids)
end
function TestKamUtils:test_seti_avps()
assertError(seti_avps, nil)
seti_avps(self.list_ids)
end
function TestKamUtils:test_clean_avp()
assertError(clean_avp, nil)
clean_avp(self.list_ids)
clean_avp("testid")
end
-- class TestKamUtils
---- Control test output:
lu = LuaUnit
lu:setOutputType( "TAP" )
lu:setVerbosity( 1 )
lu:run()
--EOF

@ -38,12 +38,23 @@ TestNGCP = {} --class
function TestNGCP:test_prefs_init()
--print("TestNGCP:test_prefs_init")
assertTrue(self.ngcp)
assertTrue(self.ngcp.prefs)
assertTrue(self.ngcp.prefs.peer)
assertTrue(self.ngcp.prefs.domain)
end
function TestNGCP:test_peerpref_clean()
--print("TestNGCP:test_peerpref_clean")
assertTrue(self.ngcp.prefs.peer)
self.ngcp.prefs.peer.inbound.peer_peer_callee_auth_user = "usertest"
self.ngcp.prefs.peer:clean("outbound")
assertEquals(self.ngcp.prefs.peer.inbound.peer_peer_callee_auth_user, "usertest")
self.ngcp.prefs.peer:clean("inbound")
assertEquals(self.ngcp.prefs.peer.inbound.peer_peer_callee_auth_user, "")
self.ngcp.prefs.peer.inbound.peer_peer_callee_auth_user = "usertest"
assertEquals(self.ngcp.prefs.peer.inbound.peer_peer_callee_auth_user, "usertest")
self.ngcp.prefs.peer:clean()
assertEquals(self.ngcp.prefs.peer.inbound.peer_peer_callee_auth_user, "")
end
function TestNGCP:test_domainpref_clean()

Loading…
Cancel
Save