#!/bin/sh
#
# Show the broken media bug. Test by Graham Keeling.
#
# For the moment this test will always fail, so do not add
#   it to the standard test set.
# The problem:
# - You begin writing a giant job onto a Volume
# - There are no other volumes available for writing (big mistake).
# - You start a second job that needs a Volume to write on.
# - The second job see that there are no JobMedia records associated with the
#    Volume (not yet written), so it purges the Volume.
# - A sort of chaos then follows.
#

TestName="broken-media-bug-test"
. scripts/functions

cwd=`pwd`
scripts/cleanup
/bin/cp -f scripts/broken-media-bug-bareos-dir.conf bin/bareos-dir.conf
/bin/cp -f scripts/test-bareos-sd.conf bin/bareos-sd.conf
/bin/cp -f scripts/test-bareos-fd.conf bin/bareos-fd.conf
/bin/cp -f scripts/test-console.conf bin/bconsole.conf
# 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

hugefile=${BackupDirectory}/hugefile
hugefilesize=300




start_test

echo "Creating huge ${hugefilesize}M file..."
dd if=/dev/urandom of="$hugefile" bs=1M count="$hugefilesize"
echo "Done"

cat >tmp/bconcmds <<END_OF_DATA
@out /dev/null
messages
@$out tmp/log1.out
setdebug level=150  storage=File
setdebug level=150  Director
label storage=File volume=TestVolume0001
label storage=File volume=TestVolume0002
run job=First yes
messages
quit
END_OF_DATA

run_bareos

# Give the first job a bit of time to get going.
sleep 5

cat >tmp/bconcmds <<END_OF_DATA
setdebug level=150  storage=File
setdebug level=150  Director
list volumes
llist volume=TestVolume0001
llist volume=TestVolume0002
messages
@$out tmp/log2.out
run job=Second yes
wait
messages
restore fileset=Set1 where=${cwd}/tmp/bareos-restores select all storage=File done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs
check_restore_diff
end_test
