Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btrfsmaintenance: add tests framework and functional test cases #103

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion btrfs-defrag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ for P in $BTRFS_DEFRAG_PATHS; do
continue
fi
find "$P" -xdev -size "$BTRFS_DEFRAG_MIN_SIZE" -type f \
-exec btrfs filesystem defrag -t 32m -f $BTRFS_VERBOSITY '{}' \;
-exec btrfs filesystem defrag -r -t 32m -f $BTRFS_VERBOSITY '{}' \;
done

} | \
Expand Down
8 changes: 4 additions & 4 deletions sysconfig.btrfsmaintenance
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ BTRFS_BALANCE_MOUNTPOINTS="/"

## Path: System/File systems/btrfs
## Type: string(none,daily,weekly,monthly)
## Default: "weekly"
## Default: *
## ServiceRestart: btrfsmaintenance-refresh
#
# Frequency of periodic balance.
#
# The frequency may be specified using one of the listed values or
# in the format documented in the "Calendar Events" section of systemd.time(7),
# if available.
BTRFS_BALANCE_PERIOD="weekly"
BTRFS_BALANCE_PERIOD="none"

## Path: System/File systems/btrfs
## Type: string
Expand Down Expand Up @@ -89,15 +89,15 @@ BTRFS_SCRUB_MOUNTPOINTS="/"

## Path: System/File systems/btrfs
## Type: string(none,weekly,monthly)
## Default: "monthly"
## Default: *
## ServiceRestart: btrfsmaintenance-refresh
#
# Frequency of periodic scrub.
#
# The frequency may be specified using one of the listed values or
# in the format documented in the "Calendar Events" section of systemd.time(7),
# if available.
BTRFS_SCRUB_PERIOD="monthly"
BTRFS_SCRUB_PERIOD="none"

## Path: System/File systems/btrfs
## Type: string(idle,normal)
Expand Down
24 changes: 24 additions & 0 deletions test/1-timer-systemd.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
btrfsmaintenance-refresh.path disabled
btrfs-balance.service static
btrfs-defrag.service static
btrfs-scrub.service static
btrfs-trim.service static
btrfsmaintenance-refresh.service static
btrfs-balance.timer enabled
btrfs-defrag.timer enabled
btrfs-scrub.timer enabled
btrfs-trim.timer enabled
btrfs-balance.timer btrfs-balance.service
btrfs-defrag.timer btrfs-defrag.service
btrfs-scrub.timer btrfs-scrub.service
btrfs-trim.timer btrfs-trim.service
btrfsmaintenance-refresh.path disabled
btrfs-balance.service static
btrfs-defrag.service static
btrfs-scrub.service static
btrfs-trim.service static
btrfsmaintenance-refresh.service static
btrfs-balance.timer disabled
btrfs-defrag.timer disabled
btrfs-scrub.timer disabled
btrfs-trim.timer disabled
17 changes: 17 additions & 0 deletions test/1-timer-systemd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Test if the systemd timers are enabled as per the config file

. $(dirname $(realpath "$0"))/utils

load_config "sysconfig.btrfsmaintenance.testall"
timer_setup "systemd-timer"

systemctl list-unit-files | grep btrfs
systemctl --all list-timers | grep btrfs | rev | awk '{print $1" "$2}' | rev

timer_reset "systemd-timer"
systemctl list-unit-files | grep btrfs
systemctl --all list-timers | grep btrfs | rev | awk '{print $1" "$2}' | rev

unload_config
27 changes: 27 additions & 0 deletions test/2-timer-cron.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
btrfsmaintenance-refresh.path disabled
btrfs-balance.service static
btrfs-defrag.service static
btrfs-scrub.service static
btrfs-trim.service static
btrfsmaintenance-refresh.service static
btrfs-balance.timer disabled
btrfs-defrag.timer disabled
btrfs-scrub.timer disabled
btrfs-trim.timer disabled
daily:
weekly:
btrfs-balance
btrfs-scrub
monthly:
btrfs-defrag
btrfs-trim
btrfsmaintenance-refresh.path disabled
btrfs-balance.service static
btrfs-defrag.service static
btrfs-scrub.service static
btrfs-trim.service static
btrfsmaintenance-refresh.service static
btrfs-balance.timer disabled
btrfs-defrag.timer disabled
btrfs-scrub.timer disabled
btrfs-trim.timer disabled
24 changes: 24 additions & 0 deletions test/2-timer-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Test if the cron timers are enabled as per the config file

. $(dirname $(realpath "$0"))/utils

load_config "sysconfig.btrfsmaintenance.testall"

timer_setup "cron"
systemctl list-unit-files | grep btrfs
echo daily:
ls /etc/cron.daily | grep btrfs
echo weekly:
ls /etc/cron.weekly| grep btrfs
echo monthly:
ls /etc/cron.monthly | grep btrfs

timer_reset "cron"
systemctl list-unit-files | grep btrfs
ls /etc/cron.daily | grep btrfs
ls /etc/cron.weekly| grep btrfs
ls /etc/cron.monthly | grep btrfs

unload_config
18 changes: 18 additions & 0 deletions test/3-task-balance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Test is the task: Balance

. $(dirname $(realpath "$0"))/utils

timer_reset "cron"
timer_reset "systemd-timer"
load_config "sysconfig.btrfsmaintenance.testall"

$TESTPATH/btrfs-balance.sh > ./results/$TESTNAME.full

cat ./results/$TESTNAME.full | grep -q "Before balance of"
[ $? != 0 ] && echo "Balance did not run"
cat ./results/$TESTNAME.full | grep -q "Done,"
[ $? != 0 ] && echo "Balance did not complete?"

