#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
#  Try to stress test autolabel by creating lots of new
#    volumes. This is to try to show up a bug 8106 where
#    at some point when a volume is changed, the JobMedia
#    EndFile < StartFile and EndBlock < StartBlock because
#    it is picking up the information from the next Volume.
#  Note, this does not reproduce the error ...
#

TestName="auto-label-jobmedia-test"
JobName=AutoLabel
. scripts/functions

copy_test_confs

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

# Default values
#  Maximum Volume Jobs = 1
#  Maximum Volume Bytes = 1500000

cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
sed "s%# Simple Label Format%  Label Format%" ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf
cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
sed "s%# Maximum Volume%  Maximum Volume%g" ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf
cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
sed "s%Maximum Volume Jobs = 1%Maximum Volume Jobs = 10%g" ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf
cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
sed "s%Maximum Volume Bytes = 1500000%Maximum Volume Bytes =45000000%g" ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf

cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
sed "s%Maximum Concurrent Jobs = 10%Maximum Concurrent Jobs = 100%g" ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf
cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
sed "s%Maximum Concurrent Jobs = 4%Maximum Concurrent Jobs = 100%g" ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf


change_jobname CompressedTest $JobName
start_test

cat <<END_OF_SCRIPT >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
messages
@#setdebug level=50 storage=File
@#setdebug level=50 dir
run job=$JobName level=Full storage=File yes
run job=$JobName storage=File yes
run job=$JobName level=Full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
run job=$JobName level=full storage=File yes
status dir
wait
sql
select * from JobMedia;

list volumes
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=0 storage=File
setdebug level=0 dir
restore where=${cwd}/tmp/bacula-restores select storage=File
unmark *
mark *
done
yes
wait
messages
@# Now restore JobId=1
restore where=${cwd}/tmp/bacula-restores storage=File
3
1
mark *
done
yes
wait
messages
@# Now restore JobId=3
restore where=${cwd}/tmp/bacula-restores storage=File
3
3
mark *
done
yes
wait
messages
quit
END_OF_SCRIPT

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
check_restore_diff
end_test
