File: lua-testsuite.lua

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (431 lines) | stat: -rwxr-xr-x 14,372 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-- Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
--
-- This program is made available under the GNU GPL version 2.0 or
-- greater. See the accompanying file COPYING for details.
--
-- This program is distributed WITHOUT ANY WARRANTY; without even the
-- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE.

monotone_path = nil
no_network_tests = false

function safe_mtn(...)
  if monotone_path == nil then
    monotone_path = os.getenv("mtn")
    if monotone_path == nil then
      err("'mtn' environment variable not set")
    end
  end
  return {monotone_path, "--norc", "--root=" .. test.root,
          "--confdir="..test.root, ...}
end

function mtn_ws_opts(...)
  -- Return a mtn command string that uses options from _MTN/options,
  -- root from current directory - as close to a normal user command
  -- line as possible.
  if monotone_path == nil then
    monotone_path = os.getenv("mtn")
    if monotone_path == nil then
      err("'mtn' environment variable not set")
    end
  end
  return {monotone_path, "--ssh-sign=no", "--no-default-confdir", "--norc", "--rcfile", test.root .. "/test_hooks.lua", ...}
end

function mtn_outside_ws(...)
  -- Return a mtn command string that is executed outside a workspace,
  -- but specifies all appropriate options.
  if monotone_path == nil then
    monotone_path = os.getenv("mtn")
    if monotone_path == nil then
      err("'mtn' environment variable not set")
    end
  end
  return {monotone_path,
         "--no-workspace",
         "--ssh-sign=no",
         "--norc", "--rcfile", test.root .. "/test_hooks.lua",
         "--confdir="..test.root,
         "--db=" .. test.root .. "/test.db",
         "--keydir", test.root .. "/keys",
         "--key=tester@test.net",
         ...}
end

-- function preexecute(x)
--   return {"valgrind", "--tool=memcheck", unpack(x)}
-- end

function raw_mtn(...)
  if preexecute ~= nil then
    return preexecute(safe_mtn(...))
  else
    return safe_mtn(...)
  end
end

function mtn(...)
  return raw_mtn("--rcfile", test.root .. "/test_hooks.lua", -- "--nostd",
         "--db=" .. test.root .. "/test.db",
         "--keydir", test.root .. "/keys",
         "--key=tester@test.net", ...)
end

function nodb_mtn(...)
  return raw_mtn("--rcfile", test.root .. "/test_hooks.lua", -- "--nostd",
         "--keydir", test.root .. "/keys",
         "--key=tester@test.net", ...)
end

function nokey_mtn(...)
  return raw_mtn("--rcfile", test.root .. "/test_hooks.lua", -- "--nostd",
         "--db=" .. test.root .. "/test.db",
         "--keydir", test.root .. "/keys", ...)
end

function minhooks_mtn(...)
  return raw_mtn("--db=" .. test.root .. "/test.db",
                 "--keydir", test.root .. "/keys",
                 "--rcfile", test.root .. "/min_hooks.lua",
                 "--key=tester@test.net", ...)
end

function commit(branch, message, mt)
  if branch == nil then branch = "testbranch" end
  if message == nil then message = "blah-blah" end
  if mt == nil then mt = mtn end
  check(mt("commit", "--message", message, "--branch", branch), 0, false, false)
end

function sha1(what)
  check(safe_mtn("identify", what), 0, false, false)
  return trim(readfile("ts-stdout"))
end

function probe_node(filename, rsha, fsha)
  remove("_MTN.old")
  rename("_MTN", "_MTN.old")
  remove(filename)
  check(mtn("checkout", "--revision", rsha, "."), 0, false, true)
  rename("_MTN.old/options", "_MTN")
  check(base_revision() == rsha)
  check(sha1(filename) == fsha)
end

function mtn_setup()
  check(getstd("test_keys"))
  check(getstd("test_hooks.lua"))
  check(getstd("min_hooks.lua"))

  check(mtn("db", "init"), 0, false, false)
  check(mtn("read", "test_keys"), 0, false, false)
  check(mtn("setup", "--branch=testbranch", "."), 0, false, false)
  remove("test_keys")
end

function base_revision()
  local workrev = readfile("_MTN/revision")
  local extract = string.gsub(workrev, "^.*old_revision %[(%x*)%].*$", "%1")
  if extract == workrev then
    err("failed to extract base revision from _MTN/revision")
  end
  return extract
end

function base_manifest()
  check(safe_mtn("automate", "get_manifest_of", base_revision()), 0, false)
  check(copy("ts-stdout", "base_manifest_temp"))
  return sha1("base_manifest_temp")
end

