#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# 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-bacula-dir.conf bin/bacula-dir.conf
/bin/cp -f scripts/test-bacula-sd.conf bin/bacula-sd.conf
/bin/cp -f scripts/test-bacula-fd.conf bin/bacula-fd.conf
/bin/cp -f scripts/test-console.conf bin/bconsole.conf
hugefile=${cwd}/build/hugefile
hugefilesize=300
echo "${cwd}/build" >${cwd}/tmp/file-list

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_bacula

# 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/bacula-restores select all storage=File done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
check_restore_diff
rm -f "$hugefile"
end_test
