Make tests more robust and readable

This commit is contained in:
Łukasz Langa 2020-05-17 20:17:56 +02:00
parent c73646d2e1
commit 9af31192c2
No known key found for this signature in database
GPG Key ID: B26995E310250568
3 changed files with 171 additions and 233 deletions

View File

@ -44,6 +44,13 @@ in under 10 minutes. Both tests on HFS+.
If you'd like to contribute some more rigorous benchmarks or any If you'd like to contribute some more rigorous benchmarks or any
performance improvements, I'm accepting pull requests! :) performance improvements, I'm accepting pull requests! :)
Tests
-----
There's a simple but comprehensive test scenario using
`BATS <https://github.com/sstephenson/bats>`. Run the
file in the `tests` directory to run it.
Change Log Change Log
---------- ----------

View File

@ -1,220 +1,212 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load test_helper LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
#change it to your testing bitrot
cmd=~/Clones/bitrot/src/bitrot.py
# cmd=bitrot
cmd='python -m bitrot'
test_dir=/tmp/bitrot_dir-$USER test_dir=/tmp/bitrot_dir-$USER
mkdir -p $test_dir mkdir -p $test_dir
cd $test_dir || exit cd $test_dir || exit
########### @test "bitrot command exists" {
# BASIC # run $cmd --help
###########
@test "bitrot detects new files in a tree dir" {
mkdir -p notemptydirs/dir2/
touch notemptydirs/dir2/new-file-{a,b}.txt
echo $RANDOM >> notemptydirs/dir2/new-file-b.txt
run $cmd -v
# check_fail "${lines[@]}"
(( $status == 0 ))
# [[ ${lines[0]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[1]} = "2 entries in the database. 2 entries new:" ]]
[[ ${lines[2]} = " ./notemptydirs/dir2/new-file-a.txt" ]]
[[ ${lines[3]} = " ./notemptydirs/dir2/new-file-b.txt" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
[ "$status" -eq 0 ]
} }
@test "bitrot detects new files in a tree dir" {
mkdir -p nonemptydirs/dir2/
touch nonemptydirs/dir2/new-file-{a,b}.txt
echo $RANDOM >> nonemptydirs/dir2/new-file-b.txt
run $cmd -v
[ "$status" -eq 0 ]
# [[ ${lines[0]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[1]} = "2 entries in the database. 2 entries new:" ]]
[[ ${lines[2]} = " ./nonemptydirs/dir2/new-file-a.txt" ]]
[[ ${lines[3]} = " ./nonemptydirs/dir2/new-file-b.txt" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
}
@test "bitrot detects modified files in a tree dir" { @test "bitrot detects modified files in a tree dir" {
sleep 1 sleep 2
echo $RANDOM >> notemptydirs/dir2/new-file-a.txt echo $RANDOM >> nonemptydirs/dir2/new-file-a.txt
run $cmd -v run $cmd -v
# check_fail "${lines[@]}"
(( $status == 0 ))
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[2]} = "2 entries in the database. 1 entries updated:" ]]
[[ ${lines[3]} = " ./notemptydirs/dir2/new-file-a.txt" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
[ "$status" -eq 0 ]
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[2]} = "2 entries in the database. 1 entries updated:" ]]
[[ ${lines[3]} = " ./nonemptydirs/dir2/new-file-a.txt" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
} }
@test "bitrot detects renamed files in a tree dir" { @test "bitrot detects renamed files in a tree dir" {
sleep 1 sleep 1
mv notemptydirs/dir2/new-file-a.txt notemptydirs/dir2/new-file-a.txt2 mv nonemptydirs/dir2/new-file-a.txt nonemptydirs/dir2/new-file-a.txt2
run $cmd -v run $cmd -v
# check_fail "${lines[@]}"
(( $status == 0 ))
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[2]} = "2 entries in the database. 1 entries renamed:" ]]
[[ ${lines[3]} = " from ./notemptydirs/dir2/new-file-a.txt to ./notemptydirs/dir2/new-file-a.txt2" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
[ "$status" -eq 0 ]
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[2]} = "2 entries in the database. 1 entries renamed:" ]]
[[ ${lines[3]} = " from ./nonemptydirs/dir2/new-file-a.txt to ./nonemptydirs/dir2/new-file-a.txt2" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
} }
@test "bitrot detects delete files in a tree dir" { @test "bitrot detects delete files in a tree dir" {
sleep 1 sleep 1
rm notemptydirs/dir2/new-file-a.txt2 rm nonemptydirs/dir2/new-file-a.txt2
run $cmd -v run $cmd -v
# check_fail "${lines[@]}"
(( $status == 0 ))
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[2]} = "1 entries in the database. 1 entries missing:" ]]
[[ ${lines[3]} = " ./notemptydirs/dir2/new-file-a.txt2" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
[ "$status" -eq 0 ]
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[2]} = "1 entries in the database. 1 entries missing:" ]]
[[ ${lines[3]} = " ./nonemptydirs/dir2/new-file-a.txt2" ]]
[[ ${lines[4]} = "Updating bitrot.sha512... done." ]]
} }
@test "bitrot detects new files and modified in a tree dir " { @test "bitrot detects new files and modified in a tree dir " {
sleep 1 sleep 1
touch more-files-{a,b,c,d,e,f,g}.txt touch more-files-{a,b,c,d,e,f,g}.txt
echo $RANDOM >> notemptydirs/dir2/new-file-b.txt echo $RANDOM >> nonemptydirs/dir2/new-file-b.txt
run $cmd -v run $cmd -v
#check_fail "${lines[@]}"
(( $status == 0 )) [ "$status" -eq 0 ]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]] [[ ${lines[2]} = "8 entries in the database. 7 entries new:" ]]
[[ ${lines[2]} = "8 entries in the database. 7 entries new:" ]] [[ ${lines[3]} = " ./more-files-a.txt" ]]
[[ ${lines[3]} = " ./more-files-a.txt" ]] [[ ${lines[4]} = " ./more-files-b.txt" ]]
[[ ${lines[4]} = " ./more-files-b.txt" ]] [[ ${lines[5]} = " ./more-files-c.txt" ]]
[[ ${lines[5]} = " ./more-files-c.txt" ]] [[ ${lines[6]} = " ./more-files-d.txt" ]]
[[ ${lines[6]} = " ./more-files-d.txt" ]] [[ ${lines[7]} = " ./more-files-e.txt" ]]
[[ ${lines[7]} = " ./more-files-e.txt" ]] [[ ${lines[8]} = " ./more-files-f.txt" ]]
[[ ${lines[8]} = " ./more-files-f.txt" ]] [[ ${lines[9]} = " ./more-files-g.txt" ]]
[[ ${lines[9]} = " ./more-files-g.txt" ]] [[ ${lines[10]} = "1 entries updated:" ]]
[[ ${lines[10]} = "1 entries updated:" ]] [[ ${lines[11]} = " ./nonemptydirs/dir2/new-file-b.txt" ]]
[[ ${lines[11]} = " ./notemptydirs/dir2/new-file-b.txt" ]] [[ ${lines[12]} = "Updating bitrot.sha512... done." ]]
[[ ${lines[12]} = "Updating bitrot.sha512... done." ]]
} }
@test "bitrot detects new files, modified, deleted and moved in a tree dir " { @test "bitrot detects new files, modified, deleted and moved in a tree dir " {
sleep 1 sleep 1
for fil in {a,b,c,d,e,f,g}; do for fil in {a,b,c,d,e,f,g}; do
echo $RANDOM >> notemptydirs/pl-more-files-$fil.txt echo $RANDOM >> nonemptydirs/pl-more-files-$fil.txt
done done
echo $RANDOM >> notemptydirs/dir2/new-file-b.txt echo $RANDOM >> nonemptydirs/dir2/new-file-b.txt
mv more-files-a.txt more-files-a.txt2 mv more-files-a.txt more-files-a.txt2
rm more-files-g.txt rm more-files-g.txt
run $cmd -v run $cmd -v
(( $status == 0 )) [ "$status" -eq 0 ]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]] [[ ${lines[2]} = "14 entries in the database. 7 entries new:" ]]
[[ ${lines[2]} = "14 entries in the database. 7 entries new:" ]] [[ ${lines[3]} = " ./nonemptydirs/pl-more-files-a.txt" ]]
[[ ${lines[3]} = " ./notemptydirs/pl-more-files-a.txt" ]] [[ ${lines[4]} = " ./nonemptydirs/pl-more-files-b.txt" ]]
[[ ${lines[4]} = " ./notemptydirs/pl-more-files-b.txt" ]] [[ ${lines[5]} = " ./nonemptydirs/pl-more-files-c.txt" ]]
[[ ${lines[5]} = " ./notemptydirs/pl-more-files-c.txt" ]] [[ ${lines[6]} = " ./nonemptydirs/pl-more-files-d.txt" ]]
[[ ${lines[6]} = " ./notemptydirs/pl-more-files-d.txt" ]] [[ ${lines[7]} = " ./nonemptydirs/pl-more-files-e.txt" ]]
[[ ${lines[7]} = " ./notemptydirs/pl-more-files-e.txt" ]] [[ ${lines[8]} = " ./nonemptydirs/pl-more-files-f.txt" ]]
[[ ${lines[8]} = " ./notemptydirs/pl-more-files-f.txt" ]] [[ ${lines[9]} = " ./nonemptydirs/pl-more-files-g.txt" ]]
[[ ${lines[9]} = " ./notemptydirs/pl-more-files-g.txt" ]] [[ ${lines[10]} = "1 entries updated:" ]]
[[ ${lines[10]} = "1 entries updated:" ]] [[ ${lines[11]} = " ./nonemptydirs/dir2/new-file-b.txt" ]]
[[ ${lines[11]} = " ./notemptydirs/dir2/new-file-b.txt" ]] [[ ${lines[12]} = "1 entries renamed:" ]]
[[ ${lines[12]} = "1 entries renamed:" ]] [[ ${lines[13]} = " from ./more-files-a.txt to ./more-files-a.txt2" ]]
[[ ${lines[13]} = " from ./more-files-a.txt to ./more-files-a.txt2" ]] [[ ${lines[14]} = "1 entries missing:" ]]
[[ ${lines[14]} = "1 entries missing:" ]] [[ ${lines[15]} = " ./more-files-g.txt" ]]
[[ ${lines[15]} = " ./more-files-g.txt" ]] [[ ${lines[16]} = "Updating bitrot.sha512... done." ]]
[[ ${lines[16]} = "Updating bitrot.sha512... done." ]]
} }
@test "bitrot detects new files, modified, deleted and moved in a tree dir 2" { @test "bitrot detects new files, modified, deleted and moved in a tree dir 2" {
sleep 1 sleep 1
for fil in {a,b,c,d,e,f,g}; do for fil in {a,b,c,d,e,f,g}; do
echo $RANDOM >> notemptydirs/pl2-more-files-$fil.txt echo $RANDOM >> nonemptydirs/pl2-more-files-$fil.txt
done done
echo $RANDOM >> notemptydirs/pl-more-files-a.txt echo $RANDOM >> nonemptydirs/pl-more-files-a.txt
mv nonemptydirs/pl-more-files-b.txt nonemptydirs/pl-more-files-b.txt2
cp nonemptydirs/pl-more-files-g.txt nonemptydirs/pl2-more-files-g.txt2
cp nonemptydirs/pl-more-files-d.txt nonemptydirs/pl2-more-files-d.txt2
rm more-files-f.txt nonemptydirs/pl-more-files-c.txt
run $cmd -v
mv notemptydirs/pl-more-files-b.txt notemptydirs/pl-more-files-b.txt2 [ "$status" -eq 0 ]
cp notemptydirs/pl-more-files-g.txt notemptydirs/pl2-more-files-g.txt2 # [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
cp notemptydirs/pl-more-files-d.txt notemptydirs/pl2-more-files-d.txt2 [[ ${lines[2]} = "21 entries in the database. 9 entries new:" ]]
[[ ${lines[3]} = " ./nonemptydirs/pl2-more-files-a.txt" ]]
rm more-files-f.txt notemptydirs/pl-more-files-c.txt [[ ${lines[4]} = " ./nonemptydirs/pl2-more-files-b.txt" ]]
[[ ${lines[5]} = " ./nonemptydirs/pl2-more-files-c.txt" ]]
run $cmd -v [[ ${lines[6]} = " ./nonemptydirs/pl2-more-files-d.txt" ]]
[[ ${lines[7]} = " ./nonemptydirs/pl2-more-files-d.txt2" ]]
# check_fail "${lines[@]}" [[ ${lines[8]} = " ./nonemptydirs/pl2-more-files-e.txt" ]]
[[ ${lines[9]} = " ./nonemptydirs/pl2-more-files-f.txt" ]]
(( $status == 0 )) [[ ${lines[10]} = " ./nonemptydirs/pl2-more-files-g.txt" ]]
[[ ${lines[11]} = " ./nonemptydirs/pl2-more-files-g.txt2" ]]
# [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]] [[ ${lines[12]} = "1 entries updated:" ]]
[[ ${lines[2]} = "21 entries in the database. 9 entries new:" ]] [[ ${lines[13]} = " ./nonemptydirs/pl-more-files-a.txt" ]]
[[ ${lines[3]} = " ./notemptydirs/pl2-more-files-a.txt" ]] [[ ${lines[14]} = "1 entries renamed:" ]]
[[ ${lines[4]} = " ./notemptydirs/pl2-more-files-b.txt" ]] [[ ${lines[15]} = " from ./nonemptydirs/pl-more-files-b.txt to ./nonemptydirs/pl-more-files-b.txt2" ]]
[[ ${lines[5]} = " ./notemptydirs/pl2-more-files-c.txt" ]] [[ ${lines[16]} = "2 entries missing:" ]]
[[ ${lines[6]} = " ./notemptydirs/pl2-more-files-d.txt" ]] [[ ${lines[17]} = " ./more-files-f.txt" ]]
[[ ${lines[7]} = " ./notemptydirs/pl2-more-files-d.txt2" ]] [[ ${lines[18]} = " ./nonemptydirs/pl-more-files-c.txt" ]]
[[ ${lines[8]} = " ./notemptydirs/pl2-more-files-e.txt" ]] [[ ${lines[19]} = "Updating bitrot.sha512... done." ]]
[[ ${lines[9]} = " ./notemptydirs/pl2-more-files-f.txt" ]]
[[ ${lines[10]} = " ./notemptydirs/pl2-more-files-g.txt" ]]
[[ ${lines[11]} = " ./notemptydirs/pl2-more-files-g.txt2" ]]
[[ ${lines[12]} = "1 entries updated:" ]]
[[ ${lines[13]} = " ./notemptydirs/pl-more-files-a.txt" ]]
[[ ${lines[14]} = "1 entries renamed:" ]]
[[ ${lines[15]} = " from ./notemptydirs/pl-more-files-b.txt to ./notemptydirs/pl-more-files-b.txt2" ]]
[[ ${lines[16]} = "2 entries missing:" ]]
[[ ${lines[17]} = " ./more-files-f.txt" ]]
[[ ${lines[18]} = " ./notemptydirs/pl-more-files-c.txt" ]]
[[ ${lines[19]} = "Updating bitrot.sha512... done." ]]
} }
@test "bitrot can operate with 3278 files easily in a dir" { @test "bitrot can operate with 3278 files easily in a dir (1)" {
sleep 1 sleep 1
mkdir -p alotfiles/here; cd alotfiles/here mkdir -p alotfiles/here; cd alotfiles/here
#create a 320KB file # create a 320KB file
dd if=/dev/urandom of=masterfile bs=1 count=327680 dd if=/dev/urandom of=masterfile bs=1 count=327680
#split it in 3277 files (instantly) + masterfile = 3278 # split it in 3277 files (instantly) + masterfile = 3278
split -b 100 -a 10 masterfile split -b 100 -a 10 masterfile
cd $test_dir cd $test_dir
run $cmd run $cmd
(( $status == 0 ))
[[ ${lines[2]} = "3299 entries in the database, 3278 new, 0 updated, 0 renamed, 0 missing." ]]
[ "$status" -eq 0 ]
[[ ${lines[2]} = "3299 entries in the database, 3278 new, 0 updated, 0 renamed, 0 missing." ]]
} }
@test "bitrot can operate with 3278 files easily in a dir 2 " { @test "bitrot can operate with 3278 files easily in a dir (2)" {
sleep 1 sleep 1
mv alotfiles/here alotfiles/here-moved mv alotfiles/here alotfiles/here-moved
run $cmd run $cmd
#check_fail "${lines[@]}"
(( $status == 0 ))
[[ ${lines[2]} = "3299 entries in the database, 0 new, 0 updated, 3278 renamed, 0 missing." ]]
[ "$status" -eq 0 ]
[[ ${lines[2]} = "3299 entries in the database, 0 new, 0 updated, 3278 renamed, 0 missing." ]]
} }
@test "bitrot can detetect a bitrot in a dir ! " { @test "bitrot can detect rotten bits in a dir (1)" {
sleep 1 sleep 1
generate_bitrot ./bitrot-file 10 2 $cmd touch non-rotten-file
run $cmd -q dd if=/dev/zero of=rotten-file bs=1k count=1000 &>/dev/null
# let's make sure they share the same timestamp
touch -r non-rotten-file rotten-file
run $cmd -v
#check_fail "${lines[@]}" [ "$status" -eq 0 ]
[[ ${lines[0]} = "Checking bitrot.db integrity... ok." ]]
(( $status == 1 )) # [[ ${lines[1]} = "Finished. 0.00 MiB of data read. 0 errors found." ]]
[[ ${lines[0]} = *"error: SHA1 mismatch for ./bitrot-file: expected"* ]] [[ ${lines[2]} = "3301 entries in the database, 2 entries new:" ]]
[[ ${lines[1]} = "error: There were 1 errors found." ]] [[ ${lines[3]} = " ./non-rotten-file" ]]
[[ ${lines[4]} = " ./rotten-file" ]]
} }
@test "bitrot can detect rotten bits in a dir (2)" {
sleep 1
# modify the rotten file...
dd if=/dev/urandom of=rotten-file bs=1k count=10 seek=1k conv=notrunc &>/dev/null
# ...but revert the modification date
touch -r non-rotten-file rotten-file
run $cmd -q
[ "$status" -eq 1 ]
[[ ${lines[0]} = *"error: SHA1 mismatch for ./rotten-file: expected"* ]]
[[ ${lines[1]} = "error: There were 1 errors found." ]]
}
@test "Clean everything" { @test "Clean everything" {
run chmod -f a+w * run chmod -Rf a+w $test_dir
\rm -rf * $test_dir $BITROT_BACKUPS run rm -rf $test_dir
} }

