From dcf0b7d5ada317b7e5abd082f7bc22ce609087ae Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Wed, 17 Jan 2024 18:53:03 -0300 Subject: [PATCH] [fix] Skip adding empty DHCP lease tables --- .../files/lib/openwisp-monitoring/dhcp.lua | 7 ++-- openwisp-monitoring/tests/test_dhcp.lua | 36 ++++++++++++++----- .../test_files/dhcp_leases_irregular.txt | 2 ++ 3 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt diff --git a/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua b/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua index 1edffd97..af4f0d71 100644 --- a/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua +++ b/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua @@ -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 diff --git a/openwisp-monitoring/tests/test_dhcp.lua b/openwisp-monitoring/tests/test_dhcp.lua index 10c76c53..700acd06 100644 --- a/openwisp-monitoring/tests/test_dhcp.lua +++ b/openwisp-monitoring/tests/test_dhcp.lua @@ -5,11 +5,23 @@ 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 } @@ -17,7 +29,6 @@ TestDhcp = { 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) @@ -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 = { @@ -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()) diff --git a/openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt b/openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt new file mode 100644 index 00000000..d83fc9aa --- /dev/null +++ b/openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt @@ -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