Skip to content

Commit

Permalink
[test] Completed utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed May 4, 2021
1 parent 10c662a commit ea55285
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 71 deletions.
55 changes: 18 additions & 37 deletions netjson-monitoring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,8 @@ uci = require('uci')
uci_cursor = uci.cursor()

neighbors_functions = require('neighbors_functions')
basic_functions = require('basic_functions')
utils = require('utils')

local function starts_with(str, start)
return str:sub(1, #start) == start
end

function is_table_empty(table_)
return not table_ or next(table_) == nil
end

function array_concat(source, destination)
table.foreach(source, function(key, value)
table.insert(destination, value)
end)
end

function dict_merge(source, destination)
table.foreach(source, function(key, value)
destination[key] = value
end)
end

function parse_dhcp_lease_file(path, leases)
local f = io.open(path, 'r')
Expand Down Expand Up @@ -116,7 +97,7 @@ iwinfo_modes = {
system_info = ubus:call('system', 'info', {})
board = ubus:call('system', 'board', {})
loadavg_output = io.popen('cat /proc/loadavg'):read()
loadavg_output = basic_functions.split(loadavg_output, ' ')
loadavg_output = utils.split(loadavg_output, ' ')
load_average = {tonumber(loadavg_output[1]), tonumber(loadavg_output[2]), tonumber(loadavg_output[3])}

function parse_disk_usage()
Expand Down Expand Up @@ -156,7 +137,7 @@ function get_vpn_interfaces()
local items = uci_cursor:get_all('openvpn')
local vpn_interfaces = {}

if is_table_empty(items) then
if utils.is_table_empty(items) then
return {}
end

Expand Down Expand Up @@ -186,20 +167,20 @@ netjson = {
}

dhcp_leases = get_dhcp_leases()
if not is_table_empty(dhcp_leases) then
if not utils.is_table_empty(dhcp_leases) then
netjson.dhcp_leases = dhcp_leases
end

neighbors = neighbors_functions.get_neighbors()
if not is_table_empty(neighbors) then
if not utils.is_table_empty(neighbors) then
netjson.neighbors = neighbors
end

-- determine the interfaces to monitor
traffic_monitored = arg[1]
include_stats = {}
if traffic_monitored then
traffic_monitored = basic_functions.split(traffic_monitored, ' ')
traffic_monitored = utils.split(traffic_monitored, ' ')
for i, name in pairs(traffic_monitored) do
include_stats[name] = true
end
Expand Down Expand Up @@ -253,13 +234,13 @@ specialized_interfaces = {
if general and pcall(function () general = cjson.decode(general) end) then
general = general.modem

if not is_table_empty(general['3gpp']) then
if not utils.is_table_empty(general['3gpp']) then
info.imei = general['3gpp'].imei
info.operator_name = general['3gpp']['operator-name']
info.operator_code = general['3gpp']['operator-code']
end

if not is_table_empty(general.generic) then
if not utils.is_table_empty(general.generic) then
info.manufacturer = general.generic.manufacturer
info.model = general.generic.model
info.connection_status = general.generic.state
Expand All @@ -270,7 +251,7 @@ specialized_interfaces = {
local signal = io.popen('mmcli --output-json -m '..modem..' --signal-get'):read()
if signal and pcall(function () signal = cjson.decode(signal) end) then
-- only send data if not empty to avoid generating too much traffic
if not is_table_empty(signal.modem) and not is_table_empty(signal.modem.signal) then
if not utils.is_table_empty(signal.modem) and not utils.is_table_empty(signal.modem.signal) then
-- omit refresh rate
signal.modem.signal.refresh = nil
info.signal = {}
Expand All @@ -281,7 +262,7 @@ specialized_interfaces = {
-- convert to number
section_values[key] = tonumber(value)
-- store in info
if is_table_empty(info[section_key]) then
if utils.is_table_empty(info[section_key]) then
info.signal[section_key] = section_values
end
end
Expand Down Expand Up @@ -321,7 +302,7 @@ function get_interface_info(name, netjson_interface)
end

-- collect interface addresses
function try.get_addresses(name)
function get_addresses(name)
addresses = {}
interface_list = interface_data['interface']
addresses_list = {}
Expand Down Expand Up @@ -357,20 +338,20 @@ function try.get_addresses(name)
proto = 'dhcp'
elseif family == 'inet6' then
family = 'ipv6'
if starts_with(addr, 'fe80') then
if utils.starts_with(addr, 'fe80') then
proto = 'static'
else
ula = uci_cursor.get('network', 'globals', 'ula_prefix')
ula_prefix = basic_functions.split(ula, '::')[1]
if starts_with(addr, ula_prefix) then
ula_prefix = utils.split(ula, '::')[1]
if utils.starts_with(addr, ula_prefix) then
proto = 'static'
else
proto = 'dhcp'
end
end
end
if family == 'ipv4' or family == 'ipv6' then
if not basic_functions.has_value(addresses_list, addr) then
if not utils.has_value(addresses_list, addr) then
table.insert(addresses, {
address = addr,
mask = nixio_data[i].prefix,
Expand Down Expand Up @@ -460,7 +441,7 @@ for name, interface in pairs(network_status) do
multicast = interface.multicast
}
if wireless_interfaces[name] then
dict_merge(wireless_interfaces[name], netjson_interface)
utils.dict_merge(wireless_interfaces[name], netjson_interface)
interface.type = netjson_interface.type
end
if interface.type == 'Network device' then
Expand Down Expand Up @@ -499,10 +480,10 @@ for name, interface in pairs(network_status) do
table.insert(interfaces, netjson_interface)
-- DNS info is independent from interface
if info.dns_servers then
array_concat(info.dns_servers, dns_servers)
utils.array_concat(info.dns_servers, dns_servers)
end
if info.dns_search then
array_concat(info.dns_search, dns_search)
utils.array_concat(info.dns_search, dns_search)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion runtests
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
set -e
cd tests/
lua test_basic_functions.lua -v
lua test_utils.lua -v
lua test_netjson_monitoring.lua -v
33 changes: 0 additions & 33 deletions tests/test_basic_functions.lua

This file was deleted.

57 changes: 57 additions & 0 deletions tests/test_utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package.path = package.path .. ";../?.lua"

local luaunit = require('luaunit')

local utils = require('../utils')

function testSplitFunction()
-- When pattern is present
luaunit.assertEquals(utils.split("OpenWISP","n"), {"Ope", "WISP"})
luaunit.assertEquals(utils.split("OpenWISP","WISP"), {"Open"})

-- When pattern is not available
luaunit.assertEquals(utils.split("OpenWISP","a"), {"OpenWISP"})
end

function testHasValue()
-- When value is present
luaunit.assertEquals(utils.has_value({2,4,5},4), true)
luaunit.assertEquals(utils.has_value({1,2,3,7,9},9), true)

-- When value is not present
luaunit.assertEquals(utils.has_value({2,4,5},3), false)
luaunit.assertEquals(utils.has_value({1,2,3,7,9},8), false)

end

function testStartsWith()
-- When string starts with the substring given
luaunit.assertEquals(utils.starts_with("OpenWISP", "Open"), true)
luaunit.assertEquals(utils.starts_with("NetJSon", "Net"), true)

-- when string doesn't starts with the substring given
luaunit.assertEquals(utils.starts_with("OpenWISP", "Ov"), false)
end

function testTableEmpty()
-- When table is empty
luaunit.assertEquals(utils.is_table_empty(nil), true)
luaunit.assertEquals(utils.is_table_empty({}), true)

-- When table is not empty
luaunit.assertEquals(utils.is_table_empty({1,2,3,4}), false)
luaunit.assertEquals(utils.is_table_empty({"wireless", "wired", "system"}), false)
end

function testArrayConcat()
luaunit.assertEquals(utils.array_concat({4,5,6},{1,2,3}), {1, 2, 3, 4, 5, 6})
luaunit.assertEquals(utils.array_concat({"wireless"},{"wired"}), {"wired", "wireless"})
luaunit.assertEquals(utils.array_concat({"system", "network"},{"firewall"}), {"firewall", "system", "network"})
end

function testDictMerge()
luaunit.assertEquals(utils.dict_merge({['1']='OpenWISP'},{['3']='NetJSon'}), {["1"]="OpenWISP", ["3"]="NetJSon"})
luaunit.assertEquals(utils.dict_merge({['1']='OpenWISP'},{['1']='NetJSon'}), {["1"]="OpenWISP"})
end

os.exit(luaunit.LuaUnit.run())
2 changes: 2 additions & 0 deletions basic_functions.lua → utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ function functions.array_concat(source, destination)
table.foreach(source, function(key, value)
table.insert(destination, value)
end)
return destination
end

function functions.dict_merge(source, destination)
table.foreach(source, function(key, value)
destination[key] = value
end)
return destination
end

return functions

0 comments on commit ea55285

Please sign in to comment.