diff --git a/openwisp-config/files/sbin/openwisp-update-config.lua b/openwisp-config/files/sbin/openwisp-update-config.lua index 19c0477..4f9e0cf 100755 --- a/openwisp-config/files/sbin/openwisp-update-config.lua +++ b/openwisp-config/files/sbin/openwisp-update-config.lua @@ -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) @@ -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) diff --git a/openwisp-config/tests/good-config.tar.gz b/openwisp-config/tests/good-config.tar.gz index dc31ada..b0ebe92 100644 Binary files a/openwisp-config/tests/good-config.tar.gz and b/openwisp-config/tests/good-config.tar.gz differ diff --git a/openwisp-config/tests/test_update_config.lua b/openwisp-config/tests/test_update_config.lua index 44d1e35..cb545c2 100644 --- a/openwisp-config/tests/test_update_config.lua +++ b/openwisp-config/tests/test_update_config.lua @@ -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) @@ -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) @@ -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 @@ -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() diff --git a/openwisp-config/tests/update/network b/openwisp-config/tests/update/network index a0d8d4e..aeb4f65 100644 --- a/openwisp-config/tests/update/network +++ b/openwisp-config/tests/update/network @@ -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'