Skip to content

Commit dec1ed0

Browse files
[tests] Added tests
1 parent e39625f commit dec1ed0

4 files changed

Lines changed: 47 additions & 14 deletions

File tree

openwisp-config/files/sbin/openwisp-update-config.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
112112
end
113113
end
114114
end
115+
-- remove entire section if empty
116+
local result = standard:get_all(file, section['.name'])
117+
if result and utils.is_uci_empty(result) then
118+
standard:delete(file, section['.name'])
119+
end
115120
end
116121
end
117122
standard:commit(file)
@@ -143,23 +148,28 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
143148
-- ensure we are acting on a file
144149
if lfs.attributes(remote_path, 'mode') == 'file' then
145150
-- if there's no backup of the file yet, create one
146-
if (not utils.file_exists(stored_path)) then
151+
if not utils.file_exists(stored_path) then
147152
os.execute('cp '..standard_path..' '..stored_path)
148-
if (utils.file_exists(remote_path)) then
153+
if utils.file_exists(remote_path) then
149154
for key, section in pairs(stored:get_all(file)) do
150155
-- check if section is in remote configuration
151156
local section_check = check:get(file, section['.name'])
152-
if section_check ~= nil then
157+
if section_check then
153158
-- check if options is in remote configuration
154159
for option, value in pairs(section) do
155160
if not utils.starts_with_dot(option) then
156161
local option_check = check:get(file, section['.name'], option)
157-
if option_check ~= nil then
162+
if option_check then
158163
-- if option is in remote configuration, remove it
159164
stored:delete(file, section['.name'], option)
160165
end
161166
end
162167
end
168+
-- remove entire section if empty
169+
local result = stored:get_all(file, section['.name'])
170+
if result and utils.is_uci_empty(result) then
171+
stored:delete(file, section['.name'])
172+
end
163173
end
164174
end
165175
stored:commit(file)
36 Bytes
Binary file not shown.

openwisp-config/tests/test_update_config.lua

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,21 @@ function TestUpdateConfig.test_update()
5555
local networkFile = io.open(config_dir .. 'network')
5656
luaunit.assertNotNil(networkFile)
5757
local networkContents = networkFile:read('*all')
58+
-- ensure interface added is present
5859
luaunit.assertNotNil(string.find(networkContents, "config interface 'added'"))
5960
luaunit.assertNotNil(string.find(networkContents, "option ifname 'added0'"))
61+
-- ensure wg1 added via remote previously is present
62+
luaunit.assertNotNil(string.find(networkContents, "config interface 'wg1'"))
63+
luaunit.assertNotNil(string.find(networkContents, "option proto 'static'"))
64+
-- ensure network file is stored for backup
65+
local storedNetworkFile = io.open(stored_dir .. '/etc/config/network')
66+
luaunit.assertNotNil(storedNetworkFile)
67+
local storedNetworkContents = storedNetworkFile:read('*all')
68+
-- ensure wg1 is not added that is downloaded from remote
69+
luaunit.assertNil(string.find(storedNetworkContents, "config interface 'wg1'"))
70+
-- ensure wan and wg0 are present
71+
luaunit.assertNotNil(string.find(storedNetworkContents, "config interface 'wan'"))
72+
luaunit.assertNotNil(string.find(storedNetworkContents, "config interface 'wg0'"))
6073
-- check system
6174
local systemFile = io.open(config_dir .. 'system')
6275
luaunit.assertNotNil(systemFile)
@@ -69,6 +82,16 @@ function TestUpdateConfig.test_update()
6982
-- ensure rest of config options are present
7083
luaunit.assertNotNil(string.find(systemContents, "config timeserver 'ntp'"))
7184
luaunit.assertNotNil(string.find(systemContents, "list server '3.openwrt.pool.ntp.org'"))
85+
-- ensure system file is stored for backup
86+
local storedSystemFile = io.open(stored_dir .. '/etc/config/network')
87+
luaunit.assertNotNil(storedSystemFile)
88+
local storedSystemContents = storedSystemFile:read('*all')
89+
-- ensure hostname is not added that is updated from remote
90+
luaunit.assertNil(string.find(storedSystemContents, "option hostname"))
91+
-- ensure custom is not added that is downloaded from remote
92+
luaunit.assertNil(string.find(storedSystemContents, "option custom 'custom'"))
93+
-- ensure new is not added that is downloaded from remote
94+
luaunit.assertNil(string.find(storedSystemContents, "config new 'new'"))
7295
-- ensure test file is present
7396
local testFile = io.open(write_dir .. 'etc/test')
7497
luaunit.assertNotNil(testFile)
@@ -109,7 +132,7 @@ function TestUpdateConfig.test_update()
109132
local modifiedListFile = io.open(openwisp_dir .. '/modified.list')
110133
luaunit.assertNotNil(modifiedListFile)
111134
luaunit.assertEquals(modifiedListFile:read('*all'), '/etc/existing\n')
112-
local storedExisitngFile = io.open(openwisp_dir .. '/stored/etc/existing')
135+
local storedExisitngFile = io.open(stored_dir .. '/etc/existing')
113136
luaunit.assertNotNil(storedExisitngFile)
114137
luaunit.assertEquals(storedExisitngFile:read('*all'), 'original\n')
115138
-- ensure it has been modified
@@ -122,12 +145,7 @@ function TestUpdateConfig.test_update()
122145
local restoreFile = io.open(write_dir..'/etc/restore-me')
123146
luaunit.assertNotNil(restoreFile)
124147
luaunit.assertEquals(restoreFile:read('*all'), 'restore-me\n')
125-
luaunit.assertNil(io.open(openwisp_dir..'/stored/etc/restore-me'))
126-
-- ensure network and configuration file is not backed up as it is overwritten by remote
127-
local storedNetworkFile = io.open(openwisp_dir .. '/etc/config/network')
128-
luaunit.assertNil(storedNetworkFile)
129-
local storedSystemFile = io.open(openwisp_dir .. '/etc/config/system')
130-
luaunit.assertNil(storedSystemFile)
148+
luaunit.assertNil(io.open(stored_dir .. '/etc/restore-me'))
131149
end
132150

