#!/bin/sh
#
# Run a simple backup
#  to two tapes where the maximum tape file size is set to 1M
#  Note, this test simulates the tape filling and writing to
#   the next tape. One minor wrinkle: we create the two tapes
#   in the special ScratchPool defined in pool, so they should
#   be switched to the Special
#   Pool automatically.
#
#  Note we use the viritual disk autochanger
#
TestName="scratchpool-pool-test"
JobName=scratchpool-pool
. scripts/functions

# This test hangs so disable it
exit

cwd=`pwd`
scripts/cleanup
scripts/copy-2disk-confs
scripts/prepare-disk-changer


# 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



outf="${cwd}/tmp/sed_tmp"
echo "s%# Maximum File Size%  Maximum File Size%g" >${outf}
cp ${cwd}/bin/bareos-sd.conf ${cwd}/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bareos-sd.conf

change_jobname NightlySave $JobName
start_test

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=tape volume=TestVolume001 slot=1 pool=SpecialScratch drive=0
label storage=tape volume=TestVolume002 slot=2 pool=SpecialScratch drive=0
label storage=tape volume=TestVolume003 slot=3 pool=Scratch drive=0
show pool=Default
show pool=Special
@########################
@# Verify catalog entries
@$out ${cwd}/tmp/pool.out
sql
SELECT Name, ScratchPoolId, RecyclePoolId FROM Pool WHERE Name IN ('Default', 'Special');

@$out ${cwd}/tmp/log1.out
@########################
@# Use SpecialScratch
run job=$JobName pool=Special yes
wait
messages
run job=$JobName pool=Special level=Full yes
wait
messages
@$out ${cwd}/tmp/log3.out
@########################
@# no more volume in SpecialScratch (but Scrach have one)
run job=$JobName pool=Special level=Full yes
wait mount
@sleep 1
cancel jobid=3
messages
@$out ${cwd}/tmp/log4.out
list volume pool=Special
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bareos-restores select all storage=tape done
yes
wait
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=tape
stop_bareos

check_two_logs
check_restore_diff


################################################################
# Check Catalog definition for ScratchPool and RecyclePool
awk '
 / Default/ { if ($4 == 0 && $6 != 0) {print "OK"} else { print "Default ERR"} }
 / Special/ { if ($4 != 0 && $6 == 0) {print "OK"} else { print "Special ERR"} }
' tmp/pool.out > tmp/res

grep ERR tmp/res
if [ $? -eq 0 ]; then
    print_debug "Found ERR about ScratchPoolId/RecyclePoolId in catalog"
    dstat=2
fi

if [ `cat tmp/res | wc -l` -ne 2 ]; then
    print_debug "Can't find all pool records in catalog"
    dstat=2
fi

################################################################
# Check for the third job that must fail because the SpecialScratch is empty

grep 'Cannot find any appendable volumes' tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "Job 3 should request a manual intervention!!"
    dstat=2
fi

grep 'Backup Error' tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "Job 3 should be in error"
    dstat=2
fi

################################################################
# Check Special pool

if [ `grep TestVolume00 tmp/log4.out | wc -l` -ne 2 ]; then
    print_debug "Special pool should contain 2 vols"
    dstat=2
fi

end_test