unload_config
13 changes: 13 additions & 0 deletions test/4-task-defrag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Test is the task: defrag

. $(dirname $(realpath "$0"))/utils

timer_reset "cron"
timer_reset "systemd-timer"
load_config "sysconfig.btrfsmaintenance.testall"

$TESTPATH/btrfs-defrag.sh > ./results/$TESTNAME.full

unload_config
13 changes: 13 additions & 0 deletions test/5-task-scrub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Test is the task: scrub

. $(dirname $(realpath "$0"))/utils

timer_reset "cron"
timer_reset "systemd-timer"
load_config "sysconfig.btrfsmaintenance.testall"

$TESTPATH/btrfs-scrub.sh > ./results/$TESTNAME.full

unload_config
13 changes: 13 additions & 0 deletions test/6-task-trim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Test is the task: trim

. $(dirname $(realpath "$0"))/utils

timer_reset "cron"
timer_reset "systemd-timer"
load_config "sysconfig.btrfsmaintenance.testall"

$TESTPATH/btrfs-trim.sh > ./results/$TESTNAME.full

unload_config
54 changes: 54 additions & 0 deletions test/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

#### Modify as needed #####

# Where is the code under test
TESTPATH="/usr/share/btrfsmaintenance"
#TESTPATH="../"

# A btrfs filesystem on which the btrfsmaintenance script shall run.
SCRATCH_MNT="/mnt/scratch"

verbose=false

###########################

. $(dirname $(realpath "$0"))/utils

do_test()
{
local testcase=$1
local testname
local should_match_expected=false

testname=$(echo $testcase | cut -d"." -f1)
rm ./results/$testname.out > /dev/null 2>&1
rm ./results/$testname.full > /dev/null 2>&1

. ./$testcase > ./results/$testname.out

# No expected output file means testcase breaks the silence upon error
if [ -f $testname.expected ]; then
diff -Z ./results/${testname}.out ./${testname}.expected
[ $? != 0 ] && \
echo "$testname ..... [Failed]" || \
echo "$testname ..... [Passed]"
elif [ $(cat ./results/$testname.out | wc -l) != 0 ]; then
echo "$testname ..... [Failed: check ./results/$testname.out|full]"
else
echo "$testname ..... [Passed]"
fi
}

update_btrfs_mnt sysconfig.btrfsmaintenance.testall

if [ ! -z $1 ]; then
tests=$*
else
tests=$(ls *.sh | grep -E '^[0-9]')
fi

for testcase in $tests
do
do_test $testcase
done
4 changes: 4 additions & 0 deletions test/results/.full
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Running fstrim on /mnt/scratch
fstrim: /mnt/scratch: the discard operation is not supported
flock: getting lock took 0.000005 seconds
flock: executing fstrim
21 changes: 21 additions & 0 deletions test/sysconfig.btrfsmaintenance.testall
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BTRFS_SCRUB_MOUNTPOINTS=/mnt/scratch
BTRFS_DEFRAG_PATHS=/mnt/scratch
BTRFS_BALANCE_MOUNTPOINTS=/mnt/scratch
BTRFS_TRIM_MOUNTPOINTS=/mnt/scratch

BTRFS_SCRUB_PERIOD="weekly"
BTRFS_DEFRAG_PERIOD="monthly"
BTRFS_BALANCE_PERIOD="weekly"
BTRFS_TRIM_PERIOD="monthly"

BTRFS_DEFRAG_MIN_SIZE="+1M"

BTRFS_BALANCE_DUSAGE="5 10"
BTRFS_BALANCE_MUSAGE="5"

BTRFS_SCRUB_PRIORITY="idle"
BTRFS_SCRUB_READ_ONLY="false"

BTRFS_LOG_OUTPUT="stdout"

BTRFS_ALLOW_CONCURRENCY="false"
74 changes: 74 additions & 0 deletions test/utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash

tell()
{
[ $verbose = true ] && echo $*
:
}

timer_setup()
{
local timertype=$1

$verbose == true && \
$TESTPATH/btrfsmaintenance-refresh-cron.sh $timertype || \
$TESTPATH/btrfsmaintenance-refresh-cron.sh $timertype > /dev/null
}

timer_reset()
{
local timertype=$1

$verbose = true && \
$TESTPATH/btrfsmaintenance-refresh-cron.sh $timertype uninstall || \
$TESTPATH/btrfsmaintenance-refresh-cron.sh $timertype uninstall > /dev/null
}

sysconf_file()
{
if [ -f /etc/sysconfig/btrfsmaintenance ]; then
echo "/etc/sysconfig/btrfsmaintenance"
return
elif [ -f /etc/default/btrfsmaintenance ]; then
echo "/etc/default/btrfsmaintenance"
return
fi

echo "ERROR: btrfsmaintenance config file is not found"
exit 1
}

load_config()
{
local sys_config=$(sysconf_file)
local test_configfile=$1

if ! [ -f "$test_configfile" ]; then
echo "ERROR: config file '$test_configfile' is not found."
exit 1
fi

# btrfsmaintenance-refresh-cron.sh and the tasks scripts have configfile
# path hard coded. So move our test config file there.

mv $sys_config ${sys_config}.org
cp $test_configfile $sys_config
}

unload_config()
{
local sys_config=$(sysconf_file)

mv ${sys_config}.org $sys_config
}

update_btrfs_mnt()
{
local configfile=$1

for script in BTRFS_SCRUB_MOUNTPOINTS BTRFS_DEFRAG_PATHS \
BTRFS_BALANCE_MOUNTPOINTS BTRFS_TRIM_MOUNTPOINTS
do
sed -i -E "s,($script=).*,\1$SCRATCH_MNT," $configfile
done
}