flags: Tests for loading and storing

parent daeff2c4
......@@ -28,6 +28,7 @@ local tostring = tostring
local tonumber = tonumber
local loadfile = loadfile
local setmetatable = setmetatable
local setfenv = setfenv
local assert = assert
local unpack = unpack
local io = io
......@@ -48,6 +49,7 @@ local sha256 = sha256
local sync = sync
local DBG = DBG
local WARN = WARN
local DataDumper = DataDumper
local utils = require "utils"
local journal = require "journal"
......@@ -944,7 +946,7 @@ end
-- Load flags from the file and warn if it isn't possible for any reason.
function flags_load()
local chunk, err = loadfile(flags_store)
local chunk, err = loadfile(flags_storage)
if not chunk then
WARN("Couldn't load flags: " .. err)
return
......@@ -1009,7 +1011,7 @@ function flags_get_ro(path)
return utils.multi_index(stored_flags, path, "proxy")
end
function flags_store(full)
function flags_write(full)
if full then
for path, data in pairs(stored_flags) do
if data.provided then
......@@ -1031,7 +1033,7 @@ function flags_store(full)
local to_store = utils.map(stored_flags, function (name, data)
return name, data.values
end)
local f, err = io.open(flags_store .. ".tmp", "w")
local f, err = io.open(flags_storage .. ".tmp", "w")
if not f then
WARN("Couldn't write the flag storage: " .. err)
return
......@@ -1039,7 +1041,7 @@ function flags_store(full)
f:write(DataDumper(to_store))
f:close()
sync()
local ok, err = os.rename(flags_store .. ".tmp", flags_store)
local ok, err = os.rename(flags_storage .. ".tmp", flags_storage)
if not ok then
WARN("Couldn't put flag storage in place: " .. err)
return
......
......@@ -250,6 +250,7 @@ end
local orig_status_file = B.status_file
local orig_info_dir = B.info_dir
local orig_root_dir = B.root_dir
local orig_flags_storage = B.flags_storage
local tmp_dirs = {}
--[[
......@@ -748,11 +749,84 @@ function test_version_cmp()
assert_equal(1, B.version_cmp("1.10", "1.2"))
end
function test_flags()
assert_table_equal({}, B.stored_flags)
B.flags_load()
-- The meta tables are not checked here by assert_table_equal
assert_table_equal({
["/path"] = {
values = {
a = "hello",
b = "hi"
},
proxy = {}
}
}, B.stored_flags)
local flags = B.flags_get("/path")
assert_table_equal({
a = "hello",
b = "hi"
}, flags)
assert_table_equal({
["/path"] = {
values = {
a = "hello",
b = "hi"
},
provided = {
a = "hello",
b = "hi"
},
proxy = {}
}
}, B.stored_flags)
flags.x = "Greetings"
assert_table_equal({
["/path"] = {
values = {
a = "hello",
b = "hi"
},
provided = {
a = "hello",
b = "hi",
x = "Greetings"
},
proxy = {}
}
}, B.stored_flags)
local ro = B.flags_get_ro("/path")
assert_equal("hello", ro.a)
assert_nil(ro.c)
assert_equal("Greetings", ro.x)
assert_nil(B.flags_get_ro("/another"))
assert_exception(function () ro.c = "xyz" end, "access violation")
assert_exception(function () ro.d = "xyz" end, "access violation")
local new = B.flags_get("/another")
new.x = "y"
assert_equal("y", B.flags_get_ro("/another").x)
local test_root = mkdtemp()
table.insert(tmp_dirs, test_root)
B.flags_storage = test_root .. "/flags"
B.flags_write(true)
assert_table_equal({
["/path"] = {
a = "hello",
b = "hi",
x = "Greetings"
},
["/another"] = {
x = "y"
}
}, loadfile(B.flags_storage)())
end
function setup()
local sdir = os.getenv("S") or "."
-- Use a shortened version of a real status file for tests
B.status_file = sdir .. "/tests/data/opkg/status"
B.info_dir = sdir .. "/tests/data/opkg/info/"
B.flags_storage = sdir .. "/tests/data/flags"
end
function teardown()
......@@ -760,6 +834,8 @@ function teardown()
B.status_file = orig_status_file
B.info_dir = orig_info_dir
B.root_dir= orig_root_dir
B.flags_storage = orig_flags_storage
utils.cleanup_dirs(tmp_dirs)
tmp_dirs = {}
B.stored_flags = {}
end
return {
["/path"] = {
["a"] = "hello",
["b"] = "hi"
}
}
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