2010-11-18 16:33:48 +00:00
|
|
|
#!/usr/bin/lua
|
|
|
|
-- a heavy duty test.
|
|
|
|
-- makes thousends of random changes to the source tree
|
|
|
|
-- checks every X changes if lsyncd managed to keep target tree in sync.
|
|
|
|
require("posix")
|
|
|
|
dofile("tests/testlib.lua")
|
|
|
|
|
|
|
|
-- always makes the same "random", so failures can be debugged.
|
2010-11-19 16:46:26 +00:00
|
|
|
math.randomseed(2)
|
2010-11-18 16:33:48 +00:00
|
|
|
|
2010-11-18 16:59:50 +00:00
|
|
|
local tdir = mktempd().."/"
|
2010-11-18 16:33:48 +00:00
|
|
|
cwriteln("using ", tdir, " as test root")
|
|
|
|
|
|
|
|
local srcdir = tdir.."src/"
|
|
|
|
local trgdir = tdir.."trg/"
|
|
|
|
|
|
|
|
posix.mkdir(srcdir)
|
|
|
|
posix.mkdir(trgdir)
|
2010-11-18 23:35:56 +00:00
|
|
|
-- local pid = spawn("./lsyncd","-nodaemon","-rsync",srcdir,trgdir)
|
2010-11-18 16:33:48 +00:00
|
|
|
|
2010-11-18 16:59:50 +00:00
|
|
|
cwriteln("waiting for Lsyncd to startup")
|
2010-11-18 16:33:48 +00:00
|
|
|
posix.sleep(1)
|
|
|
|
|
|
|
|
-- all dirs created, indexed by integer and path
|
2010-11-18 23:35:56 +00:00
|
|
|
root = {name=""}
|
|
|
|
alldirs = {root}
|
2010-11-19 11:21:12 +00:00
|
|
|
dirsWithFileI = {}
|
|
|
|
dirsWithFileD = {}
|
2010-11-18 23:35:56 +00:00
|
|
|
|
|
|
|
-----
|
|
|
|
-- returns the name of a directory
|
|
|
|
-- call it with name=nil
|
|
|
|
local function dirname(dir, name)
|
|
|
|
name = name or ""
|
|
|
|
if not dir then
|
|
|
|
return name
|
|
|
|
end
|
|
|
|
return dirname(dir.parent, dir.name .. "/" .. name)
|
|
|
|
end
|
|
|
|
|
2010-11-19 17:37:38 +00:00
|
|
|
-----
|
|
|
|
-- Picks a random dir.
|
|
|
|
--
|
|
|
|
local function pickDir(notRoot)
|
|
|
|
if notRoot then
|
|
|
|
if #alldirs <= 2 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return alldirs[2, math.random(#alldirs)]
|
|
|
|
end
|
|
|
|
return alldirs[math.random(#alldirs)]
|
|
|
|
end
|
|
|
|
|
|
|
|
----
|
|
|
|
-- Picks a random file.
|
|
|
|
--
|
|
|
|
-- Returns 3 values:
|
|
|
|
-- * the directory
|
|
|
|
-- * the filename
|
|
|
|
-- * number of files in directory
|
|
|
|
--
|
|
|
|
local function pickFile()
|
|
|
|
-- picks the random directory
|
|
|
|
if #dirsWithFileI < 1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local rdir = dirsWithFileI[math.random(1, #dirsWithFileI)]
|
|
|
|
if not rdir then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- counts the files in there
|
|
|
|
local c = 0
|
|
|
|
for name, _ in pairs(rdir) do
|
|
|
|
if #name == 2 then
|
|
|
|
c = c + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- picks one file at random
|
|
|
|
local cr = math.random(1, c)
|
|
|
|
local fn
|
|
|
|
for name, _ in pairs(rdir) do
|
|
|
|
if #name == 2 then
|
|
|
|
-- filenames are 2 chars wide.
|
|
|
|
cr = cr - 1
|
|
|
|
if cr == 0 then
|
|
|
|
fn = name
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return rdir, fn, c
|
|
|
|
end
|
|
|
|
|
|
|
|
-----
|
|
|
|
-- Removes a reference to a file
|
|
|
|
--
|
|
|
|
-- @param dir --- directory reference
|
|
|
|
-- @param fn --- filename
|
|
|
|
-- @param c --- number of files in dir
|
|
|
|
--
|
|
|
|
local function rmFileReference(dir, fn, c)
|
|
|
|
dir[fn] = nil
|
|
|
|
if c == 1 then
|
|
|
|
-- if last file from origin dir, it has no files anymore
|
|
|
|
for i, v in ipairs(dirsWithFileI) do
|
|
|
|
if v == dir then
|
|
|
|
table.remove(dirsWithFileI, i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dirsWithFileD[dir] = nil
|
|
|
|
end
|
|
|
|
end
|
2010-11-18 16:33:48 +00:00
|
|
|
|
2010-11-18 16:59:50 +00:00
|
|
|
cwriteln("making random data")
|
2010-11-19 16:46:26 +00:00
|
|
|
for ai=1,15 do
|
2010-11-18 16:33:48 +00:00
|
|
|
-- throw a die what to do
|
2010-11-19 17:37:38 +00:00
|
|
|
local acn = math.random(5)
|
2010-11-18 16:33:48 +00:00
|
|
|
|
2010-11-19 10:38:21 +00:00
|
|
|
if acn <= 1 then
|
|
|
|
-- creates a directory
|
2010-11-18 16:33:48 +00:00
|
|
|
-- chooses a random directory to create it into
|
2010-11-19 17:37:38 +00:00
|
|
|
local rdir = pickDir()
|
2010-11-18 23:35:56 +00:00
|
|
|
-- creates a new random one letter name
|
|
|
|
local nn = string.char(96 + math.random(26))
|
2010-11-19 10:38:21 +00:00
|
|
|
if not rdir[nn] then
|
2010-11-18 23:35:56 +00:00
|
|
|
local ndir = {
|
|
|
|
name = nn,
|
2010-11-19 10:38:21 +00:00
|
|
|
parent = rdir,
|
2010-11-18 23:35:56 +00:00
|
|
|
}
|
|
|
|
local dn = dirname(ndir)
|
2010-11-19 10:38:21 +00:00
|
|
|
rdir[nn] = dn
|
2010-11-18 23:35:56 +00:00
|
|
|
table.insert(alldirs, ndir)
|
2010-11-19 10:38:21 +00:00
|
|
|
cwriteln("mkdir "..srcdir..dn)
|
2010-11-18 23:35:56 +00:00
|
|
|
posix.mkdir(srcdir..dn)
|
2010-11-18 16:33:48 +00:00
|
|
|
end
|
2010-11-19 10:38:21 +00:00
|
|
|
elseif acn <= 2 then
|
|
|
|
-- creates a file
|
2010-11-18 23:35:56 +00:00
|
|
|
-- chooses a random directory to create it into
|
2010-11-19 17:37:38 +00:00
|
|
|
local rdir = pickDir()
|
2010-11-18 23:35:56 +00:00
|
|
|
-- creates a new random one letter name
|
|
|
|
local nn = 'f'..string.char(96 + math.random(26))
|
2010-11-19 10:38:21 +00:00
|
|
|
local fn = dirname(rdir) .. nn
|
|
|
|
cwriteln("mkfile "..srcdir..fn)
|
|
|
|
local f = io.open(srcdir..fn, "w")
|
|
|
|
if f then
|
|
|
|
for i=1,10 do
|
|
|
|
f:write(string.char(96 + math.random(26)))
|
|
|
|
end
|
|
|
|
f:write('\n')
|
|
|
|
f:close()
|
2010-11-19 11:21:12 +00:00
|
|
|
rdir[nn]=true
|
|
|
|
if not dirsWithFileD[rdir] then
|
|
|
|
table.insert(dirsWithFileI, rdir)
|
|
|
|
dirsWithFileD[rdir]=true
|
|
|
|
end
|
2010-11-19 10:38:21 +00:00
|
|
|
end
|
|
|
|
elseif acn <= 3 then
|
|
|
|
-- moves a directory
|
|
|
|
if #alldirs > 2 then
|
|
|
|
-- chooses a random directory to move
|
2010-11-19 17:37:38 +00:00
|
|
|
local odir = pickDir()
|
2010-11-19 10:38:21 +00:00
|
|
|
-- chooses a random directory to move to
|
2010-11-19 17:37:38 +00:00
|
|
|
local tdir = pickDir(true)
|
2010-11-19 10:38:21 +00:00
|
|
|
if tdir[odir.name] == nil then
|
|
|
|
-- origin name not in target dir already
|
|
|
|
local on = dirname(odir)
|
|
|
|
local tn = dirname(tdir)
|
2010-11-19 11:21:12 +00:00
|
|
|
cwriteln("mvdir ",srcdir,on," -> ",srcdir,tn,odir.name)
|
2010-11-19 10:38:21 +00:00
|
|
|
os.rename(srcdir..on, srcdir..tn..odir.name)
|
|
|
|
odir.parent[odir.name] = nil
|
2010-11-19 16:46:26 +00:00
|
|
|
odir.parent = tdir
|
2010-11-19 10:38:21 +00:00
|
|
|
tdir[odir.name] = odir
|
|
|
|
end
|
|
|
|
end
|
2010-11-19 11:21:12 +00:00
|
|
|
elseif acn <= 4 then
|
|
|
|
-- moves a file
|
2010-11-19 17:37:38 +00:00
|
|
|
local odir, fn, c = pickFile()
|
|
|
|
if odir then
|
2010-11-19 11:21:12 +00:00
|
|
|
-- picks a directory with a file at random
|
|
|
|
-- picks a target directory at random
|
2010-11-19 17:37:38 +00:00
|
|
|
local tdir = pickdir()
|
2010-11-19 11:21:12 +00:00
|
|
|
local on = dirname(odir)
|
|
|
|
local tn = dirname(tdir)
|
2010-11-19 16:46:26 +00:00
|
|
|
cwriteln("mvfile ",srcdir,on,mn," -> ",srcdir,tn,mn)
|
|
|
|
os.rename(srcdir..on..mn, srcdir..tn..mn)
|
2010-11-19 17:37:38 +00:00
|
|
|
rmFileReference(odir, mn, c)
|
2010-11-19 16:46:26 +00:00
|
|
|
tdir[mn] = true
|
|
|
|
if not dirsWithFileD[tdir] then
|
|
|
|
dirsWithFileD[tdir] = true
|
|
|
|
table.insert(dirsWithFileI, tdir)
|
|
|
|
end
|
2010-11-19 11:21:12 +00:00
|
|
|
end
|
2010-11-19 17:37:38 +00:00
|
|
|
elseif acn <= 5 then
|
|
|
|
-- removes a file
|
|
|
|
local dir, fn, c = pickFile()
|
|
|
|
if dir then
|
|
|
|
local dn = dirname(dir)
|
|
|
|
cwriteln("rmfile ",srcdir,dn,fn)
|
|
|
|
posix.unlink(srcdir..dn..fn)
|
|
|
|
rmFileReference(odir, mn, c)
|
|
|
|
end
|
2010-11-18 16:33:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-18 16:59:50 +00:00
|
|
|
cwriteln("waiting for Lsyncd to finish its jobs.")
|
|
|
|
posix.sleep(20)
|
2010-11-18 16:33:48 +00:00
|
|
|
|
2010-11-18 16:59:50 +00:00
|
|
|
cwriteln("killing the Lsyncd daemon")
|
|
|
|
posix.kill(pid)
|
|
|
|
local _, exitmsg, exitcode = posix.wait(lpid)
|
|
|
|
cwriteln("Exitcode of Lsyncd = ", exitmsg, " ", exitcode)
|
2010-11-18 16:33:48 +00:00
|
|
|
|
2010-11-18 16:59:50 +00:00
|
|
|
exitcode = os.execute("diff -r "..srcdir.." "..trgdir)
|
|
|
|
cwriteln("Exitcode of diff = ", exitcode)
|
|
|
|
os.exit(exitcode)
|
2010-11-18 16:33:48 +00:00
|
|
|
|