From 3371655364b4d752ea8d3a691496d0c8af4587eb Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Thu, 21 Sep 2023 19:37:49 +0530 Subject: [PATCH 1/2] [fix] Use device network section for STP on OpenWrt > 21 #123 Fixes #123 --- .../lib/openwisp-monitoring/interfaces.lua | 23 +++++++- openwisp-monitoring/tests/basic_env.lua | 3 +- openwisp-monitoring/tests/main_env.lua | 6 +++ openwisp-monitoring/tests/test_dhcp.lua | 3 +- .../tests/test_files/interface_data.lua | 52 +++++++++++++++++++ openwisp-monitoring/tests/test_interfaces.lua | 12 ++++- openwisp-monitoring/tests/test_ula_prefix.lua | 7 ++- 7 files changed, 100 insertions(+), 6 deletions(-) diff --git a/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua b/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua index 30f12fec..3fd56acf 100644 --- a/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua +++ b/openwisp-monitoring/files/lib/openwisp-monitoring/interfaces.lua @@ -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 @@ -166,7 +180,14 @@ 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' + -- On OpenWrt > 21, "stp" is present in the "device" section + local device_name = interface['device'] + if device_name and network_devices[device_name] then + info.stp = network_devices[device_name]['stp'] + else + info.stp = uci_cursor.get('network', interface['interface'], 'stp') + end + info.stp = info.stp == '1' end -- collect specialized info if available local specialized_info = specialized_interfaces[interface.proto] diff --git a/openwisp-monitoring/tests/basic_env.lua b/openwisp-monitoring/tests/basic_env.lua index 648d0017..bf0a5ba3 100644 --- a/openwisp-monitoring/tests/basic_env.lua +++ b/openwisp-monitoring/tests/basic_env.lua @@ -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 } diff --git a/openwisp-monitoring/tests/main_env.lua b/openwisp-monitoring/tests/main_env.lua index 45ae7837..c842c765 100644 --- a/openwisp-monitoring/tests/main_env.lua +++ b/openwisp-monitoring/tests/main_env.lua @@ -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[3] == 'device' then + args[4]({name = 'br-lan2', stp = '1'}) + end end } end diff --git a/openwisp-monitoring/tests/test_dhcp.lua b/openwisp-monitoring/tests/test_dhcp.lua index 6c6cabe6..10c76c53 100644 --- a/openwisp-monitoring/tests/test_dhcp.lua +++ b/openwisp-monitoring/tests/test_dhcp.lua @@ -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 } diff --git a/openwisp-monitoring/tests/test_files/interface_data.lua b/openwisp-monitoring/tests/test_files/interface_data.lua index b3489fdf..447cc073 100644 --- a/openwisp-monitoring/tests/test_files/interface_data.lua +++ b/openwisp-monitoring/tests/test_files/interface_data.lua @@ -112,6 +112,45 @@ test_data.interface_data = { up = true, updated = {"addresses"}, uptime = 7738 + }, { + autostart = true, + available = true, + data = {leasetime = 86400}, + delegation = true, + device = "br-lan2", + ["dns-search"] = {}, + ["dns-server"] = {"8.8.8.8", "8.8.4.4"}, + dns_metric = 0, + dynamic = false, + inactive = { + ["dns-search"] = {}, + ["dns-server"] = {}, + ["ipv4-address"] = {}, + ["ipv6-address"] = {}, + neighbors = {}, + route = {} + }, + interface = "lan3", + ["ipv4-address"] = {{address = "192.168.1.42", mask = 24}}, + ["ipv6-address"] = {}, + ["ipv6-prefix"] = {}, + ["ipv6-prefix-assignment"] = {{address = "fd78:adb4:afb3::", mask = 60}}, + l3_device = "br-lan2", + metric = 0, + neighbors = {}, + pending = false, + proto = "dhcp", + route = { + { + target = "0.0.0.0", + mask = 0, + nexthop = "192.168.1.1", + source = "192.168.1.41/32" + } + }, + up = true, + updated = {"addresses"}, + uptime = 773875 }, { autostart = true, available = true, @@ -304,6 +343,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" diff --git a/openwisp-monitoring/tests/test_interfaces.lua b/openwisp-monitoring/tests/test_interfaces.lua index 1e556e94..cbaae868 100644 --- a/openwisp-monitoring/tests/test_interfaces.lua +++ b/openwisp-monitoring/tests/test_interfaces.lua @@ -59,7 +59,8 @@ TestNetJSON = { 'xhci-hcd.0.auto/usb2/2-1' end return nil - end + end, + foreach = function(...) return nil end } end } @@ -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 + interface_info = interface_functions.get_interface_info('br-lan2', + interface_data.br_lan2_interface) + luaunit.assertEquals(interface_info, + {dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = true}) + end function TestInterface.test_specialized_info() @@ -156,7 +164,7 @@ function TestNetJSON.test_interfaces() local netjson = cjson.decode(netjson_file('*')) luaunit.assertEquals(netjson["interfaces"][2]["mobile"]["signal"]["umts"], nil) luaunit.assertEquals(netjson["interfaces"][3]["addresses"][1]["address"], - "192.168.1.41") + "192.168.1.41") luaunit.assertEquals(netjson["interfaces"][3]["stp"], true) luaunit.assertEquals(netjson["interfaces"][2]["mobile"]["signal"]["lte"]["snr"], 19.2) diff --git a/openwisp-monitoring/tests/test_ula_prefix.lua b/openwisp-monitoring/tests/test_ula_prefix.lua index 1724de62..d522760e 100644 --- a/openwisp-monitoring/tests/test_ula_prefix.lua +++ b/openwisp-monitoring/tests/test_ula_prefix.lua @@ -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 From 65fae4c60377c78ca0ec0b634ec13f3a7788be4d Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Thu, 28 Sep 2023 00:06:58 +0530 Subject: [PATCH 2/2] [req-changes] Added test for STP false --- openwisp-monitoring/tests/main_env.lua | 1 + .../tests/test_files/interface_data.lua | 50 +++++++++++++++++++ openwisp-monitoring/tests/test_interfaces.lua | 6 +++ 3 files changed, 57 insertions(+) diff --git a/openwisp-monitoring/tests/main_env.lua b/openwisp-monitoring/tests/main_env.lua index c842c765..1392a96c 100644 --- a/openwisp-monitoring/tests/main_env.lua +++ b/openwisp-monitoring/tests/main_env.lua @@ -30,6 +30,7 @@ env.uci = { local args = {...} if args[2] == 'network' and args[3] == 'device' then args[4]({name = 'br-lan2', stp = '1'}) + args[4]({name = 'br-lan3', stp = '0'}) end end } diff --git a/openwisp-monitoring/tests/test_files/interface_data.lua b/openwisp-monitoring/tests/test_files/interface_data.lua index 447cc073..7c01eccb 100644 --- a/openwisp-monitoring/tests/test_files/interface_data.lua +++ b/openwisp-monitoring/tests/test_files/interface_data.lua @@ -151,6 +151,45 @@ test_data.interface_data = { up = true, updated = {"addresses"}, uptime = 773875 + }, { + autostart = true, + available = true, + data = {leasetime = 86400}, + delegation = true, + device = "br-lan3", + ["dns-search"] = {}, + ["dns-server"] = {"8.8.8.8", "8.8.4.4"}, + dns_metric = 0, + dynamic = false, + inactive = { + ["dns-search"] = {}, + ["dns-server"] = {}, + ["ipv4-address"] = {}, + ["ipv6-address"] = {}, + neighbors = {}, + route = {} + }, + interface = "lan3", + ["ipv4-address"] = {{address = "192.168.1.42", mask = 24}}, + ["ipv6-address"] = {}, + ["ipv6-prefix"] = {}, + ["ipv6-prefix-assignment"] = {{address = "fd78:adb4:afb3::", mask = 60}}, + l3_device = "br-lan3", + metric = 0, + neighbors = {}, + pending = false, + proto = "dhcp", + route = { + { + target = "0.0.0.0", + mask = 0, + nexthop = "192.168.1.1", + source = "192.168.1.41/32" + } + }, + up = true, + updated = {"addresses"}, + uptime = 773875 }, { autostart = true, available = true, @@ -356,6 +395,17 @@ test_data.br_lan2_interface = { up = true } +test_data.br_lan3_interface = { + bridge_members = {"lan1", "lan2"}, + mac = "00:00:00:00:00:00", + mtu = 1500, + multicast = true, + name = "br-lan3", + txqueuelen = 1000, + type = "bridge", + up = true +} + test_data.lan2_interface = { link_supported = { "10baseT-H", "10baseT-F", "100baseT-H", "100baseT-F", "1000baseT-F" diff --git a/openwisp-monitoring/tests/test_interfaces.lua b/openwisp-monitoring/tests/test_interfaces.lua index cbaae868..1579cd3e 100644 --- a/openwisp-monitoring/tests/test_interfaces.lua +++ b/openwisp-monitoring/tests/test_interfaces.lua @@ -131,10 +131,16 @@ function TestInterface.test_get_interface_info() luaunit.assertEquals(interface_info, {dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = true}) -- For OpenWrt >= 21 + -- Test STP is set to true interface_info = interface_functions.get_interface_info('br-lan2', interface_data.br_lan2_interface) luaunit.assertEquals(interface_info, {dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = true}) + -- Test STP is set false + interface_info = interface_functions.get_interface_info('br-lan3', + interface_data.br_lan3_interface) + luaunit.assertEquals(interface_info, + {dns_servers = {"8.8.8.8", "8.8.4.4"}, stp = false}) end