monotone

monotone Mtn Source Tree

Root/testsuite.lua

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

Archive Download this file

Branches

Tags

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