#!/bin/sh

. scripts/functions

#
# Example regression test.
#
# Use this as a template for your own tests.
#

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"
#TestName="1-example-test"

# set other test specific variables
Client=bareos-fd
JobName=backup-bareos-fd

# Directory to backup.
# This directory will be created by setup_data().
# If set, $BackupDirectory will be used
# as base directory by check_restore_diff().
# Don't use a "/" at the end of the directory path,
# as it might cause problems
# (filenames given as parameter to restore must match 100%. "//" are not identical with "/").
BackupDirectory="${tmp}/data"

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data".
setup_data data/small.tgz

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

# The default fileset FS_TESTJOB backups all files and directories defined in "${tmp}/file-list".
# setup_data() sets this automatically to "$tmp/data/",
# so it only be needs to be set if another directory is used.
#echo "${BackupDirectory}" >${tmp}/file-list

# start the test
start_test

# activate plugin, e.g. bpipe-fd
#activate_plugin bpipe-fd

# ${tmp}/bconcmds lists the bconsole commands executed by "run_bareos"
cat <<END_OF_DATA >${tmp}/bconcmds
messages
@$out ${tmp}/log-config.out
show client=${Client}
@$out ${tmp}/log1.out w
label storage=File1 pool=Default volume=TestVolume001
run job=$JobName storage=File1 yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} where=${tmp}/bareos-restores select storage=File1
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

# Start the bareos daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_bareos

# verify that all are terminated
if ! check_for_zombie_jobs storage=File1 client=${Client}; then
    set_error "zombie jobs"
fi

# stop bareos
stop_bareos

# check log files for common error messages
check_log ${tmp}/log1.out
check_log ${tmp}/log2.out

# check tmp/log1.out and tmp/log2.out for errors
check_two_logs

# check for differences between original files and restored files
check_restore_diff ${BackupDirectory}

# do some manual testing
if [ ! -d ${BackupDirectory} ]; then
    set_error "Directory ${BackupDirectory} does not exists any more."
fi

check_log ${tmp}/log-config.out
if ! grep -qi "TlsRequire = yes" ${tmp}/log-config.out; then
   set_error "client is not configured to use TLS."
fi

# end tests and check for error codes
end_test