133151
function TestUpdateConfig.test_update_conf_arg()
@@ -160,7 +178,7 @@ function TestUpdateConfig.test_duplicate_list_options()
160178
luaunit.assertEquals(string_count(networkContents, "list ipaddr '192.168.10.3/24'"), 1)
161179
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.2'"), 1)
162180
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.3'"), 1)
163-
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 1)
181+
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 2)
164182
luaunit.assertNotNil(string.find(networkContents, "option test_restore '2'"))
165183
-- repeating the operation has the same result
166184
update_config('--test=1', '--conf=./test-duplicate-list.tar.gz')
@@ -172,7 +190,7 @@ function TestUpdateConfig.test_duplicate_list_options()
172190
luaunit.assertEquals(string_count(networkContents, "list ipaddr '192.168.10.3/24'"), 1)
173191
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.2'"), 1)
174192
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.3'"), 1)
175-
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 1)
193+
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 2)
176194
luaunit.assertNotNil(string.find(networkContents, "option test_restore '2'"))
177195
end
178196

@@ -187,7 +205,7 @@ function TestUpdateConfig.test_removal_list_options()
187205
luaunit.assertEquals(string_count(networkContents, "list ipaddr '192.168.10.3/24'"), 1)
188206
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.2'"), 0)
189207
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.3'"), 1)
190-
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 1)
208+
luaunit.assertEquals(string_count(networkContents, "list addresses '10.0.0.4'"), 2)
191209
luaunit.assertNotNil(string.find(networkContents, "option test_restore '2'"))
192210
end
193211

openwisp-config/tests/update/network

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ config interface 'wan'
88
config interface 'wg0'
99
list addresses '10.0.0.2'
1010
list addresses '10.0.0.3'
11+
12+
config interface 'wg1'
13+
option proto 'static'
14+
list addresses '10.0.0.4'
15+
list addresses '10.0.0.5'

0 commit comments

Comments
 (0)