Skip to content

Commit

Permalink
[wip] Tests for dhcp info and interface address
Browse files Browse the repository at this point in the history
* Test Files added
  • Loading branch information
devkapilbansal committed May 9, 2021
1 parent 2d35e5b commit 277e109
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 25 deletions.
102 changes: 102 additions & 0 deletions address.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
utils = require('utils')
nixio = require('nixio')
ubus_lib = require('ubus')

ubus = ubus_lib.connect()
-- if not ubus then
-- error('Failed to connect to ubusd')
-- end

interface_data = ubus:call('network.interface', 'dump', {})
nixio_data = nixio.getifaddrs()

function find_default_gateway(routes)
for i = 1, #routes do
if routes[i].target == '0.0.0.0' then
return routes[i].nexthop
end
end
return nil
end

function new_address_array(address, interface, family)
proto = interface['proto']
if proto == 'dhcpv6' then
proto = 'dhcp'
end
new_address = {
address = address['address'],
mask = address['mask'],
proto = proto,
family = family,
gateway = find_default_gateway(interface.route)
}
return new_address
end

-- collect interface addresses
function get_addresses(name)
addresses = {}
interface_list = interface_data['interface']
addresses_list = {}
for _, interface in pairs(interface_list) do
if interface['l3_device'] == name then
proto = interface['proto']
if proto == 'dhcpv6' then
proto = 'dhcp'
end
for _, address in pairs(interface['ipv4-address']) do
table.insert(addresses_list, address['address'])
new_address = new_address_array(address, interface, 'ipv4')
table.insert(addresses, new_address)
end
for _, address in pairs(interface['ipv6-address']) do
table.insert(addresses_list, address['address'])
new_address = new_address_array(address, interface, 'ipv6')
table.insert(addresses, new_address)
end
end
end
for i = 1, #nixio_data do
if nixio_data[i].name == name then
if not is_excluded(name) then
family = nixio_data[i].family
addr = nixio_data[i].addr
if family == 'inet' then
family = 'ipv4'
-- Since we don't already know this from the dump, we can
-- consider this dynamically assigned, this is the case for
-- example for OpenVPN interfaces, which get their address
-- from the DHCP server embedded in OpenVPN
proto = 'dhcp'
elseif family == 'inet6' then
family = 'ipv6'
if utils.starts_with(addr, 'fe80') then
proto = 'static'
else
ula = uci_cursor.get('network', 'globals', 'ula_prefix')
ula_prefix = utils.split(ula, '::')[1]
if utils.starts_with(addr, ula_prefix) then
proto = 'static'
else
proto = 'dhcp'
end
end
end
if family == 'ipv4' or family == 'ipv6' then
if not utils.has_value(addresses_list, addr) then
table.insert(addresses, {
address = addr,
mask = nixio_data[i].prefix,
proto = proto,
family = family
})
end
end
end
end
end
return addresses
end

print(get_addresses('eth2'))
42 changes: 42 additions & 0 deletions dhcp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
uci = require('uci')
uci_cursor = uci.cursor()

utils = require('utils')

