#!/bin/sh
#
# Run a backup of generated data and check if the
# "Quota Include Failed Jobs" directive works
#
TestName="quota-includefailed-test"
JobName=BackupClient1
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

rm -f bin/bareos-dir.conf
/bin/cp -f ${rconfigs}/${TestName}/bareos-dir.conf.quota-includefailed-test bin/bareos-dir.conf

echo "${cwd}/tmp/testdata" >${cwd}/tmp/file-list


start_test

# create test data
dd if=/dev/zero of=${cwd}/tmp/testdata bs=40000k count=1

#@$out /dev/null
cat >${cwd}/tmp/bconcmds <<END_OF_DATA
messages
label storage=File volume=TestVolume001
@$out ${cwd}/tmp/log1.out
run job=Errorjob Level=Full yes
wait
messages
@$out ${cwd}/tmp/log2.out
run job=$JobName Level=Full yes
wait
messages

END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

# switch to not include failed jobs

mv bin/bareos-dir.conf bin/bareos-dir.conf.bak
sed 's/Quota Include Failed Jobs = yes/Quota Include Failed Jobs = no/'< bin/bareos-dir.conf.bak  > bin/bareos-dir.conf


cat >${cwd}/tmp/bconcmds <<END_OF_DATA
messages
@$out ${cwd}/tmp/log3.out
run job=$JobName Level=Full yes
wait
messages

END_OF_DATA
#

run_bareos
check_for_zombie_jobs storage=File
stop_bareos



# check if job1 is successful
grep "^  Termination: .*Backup Error " ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "First Backup is Error, OK."
else
   estat=1
fi
#

# check if job is no successful
grep "^  Termination: .*Backup Error " ${tmp}/log2.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Check for Hardquota: Backup Error is OK."
else
   estat=2
fi


# check if job is successful again
grep  "Termination.*Backup OK" ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "After switching to not include failed jobs, Backup is OK"
else
   estat=3
fi


end_test
