Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Lua tests for netjson monitoring #26

Merged
merged 23 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5d65084
[skip ci] Added tests
devkapilbansal Jun 9, 2021
7cb0653
[ci] Add LuaCheck to Github Actions #27
devkapilbansal Jun 10, 2021
55de405
[skip ci] Tests for Interfaces and DHCP added
devkapilbansal Jun 10, 2021
864b9cd
[fix] Fix broken tests and minor improvements
devkapilbansal Jun 17, 2021
81e6f74
[refactor] Fix inconsistent indentation
devkapilbansal Jun 17, 2021
cdce6fe
[ci] Add Github CI Workflow #27
devkapilbansal Jun 17, 2021
4f71cea
[tests] Add wifi tests
devkapilbansal Jun 21, 2021
358dcef
[tests] Added resource section for netjson monitoring tests
devkapilbansal Jun 28, 2021
d52fa2c
[tests] Added neighbors test for netjson monitoring
devkapilbansal Jul 3, 2021
6830323
[tests] Added dhcp tests for netjson monitoring
devkapilbansal Jul 6, 2021
f960aaa
[tests] Added wireless test data and tests for iwinfo clients
devkapilbansal Jul 6, 2021
0cb23dc
[tests] Added wireless tests for netjson monitoring
devkapilbansal Jul 7, 2021
365e2a7
[tests] Added get_interface_info, needs_inversion and invert_rx_tx tests
devkapilbansal Jul 8, 2021
14f3929
[tests] Added stats for wireless interfaces
devkapilbansal Jul 8, 2021
88bbcd6
[tests] Add specialized info tests
devkapilbansal Jul 8, 2021
fdc452f
[ci] Add code coverage and upload to coveralls
devkapilbansal Jul 11, 2021
371110d
[tests] Increase coverage for dhcp and interface functions
devkapilbansal Jul 12, 2021
81bf403
[tests] Add some tests for netjson-monitoring interfaces section
devkapilbansal Jul 13, 2021
64a9f0c
[refactor] Remove whitespace around =
devkapilbansal Jul 15, 2021
82cef9a
[refactor] Convert all indentation to two spaces
devkapilbansal Jul 15, 2021
9834b09
[docs] Add coverage and ci badge
devkapilbansal Jul 15, 2021
03944f3
[tests] Redirect stdout to nil for tests and minor improvements
devkapilbansal Jul 17, 2021
88d23f4
[refactor] Minor improvements in quality assurance docs
devkapilbansal Jul 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ jobs:
- name: QA-Checks
run: ./run-qa-checks

- name: Tests
run: ./runtests

- name: Upload Coverage
run: |
cd openwrt-openwisp-monitoring/tests
luacov-coveralls -v
env:
COVERALLS_REPO_TOKEN: ${{ github.token }}

build:
name: Build and upload package as artifacts
needs: tests
Expand Down
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.*"}
}
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ Run quality assurance tests with::

./run-qa-checks

You can run all unit tests by launching the dedicated script::
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved

./runtests
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved

Alternatively, you can run specific tests, e.g.::

cd openwrt-openwisp-monitoring/tests/
lua test_utils.lua -v

Contributing
------------
Expand Down
22 changes: 21 additions & 1 deletion install-dev.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
#!/bin/sh
set -e
feckert marked this conversation as resolved.
Show resolved Hide resolved
apt-get update
# install lua
#install cmake and git
apt-get install -y cmake git
#install lua
apt-get install -y lua5.1 liblua5.1-0-dev luarocks
#install json-c
git clone https://github.com/json-c/json-c.git --depth=1
cd json-c && cmake . && make install && cd .. || { echo 'Installing json-c failed!' ; exit 1; }
# install openwrt libubox and uci
git clone https://git.openwrt.org/project/libubox.git --depth=1
cd libubox && cmake . && make install && cd .. || { echo 'Installing libubox failed!' ; exit 1; }
git clone https://git.openwrt.org/project/uci.git --depth=1
cd uci && cmake . && make install && cd .. || { echo 'Installing uci failed!' ; exit 1; }
#install nixio
luarocks install https://raw.githubusercontent.com/Neopallium/nixio/master/nixio-scm-0.rockspec
# update links to shared libraries
ldconfig -v
# install luaunit
luarocks install luaunit
# install luacheck
luarocks install luacheck
# install lua-cjson
luarocks install lua-cjson
#install luacov-coveralls
luarocks install luacov-coveralls
#clean
rm -rf json-c libubox uci
5 changes: 3 additions & 2 deletions openwrt-openwisp-monitoring/files/lib/openwisp/dhcp.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package.path = package.path .. ";../files/lib/?.lua"

