Skip to content

Commit

Permalink
[fix] Fix broken tests and minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed Jun 17, 2021
1 parent 76d3a0d commit 632102c
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files["openwrt-openwisp-monitoring/tests"] = {
ignore = {"Test.*"}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- retrieve neighbors information
local cjson = require('cjson')

local io = require('io')
local neighbors = {}
-- parse /proc/net/arp
function neighbors.parse_arp()
Expand Down
2 changes: 2 additions & 0 deletions openwrt-openwisp-monitoring/files/lib/openwisp/resources.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- retrieve resources usage
local io = require('io')

local resources = {}

function resources.parse_disk_usage()
Expand Down
16 changes: 12 additions & 4 deletions openwrt-openwisp-monitoring/tests/test_dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ local dhcp_data = require('test_files/dhcp_data')

local luaunit = require('luaunit')

function test_dhcp_leases()
local env = require('env1')
package.loaded.uci = env.uci
package.loaded.io = env.io
TestDhcp = {
setUp = function()
local env = require('env1')
package.loaded.uci = env.uci
package.loaded.io = env.io
end,
tearDown = function()
end
}


function TestDhcp.test_dhcp_leases()
local dhcp_functions = require('dhcp')

luaunit.assertEquals(dhcp_functions.get_dhcp_leases(), dhcp_data.leases)
luaunit.assertEquals(dhcp_functions.parse_dhcp_lease_file('/tmp/dhcp.leases',{}), dhcp_data.leases)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_data = {}
local test_data = {}


test_data.eth2_interface = {
Expand Down
2 changes: 1 addition & 1 deletion openwrt-openwisp-monitoring/tests/test_files/dhcp_data.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dhcp = {}
local dhcp = {}

dhcp.config = {
dnsmasq1 = {
Expand Down
12 changes: 6 additions & 6 deletions openwrt-openwisp-monitoring/tests/test_files/interface_data.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
data = {}
local test_data = {}

data.interface_data = {
test_data.interface_data = {
interface = { {
autostart = true,
available = true,
Expand Down Expand Up @@ -159,14 +159,14 @@ data.interface_data = {
} }
}

data.random_interface_address = {}
test_data.random_interface_address = {}

data.eth1_addresses = {
test_data.eth1_addresses = {
{address="10.0.2.4", family="ipv4", gateway="10.0.2.1", mask=24, proto="dhcp"},
}

data.br_mng_addresses = {
test_data.br_mng_addresses = {
{address="192.168.56.2", family="ipv4", mask=24, proto="static"},
}

return data
return test_data
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_data = {}
local test_data = {}

test_data.sample_parse_arp = {
{interface="eth1", ip="10.0.2.1", mac="52:54:00:12:35:00", state=""},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nixio_data = { {
local nixio_data = { {
addr = "00:00:00:00:00:00",
broadaddr = "00:00:00:00:00:00",
data = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openvpn={
local openvpn={
custom_config = {
[".anonymous"] = false,
[".index"] = 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_data = {}
local test_data = {}

test_data.disk_usage={
{
Expand Down
5 changes: 1 addition & 4 deletions openwrt-openwisp-monitoring/tests/test_interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package.path = package.path .. ";../files/lib/?.lua"

local luaunit = require('luaunit')

local test_file_dir = './test_files/'
local address_data = require('test_files/address_data')
local nixio_data = require('test_files/nixio_data')
local interface_data = require('test_files/interface_data')


TestInterface = {
setUp = function()
local env = require('env1')
Expand Down Expand Up @@ -37,7 +34,7 @@ function TestInterface.test_get_vpn_interfaces()
luaunit.assertEquals(interface_functions.get_vpn_interfaces(), {tun=true})
end

function test_get_addresses()
function TestInterface.test_get_addresses()
local interface_functions = require('openwisp.interfaces')
luaunit.assertEquals(interface_functions.get_addresses('random'), interface_data.random_interface_address)
luaunit.assertEquals(interface_functions.get_addresses('eth1'), interface_data.eth1_addresses)
Expand Down
29 changes: 12 additions & 17 deletions openwrt-openwisp-monitoring/tests/test_neighbors.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package.path = package.path .. ";../files/lib/?.lua"
package.path = package.path .. ";../files/lib/openwisp/?.lua"

local luaunit = require('luaunit')

local io = require('io')

local neighbor = require('neighbors')
local neighbor_data = require('test_files/neighbors_data')

local test_file_dir = './test_files/'

io.popen = function(arg)
if arg == 'cat /proc/net/arp 2> /dev/null' then
return io.open(test_file_dir .. 'parse_app.txt')
elseif arg == 'ip -json neigh 2> /dev/null' then
return io.open(test_file_dir .. 'ip_json_neigh.txt')
elseif arg == 'ip neigh 2> /dev/null' then
return io.open(test_file_dir .. 'ip_neigh.txt')
end
end

function testArpTable()
TestNeighbor = {
setUp = function()
local env = require('env1')
package.loaded.io = env.io
end,
tearDown = function()
end
}

function TestNeighbor.testArpTable()
local neighbor = require('neighbors')

luaunit.assertEquals(neighbor.parse_arp(), neighbor_data.sample_parse_arp)

Expand Down
34 changes: 15 additions & 19 deletions openwrt-openwisp-monitoring/tests/test_resources.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package.path = package.path .. ";../files/lib/?.lua"

local resources = require('resources')

local io = require('io')
package.path = package.path .. ";../files/lib/openwisp/?.lua"

local luaunit = require('luaunit')

local test_file_dir = './test_files/'

local resources_data = require('test_files/resources_data')

io.popen = function(arg)
if arg == 'df' then
return io.open(test_file_dir .. 'disk_usage.txt')
elseif arg == 'cat /proc/cpuinfo | grep -c processor' then
local f = assert(io.tmpfile())
f:write('8')
f:seek('set',0)
return f
end
end
TestResources = {
setUp = function()
local env = require('env1')
package.loaded.io = env.io
end,
tearDown = function()
end
}

function TestResources.test_disk_usage()
local resources = require('resources')

function test_disk_usage()
luaunit.assertEquals(resources.parse_disk_usage(), resources_data.disk_usage)
end

function test_get_cpus()
function TestResources.test_get_cpus()
local resources = require('resources')

luaunit.assertEquals(resources.get_cpus(), 8)
end

Expand Down
16 changes: 9 additions & 7 deletions openwrt-openwisp-monitoring/tests/test_utils.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package.path = package.path .. ";../files/lib/?.lua"
package.path = package.path .. ";../files/lib/openwisp/?.lua"

local luaunit = require('luaunit')

local utils = require('monitoring_utils')

function testSplitFunction()
TestUtils = {}

function TestUtils.testSplitFunction()
-- When pattern is present
luaunit.assertEquals(utils.split("OpenWISP","n"), {"Ope", "WISP"})
luaunit.assertEquals(utils.split("OpenWISP","WISP"), {"Open"})
Expand All @@ -13,7 +15,7 @@ function testSplitFunction()
luaunit.assertEquals(utils.split("OpenWISP","a"), {"OpenWISP"})
end

function testHasValue()
function TestUtils.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)
Expand All @@ -24,7 +26,7 @@ function testHasValue()

end

function testStartsWith()
function TestUtils.testStartsWith()
-- When string starts with the substring given
luaunit.assertEquals(utils.starts_with("OpenWISP", "Open"), true)
luaunit.assertEquals(utils.starts_with("NetJSON", "Net"), true)
Expand All @@ -33,7 +35,7 @@ function testStartsWith()
luaunit.assertEquals(utils.starts_with("OpenWISP", "Ov"), false)
end

function testTableEmpty()
function TestUtils.testTableEmpty()
-- When table is empty
luaunit.assertEquals(utils.is_table_empty(nil), true)
luaunit.assertEquals(utils.is_table_empty({}), true)
Expand All @@ -43,13 +45,13 @@ function testTableEmpty()
luaunit.assertEquals(utils.is_table_empty({"wireless", "wired", "system"}), false)
end

function testArrayConcat()
function TestUtils.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()
function TestUtils.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
Expand Down

0 comments on commit 632102c

Please sign in to comment.