summaryrefslogtreecommitdiff
path: root/libexec/nuageinit/tests/addfile.lua
blob: ea98369f19090c9cc74fc952aad6fbcc747065c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/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