View File

@ -1,61 +0,0 @@
#!/usr/bin/env bash
# LC_ALL=en_US.UTF-8
# LANGUAGE=en_US.UTF-8
LANG=C
check_fail() {
local temp=/tmp/bats.log
> $temp
for line; do
echo "$line" >> $temp
done
# cat /tmp/.bitrot.log >> $temp
}
generate_bitrot() {
local dest=$1 temp=/tmp/temp-base
local -i count=$(($2*100)) percent=${3:-5}
local cmd=$4
mkdir -p "${dest%/*}"
local dir_base=${dest%%/*}
touch "$dest" $temp
#let's make sure they shared the same timestamp
touch "$dest" -r $temp
dd if=/dev/zero of="$dest" bs=1k count=$count &>/dev/null
run $cmd
#modify it and change modify date to base-file, simulate real bitrot so
dd seek=1k if=/dev/urandom of="$dest" bs=1k count=$((count*percent/100)) conv=notrunc &>/dev/null
touch "$dest" -r $temp
\rm -f $tmp
run $cmd
}
generate_bitrots() {
local dest=$1 dest2=$2 temp=/tmp/temp-base
local -i count=$(($3*100)) percent=${4:-5}
mkdir -p "${dest%/*}"
mkdir -p "${dest2%/*}"
local dir_base=${dest%/*}
local dir_base2=${dest2%/*}
touch "$dest2" "$dest" $temp
#let's make sure they shared the same timestamp
touch "$dest" -r $temp
touch "$dest2" -r $temp
dd if=/dev/zero of="$dest" bs=1k count=$count &>/dev/null
dd if=/dev/zero of="$dest2" bs=1k count=$count &>/dev/null
run $r "$dir_base" "$dir_base2"
#modify it and change modify date to base-file, simulate bitrot so
dd seek=1k if=/dev/urandom of="$dest" bs=1k count=$((count*percent/100)) conv=notrunc &>/dev/null
dd seek=1k if=/dev/urandom of="$dest2" bs=1k count=$((count*percent/100)) conv=notrunc &>/dev/null
touch "$dest" -r $temp
touch "$dest2" -r $temp
\rm -f $tmp
echo $status > /tmp/status
run $r "$dir_base" "$dir_base2"
echo $status >> /tmp/status
}