function certvalue(rev, name)
  check(safe_mtn("automate", "certs", rev), 0, false)
  local parsed = parse_basic_io(readfile("ts-stdout"))
  local cname
  local goodsig
  -- note: this relies on the name and signature elements appearing
  -- before the value element, in each stanza.
  for _,l in pairs(parsed) do
    if l.name == "name" then cname = l.values[1] end
    if l.name == "signature" then goodsig = l.values[1] end
    if cname == name and l.name == "value" then return l.values[1], goodsig end
  end
  return nil
end

function qgrep(what, where)
  local ok,res = pcall(unpack(grep("-q", what, where)))
  if not ok then err(res) end
  return res == 0
end

function addfile(filename, contents, mt)
  if contents ~= nil then writefile(filename, contents) end
  if mt == nil then mt = mtn end
  check(mt("add", filename), 0, false, false)
end

function adddir(dirname, mt)
  if not isdir(dirname) then mkdir(dirname) end
  if mt == nil then mt = mtn end
  check(mt("add", dirname), 0, false, false)
end

function revert_to(rev, branch, mt)
  if type(branch) == "function" then
    mt = branch
    branch = nil
  end
  if mt == nil then mt = mtn end

  check(mt("automate", "get_manifest_of", base_revision()), 0, true, false)
  rename("stdout", "paths-new")

  remove("_MTN.old")
  rename("_MTN", "_MTN.old")

  check(mt("automate", "get_manifest_of", rev), 0, true, false)
  rename("stdout", "paths-old")

  -- remove all of the files and dirs in this
  -- manifest to clear the way for checkout

  for path in io.lines("paths-new") do
    len = string.len(path) - 1

    if (string.match(path, "^   file \"")) then
      path = string.sub(path, 10, len)
    elseif (string.match(path, "^dir \"")) then
      path = string.sub(path, 6, len)
    else
      path = ""
    end

    if (string.len(path) > 0) then
      remove(path)
    end
  end

  for path in io.lines("paths-old") do
    len = string.len(path) - 1

    if (string.match(path, "^   file \"")) then
      path = string.sub(path, 10, len)
    elseif (string.match(path, "^dir \"")) then
      path = string.sub(path, 6, len)
    else
      path = ""
    end

    if (string.len(path) > 0) then
      remove(path)
    end
  end

  if branch == nil then
    check(mt("checkout", "--revision", rev, "."), 0, false, true)
  else
    check(mt("checkout", "--branch", branch, "--revision", rev, "."), 0, false, true)
  end
  check(base_revision() == rev)
end

function canonicalize(filename)
  if ostype == "Windows" then
    L("Canonicalizing ", filename, "\n")
    local f = io.open(filename, "rb")
    local indat = f:read("*a")
    f:close()
    local outdat = string.gsub(indat, "\r\n", "\n")
    f = io.open(filename, "wb")
    f:write(outdat)
    f:close()
  else
    L("Canonicalization not needed (", filename, ")\n")
  end
end

function check_same_db_contents(db1, db2)
  check_same_stdout(mtn("--db", db1, "ls", "keys"),
                    mtn("--db", db2, "ls", "keys"))

  check(mtn("--db", db1, "complete", "revision", ""), 0, true, false)
  rename("stdout", "revs")
  check(mtn("--db", db2, "complete", "revision", ""), 0, true, false)
  check(samefile("stdout", "revs"))
  for rev in io.lines("revs") do
    rev = trim(rev)
    check_same_stdout(mtn("--db", db1, "automate", "certs", rev),
                      mtn("--db", db2, "automate", "certs", rev))
    check_same_stdout(mtn("--db", db1, "automate", "get_revision", rev),
                      mtn("--db", db2, "automate", "get_revision", rev))
    check_same_stdout(mtn("--db", db1, "automate", "get_manifest_of", rev),
                      mtn("--db", db2, "automate", "get_manifest_of", rev))
  end

  check(mtn("--db", db1, "complete", "file", ""), 0, true, false)
  rename("stdout", "files")
  check(mtn("--db", db2, "complete", "file", ""), 0, true, false)
  check(samefile("stdout", "files"))
  for file in io.lines("files") do
    file = trim(file)
    check_same_stdout(mtn("--db", db1, "automate", "get_file", file),
                      mtn("--db", db2, "automate", "get_file", file))
 end

 check_same_stdout(mtn("--db", db1, "db", "execute", "select hex(hash) from revision_certs order by 1"),
                   mtn("--db", db2, "db", "execute", "select hex(hash) from revision_certs order by 1"))
end

