#!/bin/sh
#
#  Test MaxWaitTime, MaxRunTime and MaxStartDelay
#

TestName="maxtime-test"
JobName=backup

. scripts/functions

copy_test_confs

cp -f ${cwd}/scripts/bareos-dir.conf.maxtime ${conf}/bareos-dir.conf
WHEN=`date '+%Y-%m-%d %H:%M:%S'`

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

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

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label volume=TestVolume001 pool=PoolA
@$out ${cwd}/tmp/RUN_MAXTIME.log
@# no media => have to wait (MaxWaitTime=30sec)
run job=RUN_MAXWAITTIME comment="Should fail" pool=Default yes
@sleep 2
@# storage is used by RUN_MAXWAITTIME => have to wait
run job=RUN_MAXSTARTDELAY comment="Should fail" pool=PoolA when="$WHEN" yes
@sleep 2
@# storage is used by the first MAXWAITTIME, but should work
run job=RUN_MAXWAITTIME comment="Should work" pool=PoolA yes
@sleep 120
label volume=TestVolume002 pool=Default
mount
@sleep 2
mount
wait
messages
run job=RUN_MAXRUNTIME comment="Should fail" pool=PoolA yes
wait
messages
st dir
quit
END_OF_DATA

mkfifo ${tmp}/fifo1
mkfifo ${tmp}/fifo2
mkfifo ${tmp}/fifo3

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

dstat=0
bstat=0
rstat=0
touch ${cwd}/tmp/log2.out

grep -e '1: Fatal error: Max wait time exceeded. Job canceled.' ${cwd}/tmp/RUN_MAXTIME.log >/dev/null
if [ $? -eq 0 ]; then
    print_debug "MAXWAITTIME ok"
else
   echo "MAXWAITTIME in error"
   bstat=1
fi

grep -e '2: Fatal error: Job canceled because max start delay time exceeded.' ${cwd}/tmp/RUN_MAXTIME.log > /dev/null
f=$?

grep -e 'NEVER start this' ${cwd}/tmp/RUN_MAXTIME.log >/dev/null
s=$?

if [ $f -eq 0 -a $s -ne 0 ]; then
   print_debug "MAXSTARTDELAY ok"
else
   echo "MAXSTARTDELAY in error"
   bstat=1
fi

grep -e '4: Fatal error: Max run time exceeded. Job canceled.' ${cwd}/tmp/RUN_MAXTIME.log >/dev/null
if [ $? -eq 0 ]; then
   print_debug "MAXRUNTIME ok"
else
   echo "MAXRUNTIME in error"
   bstat=1
fi

grep 'Termination: *Backup OK' ${cwd}/tmp/RUN_MAXTIME.log >/dev/null
if [ $? -eq 0 ]; then
   print_debug "MAXRUNTIME2 ok"
else
   echo "MAXRUNTIME2 in error"
   bstat=1
fi

end_test
