File: testlib.lua

package info (click to toggle)
lsyncd 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 484 kB
  • sloc: ansic: 2,640; sh: 81; makefile: 11
file content (502 lines) | stat: -rw-r--r-- 7,564 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
-- common testing environment
posix = require( 'posix' )

-- escape codes to colorize output on terminal
local c1='\027[47;34m'

local c0='\027[0m'

--
-- Writes colorized.
--
function cwriteln
(...)
	io.write( c1, '++ ', ... )
	io.write( c0, '\n' )
end

--
-- Initializes the pseudo random generator
--
-- If environment variable 'SEED' is set,
-- that one is used seed.
--
local seed = os.getenv( 'SEED')  or os.time( )

math.randomseed( seed )

cwriteln( 'random seed: ', seed )

--
-- Creates a tmp directory.
--
-- Returns the name of the directory.
--
function mktempd
( )
	local f = io.popen( 'mktemp -td ltest.XXX', 'r' )

	local s = f:read( '*a' )

	f:close( )

	s = s:gsub( '[\n\r]+', ' ' )

	s = s:match( '^%s*(.-)%s*$' )

	return s
end

--
-- Creates a tmp directory with the
-- typical lsyncd test architecture.
--
-- returns path of tmpdir
--         path of srcdir
--         path of trgdir
--
function mktemps
( )
	local tdir = mktempd() .. '/'
	cwriteln( 'using ', tdir, ' as test root' )
	local srcdir = tdir..'src/'
	local trgdir = tdir..'trg/'
	posix.mkdir( srcdir )
	posix.mkdir( trgdir )
	return tdir, srcdir, trgdir
end

--
-- Writes a file with 'text' in it
-- and adds a newline.
--
function writefile
(
	filename,
	text
)
	local f = io.open( filename, 'w' )

	if not f
	then
		cwriteln( 'Cannot open "'..filename..'" for writing.' )
		return false
	end

	f:write( text )
	f:write( '\n' )
	f:close( )

	return true
end

--
-- Spawns a subprocess.
--
-- Returns the processes pid.
--
function spawn
(...)
	args = { ... }

	cwriteln( 'spawning: ', table.concat( args, ' ' ) )

	local pid = posix.fork( )

	if pid < 0
	then
		cwriteln( 'Error, failed fork!' )

		os.exit( -1 )
	end

	if pid == 0
	then
		posix.exec( ... )
		-- should not return

		cwriteln( 'Error, failed to spawn: ', ... )

		os.exit( -1 )
	end

	return pid
end

