opnsense-src/libexec/nuageinit/tests/addfile.lua
Jose Luis Duran a5adb1ca35
nuageinit: Silence luacheck warnings and fix typos
No functional change intended.

Reviewed by:	bapt, dtxdf, kevans
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D53238

(cherry picked from commit 81af04b081402d131c7e34b30c88b7c337271fad)
2025-11-25 00:39:15 +00:00

71 lines
1.4 KiB
Lua

#!/bin/libexec/flua
local n = require("nuage")
local lfs = require("lfs")
local f = {
content = "plop"
}
local r, err = n.addfile(f, false)
if r or err ~= "No path provided for the file to write" then
n.err("addfile should not accept a file to write without a path")
end
local function addfile_and_getres(file)
local r, err = n.addfile(file, false)
if not r then
n.err(err)
end
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
if not root then
root = ""
end
local filepath = root .. file.path
local resf = assert(io.open(filepath, "r"))
local str = resf:read("*all")
resf:close()
return str
end
-- simple file
f.path="/tmp/testnuage"
local str = addfile_and_getres(f)
if str ~= f.content then
n.err("Invalid file content")
end
-- the file is overwritten
f.content = "test"
str = addfile_and_getres(f)
if str ~= f.content then
n.err("Invalid file content, not overwritten")
end
-- try to append now
f.content = "more"
f.append = true
str = addfile_and_getres(f)
if str ~= "test" .. f.content then
n.err("Invalid file content, not appended")
end
-- base64
f.content = "YmxhCg=="
f.encoding = "base64"
f.append = false
str = addfile_and_getres(f)
if str ~= "bla\n" then
n.err("Invalid file content, base64 decode")
end
-- b64
f.encoding = "b64"
str = addfile_and_getres(f)
if str ~= "bla\n" then
n.err("Invalid file content, b64 decode")
print("==>" .. str .. "<==")
end