Commit 418dc234 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

pkg: Tests for pkg_merge_files and pkg_cleanup_files

+ Fixes for these functions
+ Move of slurp to utils and reusing in the tests
parent a76cce7c
......@@ -20,6 +20,7 @@ along with Updater. If not, see <http://www.gnu.org/licenses/>.
local pairs = pairs
local next = next
local error = error
local io = io
local unpack = unpack
local events_wait = events_wait
local run_command = run_command
......@@ -74,4 +75,19 @@ function cleanup_dirs(dirs)
end
end
--[[
Read the whole content of given file. Return the content, or nil and error message.
In case of errors during the reading (instead of when opening), it calls error()
]]
function slurp(filename)
local f, err = io.open(filename)
if not f then
return nil, err
end
local content = f:read("*a")
f:close()
if not content then error("Could not read content of " .. filename) end
return content
end
return _M
......@@ -20,8 +20,10 @@ along with Updater. If not, see <http://www.gnu.org/licenses/>.
local error = error
local type = type
local pairs = pairs
local ipairs = ipairs
local pcall = pcall
local require = require
local next = next
local unpack = unpack
local io = io
local table = table
......@@ -29,6 +31,12 @@ local mkdtemp = mkdtemp
local chdir = chdir
local run_command = run_command
local events_wait = events_wait
local stat = stat
local mkdir = mkdir
local move = move
local unlink = unlink
local rmdir = rmdir
local ls = ls
local DBG = DBG
local WARN = WARN
local utils = require "utils"
......@@ -167,25 +175,10 @@ function package_postprocess(status)
return status
end
--[[
Read the whole content of given file. Return the content, or nil and error message.
In case of errors during the reading (instead of when opening), it calls error()
]]
local function slurp(filename)
local f, err = io.open(filename)
if not f then
return nil, err
end
local content = f:read("*a")
f:close()
if not content then error("Could not read content of " .. filename) end
return content
end
-- Get pkg_name's file's content with given suffix. Nil on error.
local function pkg_file(pkg_name, suffix, warn)
local fname = info_dir .. pkg_name .. "." .. suffix
local content, err = slurp(fname)
local content, err = utils.slurp(fname)
if not content then
WARN("Could not read ." .. suffix .. " file of " .. pkg_name .. ": " .. err)
end
......@@ -375,7 +368,7 @@ function pkg_examine(dir)
cidx:close()
end
-- Load the control file of the package and parse it
local control = package_postprocess(block_parse(slurp(control_dir .. "/control")));
local control = package_postprocess(block_parse(utils.slurp(control_dir .. "/control")));
-- Wait for all asynchronous processes to finish
events_wait(unpack(events))
-- How well did it go?
......@@ -534,18 +527,15 @@ function pkg_cleanup_files(files)
function get_parent()
local parent = f:match("^(.+)/[^/]+")
f = parent
if f:len() > 0 then
return f
else
return nil
end
return f
end
for parent in get_parent do
local content = ls(parent)
if next(root_dir .. parent) then
if next(ls(root_dir .. parent)) then
DBG("Directory " .. root_dir .. parent .. " not empty, keeping in place")
-- It is not empty
break
else
DBG("Removing empty directory " .. root_dir .. parent)
local ok, err = pcall(function () rmdir(root_dir .. parent) end)
if not ok then
-- It is an error, but we don't want to give up on the rest of the operation because of that
......
......@@ -238,17 +238,14 @@ end
local orig_status_file = B.status_file
local orig_info_dir = B.info_dir
local orig_root_dir = B.root_dir
local tmp_dirs = {}
--[[
Test the chain of functions ‒ unpack, examine
]]
function test_pkg_unpack()
local fname = datadir .. "updater.ipk"
local f = io.open(fname)
local input = f:read("*a")
f:close()
local path = B.pkg_unpack(input)
local path = B.pkg_unpack(utils.slurp(datadir .. "updater.ipk"))
-- Make sure it is deleted on teardown
table.insert(tmp_dirs, path)
-- Check list of extracted files
......@@ -323,6 +320,49 @@ function test_pkg_unpack()
Description = "updater",
Depends = {"libc", "vixie-cron", "openssl-util", "libatsha204", "curl", "cert-backup", "opkg", "bzip2", "cznic-cacert-bundle"}
}, control)
local test_root = mkdtemp()
table.insert(tmp_dirs, test_root)
B.root_dir = test_root
-- Try merging it to a „root“ directory. We need to find all the files and directories.
--[[
Omit the empty directories. They wouldn't get cleared currently, and
we want to test it. We may store list of directories in future.
]]
dirs["/usr/share/updater/hashes"] = nil
dirs["/etc/cron.d"] = nil
B.pkg_merge_files(path .. "/data", dirs, files, conffiles)
-- The original directory disappeared.
assert_table_equal({
["control"] = "d"
}, ls(path))
events_wait(run_command(function (ecode, killed, stdout)
assert_equal(0, ecode, "Failed to check the list of files")
assert_table_equal(lines2set([[.
./etc
./etc/config
./etc/config/updater
./etc/init.d
./etc/init.d/updater
./etc/ssl
./etc/ssl/updater.pem
./usr
./usr/bin
./usr/bin/updater-resume.sh
./usr/bin/updater.sh
./usr/bin/updater-unstuck.sh
./usr/bin/updater-utils.sh
./usr/bin/updater-wipe.sh
./usr/bin/updater-worker.sh
./usr/share
./usr/share/updater
./usr/share/updater/keys
./usr/share/updater/keys/release.pem
./usr/share/updater/keys/standby.pem
]]), lines2set(stdout))
end, function () chdir(test_root) end, nil, -1, -1, "/usr/bin/find"))
-- Now try clearing the package. When we list all the files, it should remove the directories as well.
B.pkg_cleanup_files(files)
assert_table_equal({}, ls(test_root))
end
-- Test the collision_check function
......@@ -394,5 +434,6 @@ function teardown()
-- Clean up, return the original file name
B.status_file = orig_status_file
B.info_dir = orig_info_dir
B.root_dir= orig_root_dir
utils.cleanup_dirs(tmp_dirs)
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment