opnsense-src/libexec/nuageinit/tests/addfile.lua
Baptiste Daroussin 19a7ea3cc4 nuageinit: implement write_files
write_files is a list of files that should be created at the first boot

each file content can be either plain text or encoded in base64 (note
that cloudinit specify that gzip is supported, but we do not support it
yet.)

All other specifier from cloudinit should work:
by default all files will juste overwrite exesiting files except if
"append" is set to true, permissions, ownership can be specified.
The files are create before packages are being installed and user
created.

if "defer" is set to true then the file is being created after packages
installation and package manupulation.

This feature is requested for KDE's CI.
2025-06-26 13:47:37 +02: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 overwriten
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