Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/libexec/nuageinit/tests/addfile.lua
35072 views
1
#!/bin/libexec/flua
2
3
local n = require("nuage")
4
local lfs = require("lfs")
5
6
local f = {
7
content = "plop"
8
}
9
10
local r, err = n.addfile(f, false)
11
if 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")
13
end
14
15
local 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
29
end
30
31
-- simple file
32
f.path="/tmp/testnuage"
33
local str = addfile_and_getres(f)
34
if str ~= f.content then
35
n.err("Invalid file content")
36
end
37
38
-- the file is overwriten
39
f.content = "test"
40
41
str = addfile_and_getres(f)
42
if str ~= f.content then
43
n.err("Invalid file content, not overwritten")
44
end
45
46
-- try to append now
47
f.content = "more"
48
f.append = true
49
50
str = addfile_and_getres(f)
51
if str ~= "test" .. f.content then
52
n.err("Invalid file content, not appended")
53
end
54
55
-- base64
56
f.content = "YmxhCg=="
57
f.encoding = "base64"
58
f.append = false
59
60
str = addfile_and_getres(f)
61
if str ~= "bla\n" then
62
n.err("Invalid file content, base64 decode")
63
end
64
65
-- b64
66
f.encoding = "b64"
67
str = addfile_and_getres(f)
68
if str ~= "bla\n" then
69
n.err("Invalid file content, b64 decode")
70
print("==>" .. str .. "<==")
71
end
72
73