-
Notifications
You must be signed in to change notification settings - Fork 4
/
runtests
executable file
·484 lines (402 loc) · 13.4 KB
/
runtests
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
#!/bin/bash
# Runtests will run a series of tests on unraid diskmv utilities
################################################################################
### Configurable Parameters
################################################################################
# These can be overridden with environment variables
# User share in which the test data files will be created
testShare=${TESTSHARE:-'testdata'}
# Sub-directory in which the test data files will be created
testDir=${TESTDIR:-'diskmv-testdata'}
# Disk from which the test data files will be moved
srcDisk=${SRCDISK:-'cache'}
# Disk on to which the test data files will be moved
dstDisk=${DSTDISK:-'disk7'}
# Files in which to save the output of tests
tStdOut=${TSTDOUT:-'test_stdout'}
tErrOut=${TERROUT:-'test_errout'}
################################################################################
### Function Definitions
################################################################################
header(){
# A function to put some test header text in the output files
(
echo
echo "***********************************************************************"
echo "*** $1 $(date +'%Y%m%d %H:%M:%S')"
echo "***********************************************************************"
echo
) | tee -a "${tStdOut}" | tee -a "${tErrOut}" #Redirect stdout to the 2 output files
}
runcmd(){
# A function to execute test commands and document it in the output files
echo "# Test command: $@" > >(tee -a "$tStdOut") 2> >(tee -a "$tErrOut")
echo > >(tee -a "$tStdOut") 2> >(tee -a "$tErrOut")
"$@" >> "$tStdOut" 2>> "$tErrOut"
EXITCODE=$?
# Method that keeps stdout and stderr going to the terminal also:
#"$@" > >(tee -a "$tStdOut") 2> >(tee -a "$tErrOut")
echo >> "$tStdOut" 2>> "$tErrOut"
}
evalexit(){
# A function to evaluate exit codes from test commands
if [[ "$EXITCODE" == 0 ]]; then
echo "Test command exited normally."
else
echo "Test command exited abnormally with exit code: $EXITCODE"
fi
EXPECTCODE=${1:-0} # The first positional parameter if it exist, otherwise 0
#echo "EXITCODE: $EXITCODE, EXPECTCODE: $EXPECTCODE"
if [[ "$EXITCODE" == "$EXPECTCODE" ]]; then
testFlag='pass'
((nExitPass+=1))
echo "Exited as expected."
else
testFlag='fail'
((nExitFail+=1))
echo "Failed to exit as expected."
fi
echo
}
evaltest(){
# A funtion to evaluate test results
if [[ $testFlag == 'pass' ]]; then
echo "Test ${green}Passed${reset}"; ((nTestPass+=1))
else
if [[ -d "${srcPath}" ]]; then
echo "Source Directory"
tree "${srcPath}"
else
echo "Source Directory does not exist"
fi
echo
if [[ -d "${dstPath}" ]]; then
echo "Destination Directory"
tree "${dstPath}"
else
echo "Destination Directory does not exist"
fi
echo
echo "Test ${red}Failed${reset}"; ((nTestFail+=1))
sleep 3
fi
echo
# Reset test flag
testFlag=""
}
################################################################################
### Initialization
################################################################################
# Initialize output files
if [[ -e "$tStdOut" ]]; then
echo "Output file: '${tStdOut}' already exists. Moving..."
# Get modified date/time
mDate=$(date --date="@"$(stat --printf="%Y" "$tStdOut") "+%Y%m%d_%H%M%S")
mv -v "$tStdOut"{,".$mDate"}
fi
if [[ -e "$tErrOut" ]]; then
echo "Output file: '${tErrOut}' already exists. Moving..."
# Get modified date/time
mDate=$(date --date="@"$(stat --printf="%Y" "$tErrOut") "+%Y%m%d_%H%M%S")
mv -v "$tErrOut"{,".$mDate"}
fi
# Make sure source and destination disks are there
if [[ ! -d "/mnt/$srcDisk" ]]; then
echo "Specified source directory does not exist: '/mnt/$srcDisk'" >&2
exit 1
fi
if [[ ! -d "/mnt/$dstDisk" ]]; then
echo "Specified destination directory does not exist: '/mnt/$dstDisk'" >&2
exit 1
fi
# Assemble some more complete path parameters
testPath="${testShare}/${testDir}/"
srcPath="/mnt/${srcDisk}/${testPath}"
dstPath="/mnt/${dstDisk}/${testPath}"
# Initialze some counts
nExitPass=0
nExitFail=0
nTestPass=0
nTestFail=0
# Parameters that expand to make fancy colored output
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"
# Make sure no existing data will be over-written or deleted
if [[ -e "/mnt/user/$testShare/$testDir" ]]; then
echo "Specified test data directory cannot be used." >&2
echo "Delete existing file: '$testPath' or specify a different name in this script" >&2
exit 1
else
mkdir --parents "${srcPath}"
fi
################################################################################
### Create Data
################################################################################
################################################################################
### Do some tests (finally)
################################################################################
################################################################################
header "Help option"
runcmd diskmv -h
evalexit 0 #Expect exit code 0
evaltest
################################################################################
header "Default mode is test"
touch "${srcPath}a"
runcmd diskmv "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ ! -f "${srcPath}a" ]] || [[ -f "${dstPath}a" ]]; then
testFlag='fail'
fi
evaltest
################################################################################
header ""
################################################################################
header "Keep source file option"
mkdir -p "${srcPath}"{rst,'Archer (2009)'}
mkdir -p "${dstPath}rst"
touch "${srcPath}"{a,b,"e f"}
touch "${dstPath}"{c,d}
touch "${srcPath}Archer (2009)/"{testfile.nfo,testfile.mp4}
runcmd diskmv -f -k "$testPath" "$srcDisk" "$dstDisk"
evalexit 0
# Test criteria
if [[ ! -f "${srcPath}a" ]] ||
[[ ! -f "${srcPath}b" ]] ||
[[ ! -f "${srcPath}e f" ]] ||
[[ ! -f "${srcPath}Archer (2009)/testfile.mp4" ]] ||
[[ ! -f "${srcPath}Archer (2009)/testfile.nfo" ]] ||
[[ ! -d "${srcPath}rst" ]] ||
[[ ! -f "${dstPath}a" ]] ||
[[ ! -f "${dstPath}b" ]] ||
[[ ! -f "${dstPath}c" ]] ||
[[ ! -f "${dstPath}d" ]] ||
[[ ! -f "${dstPath}e f" ]] ||
[[ ! -f "${dstPath}Archer (2009)/testfile.mp4" ]] ||
[[ ! -f "${dstPath}Archer (2009)/testfile.nfo" ]] ||
[[ ! -d "${dstPath}rst" ]]
then
testFlag='fail'
fi
evaltest
rm -r "${dstPath}"
################################################################################
header "Move a bunch of files"
mkdir -p "${dstPath}"
touch "${dstPath}"{c,d}
runcmd diskmv -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0
# Test criteria
if [[ -d "${srcPath}" ]] ||
[[ ! -f "${dstPath}a" ]] ||
[[ ! -f "${dstPath}b" ]] ||
[[ ! -f "${dstPath}c" ]] ||
[[ ! -f "${dstPath}d" ]] ||
[[ ! -f "${dstPath}e f" ]] ||
[[ ! -f "${dstPath}Archer (2009)/testfile.mp4" ]] ||
[[ ! -f "${dstPath}Archer (2009)/testfile.nfo" ]] ||
[[ ! -d "${dstPath}rst" ]]
then
testFlag='fail'
fi
evaltest
rm -r "${dstPath}"
################################################################################
header "Link option"
mkdir -p "${srcPath}"
touch "${srcPath}"{a,b}
ln -s "/mnt/user/${testPath}a" "${srcPath}alink"
ln -s "${srcPath}b" "${srcPath}blink"
runcmd diskmv -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0
# Test criteria
if [[ ! -L "${srcPath}alink" ]] ||
[[ ! -L "${srcPath}blink" ]] ||
[[ -L "${dstPath}alink" ]] ||
[[ -L "${dstPath}blink" ]] ||
[[ -f "${srcPath}a" ]] ||
[[ ! -f "${dstPath}a" ]]
then
testFlag='fail'
fi
evaltest
runcmd diskmv -f -l "$testPath" "$srcDisk" "$dstDisk"
evalexit 0
# Test criteria
if [[ -L "${srcPath}alink" ]] ||
[[ -L "${srcPath}blink" ]] ||
[[ ! -L "${dstPath}alink" ]] ||
[[ ! -L "${dstPath}blink" ]] ||
[[ -f "${srcPath}a" ]] ||
[[ ! -f "${dstPath}a" ]]
then
testFlag='fail'
fi
evaltest
################################################################################
header "File with same name on source and destination"
mkdir -p "${srcPath}"
echo "sourcefile" > "${srcPath}e f"
echo "destinationfile" > "${dstPath}e f"
runcmd diskmv -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ -f "${dstPath}e f" ]]; then
read line < "${dstPath}e f"
if [[ "$line" != 'destinationfile' ]]; then
testFlag='fail'
fi
else
testFlag='fail'
fi
if [[ ! -f "${srcPath}e f" ]]; then
testFlag='fail'
fi
evaltest
################################################################################
header "Clobber option"
runcmd diskmv -f -c "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ -f "${dstPath}e f" ]]; then
read line < "${dstPath}e f"
if [[ "$line" != 'sourcefile' ]]; then
testFlag='fail'
fi
fi
if [[ -f "${srcPath}e f" ]]; then
testFlag='fail'
fi
evaltest
################################################################################
header "File extension option"
mkdir -p "${srcPath}/ext sub dir"
touch "${srcPath}ext sub dir/exttest."{'ha sh',nfo,txt,SRR}
touch "${srcPath}exttest."{'hash',nfo,txt,srr}
runcmd diskmv -f -e 'nfo,ha sh,srr' "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ ! -f "${srcPath}exttest.txt" ]] ||
[[ ! -f "${srcPath}ext sub dir/exttest.txt" ]] ||
[[ ! -f "${srcPath}exttest.hash" ]] ||
[[ ! -f "${dstPath}ext sub dir/exttest.ha sh" ]] ||
[[ ! -f "${dstPath}ext sub dir/exttest.nfo" ]] ||
[[ ! -f "${dstPath}ext sub dir/exttest.SRR" ]] ||
[[ ! -f "${dstPath}exttest.nfo" ]] ||
[[ ! -f "${dstPath}exttest.srr" ]] ||
[[ -f "${dstPath}exttest.txt" ]] ||
[[ -f "${dstPath}ext sub dir/exttest.txt" ]] ||
[[ -f "${dstPath}exttest.hash" ]] ||
[[ -f "${srcPath}ext sub dir/exttest.ha sh" ]] ||
[[ -f "${srcPath}ext sub dir/exttest.nfo" ]] ||
[[ -f "${srcPath}ext sub dir/exttest.SRR" ]] ||
[[ -f "${srcPath}exttest.nfo" ]] ||
[[ -f "${srcPath}exttest.srr" ]]
then
testFlag='fail'
fi
evaltest
################################################################################
header "Small file option"
mkdir -p "${srcPath}"
dd if=/dev/zero of="${srcPath}5k_file" bs=1k count=5 > /dev/null 2>&1
dd if=/dev/zero of="${srcPath}4k_file" bs=1k count=4 > /dev/null 2>&1
dd if=/dev/zero of="${srcPath}3k_file" bs=1k count=3 > /dev/null 2>&1
runcmd diskmv -f -s 4 "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ ! -f "${srcPath}5k_file" ]] ||
[[ -f "${dstPath}5k_file" ]] ||
[[ -f "${srcPath}4k_file" ]] ||
[[ ! -f "${dstPath}4k_file" ]] ||
[[ -f "${srcPath}3k_file" ]] ||
[[ ! -f "${dstPath}3k_file" ]]
then
testFlag='fail'
fi
evaltest
################################################################################
header "Source has non-empty directory that conflicts with file on destination"
echo "This file was created on ${dstPath}" > "${dstPath}xyz zyx"
mkdir -p "${srcPath}xyz zyx"
touch "${srcPath}xyz zyx/a"
runcmd diskmv -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ ! -d "${srcPath}xyz zyx" ]] ||
[[ -d "${dstPath}xyz zyx" ]] ||
[[ ! -f "${dstPath}xyz zyx" ]]
then
testFlag='fail'
fi
evaltest
runcmd diskmv -c -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ -d "${srcPath}xyz zyx" ]] || [[ ! -d "${dstPath}xyz zyx" ]]; then
testFlag='fail'
fi
evaltest
################################################################################
header "Source has regular file that conflicts with directory on destination"
mkdir -p "${dstPath}abc"
mkdir -p "${srcPath}"
echo "This file was created on ${dstPath}" > "${dstPath}abc/x"
echo "This file was created on ${srcPath}" > "${srcPath}abc"
runcmd diskmv -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0, but should be error output from rsync
# Test criteria
if [[ ! -f "${srcPath}abc" ]] ||
[[ -f "${dstPath}abc" ]] ||
[[ ! -d "${dstPath}abc" ]]
then
testFlag='fail'
fi
evaltest
runcmd diskmv -c -f "$testPath" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ -f "${srcPath}abc" ]] || [[ ! -f "${dstPath}abc" ]]; then
testFlag='fail'
fi
evaltest
################################################################################
header "Specify file instead of directory on user share"
mkdir -p "${srcPath}"
touch "${srcPath}jbr"
#echo "sourcefile" > "${srcPath}e f"
#echo "destinationfile" > "${dstPath}e f"
runcmd diskmv -f "${testPath}jbr" "$srcDisk" "$dstDisk"
evalexit 0 #Expect exit code 0
# Test criteria
if [[ ! -f "${dstPath}jbr" ]]; then
testFlag='fail'
fi
if [[ -f "${srcPath}jbr" ]]; then
testFlag='fail'
fi
evaltest
################################################################################
#header "See how a failed exit code works"
#runcmd diskmv -z
#evalexit 0 #Expect exit code 0
#evaltest
################################################################################
### Summarize test results
################################################################################
echo
echo "**************************************************************"
echo
echo "Test Summary:"
echo "Exit codes passed: ${nExitPass}"
echo "Exit codes failed: ${nExitFail}"
echo
echo "Test passed: ${nTestPass}"
echo "Test failed: ${nTestFail}"
################################################################################
### Clean up
################################################################################
rm -r ${srcPath} ${dstPath} 2>/dev/null