Skip to content

Commit

Permalink
[tests] Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devkapilbansal committed Nov 29, 2021
1 parent e39625f commit dec1ed0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
18 changes: 14 additions & 4 deletions openwisp-config/files/sbin/openwisp-update-config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
end
end
end
-- remove entire section if empty
local result = standard:get_all(file, section['.name'])
if result and utils.is_uci_empty(result) then
standard:delete(file, section['.name'])
end
end
end
standard:commit(file)
Expand Down Expand Up @@ -143,23 +148,28 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
-- ensure we are acting on a file
if lfs.attributes(remote_path, 'mode') == 'file' then
-- if there's no backup of the file yet, create one
if (not utils.file_exists(stored_path)) then
if not utils.file_exists(stored_path) then
os.execute('cp '..standard_path..' '..stored_path)
if (utils.file_exists(remote_path)) then
if utils.file_exists(remote_path) then
for key, section in pairs(stored:get_all(file)) do
-- check if section is in remote configuration
local section_check = check:get(file, section['.name'])
if section_check ~= nil then
if section_check then
-- check if options is in remote configuration
for option, value in pairs(section) do
if not utils.starts_with_dot(option) then
local option_check = check:get(file, section['.name'], option)
if option_check ~= nil then
if option_check then
-- if option is in remote configuration, remove it
stored:delete(file, section['.name'], option)
end
end
end
-- remove entire section if empty
local result = stored:get_all(file, section['.name'])
if result and utils.is_uci_empty(result) then
stored:delete(file, section['.name'])
end
end
end
stored:commit(file)
Expand Down
Binary file modified openwisp-config/tests/good-config.tar.gz
Binary file not shown.
38 changes: 28 additions & 10 deletions openwisp-config/tests/test_update_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,21 @@ function TestUpdateConfig.test_update()
local networkFile = io.open(config_dir .. 'network')
luaunit.assertNotNil(networkFile)
local networkContents = networkFile:read('*all')
-- ensure interface added is present
luaunit.assertNotNil(string.find(networkContents, "config interface 'added'"))
luaunit.assertNotNil(string.find(networkContents, "option ifname 'added0'"))
-- ensure wg1 added via remote previously is present
luaunit.assertNotNil(string.find(networkContents, "config interface 'wg1'"))
luaunit.assertNotNil(string.find(networkContents, "option proto 'static'"))
-- ensure network file is stored for backup
local storedNetworkFile = io.open(stored_dir .. '/etc/config/network')
luaunit.assertNotNil(storedNetworkFile)
local storedNetworkContents = storedNetworkFile:read('*all')
-- ensure wg1 is not added that is downloaded from remote
luaunit.assertNil(string.find(storedNetworkContents, "config interface 'wg1'"))
-- ensure wan and wg0 are present
luaunit.assertNotNil(string.find(storedNetworkContents, "config interface 'wan'"))
luaunit.assertNotNil(string.find(storedNetworkContents, "config interface 'wg0'"))
-- check system
local systemFile = io.open(config_dir .. 'system')
luaunit.assertNotNil(systemFile)
Expand All @@ -69,6 +82,16 @@ function TestUpdateConfig.test_update()
-- ensure rest of config options are present
luaunit.assertNotNil(string.find(systemContents, "config timeserver 'ntp'"))
luaunit.assertNotNil(string.find(systemContents, "list server '3.openwrt.pool.ntp.org'"))
-- ensure system file is stored for backup
local storedSystemFile = io.open(stored_dir .. '/etc/config/network')
luaunit.assertNotNil(storedSystemFile)
local storedSystemContents = storedSystemFile:read('*all')
-- ensure hostname is not added that is updated from remote
luaunit.assertNil(string.find(storedSystemContents, "option hostname"))
-- ensure custom is not added that is downloaded from remote
luaunit.assertNil(string.find(storedSystemContents, "option custom 'custom'"))
-- ensure new is not added that is downloaded from remote
luaunit.assertNil(string.find(storedSystemContents, "config new 'new'"))
-- ensure test file is present
local testFile = io.open(write_dir .. 'etc/test')
luaunit.assertNotNil(testFile)
Expand Down Expand Up @@ -109,7 +132,7 @@ function TestUpdateConfig.test_update()
local modifiedListFile = io.open(openwisp_dir .. '/modified.list')
luaunit.assertNotNil(modifiedListFile)
luaunit.assertEquals(modifiedListFile:read('*all'), '/etc/existing\n')
local storedExisitngFile = io.open(openwisp_dir .. '/stored/etc/existing')
local storedExisitngFile = io.open(stored_dir .. '/etc/existing')
luaunit.assertNotNil(storedExisitngFile)
luaunit.assertEquals(storedExisitngFile:read('*all'), 'original\n')
-- ensure it has been modified
Expand All @@ -122,12 +145,7 @@ function TestUpdateConfig.test_update()
local restoreFile = io.open(write_dir..'/etc/restore-me')
luaunit.assertNotNil(restoreFile)
luaunit.assertEquals(restoreFile:read('*all'), 'restore-me\n')
luaunit.assertNil(io.open(openwisp_dir..'/stored/etc/restore-me'))
-- ensure network and configuration file is not backed up as it is overwritten by remote
local storedNetworkFile = io.open(openwisp_dir .. '/etc/config/network')
luaunit.assertNil(storedNetworkFile)
local storedSystemFile = io.open(openwisp_dir .. '/etc/config/system')
luaunit.assertNil(storedSystemFile)
luaunit.assertNil(io.open(stored_dir .. '/etc/restore-me'))
end

function TestUpdateConfig.test_update_conf_arg()
Expand Down Expand Up @@ -160,7 +178,7 @@ function TestUpdateConfig.test_duplicate_list_options()
luaunit.assertEquals(string_count(networkContents, "list ipaddr '192.168.10.3/24'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.2'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.3'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 2)
luaunit.assertNotNil(string.find(networkContents, "option test_restore '2'"))
-- repeating the operation has the same result
update_config('--test=1', '--conf=./test-duplicate-list.tar.gz')
Expand All @@ -172,7 +190,7 @@ function TestUpdateConfig.test_duplicate_list_options()
luaunit.assertEquals(string_count(networkContents, "list ipaddr '192.168.10.3/24'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.2'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.3'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 2)
luaunit.assertNotNil(string.find(networkContents, "option test_restore '2'"))
end

Expand All @@ -187,7 +205,7 @@ function TestUpdateConfig.test_removal_list_options()
luaunit.assertEquals(string_count(networkContents, "list ipaddr '192.168.10.3/24'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.2'"), 0)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.3'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 1)
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 2)
luaunit.assertNotNil(string.find(networkContents, "option test_restore '2'"))
end

Expand Down
5 changes: 5 additions & 0 deletions openwisp-config/tests/update/network
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ config interface 'wan'
config interface 'wg0'
list addresses '10.0.0.2'
list addresses '10.0.0.3'

config interface 'wg1'
option proto 'static'
list addresses '10.0.0.4'
list addresses '10.0.0.5'

0 comments on commit dec1ed0

Please sign in to comment.