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

[fix] Skip adding empty DHCP lease tables #129

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ function dhcp.parse_dhcp_lease_file(path, leases)
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, {
local lease = {
expiry = tonumber(expiry),
mac = mac,
ip = ip,
client_name = name,
client_id = id
})
}
if not utils.is_table_empty(lease) then
table.insert(leases, lease)
end
end

return leases
Expand Down
36 changes: 28 additions & 8 deletions openwisp-monitoring/tests/test_dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ local cjson = require('cjson')
local luaunit = require('luaunit')
local dhcp_data = require('test_files/dhcp_data')

local dhcp_open = function(arg)
local test_file_dir = './test_files/'
if arg == '/tmp/dhcp.leases' then
return io.open(test_file_dir .. 'dhcp_leases.txt')
elseif arg == '/tmp/dhcp.leases.irregular' then
return io.open(test_file_dir .. 'dhcp_leases_irregular.txt')
else
return nil
end
end

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

TestNetJSON = {
setUp = function()
local env = require('basic_env')
local test_file_dir = './test_files/'
package.loaded.ubus = env.ubus
package.loaded.io = {
popen = function(arg)
Expand All @@ -30,13 +41,7 @@ TestNetJSON = {
f:seek('set', 0)
return f
end,
open = function(arg)
if arg == '/tmp/dhcp.leases' then
return io.open(test_file_dir .. 'dhcp_leases.txt')
else
return nil
end
end,
open = dhcp_open,
write = function(...) return nil end
}
package.loaded.uci = {
Expand Down Expand Up @@ -78,4 +83,19 @@ function TestNetJSON.test_dhcp()
luaunit.assertEquals(netjson["dhcp_leases"][1]["expiry"], 1620788343)
end

function TestDhcp.test_dhcp_leases_irregular()
local dhcp_functions = require('dhcp')
local expected = {
{
client_id = "01:c6:df:44:00:00:00",
client_name = "*",
expiry = 1705556643,
ip = "192.168.1.163",
mac = "c6:df:44:00:00:00"
}
}
local parsed = dhcp_functions.parse_dhcp_lease_file('/tmp/dhcp.leases.irregular', {})
luaunit.assertEquals(parsed, expected)
end

os.exit(luaunit.LuaUnit.run())
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1705556643 c6:df:44:00:00:00 192.168.1.163 * 01:c6:df:44:00:00:00
duid 00:01:00:01:00:00:00:00:00:a0:26:36:01:90
Loading