diff --git a/openwisp-config/tests/config/network b/openwisp-config/tests/config/network new file mode 100644 index 0000000..d0a7799 --- /dev/null +++ b/openwisp-config/tests/config/network @@ -0,0 +1,44 @@ +config interface 'loopback' + option ifname 'lo' + option proto 'static' + option ipaddr '127.0.0.1' + option netmask '255.0.0.0' + +config globals 'globals' + option ula_prefix 'fd8e:f40a:6701::/48' + +config interface 'lan' + option ifname 'eth0.1' + option type 'bridge' + option proto 'dhcp' + option ip6assign '60' + option force_link '0' + +config interface 'wan' + option ifname 'eth0.2' + option proto 'none' + +config interface 'wlan0' + option ifname 'wlan0' + option ipaddr '172.27.254.251/16' + option proto 'static' + +config interface 'wlan1' + option ifname 'wlan1' + option ipaddr '172.27.254.252/16' + option proto 'static' + +config switch + option name 'switch0' + option reset '1' + option enable_vlan '1' + +config switch_vlan + option device 'switch0' + option vlan '1' + option ports '0t 2 3 4 5' + +config switch_vlan + option device 'switch0' + option vlan '2' + option ports '0t 1' diff --git a/openwisp-config/tests/config/system b/openwisp-config/tests/config/system new file mode 100644 index 0000000..0828c1d --- /dev/null +++ b/openwisp-config/tests/config/system @@ -0,0 +1,30 @@ +config system + option hostname 'OpenWrt' + option timezone 'UTC' + +config timeserver 'ntp' + list server '0.openwrt.pool.ntp.org' + list server '1.openwrt.pool.ntp.org' + list server '2.openwrt.pool.ntp.org' + list server '3.openwrt.pool.ntp.org' + option enabled '1' + option enable_server '0' + +config led 'led_usb1' + option name 'USB1' + option sysfs 'tp-link:green:usb1' + option trigger 'usbdev' + option dev '1-1.1' + option interval '50' + +config led 'led_usb2' + option name 'USB2' + option sysfs 'tp-link:green:usb2' + option trigger 'usbdev' + option dev '1-1.2' + option interval '50' + +config led 'led_wlan2g' + option name 'WLAN2G' + option sysfs 'tp-link:blue:wlan2g' + option trigger 'phy0tpt' diff --git a/openwisp-config/tests/test_store_unmanaged.lua b/openwisp-config/tests/test_store_unmanaged.lua new file mode 100644 index 0000000..8685919 --- /dev/null +++ b/openwisp-config/tests/test_store_unmanaged.lua @@ -0,0 +1,105 @@ +require('os') +require('io') +local luaunit = require('luaunit') +local store_unmanaged = assert(loadfile("../files/store_unmanaged.lua")) +local default_blocks = "system.ntp " .. + "system.@led " .. + "network.loopback " .. + "network.globals " .. + "network.lan " .. + "network.wan " .. + "network.@switch " .. + "network.@switch_vlan" +local string = string +local prefix = './unmanaged/' +assertNotNil = luaunit.assertNotNil +assertNil = luaunit.assertNil + +local function _clean() + os.remove(prefix .. 'network') + os.remove(prefix .. 'system') +end + +TestStoreUnmanaged = {} + +TestStoreUnmanaged.setUp = _clean +TestStoreUnmanaged.tearDown = _clean + +function TestStoreUnmanaged.test_empty() + store_unmanaged('--test=1') + assertNil(io.open(prefix .. 'network')) + assertNil(io.open(prefix .. 'system')) +end + +function TestStoreUnmanaged.test_default() + store_unmanaged('--test=1', '-o=' .. default_blocks) + -- ensure network config file has been created correctly + local file = io.open(prefix .. 'network') + assertNotNil(file) + local contents = file:read('*all') + assertNotNil(string.find(contents, "config interface 'loopback'")) + assertNotNil(string.find(contents, "option ipaddr '127.0.0.1'")) + assertNotNil(string.find(contents, "option ula_prefix 'fd8e:f40a:6701::/48'")) + assertNotNil(string.find(contents, "config interface 'wan'")) + assertNotNil(string.find(contents, "config switch")) + assertNotNil(string.find(contents, "option vlan '2'")) + assertNotNil(string.find(contents, "option vlan '1'")) + assertNil(string.find(contents, "config interface 'wlan0'")) + assertNil(string.find(contents, "config interface 'wlan1'")) + -- ensure system config file exists + local file = io.open(prefix .. 'system') + assertNotNil(file) + local contents = file:read('*all') + assertNotNil(string.find(contents, "list server '1.openwrt.pool.ntp.org'")) + assertNotNil(string.find(contents, "config led 'led_usb1'")) + assertNotNil(string.find(contents, "config led 'led_usb2'")) + assertNotNil(string.find(contents, "config led 'led_wlan2g'")) + assertNil(string.find(contents, "option hostname 'OpenWrt'")) +end + +function TestStoreUnmanaged.test_specific_name() + store_unmanaged('--test=1', '-o="system.ntp"') + local file = io.open(prefix .. 'system') + assertNotNil(file) + local contents = file:read('*all') + assertNotNil(file) + assertNil(string.find(contents, "config led 'led_usb1'")) + assertNil(string.find(contents, "option hostname 'OpenWrt'")) + assertNotNil(string.find(contents, "list server '1.openwrt.pool.ntp.org'")) +end + +function TestStoreUnmanaged.test_specific_type() + store_unmanaged('--test=1', '-o="system.@led"') + local file = io.open(prefix .. 'system') + assertNotNil(file) + local contents = file:read('*all') + assertNotNil(file) + assertNotNil(string.find(contents, "config led 'led_usb1'")) + assertNotNil(string.find(contents, "config led 'led_usb2'")) + assertNotNil(string.find(contents, "config led 'led_wlan2g'")) + assertNil(string.find(contents, "option hostname 'OpenWrt'")) + assertNil(string.find(contents, "list server '1.openwrt.pool.ntp.org'")) +end + +function TestStoreUnmanaged.test_unrecognized_config_option() + store_unmanaged('--test=1', '-o="network.vpn"') + local file = io.open(prefix .. 'network') + assertNotNil(file) + local contents = file:read('*all') + assertNil(string.find(contents, "vpn")) +end + +function TestStoreUnmanaged.test_unrecognized_config_type() + store_unmanaged('--test=1', '-o="network.@vpn"') + local file = io.open(prefix .. 'network') + assertNotNil(file) + local contents = file:read('*all') + assertNil(string.find(contents, "vpn")) +end + +function test_unrecognized_config() + store_unmanaged('--test=1', '-o="totally.@wrong"') + assertNil(io.open(prefix .. 'totally')) +end + +os.exit(luaunit.LuaUnit.run()) diff --git a/openwisp-config/tests/unmanaged/.gitignore b/openwisp-config/tests/unmanaged/.gitignore new file mode 100644 index 0000000..e69de29