monotone

monotone Mtn Source Tree

Root/lua-testsuite.lua

  • Property mtn:execute set to true
1#!./tester
2
3monotone_path = nil
4
5function safe_mtn(...)
6 if monotone_path == nil then
7 monotone_path = os.getenv("mtn")
8 if monotone_path == nil then
9 err("'mtn' environment variable not set")
10 end
11 end
12 return {monotone_path, "--norc", "--root=" .. test.root,
13 "--confdir="..test.root, unpack(arg)}
14end
15
16-- function preexecute(x)
17-- return {"valgrind", "--tool=memcheck", unpack(x)}
18-- end
19
20function raw_mtn(...)
21 if preexecute ~= nil then
22 return preexecute(safe_mtn(unpack(arg)))
23 else
24 return safe_mtn(unpack(arg))
25 end
26end
27
28function mtn(...)
29 return raw_mtn("--rcfile", test.root .. "/test_hooks.lua", -- "--nostd",
30 "--db=" .. test.root .. "/test.db",
31 "--keydir", test.root .. "/keys",
32 "--key=tester@test.net", unpack(arg))
33end
34
35function nodb_mtn(...)
36 return raw_mtn("--rcfile", test.root .. "/test_hooks.lua", -- "--nostd",
37 "--keydir", test.root .. "/keys",
38 "--key=tester@test.net", unpack(arg))
39end
40
41function minhooks_mtn(...)
42 return raw_mtn("--db=" .. test.root .. "/test.db",
43 "--keydir", test.root .. "/keys",
44 "--rcfile", test.root .. "/min_hooks.lua",
45 "--key=tester@test.net", unpack(arg))
46end
47
48function commit(branch, message, mt)
49 if branch == nil then branch = "testbranch" end
50 if message == nil then message = "blah-blah" end
51 if mt == nil then mt = mtn end
52 check(mt("commit", "--message", message, "--branch", branch), 0, false, false)
53end
54
55function sha1(what)
56 check(safe_mtn("identify", what), 0, false, false)
57 return trim(readfile("ts-stdout"))
58end
59
60function probe_node(filename, rsha, fsha)
61 remove("_MTN.old")
62 rename("_MTN", "_MTN.old")
63 remove(filename)
64 check(mtn("checkout", "--revision", rsha, "."), 0, false, true)
65 rename("_MTN.old/options", "_MTN")
66 check(base_revision() == rsha)
67 check(sha1(filename) == fsha)
68end
69
70function mtn_setup()
71 check(getstd("test_keys"))
72 check(getstd("test_hooks.lua"))
73 check(getstd("min_hooks.lua"))
74
75 check(mtn("db", "init"), 0, false, false)
76 check(mtn("read", "test_keys"), 0, false, false)
77 check(mtn("setup", "--branch=testbranch", "."), 0, false, false)
78 remove("test_keys")
79end
80
81function base_revision()
82 local workrev = readfile("_MTN/revision")
83 local extract = string.gsub(workrev, "^.*old_revision %[(%x*)%].*$", "%1")
84 if extract == workrev then
85 err("failed to extract base revision from _MTN/revision")
86 end
87 return extract
88end
89
90function base_manifest()
91 check(safe_mtn("automate", "get_manifest_of", base_revision()), 0, false)
92 check(copy("ts-stdout", "base_manifest_temp"))
93 return sha1("base_manifest_temp")
94end
95
96function certvalue(rev, name)
97 check(safe_mtn("automate", "certs", rev), 0, false)
98 local parsed = parse_basic_io(readfile("ts-stdout"))
99 local cname
100 for _,l in pairs(parsed) do
101 if l.name == "name" then cname = l.values[1] end
102 if cname == name and l.name == "value" then return l.values[1] end
103 end
104 return nil
105end
106
107function qgrep(what, where)
108 local ok,res = pcall(unpack(grep("-q", what, where)))
109 if not ok then err(res) end
110 return res == 0
111end
112
113function addfile(filename, contents, mt)
114 if contents ~= nil then writefile(filename, contents) end
115 if mt == nil then mt = mtn end
116 check(mt("add", filename), 0, false, false)
117end
118
119function adddir(dirname, mt)
120 if not isdir(dirname) then mkdir(dirname) end
121 if mt == nil then mt = mtn end
122 check(mt("add", dirname), 0, false, false)
123end
124
125function revert_to(rev, branch, mt)
126 if type(branch) == "function" then
127 mt = branch
128 branch = nil
129 end
130 if mt == nil then mt = mtn end
131
132 check(mt("automate", "get_manifest_of", base_revision()), 0, true, false)
133 rename("stdout", "paths-new")
134
135 remove("_MTN.old")
136 rename("_MTN", "_MTN.old")
137
138 check(mt("automate", "get_manifest_of", rev), 0, true, false)
139 rename("stdout", "paths-old")
140
141 -- remove all of the files and dirs in this
142 -- manifest to clear the way for checkout
143
144 for path in io.lines("paths-new") do
145 len = string.len(path) - 1
146
147 if (string.match(path, "^ file \"")) then
148 path = string.sub(path, 10, len)
149 elseif (string.match(path, "^dir \"")) then
150 path = string.sub(path, 6, len)
151 else
152 path = ""
153 end
154
155 if (string.len(path) > 0) then
156 remove(path)
157 end
158 end
159
160 for path in io.lines("paths-old") do
161 len = string.len(path) - 1
162
163 if (string.match(path, "^ file \"")) then
164 path = string.sub(path, 10, len)
165 elseif (string.match(path, "^dir \"")) then
166 path = string.sub(path, 6, len)
167 else
168 path = ""
169 end
170
171 if (string.len(path) > 0) then
172 remove(path)
173 end
174 end
175
176 if branch == nil then
177 check(mt("checkout", "--revision", rev, "."), 0, false, true)
178 else
179 check(mt("checkout", "--branch", branch, "--revision", rev, "."), 0, false, true)
180 end
181 check(base_revision() == rev)
182end
183
184function canonicalize(filename)
185 if ostype == "Windows" then
186 L("Canonicalizing ", filename, "\n")
187 local f = io.open(filename, "rb")
188 local indat = f:read("*a")
189 f:close()
190 local outdat = string.gsub(indat, "\r\n", "\n")
191 f = io.open(filename, "wb")
192 f:write(outdat)
193 f:close()
194 else
195 L("Canonicalization not needed (", filename, ")\n")
196 end
197end
198
199function check_same_db_contents(db1, db2)
200 check_same_stdout(mtn("--db", db1, "ls", "keys"),
201 mtn("--db", db2, "ls", "keys"))
202
203 check(mtn("--db", db1, "complete", "revision", ""), 0, true, false)
204 rename("stdout", "revs")
205 check(mtn("--db", db2, "complete", "revision", ""), 0, true, false)
206 check(samefile("stdout", "revs"))
207 for rev in io.lines("revs") do
208 rev = trim(rev)
209 check_same_stdout(mtn("--db", db1, "automate", "certs", rev),
210 mtn("--db", db2, "automate", "certs", rev))
211 check_same_stdout(mtn("--db", db1, "automate", "get_revision", rev),
212 mtn("--db", db2, "automate", "get_revision", rev))
213 check_same_stdout(mtn("--db", db1, "automate", "get_manifest_of", rev),
214 mtn("--db", db2, "automate", "get_manifest_of", rev))
215 end
216
217 check(mtn("--db", db1, "complete", "file", ""), 0, true, false)
218 rename("stdout", "files")
219 check(mtn("--db", db2, "complete", "file", ""), 0, true, false)
220 check(samefile("stdout", "files"))
221 for file in io.lines("files") do
222 file = trim(file)
223 check_same_stdout(mtn("--db", db1, "automate", "get_file", file),
224 mtn("--db", db2, "automate", "get_file", file))
225 end
226end
227
228-- maybe these should go in tester.lua?
229function do_check_same_stdout(cmd1, cmd2)
230 check(cmd1, 0, true, false)
231 rename("stdout", "stdout-first")
232 check(cmd2, 0, true, false)
233 rename("stdout", "stdout-second")
234 check(samefile("stdout-first", "stdout-second"))
235end
236function do_check_different_stdout(cmd1, cmd2)
237 check(cmd1, 0, true, false)
238 rename("stdout", "stdout-first")
239 check(cmd2, 0, true, false)
240 rename("stdout", "stdout-second")
241 check(not samefile("stdout-first", "stdout-second"))
242end
243function check_same_stdout(a, b, c)
244 if type(a) == "table" and type(b) == "table" then
245 return do_check_same_stdout(a, b)
246 elseif type(a) == "table" and type(b) == "function" and type(c) == "function" then
247 return do_check_same_stdout(b(unpack(a)), c(unpack(a)))
248 elseif type(a) == "table" and type(b) == "nil" and type(c) == "nil" then
249 return do_check_same_stdout(mtn(unpack(a)), mtn2(unpack(a)))
250 else
251 err("bad arguments ("..type(a)..", "..type(b)..", "..type(c)..") to check_same_stdout")
252 end
253end
254function check_different_stdout(a, b, c)
255 if type(a) == "table" and type(b) == "table" then
256 return do_check_different_stdout(a, b)
257 elseif type(a) == "table" and type(b) == "function" and type(c) == "function" then
258 return do_check_different_stdout(b(unpack(a)), c(unpack(a)))
259 elseif type(a) == "table" and type(b) == "nil" and type(c) == "nil" then
260 return do_check_different_stdout(mtn(unpack(a)), mtn2(unpack(a)))
261 else
262 err("bad arguments ("..type(a)..", "..type(b)..", "..type(c)..") to check_different_stdout")
263 end
264end
265
266function write_large_file(name, size)
267 local file = io.open(name, "wb")
268 for i = 1,size do
269 for j = 1,128 do -- write 1MB
270 local str8k = ""
271 for k = 1,256 do
272 -- 32
273 str8k = str8k .. string.char(math.random(255), math.random(255),
274 math.random(255), math.random(255),
275 math.random(255), math.random(255),
276 math.random(255), math.random(255),
277 math.random(255), math.random(255),
278 math.random(255), math.random(255),
279 math.random(255), math.random(255),
280 math.random(255), math.random(255),
281 math.random(255), math.random(255),
282 math.random(255), math.random(255),
283 math.random(255), math.random(255),
284 math.random(255), math.random(255),
285 math.random(255), math.random(255),
286 math.random(255), math.random(255),
287 math.random(255), math.random(255),
288 math.random(255), math.random(255))
289 end
290 file:write(str8k)
291 end
292 end
293 file:close()
294end
295
296------------------------------------------------------------------------
297--====================================================================--
298------------------------------------------------------------------------
299testdir = srcdir.."/tests"
300
301function prepare_to_run_tests (P)
302 -- We have a bunch of tests that depend on being able to create
303 -- files or directories that we cannot read or write (mostly to
304 -- test error handling behavior).
305 require_not_root()
306
307 -- Several tests require the ability to create temporary
308 -- directories outside the workspace.
309 local d = make_temp_dir()
310 if d == nil then
311 P("This test suite requires the ability to create files\n"..
312 "in the system-wide temporary directory. Please correct the\n"..
313 "access permissions on this directory and try again.\n")
314 return 1
315 end
316 unlogged_remove(d)
317
318 monotone_path = getpathof("mtn")
319 if monotone_path == nil then monotone_path = "mtn" end
320 set_env("mtn", monotone_path)
321
322 writefile_q("in", nil)
323 prepare_redirect("in", "out", "err")
324
325 local status = execute(monotone_path, "version", "--full")
326 local out = readfile_q("out")
327 local err = readfile_q("err")
328
329 if status == 0 and err == "" and out ~= "" then
330 logfile:write(out)
331 else
332 P(string.format("mtn version --full: exit %d\nstdout:\n", status))
333 P(out)
334 P("stderr:\n")
335 P(err)
336
337 if status == 0 then status = 1 end
338 end
339
340 unlogged_remove("in")
341 unlogged_remove("out")
342 unlogged_remove("err")
343 return status
344end

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status