--
-- Makes a lot of random data
--
--
function churn
(
	rootdir,  -- the directory to make data in
	n,        -- roughly how much data action will be done
	init      -- if true init random data only, no sleeps or moves
)
	-- all dirs created, indexed by integer and path
	root = { name = '' }
	alldirs = { root }
	dirsWithFileI = { }
	dirsWithFileD = { }

	--
	-- returns the name of a directory
	--
	-- name is internal recursive paramter, keep it nil.
	--
	local function dirname
	(
		dir,
		name
	)
		name = name or ''

		if not dir
		then
			return name
		end

		return dirname( dir.parent, dir.name .. '/' .. name )
	end

	--
	-- Picks a random dir.
	--
	local function pickDir
	(
		notRoot
	)
		if notRoot
		then
			if #alldirs <= 2
			then
				return nil
			end

			return alldirs[ math.random( 2, #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

	--
	-- possible randomized behaviour.
	-- just gives it a pause
	--
	local function sleep
	( )
		cwriteln( '..zzz..' )

		posix.sleep( 1 )
	end

	--
	-- possible randomized behaviour.
	-- creates a directory
	--
	local function mkdir
	( )
		-- chooses a random directory to create it into
		local rdir = pickDir( )

		-- creates a new random one letter name
		local nn = string.char( 96 + math.random( 26 ) )

		if not rdir[nn]
		then
			local ndir = {
				name   = nn,
				parent = rdir,
			}

			local dn = dirname( ndir )

			rdir[ nn ] = dn

			table.insert( alldirs, ndir )

			cwriteln( 'mkdir  '..rootdir..dn )

			posix.mkdir( rootdir..dn )
		end
	end

	--
	-- Possible randomized behaviour:
	-- Creates a file.
	--
	local function mkfile
	( )
		-- chooses a random directory to create it into
		local rdir = pickDir()

		-- creates a new random one letter name
		local nn = 'f'..string.char( 96 + math.random( 26 ) )

		local fn = dirname( rdir ) .. nn

		cwriteln( 'mkfile ' .. rootdir .. fn )

		local f = io.open(rootdir..fn, 'w')

		if f
		then
			for i = 1, 10
			do
				f:write( string.char( 96 + math.random( 26 ) ) )
			end

			f:write( '\n' )

			f:close( )

			rdir[ nn ]=true

			if not dirsWithFileD[ rdir ]
			then
				table.insert( dirsWithFileI, rdir )

				dirsWithFileD[ rdir ]=true
			end
		end
	end

	--
	-- Possible randomized behaviour:
	-- Moves a directory.
	--
	local function mvdir
	( )
		if #alldirs <= 2
		then
			return
		end

		-- chooses a random directory to move
		local odir = pickDir( true )

		-- chooses a random directory to move to
		local tdir = pickDir( )

		-- makes sure tdir is not a subdir of odir
		local dd = tdir

		while dd
		do
			if odir == dd
			then
				return
			end

			dd = dd.parent
		end

		-- origin name in the target dir already
		if tdir[odir.name] ~= nil
		then
			return
		end

		local on = dirname( odir )

		local tn = dirname( tdir )

		cwriteln( 'mvdir  ', rootdir,on, ' -> ', rootdir, tn, odir.name )

		os.rename( rootdir..on, rootdir..tn..odir.name )

		odir.parent[ odir.name ] = nil

		odir.parent = tdir

		tdir[ odir.name ] = odir
	end

	--
	-- possible randomized behaviour,
	-- moves a file.
	--
	local function mvfile
	( )
		local odir, fn, c = pickFile( )

		if not odir
		then
			return
		end

		-- picks a directory with a file at random
		-- picks a target directory at random
		local tdir = pickDir( )

		local on = dirname( odir )

		local tn = dirname( tdir )

		cwriteln( 'mvfile ', rootdir, on, fn, ' -> ', rootdir, tn, fn )

		os.rename( rootdir..on..fn, rootdir..tn..fn )
		rmFileReference( odir, fn, c )

		tdir[ fn ] = true

		if not dirsWithFileD[ tdir ]
		then
			dirsWithFileD[ tdir ] = true

			table.insert( dirsWithFileI, tdir )
		end
	end

	--
	-- Possible randomized behaviour:
	-- Removes a file.
	--
	local function rmfile
	( )
		local dir, fn, c = pickFile( )

		if dir
		then
			local dn = dirname( dir )

			cwriteln( 'rmfile ', rootdir, dn, fn )

			posix.unlink( rootdir..dn..fn )

			rmFileReference( dir, fn, c )
		end
	end

	local dice

	if init
	then
		dice =
		{
			{ 10,   mkfile },
			{ 10, 	mkdir  },
		}
	else
		dice =
		{
			{ 50,	sleep  },
			{ 20,   mkfile },
			{ 20, 	mkdir  },
			{ 20,   mvdir  },
			{ 20,   rmfile },
		}
	end

	cwriteln( 'making random data' )

	local ndice = 0

	for i, d in ipairs( dice )
	do
		ndice = ndice + d[ 1 ]
		d[ 1 ] = ndice
	end

	for ai = 1, n
	do
		-- throws a die what to do
		local acn = math.random( ndice )

		for i, d in ipairs( dice )
		do
			if acn <= d[ 1 ]
			then
				d[ 2 ]( )

				break
			end
		end
	end
end