diff --git a/openwisp-config/files/sbin/openwisp-update-config.lua b/openwisp-config/files/sbin/openwisp-update-config.lua index 2df5e34..d5deaa4 100755 --- a/openwisp-config/files/sbin/openwisp-update-config.lua +++ b/openwisp-config/files/sbin/openwisp-update-config.lua @@ -32,7 +32,7 @@ local test_root_dir = working_dir .. '/update-test' local remote_dir = openwisp_dir .. '/remote' local remote_config_dir = remote_dir .. '/etc/config' local stored_dir = openwisp_dir .. '/stored' -local stored_config_dir = openwisp_dir .. '/etc/config' +local stored_config_dir = stored_dir .. '/etc/config' local added_file = openwisp_dir .. '/added.list' local modified_file = openwisp_dir .. '/modified.list' local get_standard = function() return uci.cursor(standard_config_dir) end @@ -84,8 +84,8 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then if section_stored == nil then utils.remove_uci_options(standard, file, section) -- section is in the backup configuration -> restore - -- delete all options first else + -- delete all options first for option, value in pairs(section) do if not utils.starts_with_dot(option) then standard:delete(file, section['.name'], option) @@ -148,7 +148,7 @@ 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) and not utils.file_exists(remote_path)) then os.execute('cp '..standard_path..' '..stored_path) end -- MERGE mode diff --git a/openwisp-config/tests/test_update_config.lua b/openwisp-config/tests/test_update_config.lua index 051a6bf..6265f8f 100644 --- a/openwisp-config/tests/test_update_config.lua +++ b/openwisp-config/tests/test_update_config.lua @@ -7,6 +7,7 @@ local update_config = assert(loadfile("../files/sbin/openwisp-update-config.lua" local write_dir = './update-test/' local config_dir = write_dir .. 'etc/config/' local openwisp_dir = './openwisp/' +local stored_dir = openwisp_dir .. '/stored/' local remote_config_dir = openwisp_dir .. 'remote/etc/config' local function string_count(base, pattern) @@ -38,8 +39,8 @@ TestUpdateConfig = { os.execute('echo restore-me > '..openwisp_dir..'/stored/etc/restore-me') os.execute('echo /etc/restore-me > '..openwisp_dir..'/modified.list') -- this file is stored in the backup - os.execute('mkdir -p ' .. openwisp_dir ..'etc/config/') - os.execute("cp ./update/stored_wireless " ..openwisp_dir.. '/etc/config/wireless') + os.execute('mkdir -p ' .. stored_dir ..'etc/config/') + os.execute("cp ./update/stored_wireless " ..stored_dir.. '/etc/config/wireless') end, tearDown = function() os.execute('rm -rf ' .. write_dir) @@ -123,12 +124,12 @@ function TestUpdateConfig.test_update() luaunit.assertEquals(restoreFile:read('*all'), 'restore-me\n') luaunit.assertNil(io.open(openwisp_dir..'/stored/etc/restore-me')) -- ensure network configuration file is backed up - local storedNetworkFile = io.open(openwisp_dir .. '/etc/config/network') + local storedNetworkFile = io.open(stored_dir .. '/etc/config/network') luaunit.assertNotNil(storedNetworkFile) local initialNetworkFile = io.open('update/network') luaunit.assertEquals(storedNetworkFile:read('*all'), initialNetworkFile:read('*all')) -- ensure system configuration file is backed up - local storedSystemFile = io.open(openwisp_dir .. '/etc/config/system') + local storedSystemFile = io.open(stored_dir .. '/etc/config/system') luaunit.assertNotNil(storedSystemFile) local initialSystemFile = io.open('update/system') luaunit.assertEquals(storedSystemFile:read('*all'), initialSystemFile:read('*all'))