#!/bin/sh
#
# Run a backup of generated data and check if the hard quota limits are respected
#
#
TestName="quota-hardquota-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-hardquota-test bin/bareos-dir.conf

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

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



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

delete job jobid=1 yes
delete job jobid=2 yes

@$out ${cwd}/tmp/log3.out
run job=$JobName Level=Full yes
wait
messages
@$out ${cwd}/tmp/log4.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 OK" ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Check for Hardquota: First Backup is OK."
else
   estat=1
fi
#


# check if job2 gets a over quota warning
grep "Quota Exceeded" ${tmp}/log2.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "'Quota Exceeded.' warning found. OK"
else
   estat=2
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=3
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 "Check for Hardquota: First Backup is OK."
else
   estat=4
fi
#

# check if job gets a over quota warning
grep "Quota Exceeded" ${tmp}/log4.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "'Quota Exceeded.' warning found. OK."
else
   estat=5
fi
# check if job is successful
grep "^  Termination: .*Backup Error " ${tmp}/log4.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Check for Hardquota: Backup Error is OK."
else
   estat=6
fi



end_test
