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 all 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
12 changes: 11 additions & 1 deletion .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 Expand Up @@ -85,7 +95,7 @@ jobs:
export_default_credentials: true

- name: Upload compiled packages to downloads.openwisp.io
if: ${{ github.event_name == 'push' }}
if: ${{ github.event_name=='push' }}
run: gsutil -m rsync -r ${{ env.SRC_URL }} ${{ env.DST_URL }}
env:
SRC_URL: ${{ env.DOWNLOADS_DIR }}/${{ env.START_TIME }}/openwisp
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.*"}
}
24 changes: 22 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
openwrt-openwisp-monitoring
===========================

.. image:: https://github.com/openwisp/openwrt-openwisp-monitoring/workflows/OpenWRT%20OPENWISP%20MONITORING%20CI%20Build/badge.svg?branch=master
:target: https://github.com/openwisp/openwrt-openwisp-monitoring/actions?query=OpenWRT+OPENWISP+MONITORING+CI+Build%22
:alt: CI build status

.. image:: https://coveralls.io/repos/github/openwisp/openwrt-openwisp-monitoring/badge.svg
:target: https://coveralls.io/github/openwisp/openwrt-openwisp-monitoring
:alt: Test Coverage

.. image:: http://img.shields.io/github/release/openwisp/openwrt-openwisp-monitoring.svg
:target: https://github.com/openwisp/openwrt-openwisp-monitoring/releases

Expand Down Expand Up @@ -53,7 +61,7 @@ The following procedure illustrates how to compile all variants of *openwisp-mon

git clone https://git.openwrt.org/openwrt/openwrt.git
cd openwrt
git checkout openwrt-19.07
git checkout openwrt-21.02

# configure feeds
echo "src-git monitoring https://github.com/openwisp/openwrt-openwisp-monitoring.git" > feeds.conf
Expand Down Expand Up @@ -130,12 +138,24 @@ a look at the `install-dev.sh <https://github.com/openwisp/openwisp-config/blob/

Install test requirements::

./install-dev.sh
sudo ./install-dev.sh

Run quality assurance tests with::

#install openwisp-utils QA tools first
pip install openwisp-utils[qa]

#run QA checks before committing code
./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
63 changes: 32 additions & 31 deletions openwrt-openwisp-monitoring/files/lib/openwisp/dhcp.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
-- retrieve dhcp leases
local utils = require('openwisp.monitoring_utils')

local uci = require('uci')
local uci_cursor = uci.cursor()
package.path=package.path .. ";../files/lib/?.lua"

local dhcp = {}
-- 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)
local f = io.open(path, 'r')
if not f then
return leases
end
for line in f:lines() do
local expiry, mac, ip, name, id = line:match('(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
table.insert(leases, {
expiry = tonumber(expiry),
mac = mac,
ip = ip,
client_name = name,
client_id = id
})
end

local f=io.open(path, 'r')
if not f then
return leases
end
for line in f:lines() do
local expiry, mac, ip, name, id=line:match('(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)')
table.insert(leases, {
expiry=tonumber(expiry),
mac=mac,
ip=ip,
client_name=name,
client_id=id
})
end

return leases
end

function dhcp.get_dhcp_leases()
local dhcp_configs = uci_cursor:get_all('dhcp')
local leases = {}
local dhcp_configs=uci_cursor:get_all('dhcp')
local leases={}

if utils.is_table_empty(dhcp_configs) then
return nil
end
if utils.is_table_empty(dhcp_configs) then
return nil
end

for _, config in pairs(dhcp_configs) do
if config and config['.type'] == 'dnsmasq' and config.leasefile then
leases = dhcp.parse_dhcp_lease_file(config.leasefile, leases)
end
for _, config in pairs(dhcp_configs) do
if config and config['.type']=='dnsmasq' and config.leasefile then
leases=dhcp.parse_dhcp_lease_file(config.leasefile, leases)
end
return leases
end
return leases
end

return dhcp
Loading