xref: /freebsd/libexec/nuageinit/tests/addfile.lua (revision 19a7ea3cc4de5af80e2913fda70bd65ad72835c0)
1#!/bin/libexec/flua
2
3local n = require("nuage")
4local lfs = require("lfs")
5
6local f = {
7	content = "plop"
8}
9
10local r, err = n.addfile(f, false)
11if r or err ~= "No path provided for the file to write" then
12	n.err("addfile should not accept a file to write without a path")
13end
14
15local function addfile_and_getres(file)
16	local r, err = n.addfile(file, false)
17	if not r then
18		n.err(err)
19	end
20	local root = os.getenv("NUAGE_FAKE_ROOTDIR")
21	if not root then
22		root = ""
23	end
24	local filepath = root .. file.path
25	local resf = assert(io.open(filepath, "r"))
26	local str = resf:read("*all")
27	resf:close()
28	return str
29end
30
31-- simple file
32f.path="/tmp/testnuage"
33local str = addfile_and_getres(f)
34if str ~= f.content then
35	n.err("Invalid file content")
36end
37
38-- the file is overwriten
39f.content = "test"
40
41str = addfile_and_getres(f)
42if str ~= f.content then
43	n.err("Invalid file content, not overwritten")
44end
45
46-- try to append now
47f.content = "more"
48f.append = true
49
50str = addfile_and_getres(f)
51if str ~= "test" .. f.content then
52	n.err("Invalid file content, not appended")
53end
54
55-- base64
56f.content = "YmxhCg=="
57f.encoding = "base64"
58f.append = false
59
60str = addfile_and_getres(f)
61if str ~= "bla\n" then
62	n.err("Invalid file content, base64 decode")
63end
64
65-- b64
66f.encoding = "b64"
67str = addfile_and_getres(f)
68if str ~= "bla\n" then
69	n.err("Invalid file content, b64 decode")
70	print("==>" .. str .. "<==")
71end
72