Skip to content

Commit

Permalink
[fix] Use device network section for STP on OpenWrt > 21 #123
Browse files Browse the repository at this point in the history
Fixes #123
  • Loading branch information
pandafy committed Sep 21, 2023
1 parent 0f829f8 commit 93d0ea5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
21 changes: 20 additions & 1 deletion openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ function interfaces.get_addresses(name)
return addresses
end

function interfaces.get_network_devices()
local devices = {}
uci_cursor:foreach('network', 'device', function(uci_device)
local device = {}
for key, value in pairs(uci_device) do
if not string.match(key, '^%.') then device[key] = value end
end
devices[uci_device['name']] = device
end)
return devices
end

local network_devices = interfaces.get_network_devices()

function interfaces.get_interface_info(name, netjson_interface)
local info = {dns_search = nil, dns_servers = nil}
for _, interface in pairs(interface_data['interface']) do
Expand All @@ -166,7 +180,12 @@ function interfaces.get_interface_info(name, netjson_interface)
info.dns_servers = interface['dns-server']
end
if netjson_interface.type == 'bridge' then
info.stp = uci_cursor.get('network', interface['interface'], 'stp') == '1'
local device_name = interface['device']
if device_name and network_devices[device_name] then
info.stp = network_devices[device_name]['stp'] == '1' or '0'
else
info.stp = uci_cursor.get('network', interface['interface'], 'stp') == '1'
end
end
-- collect specialized info if available
local specialized_info = specialized_interfaces[interface.proto]
Expand Down
3 changes: 2 additions & 1 deletion openwisp-monitoring/tests/basic_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ env.uci = {
cursor = function()
return {
get_all = function(...) return nil end,
get = function(...) return nil end
get = function(...) return nil end,
foreach = function(...) return nil end
}
end
}
Expand Down
6 changes: 6 additions & 0 deletions openwisp-monitoring/tests/main_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ env.uci = {
return '/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/' ..
'xhci-hcd.0.auto/usb2/2-1'
end
end,
foreach = function(...)
local args = {...}
if args[2] == 'network' and args[1] == 'device' then
args[4]({name = 'br-lan2', stp = '1'})
end
end
}
end
Expand Down
3 changes: 2 additions & 1 deletion openwisp-monitoring/tests/test_dhcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ TestNetJSON = {
return nil
end
end,
get = function(...) return nil end
get = function(...) return nil end,
foreach = function(...) return nil end
}
end
}
Expand Down
13 changes: 13 additions & 0 deletions openwisp-monitoring/tests/test_files/interface_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ test_data.br_lan_interface = {
up = true
}

test_data.br_lan2_interface = {
bridge_members = {
"lan1", "lan2", "mesh0", "mesh1", "wan", "wlan0", "wlan1", "wlan2"
},
mac = "00:00:00:00:00:00",
mtu = 1500,
multicast = true,
name = "br-lan2",
txqueuelen = 1000,
type = "bridge",
up = true
}

test_data.lan2_interface = {
link_supported = {
"10baseT-H", "10baseT-F", "100baseT-H", "100baseT-F", "1000baseT-F"
Expand Down
10 changes: 9 additions & 1 deletion openwisp-monitoring/tests/test_interfaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ TestNetJSON = {
'xhci-hcd.0.auto/usb2/2-1'
end
return nil
end
end,
foreach = function(...) return nil end
}
end
}
Expand Down Expand Up @@ -123,11 +124,18 @@ function TestInterface.test_get_addresses()
end

function TestInterface.test_get_interface_info()
-- For OpenWrt < 21
local interface_functions = require('interfaces')
local interface_info = interface_functions.get_interface_info('br-lan',
interface_data.br_lan_interface)
luaunit.assertEquals(interface_info,
{dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = true})
-- For OpenWrt >= 21
-- local interface_info = interface_functions.get_interface_info('br-lan2',
-- interface_data.br_lan2_interface)
-- luaunit.assertEquals(interface_info,
-- {stp = true})

end

function TestInterface.test_specialized_info()
Expand Down
7 changes: 6 additions & 1 deletion openwisp-monitoring/tests/test_ula_prefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ TestUla = {
local env = require('main_env')
package.loaded.ubus = env.ubus
package.loaded.uci = {
cursor = function() return {get = function(...) return nil end} end
cursor = function()
return {
get = function(...) return nil end,
foreach = function(...) return nil end
}
end
}
package.loaded.nixio = {
getifaddrs = function() return require('test_files/nixio_data') end
Expand Down

0 comments on commit 93d0ea5

Please sign in to comment.