Skip to content

Commit

Permalink
[test] Added automated tests for lua code using mocks + test coverage
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
devkapilbansal authored Jul 20, 2021
1 parent b21f3bd commit 91f319a
Show file tree
Hide file tree
Showing 41 changed files with 3,467 additions and 583 deletions.
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::

./runtests

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
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

0 comments on commit 91f319a

Please sign in to comment.