Files
glacier-ng/tests/test_config_files.lua
T
2026-10-29 09:15:34 -04:00

140 lines
6.2 KiB
Lua

-- test_config_files.lua
--
-- /etc handling (decision GLACIER_BOOTSTRAP.txt 9c: option 1, copy on
-- install, .new on conflict). A system-scope package's etc/ is COPIED
-- into /etc, never linked; an admin-modified, admin-deleted or
-- not-glacier-installed file is never overwritten; package changes to
-- such a file are offered as FILE.new, once.
--
-- Uses the real /etc of the (throwaway) test container, confined to
-- /etc/glacier-cfgtest.
local glacier = require("glacier_ffi")
local h = require("test_helpers")
h.reset_glacier_root()
io.write("== test_config_files ==\n")
local D = "/etc/glacier-cfgtest"
h.sh("rm -rf " .. D)
h.check(tonumber(glacier.lib.gl_init_sys(false)) == 0, "gl_init_sys")
local function read(path)
local f = io.open(path, "r")
if not f then return nil end
local c = f:read("*a")
f:close()
return c
end
local function write(path, c)
local f = assert(io.open(path, "w"))
f:write(c)
f:close()
end
local function is_symlink(path)
local ok = os.execute("test -L " .. h.quote(path))
return ok == true or ok == 0
end
local function readlink(path)
local p = io.popen("readlink " .. h.quote(path) .. " 2>/dev/null")
local s = p:read("*l")
p:close()
return s
end
-- One system-scope transaction: remove `remove` (list of {name, repo}),
-- then install `pkgs`.
local function sys_txn(pkgs, remove)
local ctx = glacier.new_context()
local st = glacier.lib.gl_init_stage_context(ctx, glacier.SCOPE_SYS, 0)
if tonumber(st) ~= 0 then return "stage:" .. glacier.txn_name(st) end
for _, r in ipairs(remove or {}) do
local rr = glacier.lib.gl_remove_pkg(ctx, r[1], r[2])
if tonumber(rr) ~= 0 then
glacier.lib.gl_abort_transaction(ctx)
return "remove:" .. glacier.remove_name(rr)
end
end
for _, p in ipairs(pkgs or {}) do
local r = glacier.lib.gl_install_pkg(ctx, p)
if tonumber(r) ~= 0 then
glacier.lib.gl_abort_transaction(ctx)
return glacier.install_name(r)
end
end
return glacier.txn_name(glacier.lib.gl_commit_transaction(ctx))
end
local function cfgpkg(ver, files, symlinks)
local f = {}
for k, v in pairs(files) do f["etc/glacier-cfgtest/" .. k] = v end
local l = {}
for k, v in pairs(symlinks or {}) do l["etc/glacier-cfgtest/" .. k] = v end
return h.build_fixture_pkg{ name = "cfgpkg", ver = ver, repo = "extra", files = f, symlinks = l }
end
-- ---------------------------------------------------------------------
io.write("-- first install\n")
h.sh("mkdir -p " .. D)
write(D .. "/c.conf", "admin c\n") -- already there, not glacier's
local v1 = cfgpkg("1.0.0",
{ ["a.conf"] = "a1\n", ["b.conf"] = "b1\n", ["c.conf"] = "c1\n", ["d.conf"] = "d1\n" },
{ ["link.conf"] = "a.conf" })
h.check(sys_txn({ v1 }) == "GL_TXN_OK", "install cfgpkg 1.0.0")
h.check(read(D .. "/a.conf") == "a1\n" and not is_symlink(D .. "/a.conf"),
"a.conf COPIED into /etc (a regular file, not a link into the store)")
h.check(read(D .. "/b.conf") == "b1\n", "b.conf installed")
h.check(readlink(D .. "/link.conf") == "a.conf", "a config symlink is copied as a symlink")
h.check(read(D .. "/c.conf") == "admin c\n", "pre-existing c.conf (not glacier's) untouched")
h.check(read(D .. "/c.conf.new") == "c1\n", "package's c.conf offered as c.conf.new")
h.check(not h.exists("/usr/etc") and not h.exists("/glacier/sys/links/etc"),
"nothing linked under /usr/etc or the links tree")
-- ---------------------------------------------------------------------
io.write("-- admin edits / deletes, then an unrelated commit\n")
write(D .. "/a.conf", "a-admin\n")
os.remove(D .. "/d.conf")
local other = h.build_fixture_pkg{ name = "cfgother", ver = "1.0.0", repo = "extra" }
h.check(sys_txn({ other }) == "GL_TXN_OK", "unrelated transaction")
h.check(read(D .. "/a.conf") == "a-admin\n", "edited a.conf left alone")
h.check(not h.exists(D .. "/a.conf.new"), "no .new for an edit when the package didn't change")
h.check(not h.exists(D .. "/d.conf"), "deleted d.conf stays deleted")
-- ---------------------------------------------------------------------
io.write("-- upgrade to 2.0.0\n")
local v2 = cfgpkg("2.0.0",
{ ["a.conf"] = "a2\n", ["b.conf"] = "b2\n", ["c.conf"] = "c1\n", ["d.conf"] = "d2\n" },
{ ["link.conf"] = "a.conf" })
h.check(sys_txn({ v2 }, { { "cfgpkg", "extra" } }) == "GL_TXN_OK", "upgrade (remove 1.0.0 + install 2.0.0)")
h.check(read(D .. "/a.conf") == "a-admin\n", "edited a.conf kept")
h.check(read(D .. "/a.conf.new") == "a2\n", "package's new a.conf offered as a.conf.new")
h.check(read(D .. "/b.conf") == "b2\n" and not h.exists(D .. "/b.conf.new"),
"untouched b.conf upgraded in place, no .new")
h.check(read(D .. "/c.conf") == "admin c\n", "foreign c.conf still untouched")
h.check(not h.exists(D .. "/d.conf"), "deleted d.conf not resurrected by the upgrade")
-- ---------------------------------------------------------------------
io.write("-- an offer is made once\n")
os.remove(D .. "/a.conf.new")
os.remove(D .. "/c.conf.new")
local other2 = h.build_fixture_pkg{ name = "cfgother2", ver = "1.0.0", repo = "extra" }
h.check(sys_txn({ other2 }) == "GL_TXN_OK", "another unrelated transaction")
h.check(not h.exists(D .. "/a.conf.new") and not h.exists(D .. "/c.conf.new"),
"dismissed .new files are not recreated")
-- ---------------------------------------------------------------------
io.write("-- removal\n")
h.check(sys_txn({}, { { "cfgpkg", "extra" } }) == "GL_TXN_OK", "remove cfgpkg")
h.check(not h.exists(D .. "/b.conf"), "unmodified b.conf removed with its package")
h.check(not is_symlink(D .. "/link.conf"), "unmodified config symlink removed")
h.check(read(D .. "/a.conf") == "a-admin\n", "edited a.conf kept after removal")
h.check(read(D .. "/c.conf") == "admin c\n", "foreign c.conf kept after removal")
h.check(not h.exists("/glacier/sys/store/.etc-state/glacier-cfgtest/b.conf"),
"records for the removed package are gone")
h.sh("rm -rf " .. D)
h.summary()