function parse_dhcp_lease_file(path, leases)
local f = io.open('tests/test_files/dhcp_leases.txt', '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 get_dhcp_leases()
local dhcp_configs = uci_cursor:get_all('dhcp')
local leases = {}

if utils.is_table_empty(dhcp_configs) then
return nil
end

for name, config in pairs(dhcp_configs) do
if config and config['.type'] == 'dnsmasq' and config.leasefile then
leases = parse_dhcp_lease_file(config.leasefile, leases)
end
end
return leases
end

print(get_dhcp_leases())
26 changes: 1 addition & 25 deletions netjson-monitoring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function get_dhcp_leases()
local dhcp_configs = uci_cursor:get_all('dhcp')
local leases = {}

if not dhcp_configs or not next(dhcp_configs) then
if utils.is_table_empty(dhcp_configs) then
return nil
end

Expand Down Expand Up @@ -190,15 +190,6 @@ function is_excluded(name)
return name == 'lo'
end

function find_default_gateway(routes)
for i = 1, #routes do
if routes[i].target == '0.0.0.0' then
return routes[i].nexthop
end
end
return nil
end

-- collect device data
network_status = ubus:call('network.device', 'status', {})
wireless_status = ubus:call('network.wireless', 'status', {})
Expand All @@ -210,21 +201,6 @@ interfaces = {}
dns_servers = {}
dns_search = {}

function new_address_array(address, interface, family)
proto = interface['proto']
if proto == 'dhcpv6' then
proto = 'dhcp'
end
new_address = {
address = address['address'],
mask = address['mask'],
proto = proto,
family = family,
gateway = find_default_gateway(interface.route)
}
return new_address
end

specialized_interfaces = {
modemmanager = function(name, interface)
local modem = uci_cursor.get('network', interface['interface'], 'device')
Expand Down
110 changes: 110 additions & 0 deletions tests/test_address.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package.path = package.path .. ";../?.lua"

local address_data = require('test_files/address_data')

local luaunit = require('luaunit')

local test_file_dir = './test_files/'

local interface_data = io.open('test_files/interface_data.lua'):read('*a')

routes = { {
mask = 0,
nexthop = "192.168.0.1",
source = "192.168.0.144/32",
target = "0.0.0.0"
} }

ipv4_address = {
address = "192.168.0.144",
mask = 24
}

interface = {
autostart = true,
available = true,
data = {
hostname = "08-00-27-4F-CB-2E",
leasetime = 86400
},
delegation = true,
device = "eth2",
["dns-search"] = {},
["dns-server"] = { "192.168.0.1" },
dns_metric = 0,
dynamic = false,
inactive = {
["dns-search"] = {},
["dns-server"] = {},
["ipv4-address"] = {},
["ipv6-address"] = {},
neighbors = {},
route = {}
},
interface = "lan",
["ipv4-address"] = { {
address = "192.168.0.144",
mask = 24
} },
["ipv6-address"] = {},
["ipv6-prefix"] = {},
["ipv6-prefix-assignment"] = {},
l3_device = "eth2",
metric = 0,
neighbors = {},
pending = false,
proto = "dhcp",
route = { {
mask = 0,
nexthop = "192.168.0.1",
source = "192.168.0.144/32",
target = "0.0.0.0"
} },
up = true,
updated = { "addresses", "routes", "data" },
uptime = 1973
}

address_array = {
address="192.168.0.144",
family="ipv4",
gateway="192.168.0.1",
mask=24,
proto="dhcp"
}

-- to be removed once address.lua is connected
function find_default_gateway(routes)
for i = 1, #routes do
if routes[i].target == '0.0.0.0' then
return routes[i].nexthop
end
end
return nil
end

-- remove once address.lua is connected
function new_address_array(address, interface, family)
proto = interface['proto']
if proto == 'dhcpv6' then
proto = 'dhcp'
end
new_address = {
address = address['address'],
mask = address['mask'],
proto = proto,
family = family,
gateway = find_default_gateway(interface.route)
}
return new_address
end

function test_find_default_gateway()
luaunit.assertEquals(find_default_gateway(address_data.routes), "192.168.0.1")
end

function test_new_address_array()
luaunit.assertEquals(new_address_array(address_data.ipv4_address, address_data.eth2_interface, 'ipv4'), address_data.address_array)
end

os.exit( luaunit.LuaUnit.run() )
24 changes: 24 additions & 0 deletions tests/test_dhcp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package.path = package.path .. ";../?.lua"

local luaunit = require('luaunit')

local dhcp = require('dhcp')

local test_file_dir = './test_files/'

uci_cursor.get_all = function(arg)
if arg == 'dhcp' then
return io.open(test_file_dir .. 'dhcp.txt')
end
end

io.open = function(arg)
if arg == '/tmp/dhcp.leases' then
return io.open(test_file_dir .. 'dhcp_leases.txt')


function test_get_dhcp_leases()

end

os.exit( luaunit.LuaUnit.run() )
61 changes: 61 additions & 0 deletions tests/test_files/address_data.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
test_data = {}


test_data.eth2_interface = {
autostart = true,
available = true,
data = {
hostname = "08-00-27-4F-CB-2E",
leasetime = 86400
},
delegation = true,
device = "eth2",
["dns-search"] = {},
["dns-server"] = { "192.168.0.1" },
dns_metric = 0,
dynamic = false,
inactive = {
["dns-search"] = {},
["dns-server"] = {},
["ipv4-address"] = {},
["ipv6-address"] = {},
neighbors = {},
route = {}
},
interface = "lan",
["ipv4-address"] = { {
address = "192.168.0.144",
mask = 24
} },
["ipv6-address"] = {},
["ipv6-prefix"] = {},
["ipv6-prefix-assignment"] = {},
l3_device = "eth2",
metric = 0,
neighbors = {},
pending = false,
proto = "dhcp",
route = { {
mask = 0,
nexthop = "192.168.0.1",
source = "192.168.0.144/32",
target = "0.0.0.0"
} },
up = true,
updated = { "addresses", "routes", "data" },
uptime = 1973
}

test_data.routes = test_data.eth2_interface.route

test_data.ipv4_address = test_data.eth2_interface['ipv4-address'][1]

test_data.address_array = {
address="192.168.0.144",
family="ipv4",
gateway="192.168.0.1",
mask=24,
proto="dhcp"
}

return test_data
5 changes: 5 additions & 0 deletions tests/test_files/dhcp_leases.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
subnet 239.252.197.0 netmask 255.255.255.0 {
range 239.252.197.10 239.252.197.107;
default-lease-time 600;
max-lease-time 7200;
}
Loading

0 comments on commit 277e109

Please sign in to comment.