-- maybe these should go in tester.lua?
function do_check_same_stdout(cmd1, cmd2)
  check(cmd1, 0, true, false)
  rename("stdout", "stdout-first")
  check(cmd2, 0, true, false)
  rename("stdout", "stdout-second")
  check(samefile("stdout-first", "stdout-second"))
end
function do_check_different_stdout(cmd1, cmd2)
  check(cmd1, 0, true, false)
  rename("stdout", "stdout-first")
  check(cmd2, 0, true, false)
  rename("stdout", "stdout-second")
  check(not samefile("stdout-first", "stdout-second"))
end
function check_same_stdout(a, b, c)
  if type(a) == "table" and type(b) == "table" then
    return do_check_same_stdout(a, b)
  elseif type(a) == "table" and type(b) == "function" and type(c) == "function" then
    return do_check_same_stdout(b(unpack(a)), c(unpack(a)))
  elseif type(a) == "table" and type(b) == "nil" and type(c) == "nil" then
    return do_check_same_stdout(mtn(unpack(a)), mtn2(unpack(a)))
  else
    err("bad arguments ("..type(a)..", "..type(b)..", "..type(c)..") to check_same_stdout")
  end
end
function check_different_stdout(a, b, c)
  if type(a) == "table" and type(b) == "table" then
    return do_check_different_stdout(a, b)
  elseif type(a) == "table" and type(b) == "function" and type(c) == "function" then
    return do_check_different_stdout(b(unpack(a)), c(unpack(a)))
  elseif type(a) == "table" and type(b) == "nil" and type(c) == "nil" then
    return do_check_different_stdout(mtn(unpack(a)), mtn2(unpack(a)))
  else
    err("bad arguments ("..type(a)..", "..type(b)..", "..type(c)..") to check_different_stdout")
  end
end

function write_large_file(name, size)
  local file = io.open(name, "wb")
  for i = 1,size do
    for j = 1,128 do -- write 1MB
      local str8k = ""
      for k = 1,256 do
        -- 32
        str8k = str8k .. string.char(math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255),
                                     math.random(255), math.random(255))
      end
      file:write(str8k)
    end
  end
  file:close()
end

------------------------------------------------------------------------
--====================================================================--
------------------------------------------------------------------------
testdir = srcdir.."/tests"

function prepare_to_run_tests (P)
   -- We have a bunch of tests that depend on being able to create
   -- files or directories that we cannot read or write (mostly to
   -- test error handling behavior).
   require_not_root()

   -- Several tests require the ability to create temporary
   -- directories outside the workspace.
   local d = make_temp_dir()
   if d == nil then
      P("This test suite requires the ability to create files\n"..
        "in the system-wide temporary directory.  Please correct the\n"..
        "access permissions on this directory and try again.\n")
      return 1
   end
   unlogged_remove(d)

   -- Several tests require the ability to run a network server on
   -- the loopback interface, and connect to it from another process
   -- on this computer.  Unlike the above, we just skip those tests
   -- (loudly) if we can't do that.  Verifying that this is possible
   -- requires a helper program.

   local checknet = getpathof("check_net")
   if os.getenv("DISABLE_NETWORK_TESTS") ~= nil then
      P("warning: DISABLE_NETWORK_TESTS set, skipping network server tests\n")
      no_network_tests = true
   elseif checknet == nil then
      P("warning: check_net helper is missing, skipping network server tests\n")
      no_network_tests = true
   else
      writefile_q("in", nil)
      prepare_redirect("in", "out", "err")
      local status = execute(checknet)
      local out = readfile_q("out")
      local err = readfile_q("err")

      if status == 0 and err == "" and out == "" then
	 logfile:write("check_net: Can use the loopback interface.\n")
      else
	 logfile:write(string.format("check_net: failed with status %d\n"..
				     "stdout:\n%s\nstderr:\n%s\n",
				     status, out, err))
	 P("warning: network unavailable, skipping network server tests\n")
	 no_network_tests = true
      end
   end

   -- Record the full version of monotone under test in the logfile.

   monotone_path = getpathof("mtn")
   if monotone_path == nil then monotone_path = "mtn" end
   set_env("mtn", monotone_path)

   writefile_q("in", nil)
   prepare_redirect("in", "out", "err")

   local status = execute(monotone_path, "version", "--full")
   local out = readfile_q("out")
   local err = readfile_q("err")

   if status == 0 and err == "" and out ~= "" then
      logfile:write(out)
   else
      P(string.format("mtn version --full: exit %d\nstdout:\n", status))
      P(out)
      P("stderr:\n")
      P(err)

      if status == 0 then status = 1 end
   end

   unlogged_remove("in")
   unlogged_remove("out")
   unlogged_remove("err")
   return status
end