-- retrieve dhcp leases
local utils = require('openwisp.monitoring_utils')

local uci = require('uci')
local uci_cursor = uci.cursor()

local io = require('io')
local dhcp = {}

function dhcp.parse_dhcp_lease_file(path, leases)
Expand Down
10 changes: 6 additions & 4 deletions openwrt-openwisp-monitoring/files/lib/openwisp/interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local utils = require('openwisp.monitoring_utils')
local cjson = require('cjson')
local nixio = require('nixio')
local nixio_data = nixio.getifaddrs()
local io = require('io')

local uci = require('uci')
local uci_cursor = uci.cursor()
Expand All @@ -21,9 +22,9 @@ local specialized_interfaces = {
modemmanager = function(_, interface)
local modem = uci_cursor.get('network', interface['interface'], 'device')
local info = {}

local general = io.popen('mmcli --output-json -m '..modem):read("*a")
if general and pcall(function () general = cjson.decode(general) end) then
if general and pcall(cjson.decode, general) then
general = cjson.decode(general)
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
general = general.modem

if not utils.is_table_empty(general['3gpp']) then
Expand All @@ -40,8 +41,9 @@ local specialized_interfaces = {
end
end

local signal = io.popen('mmcli --output-json -m '..modem..' --signal-get'):read()
if signal and pcall(function () signal = cjson.decode(signal) end) then
local signal = io.popen('mmcli --output-json -m '..modem..' --signal-get'):read("*a")
if signal and pcall(cjson.decode, signal) then
signal = cjson.decode(signal)
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
-- only send data if not empty to avoid generating too much traffic
if not utils.is_table_empty(signal.modem) and not utils.is_table_empty(signal.modem.signal) then
-- omit refresh rate
Expand Down
2 changes: 2 additions & 0 deletions openwrt-openwisp-monitoring/files/lib/openwisp/monitoring.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package.path = package.path .. ";../files/lib/?.lua"

local monitoring = {}

monitoring.dhcp = require('openwisp.dhcp')
Expand Down
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
42 changes: 21 additions & 21 deletions openwrt-openwisp-monitoring/files/lib/openwisp/wifi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ function wifi.invert_rx_tx(interface)
end

function wifi.parse_hostapd_clients(clients)
local data = {}
for mac, properties in pairs(clients) do
properties.mac = mac
table.insert(data, properties)
end
return data
local data = {}
for mac, properties in pairs(clients) do
properties.mac = mac
table.insert(data, properties)
end
return data
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
end

function wifi.parse_iwinfo_clients(clients)
local data = {}
for _, p in pairs(clients) do
local client = {}
client.ht = p.rx.ht
client.mac = p.mac
client.authorized = p.authorized
client.vht = p.rx.vht
client.wmm = p.wme
client.mfp = p.mfp
client.auth = p.authenticated
client.signal = p.signal
client.noise = p.noise
table.insert(data, client)
end
return data
local data = {}
for _, p in pairs(clients) do
local client = {}
client.ht = p.rx.ht
client.mac = p.mac
client.authorized = p.authorized
client.vht = p.rx.vht
client.wmm = p.wme
client.mfp = p.mfp
client.auth = p.authenticated
client.signal = p.signal
client.noise = p.noise
table.insert(data, client)
end
return data
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
end

-- takes ubus wireless.status clients output and converts it to NetJSON
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env lua
-- retrieve monitoring information
-- and return it as NetJSON Output
package.path = package.path .. ";../files/lib/?.lua"

local cjson = require('cjson')
local io = require('io')

Expand All @@ -19,7 +21,6 @@ local loadavg_output = io.popen('cat /proc/loadavg'):read()
loadavg_output = monitoring.utils.split(loadavg_output, ' ')
local load_average = {tonumber(loadavg_output[1]), tonumber(loadavg_output[2]), tonumber(loadavg_output[3])}


-- init netjson data structure
local netjson = {
type = 'DeviceMonitoring',
Expand Down Expand Up @@ -48,6 +49,7 @@ if not monitoring.utils.is_table_empty(host_neighbors) then
end

-- determine the interfaces to monitor
local arg = {...}
local traffic_monitored = arg[1]
local include_stats = {}
if traffic_monitored and traffic_monitored ~= '*' then
Expand Down Expand Up @@ -185,3 +187,4 @@ if next(dns_search) ~= nil then
end

print(cjson.encode(netjson))
return cjson.encode(netjson)
5 changes: 5 additions & 0 deletions openwrt-openwisp-monitoring/tests/.luacov
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
['exclude'] = {
'luaunit$'
}
}
55 changes: 55 additions & 0 deletions openwrt-openwisp-monitoring/tests/basic_env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
local env={}
env.io = {
popen = function(arg)
if arg == 'cat /proc/loadavg' then
local f = assert(io.tmpfile())
f:write('0.37 0.95 1.23 2/873 56899\n')
f:seek('set',0)
return f
end
local f = assert(io.tmpfile())
f:write('')
f:seek('set',0)
return f
end,
open = function(arg)
return nil
end
}

env.ubus = {
connect =function()
return {
call = function(...)
local arg = {...}
if arg[2]=='system' and arg[3]=='board' then
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
return {hostname = "08-00-27-56-92-F5"}
elseif arg[2]=='system' and arg[3]=='info' then
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
return {
memory=nil,
local_time=nil,
uptime=nil,
swap=nil
}
else
return {}
end
end
}
end
}

env.uci = {
cursor = function()
return {
get_all = function(...)
return nil
end,
get = function(...)
return nil
end
}
end
}

return env
79 changes: 79 additions & 0 deletions openwrt-openwisp-monitoring/tests/main_env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package.path = package.path .. ";../files/lib/?.lua"

local env = {}
local dhcp_data = require('test_files/dhcp_data')
local test_file_dir = './test_files/'

env.uci = {
cursor = function()
return {
get_all = function(...)
local arg = {...}
if arg[2]=='dhcp' then
return dhcp_data.config
elseif arg[2] == 'openvpn' then
return require('test_files/openvpn_data')
end
end,
get = function(...)
local arg = {...}
if arg[1]=='network' and arg[3]=='ula_prefix' then
return "fdf7:0c44:27ae::/48"
elseif arg[1]=='network' and arg[3]=='stp' then
return '1'
elseif arg[1]=='network' and arg[3]=='device' then
return '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/xhci-hcd.0.auto/usb2/2-1'
end
end
}
end
}

env.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
elseif 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')
else
local modem = '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/xhci-hcd.0.auto/usb2/2-1'
if arg == 'mmcli --output-json -m '.. modem then
return io.open(test_file_dir .. 'modem_data.txt')
elseif arg == 'mmcli --output-json -m '..modem..' --signal-get' then
return io.open(test_file_dir .. 'umts_sample.txt')
end
end
end,
open = function(arg)
if arg == '/tmp/dhcp.leases' then
return io.open(test_file_dir .. 'dhcp_leases.txt')
else
return io.open(arg)
end
end
}

env.ubus = {
connect = function()
return {
call = function(...)
local arg = {...}
devkapilbansal marked this conversation as resolved.
Show resolved Hide resolved
if arg[2]=='network.interface' and arg[3]=='dump' then
local f = require('test_files/interface_data')
return f.interface_data
end
end
}
end
}

return env
Loading