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

Archive Download this file

Branches

Tags

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